Class variance authority

From TerminalSkills/skills · View interactive version

Class variance authority is an AI agent skill from the GitHub repository TerminalSkills/skills. Class Variance Authority (CVA) is a small TypeScript library that turns a base class string plus named variants into a typed function returning class names. Use when building Tailwind components with variants such as size, intent or state, writing a shadcn/ui-style Button or Badge, replacing conditi

How to use this skill

Skill file

Class Variance Authority (CVA)

Overview

class-variance-authority (0.7.1, the current stable release) exports cva, cx and the VariantProps type. You describe a base string, a set of variants, optional compoundVariants and defaultVariants; calling the result with props returns one class string. It does no CSS conflict resolution, so it is usually paired with clsx and tailwind-merge in a cn() helper. shadcn/ui builds its components this way. A newer rewrite is published as cva (still in beta, 1.0.0-beta.x); this skill covers class-variance-authority.

Instructions

Step 1: Install and add the `cn` helper

npm install class-variance-authority clsx tailwind-merge
// lib/utils.ts
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

Step 2: Define variants

// components/button.tsx
import { cva, type VariantProps } from 'class-variance-authority'
import type { ButtonHTMLAttributes } from 'react'
import { cn } from '@/lib/utils'

export const buttonVariants = cva(
  'inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors disabled:pointer-events-none disabled:opacity-50',
  {
    variants: {
      intent: {
        primary: 'bg-blue-600 text-white hover:bg-blue-700',
        danger: 'bg-red-600 text-white hover:bg-red-700',
        ghost: 'hover:bg-gray-100',
      },
      size: { sm: 'h-8 px-3', md: 'h-10 px-4', lg: 'h-12 px-6' },
      fullWidth: { true: 'w-full' },
      loading: { true: 'cursor-wait opacity-70', false: null },
    },
    compoundVariants: [
      { intent: 'danger', size: 'lg', class: 'font-bold uppercase' },
      { intent: ['primary', 'danger'], size: ['sm', 'md'], class: 'shadow-sm' },
    ],
    defaultVariants: { intent: 'primary', size: 'md' },
  },
)

export type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> &
  VariantProps<typeof buttonVariants>

export function Button({ className, intent, size, fullWidth, loading, ...props }: ButtonProps) {
  return (
    <button
      className={cn(buttonVariants({ intent, size, fullWidth, loading }), className)}
      {...props}
    />
  )
}

How it behaves:

Examples

Example 1: "Add a status badge with sizes"

const badgeVariants = cva('inline-flex items-center rounded-full font-semibold', {
  variants: {
    status: {
      active: 'bg-green-100 text-green-800',
      paused: 'bg-yellow-100 text-yellow-800',
      failed: 'bg-red-100 text-red-800',
    },
    size: { sm: 'px-1.5 text-[10px]', md: 'px-2.5 py-0.5 text-xs' },
  },
  defaultVariants: { status: 'active', size: 'md' },
})

<span className={badgeVariants({ status: 'failed', size: 'sm' })}>Deploy failed</span>

The call returns inline-flex items-center rounded-full font-semibold bg-red-100 text-red-800 px-1.5 text-[10px]. TypeScript rejects status: 'done'.

Example 2: "Let callers override padding on the Button"

<Button size="sm" className="px-8">Save</Button>

Because Button wraps the result in cn(), tailwind-merge drops px-3 and keeps px-8. Calling buttonVariants({ size: 'sm', class: 'px-8' }) directly returns both px-3 and px-8 in the string, since CVA only concatenates; the browser then picks whichever Tailwind emitted later in the stylesheet.

Guidelines

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.