Sub topic 8
Defining variants with cva
This chapter assumes you know React props and basic TypeScript, and that you have read the previous chapter on the cn helper. It also assumes you can read Tailwind utility classes.
Why variants need a tool
A button usually comes in several looks (default, outline) and sizes (default, small). Without a helper, you end up with nested ternaries inside className. These are hard to read, and nothing checks that the values you pass are valid.
class-variance-authority, usually called cva, moves that logic into a declared object. You describe the variants once. cva returns a function that turns props into a class string.
The anatomy of a cva call
import { cva, type VariantProps } from "class-variance-authority"
export const buttonVariants = cva(
"inline-flex items-center justify-center rounded-md text-sm font-medium",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
outline: "border border-input bg-background hover:bg-accent",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 px-3",
},
},
compoundVariants: [
{ variant: "outline", size: "sm", class: "border-2" },
],
defaultVariants: { variant: "default", size: "default" },
}
)
console.log(buttonVariants({ variant: "outline", size: "sm" }))
console.log(buttonVariants())The call has two arguments:
- Base classes (first argument) are always applied.
variantsmaps each variant name to its options. Each option maps to the classes it adds.defaultVariantssupplies a value when the caller omits one. This is whybuttonVariants()still returns a complete class string.compoundVariantsadds classes only when several variant values match at once. Here, a small outline button gets a thicker border.
flowchart TDTyping props with VariantProps
VariantProps<typeof buttonVariants> extracts the allowed props from the definition. The component's types then follow the variants, so variant="ghost" is a compile error until you add a ghost option.
import * as React from "react"
import { cn } from "@/lib/utils"
import { buttonVariants } from "./button-variants"
import type { VariantProps } from "class-variance-authority"
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {}
export function Button({ className, variant, size, ...props }: ButtonProps) {
return (
<button
className={cn(buttonVariants({ variant, size }), className)}
{...props}
/>
)
}The order inside cn matters. The caller's className comes last, so tailwind-merge lets it override a conflicting variant class. For example, className="h-12" replaces h-10.
Common errors
- Expecting cva to resolve conflicts. cva only joins strings. If two classes conflict, such as
h-10andh-12, you needcnaround it. - Building class names dynamically. Tailwind scans source files for complete class names. Writing `
bg-${color}-500` produces classes that never get generated. Write each full class in the variant map. - Adding variants for one-off tweaks. If only one call site needs a style, pass it through
className. Keep variants for looks that repeat. - Forgetting defaults. Without
defaultVariants, omitting a prop adds no variant classes, and the element can look unstyled.
Exercise
You add a destructive option to variant in buttonVariants. A teammate also wants destructive buttons to have no shadow only when size is sm. Which cva feature expresses that rule, and what does the entry look like?
Show answer
Use compoundVariants, which applies classes only when several variant values match. The entry is { variant: "destructive", size: "sm", class: "shadow-none" }.
Exercise
A caller passes className="h-12" to your Button, but the button stays h-10. What is the most likely cause?
Show answer
The component is joining classes without cn (twMerge), or it places the caller's className before the variant classes. With plain string joining, both h-10 and h-12 apply and the stylesheet order decides. Use cn(buttonVariants({ variant, size }), className) so the caller's class comes last and wins.