cn
Joins class names and resolves conflicting Tailwind CSS utilities.
cn combines class names from strings, arrays, objects and conditionals, then resolves conflicting Tailwind CSS utilities so the last one wins. It is the cn package, a drop-in replacement for twMerge(clsx(...)), re-exported through a Dify UI subpath.
tsx
Usage guidelines
- Put defaults first: when a component merges its own classes with an incoming
className, pass the defaults before the caller's value so the caller can override them. - Resolve state callbacks before merging: a
className(state)callback is a function, not a class value. Call it with the part's state and pass the resulting string tocn. - Override within the same utility family:
cnresolvespx-2againstpx-3, but not a physical utility against a logical one.pl-*andps-*both stay in the output, so caller overrides use the same family as the defaults. See Styling. - Prefer data attributes for state: before writing a conditional class from React state, check whether the primitive exposes a data attribute such as
data-checkedordata-disabledthat a Tailwind variant can read. - Skip it for a single static string: a literal
classNamewith no caller override and no conditions does not needcn.
Class values can be strings, numbers, bigints, booleans, nullish values, nested arrays or dictionaries. Falsy values are dropped, object keys are kept when their value is truthy, and arrays are flattened.
Examples
Defaults with a caller override
tsx
Conditional classes
tsx
API reference
cn
Merge Tailwind CSS classes with clsx-style arguments (strings, arrays, objects, conditionals). Drop-in replacement for twMerge(clsx(...)).
| Parameter | Type | Description |
|---|---|---|
...inputs required | ClassValue[] | No description |
Returns
string