class-variance-authority

Build type-safe, variant-driven component class names — the styling backbone behind shadcn/ui.

class-variance-authority

General JS

Build type-safe, variant-driven component class names — the styling backbone behind shadcn/ui.

Fit

Bundle (gzip)

655 B

1.3 kB raw

Install size includes transitive dependencies. Bundle size is gzipped browser payload.

Freshness

The Problem

Tailwind components with multiple looks (variant, size, state) devolve into unreadable ternary soup inside className. Keeping the combinations consistent and typed — so a size="lg" actually maps to the right classes — is hard to do by hand.

What It Does

CVA (class-variance-authority) is exactly what shadcn/ui uses under the hood to define component variants. You declare base classes, named variants, and default variants once; calling the resulting function with props returns the correct class string. It's fully typed, so invalid variant values are caught at compile time, and it pairs naturally with tailwind-merge.

Installation

bash
npm install class-variance-authority

Usage Example

js
import { cva } from 'class-variance-authority';

const button = cva('inline-flex items-center rounded-md font-medium', {
  variants: {
    intent: { primary: 'bg-black text-white', ghost: 'bg-transparent' },
    size: { sm: 'h-8 px-3 text-sm', lg: 'h-11 px-6 text-base' },
  },
  defaultVariants: { intent: 'primary', size: 'sm' },
});

button({ intent: 'ghost', size: 'lg' });

Related packages