Skip to content

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 to cn.
  • Override within the same utility family: cn resolves px-2 against px-3, but not a physical utility against a logical one. pl-* and ps-* 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-checked or data-disabled that a Tailwind variant can read.
  • Skip it for a single static string: a literal className with no caller override and no conditions does not need cn.

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(...)).

Parameters
ParameterTypeDescription
...inputs requiredClassValue[]No description

Returns

string