Skip to content
Class Utilities

Compose Tailwind classes with cva, clsx, and TwMerge

ShardUI keeps component styling close to the markup without letting class strings become noise. Use cva to turn design choices into reusable variant recipes, clsx to keep conditional classes readable, and TwMerge through CssUtils.cn so caller overrides resolve predictably instead of piling up conflicting Tailwind utilities.

Simple Example
Conditional classes, then merge

clsx builds the class string from base classes, conditional values, and user classes. CssUtils.cn then merges that result with TwMerge.

Live Result
clsx output vs merged output

Preview card

Try adding p-2, bg-destructive, or rounded-full.

Raw clsx result

rounded-md border border-input bg-background p-4 text-sm text-foreground transition-colors border-primary bg-primary/10 text-primary p-6 rounded-xl shadow-md

After CssUtils.cn

border text-sm transition-colors border-primary bg-primary/10 text-primary p-6 rounded-xl shadow-md
Sophisticated Example
Create a reusable variant recipe

cva owns the stable component recipe. clsx still handles one-off conditions, and TwMerge keeps consumer overrides ergonomic.

Try bg-primary text-primary-foreground px-8 when you want to see caller classes intentionally override the selected variants.

Variant Result
Recipe plus caller classes

Intent

Success

Size

Md

Density

Cozy

cva + clsx result

inline-flex items-center justify-center rounded-md border text-sm font-medium shadow-xs transition-all outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 border-emerald-500/30 bg-emerald-500/10 text-emerald-700 hover:bg-emerald-500/20 dark:text-emerald-300 h-9 px-4 py-2 gap-2 [&_svg:not([class*='size-'])]:size-4 shadow-md

After CssUtils.cn

inline-flex items-center justify-center rounded-md border text-sm font-medium transition-all outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 border-emerald-500/30 bg-emerald-500/10 text-emerald-700 hover:bg-emerald-500/20 dark:text-emerald-300 h-9 px-4 py-2 gap-2 [&_svg:not([class*='size-'])]:size-4 shadow-md
An unhandled error has occurred. Reload 🗙

    Rejoining the server...

    Rejoin failed... trying again in seconds.

    Failed to rejoin.
    Please retry or reload the page.

    The session has been paused by the server.

    Failed to resume the session.
    Please reload the page.