Skip to content

Utils

cn(): clsx plus tailwind-merge.

cn() joins class names and settles conflicts between Tailwind utilities: of two classes that set the same property, the one written later stays. It takes strings, arrays and objects of class to condition, and ignores false, null and undefined. Every Registry component passes its own classes first and your className last, which is why a class you pass replaces the component's.

Conflicting classes

cn("px-2 py-1", "px-4")
py-1 px-4
cn("text-sm text-muted-foreground", "text-foreground")
text-sm text-foreground
cn("h-7 w-7", "size-8")
size-8
cn("bg-secondary hover:bg-muted", "bg-tint-mint")
hover:bg-muted bg-tint-mint

Conditional classes

Invoice INV-1042, due on 30 November

rounded-md px-3 py-2 text-foreground bg-primary-subtle

A class from the caller

DraftPaidDue soonLarger

Installation

npx shadcn@latest add @khaeli/utils

Installs with

npm packages
  • clsx ^2.1.1
  • tailwind-merge ^3.5.0

Registry JSON

The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/utils.json