Tailwind CSS Course
Tailwind CSS
/
Advanced

Class Variance Authority (CVA)

Definition

A popular JavaScript library (`class-variance-authority`) used to construct highly complex, scalable React components with multiple visual variants (e.g., solid, outline, ghost) using Tailwind classes.

Explain Like I'm New

Building a React Button component that takes an `intent` prop (primary/danger) and a `size` prop (small/large). CVA elegantly handles the messy logic of stitching all those different Tailwind classes together based on the props you pass in.

Real World Example

The foundation of the wildly popular `shadcn/ui` component library. It uses CVA to manage all component variants.

Common Use Cases

  • •Enterprise component libraries
  • •Design systems

Interactive Example

Loading Code Playground...

Interview Questions

basic

  • What problem does CVA solve when building Tailwind components in React?

intermediate

  • Does CVA conflict with Tailwind, or complement it?

Flash Cards

Question

What problem?

Click to reveal answer
Answer

It solves 'Class Name Spaghetti'. Writing massive ternary operators (`className={intent === 'primary' ? 'bg-blue-500' : 'bg-red-500'}`) becomes unreadable. CVA organizes these variants neatly into an object.

Question

Conflict or complement?

Click to reveal answer
Answer

It complements it perfectly. CVA is just a JavaScript string-builder. It generates the final string of Tailwind classes to give to the DOM.