class-variance-authority
БесплатноБез исполняемых скриптовНе проверен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…
от TerminalSkills★ 221 звезда на GitHubОб этом скилле
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:
- A variant left
undefinedfalls back todefaultVariants; passingnullswitches the variant off, default included. - Keys named
trueorfalsemake boolean variants (fullWidth,loading); pass real booleans. compoundVariantsapply when every listed variant matches; a value may be an array to match several.- The returned function also accepts
classorclassName, appended last. - Base and variant values can be strings, arrays or
null.
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
- Always wrap CVA output in
cn()when a component acceptsclassName, otherwise overrides are unreliable. - Use
VariantProps<typeof buttonVariants>for prop types; wrap the HTML attributes inOmit<..., 'size'>(for exampleOmit<InputHTMLAttributes<HTMLInputElement>, 'size'> & VariantProps<...>) if a variant name clashes with an HTML attribute, because the HTMLsizeattribute on inputs is a number. - Tailwind scans source text, so write complete class names in the variant maps; building
`bg-${color}-500`at runtime is not detected and ships no CSS. - Use
tailwind-mergev3 with Tailwind v4 (v2 targets Tailwind v3). - Keep one
cvadefinition per component and export it, so other elements (a link styled as a button) can reuse the same classes. - CVA is for variants, not state logic; compute booleans in the component and pass them in.
Установить class-variance-authority в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл class-variance-authority?
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 conditional className ternaries, or typing component props with VariantProps.
Как установить скилл class-variance-authority?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл class-variance-authority запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic