引言
设计系统不是组件库的同义词。它是一套共享的决策和约束:颜色、间距、字体、交互模式,以及让这些决策在代码中可复用的机制。
设计令牌
令牌是单一事实源:原始值(色值、尺寸)映射到语义(背景、正文、危险),组件只引用语义令牌。禁止在组件里硬编码颜色或间距,否则主题切换和品牌定制会失败。
主题与组件
浅色、深色和品牌主题通过覆盖语义令牌实现,而不是为每个主题复制组件。组件应可组合、职责单一,并通过 props 暴露必要的变体;变体过多时优先组合而不是增加布尔参数。
无障碍与文档
无障碍是组件的内置属性:语义 HTML、可见焦点、键盘操作和正确的 ARIA 状态。文档应是活的:每个组件都有可运行示例、用法说明、禁止用法和可访问性注意事项,并与代码一起评审。
治理与迁移
定义组件生命周期:提案、实验、稳定、弃用、移除。用 codemod 和迁移指南推动采用,监控旧组件的使用量。设计系统的成功指标是采用率和一致性,而不是组件数量。
代码示例
/* tokens.css — the single source of truth, consumed by every component. */
:root {
/* Color: a small, named scale, not every shade. */
--color-primary-50: #eff6ff;
--color-primary-500: #3b82f6;
--color-primary-700: #1d4ed8;
--color-neutral-900: #111827;
--color-neutral-100: #f3f4f6;
--color-danger-500: #ef4444;
--color-focus: #2563eb;
/* Spacing: a scale based on a base unit, not arbitrary pixels. */
--spacing-1: 0.25rem;
--spacing-2: 0.5rem;
--spacing-3: 0.75rem;
--spacing-4: 1rem;
--spacing-8: 2rem;
/* Typography: a type scale, not one-off sizes. */
--font-size-sm: 0.875rem;
--font-size-base: 1rem;
--font-size-lg: 1.25rem;
--font-line-height-base: 1.5;
/* Radius, shadow, motion — each a small scale. */
--radius-sm: 4px;
--radius-md: 8px;
--shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
--transition-fast: 150ms ease;
}
:root, [data-theme="light"] {
--color-bg: var(--color-neutral-100);
--color-text: var(--color-neutral-900);
--color-surface: #ffffff;
}
[data-theme="dark"] {
--color-bg: var(--color-neutral-900);
--color-text: var(--color-neutral-100);
--color-surface: #1f2937;
}
.button {
display: inline-flex;
align-items: center;
gap: var(--spacing-2);
padding: var(--spacing-2) var(--spacing-4);
font-size: var(--font-size-base);
line-height: var(--font-line-height-base);
border-radius: var(--radius-md);
border: none;
cursor: pointer;
transition: background-color var(--transition-fast);
}
.button--primary { background: var(--color-primary-500); color: white; }
.button--primary:hover { background: var(--color-primary-700); }
.button:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }
.button:disabled { opacity: 0.5; cursor: not-allowed; }
function Button({ variant = "primary", size = "md", children, ...props }: ButtonProps) {
return <button className={cx("button", `button--${variant}`, `button--${size}`)} {...props}>{children}</button>;
}
## Button
A button triggers an action. Use it for things the user does, not places the user goes (use a link for navigation).
### Variants
- **Primary**: the main action on the screen. One per screen.
- **Ghost**: a secondary action.
- **Danger**: a destructive action; pair with a confirmation.
### Do
<Example><Button variant="primary">Save changes</Button></Example>
### Don't
<Example><Button variant="primary" onClick={deleteAll}>Delete everything</Button></Example>
Use `variant="danger"` for destructive actions; a primary button labeled "Delete" invites a mistake.
import { render, fireEvent } from "@testing-library/react";
import { axe } from "vitest-axe";
test("Button calls onClick when activated by keyboard", async () => {
const onClick = vi.fn();
const { getByRole } = render(<Button onClick={onClick}>Save</Button>);
const button = getByRole("button", { name: "Save" });
button.focus();
fireEvent.keyDown(button, { key: "Enter" });
expect(onClick).toHaveBeenCalledOnce();
});
test("Button has no accessibility violations", async () => {
const { container } = render(<Button>Save</Button>);
expect(await axe(container)).toHaveNoViolations();
});
// A codemod that replaces raw <button> with the design system <Button>.
// Run it incrementally, directory by directory, with a review of each diff.
export default function transform(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.JSXOpeningElement, { name: { name: "button" } })
.forEach((path) => { path.value.name.name = "Button"; })
.toSource();
}