CVA 驱动的设计系统组件工程
简介
基于 CVA 和 Surface 原语构建可复用的设计系统组件,提供类型安全的变体 API 与设计令牌一致性保障。
原始描述
Patterns for building design system components using Surface primitives, CVA variants, and consistent styling. Use when building reusable UI components that follow design token architecture. Triggers on Surface component, CVA, class-variance-authority, component variants, design tokens.
通过你的 Agent 添加
安装目录:design-system-components/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
中文使用说明 来自收录源
核心用法
本技能提供了一套基于 CVA (class-variance-authority) 和 Surface 原语的设计系统组件构建模式,涵盖六大核心模式:
- Surface Primitive — 统一的分层表面组件,通过
layer变体(panel/tile/chip/deep/metric/glass)实现一致的视觉层级,支持交互状态与发光效果 - CVA Button Variants — 完整的按钮变体系统,包含 default/destructive/outline/ghost/link/cyber 等样式及 sm/default/lg/icon 尺寸
- Metric Display — 数据指标展示组件,支持 lg/md/sm 尺寸与 positive/negative/neutral 趋势状态
- Card with Header — 基于 Surface 封装的卡片组件,集成标题、描述与操作区域
- Badge/Chip Variants — 标签徽章系统,覆盖 default/success/warning/destructive/outline 变体
- Composing Variants — CVA 与条件类名组合模式,处理动态状态如在线指示器
显著优点
- 类型安全: CVA 自动生成 TypeScript 变体类型,IDE 智能提示完备
- 设计令牌一致性: 强制使用
bg-tone-*、border-tone-* 等语义化令牌,杜绝硬编码
- 组合灵活性: Surface 原语覆盖 90% 容器场景,减少自定义卡片实现
- 开发效率:
cn() 工具函数处理类名合并,条件渲染简洁
- 可维护性: 变体逻辑集中管理,样式变更单点修改
潜在局限
- 学习曲线: 需理解 CVA 变体语法与 Tailwind 类名优先级规则
- 运行时开销: CVA 在运行时计算类名字符串,高频渲染场景(如大数据表格)需注意性能
- 设计令牌耦合: 强依赖预设的
tone-*、glass-* 等令牌,迁移至其他设计系统需重构
- 无障碍支持: 示例中未内置 ARIA 属性,需开发者自行补充
适合人群
- 使用 React + Tailwind CSS 构建组件库的前端工程师
- 需要建立企业级设计系统的技术负责人
- 追求类型安全与样式一致性的 TypeScript 开发者
- 熟悉 Shadcn UI 设计理念的开发者(模式高度兼容)
常规风险
风险类型
说明
缓解建议
类名冲突
cn() 合并时 Tailwind 优先级可能非预期
使用 tailwind-merge 确保正确覆盖
令牌漂移
硬编码颜色绕过设计系统
ESLint 规则强制 tone-* 前缀使用
过度抽象
Surface 无法满足特殊场景时强行套用
文档明确「NEVER」条款,允许合理例外
无障碍缺陷
交互组件缺少键盘与屏幕阅读器支持
结合 @radix-ui 原语补充 A11y
bg-tone-*、border-tone-* 等语义化令牌,杜绝硬编码cn() 工具函数处理类名合并,条件渲染简洁- 学习曲线: 需理解 CVA 变体语法与 Tailwind 类名优先级规则
- 运行时开销: CVA 在运行时计算类名字符串,高频渲染场景(如大数据表格)需注意性能
- 设计令牌耦合: 强依赖预设的
tone-*、glass-*等令牌,迁移至其他设计系统需重构 - 无障碍支持: 示例中未内置 ARIA 属性,需开发者自行补充
适合人群
- 使用 React + Tailwind CSS 构建组件库的前端工程师
- 需要建立企业级设计系统的技术负责人
- 追求类型安全与样式一致性的 TypeScript 开发者
- 熟悉 Shadcn UI 设计理念的开发者(模式高度兼容)
常规风险
风险类型
说明
缓解建议
类名冲突
cn() 合并时 Tailwind 优先级可能非预期
使用 tailwind-merge 确保正确覆盖
令牌漂移
硬编码颜色绕过设计系统
ESLint 规则强制 tone-* 前缀使用
过度抽象
Surface 无法满足特殊场景时强行套用
文档明确「NEVER」条款,允许合理例外
无障碍缺陷
交互组件缺少键盘与屏幕阅读器支持
结合 @radix-ui 原语补充 A11y
| 风险类型 | 说明 | 缓解建议 |
|---|---|---|
| 类名冲突 | cn() 合并时 Tailwind 优先级可能非预期 |
使用 tailwind-merge 确保正确覆盖 |
| 令牌漂移 | 硬编码颜色绕过设计系统 | ESLint 规则强制 tone-* 前缀使用 |
| 过度抽象 | Surface 无法满足特殊场景时强行套用 | 文档明确「NEVER」条款,允许合理例外 |
| 无障碍缺陷 | 交互组件缺少键盘与屏幕阅读器支持 | 结合 @radix-ui 原语补充 A11y |
本技能属于 T2 来源(行业最佳实践聚合),安全性评级 A(纯代码模式,无外部依赖执行风险)。
技能正文
name: design-system-components
model: standard
description: Patterns for building design system components using Surface primitives, CVA variants, and consistent styling. Use when building reusable UI components that follow design token architecture. Triggers on Surface component, CVA, class-variance-authority, component variants, design tokens.
Design System Components
Build reusable components that leverage design tokens with Surface primitives and CVA (class-variance-authority).
When to Use
- Building component libraries with design tokens
- Need variant-based styling (size, color, state)
- Creating layered UI with consistent surfaces
- Want type-safe component APIs
Pattern 1: Surface Primitive
Single component for all layered surfaces:
import { cva, type VariantProps } from 'class-variance-authority';
import { cn } from '@/lib/utils';
const surfaceVariants = cva(
'rounded-lg backdrop-blur-sm transition-colors',
{
variants: {
layer: {
panel: 'bg-tone-cadet/40 border border-tone-jordy/10 shadow-card',
tile: 'bg-tone-midnight/60 border border-tone-jordy/5',
chip: 'bg-tone-cyan/10 border border-tone-cyan/20 rounded-full',
deep: 'bg-tone-void/80',
metric: 'bg-tone-cadet/20 border border-tone-jordy/8',
glass: 'bg-glass-bg backdrop-blur-lg border border-glass-border',
},
interactive: {
true: 'cursor-pointer hover:bg-tone-cadet/50 active:scale-[0.98]',
false: '',
},
glow: {
true: 'shadow-glow',
false: '',
},
},
defaultVariants: {
layer: 'tile',
interactive: false,
glow: false,
},
}
);
interface SurfaceProps
extends React.HTMLAttributes<HTMLDivElement>,
VariantProps<typeof surfaceVariants> {}
export function Surface({
layer,
interactive,
glow,
className,
...props
}: SurfaceProps) {
return (
<div
className={cn(surfaceVariants({ layer, interactive, glow }), className)}
{...props}
/>
);
}
Usage
<Surface layer="panel" className="p-4">
<h2>Dashboard</h2>
</Surface>
<Surface layer="chip" interactive>
<span>Active</span>
</Surface>
<Surface layer="metric" glow>
<span className="text-2xl">$1,234.56</span>
</Surface>
Pattern 2: CVA Button Variants
const buttonVariants = cva(
'inline-flex items-center justify-center rounded-md font-medium transition-all focus-visible:outline-none focus-visible:ring-2 disabled:pointer-events-none disabled:opacity-50',
{
variants: {
variant: {
default: 'bg-primary text-primary-foreground hover:bg-primary/90',
destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
outline: 'border border-input bg-background hover:bg-accent hover:text-accent-foreground',
ghost: 'hover:bg-accent hover:text-accent-foreground',
link: 'text-primary underline-offset-4 hover:underline',
cyber: 'bg-gradient-to-r from-tone-cadet to-tone-azure text-white border border-tone-cyan/30 shadow-glow hover:shadow-glow-lg',
},
size: {
default: 'h-10 px-4 py-2',
sm: 'h-9 rounded-md px-3',
lg: 'h-11 rounded-md px-8',
icon: 'h-10 w-10',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
}
);
Pattern 3: Metric Display Component
const metricVariants = cva(
'font-mono tabular-nums',
{
variants: {
size: {
lg: 'text-3xl font-bold tracking-tight',
md: 'text-xl font-semibold',
sm: 'text-base font-medium',
},
trend: {
positive: 'text-success',
negative: 'text-destructive',
neutral: 'text-foreground',
},
},
defaultVariants: {
size: 'md',
trend: 'neutral',
},
}
);
interface MetricProps extends VariantProps<typeof metricVariants> {
value: string | number;
label?: string;
prefix?: string;
suffix?: string;
}
export function Metric({
value,
label,
prefix = '',
suffix = '',
size,
trend,
}: MetricProps) {
return (
<div className="flex flex-col">
{label && (
<span className="text-xs uppercase tracking-wider text-muted-foreground mb-1">
{label}
</span>
)}
<span className={cn(metricVariants({ size, trend }))}>
{prefix}{value}{suffix}
</span>
</div>
);
}
Pattern 4: Card with Header
interface CardProps {
title?: string;
description?: string;
action?: React.ReactNode;
children: React.ReactNode;
}
export function Card({ title, description, action, children }: CardProps) {
return (
<Surface layer="panel" className="flex flex-col">
{(title || action) && (
<div className="flex items-center justify-between px-4 py-3 border-b border-tone-jordy/10">
<div>
{title && (
<h3 className="font-display text-sm font-medium">{title}</h3>
)}
{description && (
<p className="text-xs text-muted-foreground">{description}</p>
)}
</div>
{action}
</div>
)}
<div className="p-4">{children}</div>
</Surface>
);
}
Pattern 5: Badge/Chip Variants
const badgeVariants = cva(
'inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium transition-colors',
{
variants: {
variant: {
default: 'bg-primary/10 text-primary border border-primary/20',
success: 'bg-success/10 text-success border border-success/20',
warning: 'bg-warning/10 text-warning border border-warning/20',
destructive: 'bg-destructive/10 text-destructive border border-destructive/20',
outline: 'border border-input text-foreground',
},
},
defaultVariants: {
variant: 'default',
},
}
);
Pattern 6: Composing Variants
<Surface layer="panel" className="p-4">
<h2>Dashboard</h2>
</Surface>
<Surface layer="chip" interactive>
<span>Active</span>
</Surface>
<Surface layer="metric" glow>
<span className="text-2xl">$1,234.56</span>
</Surface>
const buttonVariants = cva(
'inline-flex items-center justify-center rounded-md font-medium transition-all focus-visible:outline-none focus-visible:ring-2 disabled:pointer-events-none disabled:opacity-50',
{
variants: {
variant: {
default: 'bg-primary text-primary-foreground hover:bg-primary/90',
destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
outline: 'border border-input bg-background hover:bg-accent hover:text-accent-foreground',
ghost: 'hover:bg-accent hover:text-accent-foreground',
link: 'text-primary underline-offset-4 hover:underline',
cyber: 'bg-gradient-to-r from-tone-cadet to-tone-azure text-white border border-tone-cyan/30 shadow-glow hover:shadow-glow-lg',
},
size: {
default: 'h-10 px-4 py-2',
sm: 'h-9 rounded-md px-3',
lg: 'h-11 rounded-md px-8',
icon: 'h-10 w-10',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
}
);
Pattern 3: Metric Display Component
const metricVariants = cva(
'font-mono tabular-nums',
{
variants: {
size: {
lg: 'text-3xl font-bold tracking-tight',
md: 'text-xl font-semibold',
sm: 'text-base font-medium',
},
trend: {
positive: 'text-success',
negative: 'text-destructive',
neutral: 'text-foreground',
},
},
defaultVariants: {
size: 'md',
trend: 'neutral',
},
}
);
interface MetricProps extends VariantProps<typeof metricVariants> {
value: string | number;
label?: string;
prefix?: string;
suffix?: string;
}
export function Metric({
value,
label,
prefix = '',
suffix = '',
size,
trend,
}: MetricProps) {
return (
<div className="flex flex-col">
{label && (
<span className="text-xs uppercase tracking-wider text-muted-foreground mb-1">
{label}
</span>
)}
<span className={cn(metricVariants({ size, trend }))}>
{prefix}{value}{suffix}
</span>
</div>
);
}
Pattern 4: Card with Header
interface CardProps {
title?: string;
description?: string;
action?: React.ReactNode;
children: React.ReactNode;
}
export function Card({ title, description, action, children }: CardProps) {
return (
<Surface layer="panel" className="flex flex-col">
{(title || action) && (
<div className="flex items-center justify-between px-4 py-3 border-b border-tone-jordy/10">
<div>
{title && (
<h3 className="font-display text-sm font-medium">{title}</h3>
)}
{description && (
<p className="text-xs text-muted-foreground">{description}</p>
)}
</div>
{action}
</div>
)}
<div className="p-4">{children}</div>
</Surface>
);
}
Pattern 5: Badge/Chip Variants
const badgeVariants = cva(
'inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium transition-colors',
{
variants: {
variant: {
default: 'bg-primary/10 text-primary border border-primary/20',
success: 'bg-success/10 text-success border border-success/20',
warning: 'bg-warning/10 text-warning border border-warning/20',
destructive: 'bg-destructive/10 text-destructive border border-destructive/20',
outline: 'border border-input text-foreground',
},
},
defaultVariants: {
variant: 'default',
},
}
);
Pattern 6: Composing Variants
const metricVariants = cva(
'font-mono tabular-nums',
{
variants: {
size: {
lg: 'text-3xl font-bold tracking-tight',
md: 'text-xl font-semibold',
sm: 'text-base font-medium',
},
trend: {
positive: 'text-success',
negative: 'text-destructive',
neutral: 'text-foreground',
},
},
defaultVariants: {
size: 'md',
trend: 'neutral',
},
}
);
interface MetricProps extends VariantProps<typeof metricVariants> {
value: string | number;
label?: string;
prefix?: string;
suffix?: string;
}
export function Metric({
value,
label,
prefix = '',
suffix = '',
size,
trend,
}: MetricProps) {
return (
<div className="flex flex-col">
{label && (
<span className="text-xs uppercase tracking-wider text-muted-foreground mb-1">
{label}
</span>
)}
<span className={cn(metricVariants({ size, trend }))}>
{prefix}{value}{suffix}
</span>
</div>
);
}
interface CardProps {
title?: string;
description?: string;
action?: React.ReactNode;
children: React.ReactNode;
}
export function Card({ title, description, action, children }: CardProps) {
return (
<Surface layer="panel" className="flex flex-col">
{(title || action) && (
<div className="flex items-center justify-between px-4 py-3 border-b border-tone-jordy/10">
<div>
{title && (
<h3 className="font-display text-sm font-medium">{title}</h3>
)}
{description && (
<p className="text-xs text-muted-foreground">{description}</p>
)}
</div>
{action}
</div>
)}
<div className="p-4">{children}</div>
</Surface>
);
}
Pattern 5: Badge/Chip Variants
const badgeVariants = cva(
'inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium transition-colors',
{
variants: {
variant: {
default: 'bg-primary/10 text-primary border border-primary/20',
success: 'bg-success/10 text-success border border-success/20',
warning: 'bg-warning/10 text-warning border border-warning/20',
destructive: 'bg-destructive/10 text-destructive border border-destructive/20',
outline: 'border border-input text-foreground',
},
},
defaultVariants: {
variant: 'default',
},
}
);
Pattern 6: Composing Variants
const badgeVariants = cva(
'inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium transition-colors',
{
variants: {
variant: {
default: 'bg-primary/10 text-primary border border-primary/20',
success: 'bg-success/10 text-success border border-success/20',
warning: 'bg-warning/10 text-warning border border-warning/20',
destructive: 'bg-destructive/10 text-destructive border border-destructive/20',
outline: 'border border-input text-foreground',
},
},
defaultVariants: {
variant: 'default',
},
}
);
Combine CVA with conditional classes:
function StatusIndicator({
status,
size = 'md'
}: {
status: 'online' | 'offline' | 'away';
size?: 'sm' | 'md' | 'lg';
}) {
const sizeClasses = {
sm: 'size-2',
md: 'size-3',
lg: 'size-4',
};
const statusClasses = {
online: 'bg-success animate-pulse',
offline: 'bg-muted-foreground',
away: 'bg-warning',
};
return (
<span
className={cn(
'rounded-full',
sizeClasses[size],
statusClasses[status]
)}
/>
);
}
Related Skills
- Meta-skill: ai/skills/meta/design-system-creation/ — Complete design system workflow
- distinctive-design-systems — Token architecture and aesthetic foundations
- loading-state-patterns — Skeleton components for loading states
NEVER Do
- Build custom card containers — Use Surface primitive
- Hardcode colors in components — Use design tokens
- Skip variant types — CVA provides type safety
- Mix styling approaches — Pick CVA or cn(), not random inline styles
- Forget default variants — Components should work without props
Quick Reference
// 1. Define variants with CVA
const variants = cva('base-classes', {
variants: {
size: { sm: '...', md: '...', lg: '...' },
color: { primary: '...', secondary: '...' },
},
defaultVariants: { size: 'md', color: 'primary' },
});
// 2. Type props from variants
interface Props extends VariantProps<typeof variants> {}
// 3. Apply in component
<div className={cn(variants({ size, color }), className)} />
- Build custom card containers — Use Surface primitive
- Hardcode colors in components — Use design tokens
- Skip variant types — CVA provides type safety
- Mix styling approaches — Pick CVA or cn(), not random inline styles
- Forget default variants — Components should work without props
Quick Reference
// 1. Define variants with CVA
const variants = cva('base-classes', {
variants: {
size: { sm: '...', md: '...', lg: '...' },
color: { primary: '...', secondary: '...' },
},
defaultVariants: { size: 'md', color: 'primary' },
});
// 2. Type props from variants
interface Props extends VariantProps<typeof variants> {}
// 3. Apply in component
<div className={cn(variants({ size, color }), className)} />
// 1. Define variants with CVA
const variants = cva('base-classes', {
variants: {
size: { sm: '...', md: '...', lg: '...' },
color: { primary: '...', secondary: '...' },
},
defaultVariants: { size: 'md', color: 'primary' },
});
// 2. Type props from variants
interface Props extends VariantProps<typeof variants> {}
// 3. Apply in component
<div className={cn(variants({ size, color }), className)} />
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。