or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

dom-events.mddom-utilities.mdfocus-accessibility.mdfunction-utilities.mdindex.mdmath-utilities.mdobject-manipulation.mdreact-utilities.mdresponsive.mdtype-checking.md
tile.json

tessl/npm-chakra-ui--utils

Common utilities and types for Chakra UI providing type checking, object manipulation, DOM utilities, and React helpers

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@chakra-ui/utils@2.2.x

To install, run

npx @tessl/cli install tessl/npm-chakra-ui--utils@2.2.0

index.mddocs/

Chakra UI Utils

Chakra 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.

Package Information

  • Package Name: @chakra-ui/utils
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @chakra-ui/utils

Core Imports

import { 
  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");

Basic Usage

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();
}

Architecture

Chakra UI Utils is organized around several key architectural patterns:

  • Functional API: Pure utility functions that can be imported individually for tree-shaking
  • Type Safety: Full TypeScript integration with comprehensive type guards and interfaces
  • Browser Compatibility: Cross-browser utilities for DOM manipulation and event handling
  • Accessibility First: Built-in ARIA and focus management utilities following WAI-ARIA standards
  • React Integration: Specialized utilities for React components, context, and prop handling
  • Responsive Design: Breakpoint analysis and responsive value mapping utilities

Capabilities

Type Checking Utilities

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;

Type Checking

Object Manipulation

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>];

Object Manipulation

DOM and Event Utilities

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;

DOM and Events

DOM Utilities

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;

DOM Utilities

Focus and Accessibility

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;

Focus and Accessibility

Responsive Design

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"];

Responsive Design

React Utilities

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;
}

React Utilities

Mathematical Utilities

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;

Mathematical Utilities

Function and Control Flow

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;

Function Utilities

Types and Interfaces

// 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";
}