Common utilities and types for Chakra UI providing type checking, object manipulation, DOM utilities, and React helpers
npx @tessl/cli install tessl/npm-chakra-ui--utils@2.2.0Chakra UI Utils is a comprehensive TypeScript utility library that provides essential functions and types for building accessible and responsive React applications. It offers 100+ utilities across 35 modules including type checking functions, object manipulation utilities, DOM event handling, accessibility helpers, responsive breakpoint utilities, React context helpers, and mathematical operations.
npm install @chakra-ui/utilsimport {
isString, isNumber, isObject, // Type checking
omit, pick, merge, get, // Object manipulation
cx, dataAttr, ariaAttr, // Utility functions
addDomEvent, getEventPoint, // DOM utilities
createContext, getValidChildren, // React utilities
toPrecision, clampValue, // Mathematical utilities
isFocusable, getAllTabbable, // Accessibility
mapResponsive, breakpoints // Responsive design
} from "@chakra-ui/utils";For CommonJS:
const {
isString, isNumber, omit, pick, cx,
addDomEvent, createContext, toPrecision
} = require("@chakra-ui/utils");import {
isString, omit, cx, dataAttr,
clampValue, isFocusable
} from "@chakra-ui/utils";
// Type checking
if (isString(value)) {
console.log("It's a string:", value.toUpperCase());
}
// Object manipulation
const cleanProps = omit(props, ["children", "className"]);
// CSS class combination
const className = cx("btn", isActive && "btn--active", customClass);
// Data attributes for CSS/testing
const dataProps = {
"data-active": dataAttr(isActive),
"aria-expanded": ariaAttr(isExpanded)
};
// Mathematical operations
const constrainedValue = clampValue(userInput, 0, 100);
// Accessibility checks
if (isFocusable(element)) {
element.focus();
}Chakra UI Utils is organized around several key architectural patterns:
Comprehensive type guards and validation functions for JavaScript values. Essential for runtime type checking and TypeScript type narrowing.
function isString(value: any): value is string;
function isNumber(value: any): value is number;
function isObject(value: any): value is Record<string, any>;
function isArray<T>(value: any): value is Array<T>;
function isFunction<T extends Function>(value: any): value is T;
function isEmpty(value: any): boolean;Utilities for manipulating objects including picking, omitting, merging, splitting, and deep property access. Optimized for React prop handling and configuration objects.
function omit<T, K extends keyof T>(object: T, keysToOmit: K[]): Omit<T, K>;
function pick<T, K extends keyof T>(object: T, keysToPick: K[]): { [P in K]: T[P] };
function get(obj: Record<string, any>, path: string | number, fallback?: any): any;
function split<T, K extends keyof T>(object: T, keys: K[]): [{ [P in K]: T[P] }, Omit<T, K>];Cross-browser DOM utilities for event handling, element queries, and pointer interactions. Includes support for mouse, touch, and pointer events with unified API.
function addDomEvent(
target: EventTarget,
eventName: string,
handler: EventListener,
options?: AddEventListenerOptions
): () => void;
function getEventPoint(event: AnyPointerEvent, type?: PointType): { x: number, y: number };
function contains(parent: HTMLElement | null, child: HTMLElement): boolean;Cross-browser DOM utilities for element queries, document access, and scroll handling. Essential for building components that work consistently across different browser environments.
function getOwnerDocument(node?: Element | null): Document;
function getOwnerWindow(node?: Element | null): typeof globalThis;
function getEventWindow(event: Event): typeof window;
function getActiveElement(node?: HTMLElement): HTMLElement;
function contains(parent: HTMLElement | null, child: HTMLElement): boolean;
function getScrollParent(el: HTMLElement): HTMLElement;
function warn(options: WarnOptions): void;Comprehensive accessibility utilities for focus management, tabbable elements, and ARIA attributes. Follows WAI-ARIA guidelines and supports keyboard navigation patterns.
function isFocusable(element: HTMLElement): boolean;
function isTabbable(element?: HTMLElement | null): boolean;
function getAllFocusable<T extends HTMLElement>(container: T): T[];
function getAllTabbable<T extends HTMLElement>(container: T, fallbackToFocusable?: boolean): T[];
function dataAttr(condition: boolean | undefined): Booleanish;
function ariaAttr(condition: boolean | undefined): true | undefined;Utilities for working with responsive breakpoints, media queries, and responsive prop values. Supports both object and array notation for responsive values.
function mapResponsive(prop: any, mapper: (val: any) => any): any;
function analyzeBreakpoints(breakpoints: Record<string, any>): AnalyzeBreakpointsReturn | null;
function px(value: number | string | null): string | null;
function toMediaQueryString(min: string | null, max?: string): string;
const breakpoints: ["base", "sm", "md", "lg", "xl", "2xl"];Specialized utilities for React applications including context creation with error handling, children filtering, and prop manipulation patterns.
function createContext<T>(options: CreateContextOptions<T>): CreateContextReturn<T>;
function getValidChildren(children: React.ReactNode): React.ReactElement[];
function splitProps(props: Dict, ...keys: Key[]): Dict[];
interface CreateContextOptions<T> {
strict?: boolean;
hookName?: string;
providerName?: string;
errorMessage?: string;
name?: string;
defaultValue?: T;
}Number manipulation utilities including precision control, percentage calculations, clamping, and step-based rounding. Designed for UI controls like sliders and numeric inputs.
function toPrecision(value: number, precision?: number): string;
function clampValue(value: number, min: number, max: number): number;
function valueToPercent(value: number, min: number, max: number): number;
function percentToValue(percent: number, min: number, max: number): number;
function roundValueToStep(value: number, from: number, step: number): string;Utilities for function composition, conditional execution, and event handler management. Includes patterns for combining multiple event handlers and lazy evaluation.
function callAll<T extends AnyFunction>(...fns: (T | undefined)[]): (...args: Parameters<T>) => void;
function runIfFn<T, U>(valueOrFn: T | ((...fnArgs: U[]) => T), ...args: U[]): T;
function cx(...classNames: any[]): string;
function lazyDisclosure(options: LazyOptions): boolean;
function interopDefault<T>(mod: T): T;// Core utility types
type AnyFunction<T = any> = (...args: T[]) => any;
type Dict<T = any> = Record<string, T>;
type Merge<M, N> = N extends Record<string, unknown> ? M : Omit<M, keyof N> & N;
// Component utility types
type LazyMode = "unmount" | "keepMounted";
interface LazyOptions {
enabled?: boolean;
isSelected?: boolean;
wasSelected?: boolean;
mode?: LazyMode;
}
interface WarnOptions {
condition: boolean;
message: string;
}
// Event types
type AnyPointerEvent = MouseEvent | TouchEvent | PointerEvent;
type PointType = "page" | "client";
interface Point {
x: number;
y: number;
}
// Element types
interface FocusableElement {
focus(options?: FocusOptions): void;
}
// ARIA and DOM types
type Booleanish = boolean | "true" | "false";
// React prop types
type PropGetter<P = Record<string, unknown>, R = DOMAttributes> = (
props?: Merge<DOMAttributes, P>,
ref?: React.Ref<any>
) => R & React.RefAttributes<any>;
type MaybeRenderProp<P = Record<string, unknown>> =
React.ReactNode | ((props: P) => React.ReactNode);
// ARIA and accessibility interfaces
interface AriaLabelingProps {
"aria-label"?: string;
"aria-labelledby"?: string;
"aria-describedby"?: string;
"aria-details"?: string;
}
interface AriaValidationProps {
"aria-errormessage"?: string;
}
interface IdProps {
id?: string;
}
// DOM attributes
interface DOMAttributes<T = DOMElement> extends React.AriaAttributes, React.DOMAttributes<T> {
id?: string;
role?: React.AriaRole;
tabIndex?: number;
style?: React.CSSProperties;
[dataAttr: string]: any;
}
interface DOMElement extends Element, HTMLOrSVGElement {}
// Input-specific props
interface InputDOMProps extends IdProps {
autoComplete?: string;
maxLength?: number;
minLength?: number;
name?: string;
pattern?: string;
placeholder?: string;
type?: "text" | "search" | "url" | "tel" | "email" | "password" | "hidden" | (string & {});
inputMode?: "none" | "text" | "tel" | "url" | "email" | "numeric" | "decimal" | "search";
}