class-variance-authority
Build type-safe, variant-driven component class names — the styling backbone behind shadcn/ui.
class-variance-authority
General JSBuild 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
npm install class-variance-authorityUsage Example
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
Unified TypeScript SDK for building AI-powered apps — switch between 20+ LLM providers without rewriting code.
animejsGeneral JSLightweight JavaScript animation library with a tiny footprint and a powerful, expressive API.
AOSGeneral JSAn easy-to-use library that makes elements fade, slide, or zoom into view as the user scrolls down your page.
archiverGeneral JSStreaming interface for generating ZIP and TAR archives in Node.js — create compressed archives programmatically from files, directories, and buffers.
@formkit/auto-animateGeneral JS> Add smooth transitions to any element with a single line — zero-config animations for adds, removes, and moves.