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
import { cn } from "@/lib/utils"
// Each row is one call of cn(). The later class wins where two set the same thing.
const calls = [
["px-2 py-1", "px-4"],
["text-sm text-muted-foreground", "text-foreground"],
["h-7 w-7", "size-8"],
["bg-secondary hover:bg-muted", "bg-tint-mint"],
]
export default function UtilsConflictingClasses() {
return (
<dl className="flex w-full max-w-md flex-col divide-y divide-border border-y border-border font-mono text-sm">
{calls.map((classes) => (
<div key={classes.join()} className="flex flex-col gap-1 py-2">
<dt className="text-muted-foreground">cn({classes.map((value) => `"${value}"`).join(", ")})</dt>
<dd>{cn(...classes)}</dd>
</div>
))}
</dl>
)
}
Conditional classes
Invoice INV-1042, due on 30 November
rounded-md px-3 py-2 text-foreground bg-primary-subtle"use client"
import { useState } from "react"
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
import { cn } from "@/lib/utils"
export default function UtilsConditionalClasses() {
const [selected, setSelected] = useState(true)
const [overdue, setOverdue] = useState(false)
// Strings, falsy values and objects of class to condition can be mixed.
const className = cn(
"rounded-md px-3 py-2 text-foreground",
selected && "bg-primary-subtle",
{ "text-destructive-ink": overdue },
)
return (
<div className="flex w-full max-w-sm flex-col gap-4">
<div className="flex flex-wrap gap-x-6 gap-y-2">
<div className="flex items-center gap-2">
<Switch id="utils-selected" checked={selected} onCheckedChange={setSelected} />
<Label htmlFor="utils-selected">Selected</Label>
</div>
<div className="flex items-center gap-2">
<Switch id="utils-overdue" checked={overdue} onCheckedChange={setOverdue} />
<Label htmlFor="utils-overdue">Overdue</Label>
</div>
</div>
<p className={className}>Invoice INV-1042, due on 30 November</p>
<code className="font-mono text-sm text-muted-foreground">{className}</code>
</div>
)
}
A class from the caller
DraftPaidDue soonLarger
import type { ComponentProps } from "react"
import { cn } from "@/lib/utils"
/** The component's own classes come first and the caller's `className` last, so the caller wins. */
function Tag({ className, ...props }: ComponentProps<"span">) {
return (
<span
className={cn("rounded-small bg-secondary px-2 py-0.5 text-xs text-secondary-foreground", className)}
{...props}
/>
)
}
export default function UtilsAClassFromTheCaller() {
return (
<div className="flex flex-wrap items-center justify-center gap-2">
<Tag>Draft</Tag>
<Tag className="bg-tint-mint text-tint-mint-foreground">Paid</Tag>
<Tag className="bg-tint-peach text-tint-peach-foreground">Due soon</Tag>
<Tag className="px-3 text-sm">Larger</Tag>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/utilsbunx --bun shadcn@latest add @khaeli/utilsInstalls 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