cx
A class name composition utility that combines conditional class names and resolves Tailwind CSS conflicts using left-to-right merge order.
Powered by a vendored, byte-for-byte port of clsx (conditional class handling) and tailwind-merge (conflict resolution), adapted from cnfast. Later classes override earlier ones (same behavior as inline styles). Mantle's Tailwind overrides, the em spacing scale (w-em, p-em, …) and the text-mono / text-size-inherit font-size utilities, are baked in, so neither clsx nor tailwind-merge is a runtime dependency.
Usage
Tailwind conflict resolution
cx uses tailwind-merge to resolve conflicts between Tailwind utility classes. When two classes target the same CSS property, the last one wins:
This makes cx safe to use for component prop overrides:
Tagged template
cx can also be called as a tagged template. A tagged template reuses the same static
strings array on every render, so cx caches the result by call-site identity, useful for
hot re-rendering paths. The behavior is otherwise identical to the call form.
API
Accepts any combination of:
The return value is a single deduplicated, conflict-resolved class string.
Convention
All className values in Mantle must be built with cx.
String interpolation, +, ternaries, or conditional expressions directly on className are not allowed; see CONVENTIONS.md.