Class Variance Authority
A small JavaScript library that turns a component's variants, such as intent and size, into the right list of CSS classes.
In more detail
You call cva() with base classes, a variants object, optional compoundVariants and defaultVariants. The returned function takes props such as { intent: "danger", size: "lg" } and returns a class string. Its VariantProps type lets TypeScript derive the component's props from the same definition.
Related terms
- variant — A named option of a component, such as intent primary or danger and size sm or lg, each mapped to its own styling.
- compoundVariants — A CVA option that adds classes only when a specific combination of variants is chosen, such as intent danger with size lg.
- defaultVariants — A CVA option giving the variant values to use when the caller does not pass one.
- VariantProps — A TypeScript helper from CVA that extracts a component's prop types from its cva() definition.
- cn utility — A tiny helper, cn(...classes) = twMerge(clsx(classes)), that joins class names conditionally and then removes conflicting Tailwind classes.
Taught in
- Class Variance Authority: Type-Safe Variant & Prop Orchestration — Design Systems & Component Engineering