A tiny library for building type-safe, variant-driven class names with Tailwind CSS or any other styling approach
open sourceApache-2.0self-hostablefree
Class Variance Authority (cva) builds a component's class names from its variants: you declare base classes, variants such as intent and size, default variants and compound variants that apply when several conditions match, and get a function that returns the right class string for the props you pass, with TypeScript types for them. It works with Tailwind CSS or any other styling approach (plain CSS, BEM, CSS Modules) and in React, Vue, Svelte, Astro or 11ty, giving the type-safe variants of CSS-in-TS libraries such as vanilla-extract while you keep control of your stylesheet. Version 1.0, in beta as the cva package, adds first-class composition. It's Apache-2.0 and privately maintained by Joe Bell.
Problems it solves
- manually matching CSS classes to component props for every variant is tedious and error-prone
- typed variant props for our Tailwind components without a CSS-in-JS library
- apply extra classes only when a component has two variants at once, like primary and large
- keep full control of our stylesheet but still get Stitches-style variants
- the same variant logic for components written with BEM classes or CSS Modules
- set default variants so most call sites don't pass every prop
Features
- variants, default variants and compound variants that resolve to class names
- TypeScript types for variant props
- works with Tailwind CSS, plain CSS, BEM and CSS Modules
- examples for React, Vue, Svelte, Astro and 11ty
- extending and composing components; first-class composition with composes in the 1.0 beta
- llms.txt and Markdown copies of the docs
Alternatives
The same job
These do what Class Variance Authority does.
- Tailwind Variants A first-class variant API for Tailwind CSS
