clsx
A tiny JavaScript function that builds a class string from strings, objects and conditions, skipping anything falsy.
In more detail
clsx("btn", { active: isActive }, disabled && "opacity-50") returns only the parts whose conditions are truthy. It does not know about Tailwind conflicts, which is why cn passes its result through tailwind-merge.
Related terms
- cn utility — A tiny helper, cn(...classes) = twMerge(clsx(classes)), that joins class names conditionally and then removes conflicting Tailwind classes.
- truthy and falsy — In JavaScript, values that count as true or false when used in a condition: false, 0, empty string, null, undefined and NaN are falsy, everything else is truthy.
- tailwind-merge — A JavaScript library that removes Tailwind classes overridden by later ones in the same list, such as dropping p-4 when px-6 follows.
Taught in
- Class Variance Authority: Type-Safe Variant & Prop Orchestration — Design Systems & Component Engineering