Web · APIs and utilities
Utilities
Passes its props to its single child instead of rendering its own element.
Import
Passes its props to its single child instead of rendering its own element. The asChild prop on components works through this; e.g. used to give a Button's look to a Next.js Link element.
Merge rules:
classNamevalues are merged withcn; the child's wins.styleobjects are merged; the child's wins (CSS anchor names from both are kept).- Both event handlers starting with
onrun; the child's first. aria-describedbyID lists are merged.refs are merged.- For other props the child's value wins.
Examples
cn
functionConditionally joins class names and keeps only the last of conflicting Tailwind classes.
All components use it so their own classes can be safely overridden by a className passed from outside. Uses no external dependency; replaces the clsx + tailwind-merge pair.
Examples
cx
functionConditionally joins class names; does not resolve conflicts. Works like clsx and is faster than cn. Use it where you are sure there will be no conflicts.
Examples
composeRefs
functionCombines several refs into a single ref callback. Used to attach both the component's own ref and the user's ref to one element. Supports React 19's ref cleanup functions.
Examples
useComposedRefs
functionHook version of composeRefs. Returns the same callback as long as the refs do not change, so React does not reattach the ref on every render.
Examples
mergeClasses
functionResolves conflicting Tailwind classes; of classes targeting the same property, only the last remains.
In Tailwind, which class wins depends on its order in the generated CSS, not in className. So the result of a combination like "px-4 px-2" is unpredictable. This function lets a className passed to a component safely override the component's own classes.
Scope: common groups of Tailwind v4's default utilities (spacing, sizing, color, typography, borders, layout, effects and transforms). Unknown classes are left untouched.
Examples
prefersReducedMotion
functionHas the user turned on "reduce motion" in the OS? Returns false on the server and in environments without matchMedia.
Examples
scrollBehavior
functionScroll behavior for scrollTo / scrollIntoView: "auto" (instant) if motion is reduced, otherwise "smooth".
Examples
variants
functionCreates a typed function that generates classes from a component's variants. Same idea as class-variance-authority (cva), with no external dependency. Since the result is merged with cn, a className passed from outside overrides conflicting classes.
Examples
Placement
typePlacement of a floating element (Menu, Popover, Tooltip…) relative to its trigger: the side and the alignment on that side.
"auto": The component picks the best place itself and moves elsewhere if it does not fit on screen. Menu/Popover start below with left edges aligned; without room they align right or flip to the top. Tooltip starts centered on top."top","bottom","left","right": Centered on that side (same as"top-center")."bottom-left": Below; left edge aligned with the trigger's left edge, opening to the right."bottom-right"aligns right edges, opening to the left."top-left","top-right"the same way on top."right-top": On the right; top edges aligned, opening downward."right-bottom"aligns bottom edges, opening upward."left-top","left-bottom"the same way on the left.
If the chosen placement does not fit, it flips to the opposite side and/or alignment.
ClassValue
typeValue types accepted by cn.
stringandnumberare added as-is.false,null,undefinedand empty values other than0are ignored.- Arrays are flattened.
- For objects, keys with truthy values are added.
SlotProps
interfaceProps of the Slot component.
Extends: HTMLAttributes<HTMLElement>
| Name | Type | Description |
|---|---|---|
children? | ReactNode | A single React element to pass the props to. |
ref? | Ref<HTMLElement> | Ref merged with the child's own ref. |
Tone
typeSemantic color of components. The same names are used across the library (web and React Native) (Button, Badge, Alert, Progress, toast…).
primary: the theme's primary color.neutral: uncolored, plain.danger: errors and irreversible actions.success: success and confirmation.warning: warning.
Size
typeCommon component sizes.
VariantSchema
typeVariant definition: a value → class map for each variant name.
VariantSelection
typeSelection object generated from a variant schema.
VariantsConfig
interfaceConfiguration of the variants function.
| Name | Type | Description |
|---|---|---|
base? | ClassValue | Base classes always added. |
variants? | V | Variants and the classes of each value. |
defaultVariants? | VariantSelection<V> | Variant values used when nothing is selected. |
compoundVariants? | Array<{ [K in keyof V]?: StringToBoolean<keyof V[K]> | ReadonlyArray<StringToBoolean<keyof V[K]>>; } & { className: ClassValue; }> | Classes added when several variants are selected together. If a condition is given as an array, any of its values matching is enough. |
VariantsFunction
typeFunction generated by variants.
VariantProps
typeInfers the type of the variant selections accepted by a function created with variants. Used when defining component props.