@tint-ui/tools
v0.3.0
Published
A collection of development utilities and helper functions for the Tint UI library. This package provides modular tools that can be imported individually to keep your bundle size minimal.
Downloads
41
Readme
@tint-ui/tools
A collection of development utilities and helper functions for the Tint UI library. This package provides modular tools that can be imported individually to keep your bundle size minimal.
Installation
npm install @tint-ui/toolsUsage
Import only the specific utilities you need:
import { isPlainObject } from "@tint-ui/tools/is-plain-object";
import { logger } from "@tint-ui/tools/logger";Available Utilities
@tint-ui/tools/as-props
Utility for handling polymorphic "as" prop pattern in React components.
asProps(props, options?)- Processes component props to handle the 'as' prop pattern for polymorphic components
Types:
AsPropsOptions- Configuration options for component rendering
@tint-ui/tools/browser-support
Utilities for checking browser environment and features.
isBrowserEnvironment()- Checks if the current environment is a browserisLocalStorage()- Checks if the localStorage is supported in the current environment
@tint-ui/tools/class-group
Utility for generating BEM-style class names.
classGroup(name)- Creates an object for generating class name strings with BEM-style naming conventions
Types:
ClassGroup- Interface for class name generation with methods:base- Returns the base class namea(name)- Returns class name for child elementsb(name, suffix?)- Returns base class name with a modifier
@tint-ui/tools/clone-plain-object
Utilities for deep cloning plain objects and arrays.
clonePlainObject(obj, options?)- Creates a deep clone of a plain objectobj- The object to cloneoptions- Optional configuration- Handles primitive types, Date objects, BigInt values, Arrays and plain objects
- Throws if input is not a plain object
clonePlainArray(arr, options?)- Creates a deep clone of an arrayarr- The array to cloneoptions- Same options as clonePlainObject- Throws if input is not an array
Types:
CloneObjectOptions- Configuration options for cloning:unknown?: (object: unknown) => any- Custom handler for unsupported typesthrowable?: boolean- Whether to throw on unsupported types
@tint-ui/tools/error-message
Utilities for handling and formatting error messages.
errorMessage(error, defaultMessage?)- Converts any error type to a standardized error message format
Types:
- ErrorMessage - Standardized error message interface with
messagestring
@tint-ui/tools/is-empty
Utility for checking if a value is empty.
isEmpty(value)- Checks if a value is empty (null, undefined, empty string, empty array, empty object)isEmptyObject(value)- Checks if a value is an empty object (null, undefined, or object with no properties)isEmptyArray(value)- Checks if a value is an empty array (null, undefined, or array with length 0)isEmptyNumber(value)- Checks if a value is an empty number (null, undefined, NaN, or non-finite number)isEmptyString(value)- Checks if a value is an empty string (null, undefined, or empty string "")
@tint-ui/tools/is-plain-object
Utility for checking if a value is a plain JavaScript object.
isObject(value)- Check if value is object (not null)isPlainObject(value)- Checks if value is a plain object (not null, not array, not date, etc.)
@tint-ui/tools/logger
Logging utility with different severity levels.
Logger- Logger classlogger- Default logger instancelogger.debug(message, ...args)- Log debug messageslogger.info(message, ...args)- Log info messageslogger.warn(message, ...args)- Log warning messageslogger.error(message, ...args)- Log error messages
@tint-ui/tools/to-case
String case manipulation utilities.
toCamel(str)- Converts a string to CamelCasetoLowerCamel(str)- Converts a string to lowerCamelCasetoKebab(str)- Converts a string to kebab-casetoSnake(str)- Converts a string to snake_casetoTitle(str)- Converts a string to Title Case
@tint-ui/tools/make-option
Utility for creating InputSelectOption objects, required for @tint-ui/input-checkbox and @tint-ui/input-select packages.
makeOption(item, dump)- Creates an option for a select input
@tint-ui/tools/merge-void-callback
Utility for merging multiple void callback functions into a single function. If one of the callbacks throws an error, subsequent callbacks will not be executed.
mergeVoidCallback(...callbacks)- Merges multiple void callback functions into a single function that executes all callbacks in sequence. Safely handles null/undefined callbacks by skipping them.mergeVoidCallbackAsync(...callbacks)- Similar tomergeVoidCallback, but works with async functions.
@tint-ui/tools/noop
Empty function that does nothing. Contains a single noop function that can be used as a placeholder or default callback.
noop()- Empty function that does nothing and returns void
@tint-ui/tools/proof
Utility for runtime type checking and warnings.
invariant(condition, message)- Throws an error if condition is falsywarning(condition, message)- Logs a warning if condition is falsywarningOnce(key, condition, message)- Logs a warning once for a given key if condition is falsydisableWarning()- Disables all warnings
The warning functions check if a condition is null, false, or NaN/non-finite for numbers. The warning messages are logged using the logger utility and include stack traces in development for easier debugging.
@tint-ui/tools/resize-element
Utility for observing element size changes using ResizeObserver.
resizeElement(element, callback, options)- Observes size changes of a DOM element and calls the callback with new dimensionselement- DOM element to observecallback- Function called with new width/height when element resizesoptions- The options for the resizeElement function.- Returns a cleanup function to stop observing
@tint-ui/tools/resize-page
Utility for observing page resize events.
resizePage(callback, initialEmit, delay)- Adds a listener to the page resize eventcallback- Function called when page resizesinitialEmit- Whether to execute callback immediately (default: false)delay- Debounce delay in milliseconds (default: 200)- Returns a cleanup function to remove the listener
@tint-ui/tools/scroll-page
Utility for observing page scroll events.
scrollPage(callback, initialEmit)- Adds a listener to the page scroll eventcallback- Function called when page scrollsinitialEmit- Whether to execute callback immediately (default: false)- Returns a cleanup function to remove the listener
@tint-ui/tools/use-fork-ref
React hook for combining multiple refs into a single ref. Useful when you need to pass multiple refs to a component.
useForkRef(...refs)- Combines multiple refs into a single ref callbackrefs- Array of refs to combine (RefObject or callback refs)- Returns a single callback ref that updates all provided refs
setRef(ref, value)- Helper function used internally to set a ref valueref- The ref to update (can be callback ref or RefObject)value- The value to set the ref to
Note: This is a React hook and should be used according to the Rules of Hooks.
@tint-ui/tools/use-local-storage-state
React hook for persisting state in localStorage. Works similarly to React.useState but automatically saves/loads the state value to/from localStorage.
useLocalStorageState(key, initialValue)- Creates state that persists in localStoragekey- Unique localStorage key to store the value underinitialValue- Initial state value if nothing exists in localStorage- Returns [value, setValue] tuple like useState
Key differences from React.useState:
- State persists across page refreshes by saving to localStorage
- Initial load checks localStorage first before using initialValue
- State updates are automatically synced to localStorage
- Changes in other tabs/windows trigger state updates
- Supports serializable values (objects, arrays etc) through JSON parsing
Important note about server-side rendering (SSR):
Since this hook reads from localStorage which is only available in the browser, the initial value during server-side rendering will always be the default value, potentially causing hydration mismatches. To handle this:
- Either use this hook only in client-side components
- Or use it together with
useAppMounthook and render component content only after mounting
Note: This is a React hook and should be used according to the Rules of Hooks.
@tint-ui/tools/use-media-query
React hook for detecting if a media query matches. Useful for responsive behavior based on screen size or other media features.
useMediaQuery(query, options?)- Returns whether the media query currently matchesquery- Media query string to check (e.g. '(min-width: 768px)')options- Optional options object:defaultMatches- Initial match state before browser check (default: false)noSsr- Skip SSR handling if true (default: false)
- Returns boolean indicating if query matches
- Re-renders component when match state changes
Key features:
- Automatically handles adding/removing media query listeners
- Updates in real-time as viewport/media changes
- Works with any valid media query string
- Cleans up listeners when component unmounts
Note: This is a React hook and should be used according to the Rules of Hooks.
@tint-ui/tools/use-mount
React hook for checking if the component is mounted. Useful for handling browser-only code and avoiding state updates on unmounted components.
If you are using the tint-ui ecosystem, consider using the useAppMount hook instead - its initial state will be true if the component is already mounted, avoiding hydration conflicts after SSR.
useMount()- Returns whether component is currently mounted- Returns boolean indicating mount status
- Value is
falseduring initial render - Changes to
trueafter component mounts
Note: This is a React hook and should be used according to the Rules of Hooks.
License
MIT
