设计系统架构实战:从 Design Token 到组件库的完整构建路径

前端工程(更新于 2026年6月2日)

设计系统的三层架构

┌─────────────────────────────────┐
│  组件层(Components)            │  Button, Card, Modal...
├─────────────────────────────────┤
│  模式层(Patterns)              │  表单、导航、数据表格
├─────────────────────────────────┤
│  Token 层(Design Tokens)       │  颜色、间距、字号、阴影
└─────────────────────────────────┘

Token 是基础,模式是组合,组件是终端。自下而上构建,自上而下消费


Design Token 体系

1. 颜色 Token

/* 语义化三层 Token */

/* 第一层:原始值(Primitive) */
:root {
  --blue-50: #eff6ff;
  --blue-500: #3b82f6;
  --blue-700: #1d4ed8;
  --gray-50: #f9fafb;
  --gray-900: #111827;
  --red-500: #ef4444;
  --green-500: #22c55e;
}

/* 第二层:语义映射(Semantic) */
:root {
  --color-primary: var(--blue-500);
  --color-primary-hover: var(--blue-700);
  --color-surface: var(--gray-50);
  --color-text: var(--gray-900);
  --color-danger: var(--red-500);
  --color-success: var(--green-500);
}

/* 第三层:组件专用(Component) */
:root {
  --button-bg: var(--color-primary);
  --button-bg-hover: var(--color-primary-hover);
  --button-text: white;
  --input-border: var(--color-border);
  --input-border-focus: var(--color-primary);
}

2. 间距 Token(8pt 网格系统)

:root {
  --space-0: 0;
  --space-1: 0.25rem;  /* 4px */
  --space-2: 0.5rem;   /* 8px */
  --space-3: 0.75rem;  /* 12px */
  --space-4: 1rem;     /* 16px */
  --space-5: 1.5rem;   /* 24px */
  --space-6: 2rem;     /* 32px */
  --space-8: 3rem;     /* 48px */
  --space-10: 4rem;    /* 64px */
}

3. 排版 Token

:root {
  /* 字号 */
  --text-xs: 0.75rem;    /* 12px */
  --text-sm: 0.875rem;   /* 14px */
  --text-base: 1rem;     /* 16px */
  --text-lg: 1.125rem;   /* 18px */
  --text-xl: 1.25rem;    /* 20px */
  --text-2xl: 1.5rem;    /* 24px */
  --text-3xl: 1.875rem;  /* 30px */

  /* 行高 */
  --leading-tight: 1.25;
  --leading-normal: 1.5;
  --leading-relaxed: 1.75;

  /* 字重 */
  --font-normal: 400;
  --font-medium: 500;
  --font-semibold: 600;
  --font-bold: 700;
}

4. 阴影 Token

:root {
  --shadow-xs: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 15px rgba(0,0,0,0.1), 0 4px 6px rgba(0,0,0,0.05);
  --shadow-xl: 0 20px 25px rgba(0,0,0,0.1), 0 8px 10px rgba(0,0,0,0.04);
}

主题切换机制

CSS 变量方案

/* 亮色主题(默认) */
:root {
  --color-surface: #ffffff;
  --color-text: #111827;
  --color-border: #e5e7eb;
  --color-primary: #3b82f6;
}

/* 暗色主题 */
[data-theme="dark"] {
  --color-surface: #1f2937;
  --color-text: #f9fafb;
  --color-border: #374151;
  --color-primary: #60a5fa;
}

/* 高对比度主题 */
[data-theme="high-contrast"] {
  --color-surface: #000000;
  --color-text: #ffffff;
  --color-border: #ffffff;
  --color-primary: #ffff00;
}

React 主题切换

import { useEffect, useState } from 'react';

type Theme = 'light' | 'dark' | 'system';

function useTheme() {
  const [theme, setTheme] = useState<Theme>(() => {
    return (localStorage.getItem('theme') as Theme) || 'system';
  });

  useEffect(() => {
    const root = document.documentElement;
    const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
    const resolved = theme === 'system' ? (systemDark ? 'dark' : 'light') : theme;

    root.setAttribute('data-theme', resolved);
    localStorage.setItem('theme', theme);
  }, [theme]);

  return { theme, setTheme };
}

Tailwind CSS v4 暗色模式

/* tailwind.config.ts 使用 CSS 变量模式 */
@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));
<!-- 自动响应主题 -->
<div class="bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100">
  内容自动适配当前主题
</div>

组件 API 设计原则

1. 单一职责 + 组合优先

// ❌ 过度封装:所有功能塞进一个组件
<SuperButton variant="primary" size="lg" icon="save" loading disabled />

// ✅ 组合模式:通过子组件组合
<Button variant="primary" size="lg">
  <Icon name="save" />
  <Button.Label>保存</Button.Label>
  <Button.Loading />
</Button>

2. 受控 vs 非受控

// 受控模式:状态由外部管理
<Input value={value} onChange={setValue} />

// 非受控模式:状态由组件内部管理
<Input defaultValue="hello" onChange={handleChange} />

// 兼容两者
interface InputProps {
  value?: string;          // 受控
  defaultValue?: string;   // 非受控
  onChange?: (value: string) => void;
}

3. 多态组件(Polymorphic)

// Button 可以渲染为 button、a、或 Next.js Link
type ButtonProps<C extends React.ElementType = 'button'> = {
  as?: C;
} & React.ComponentPropsWithoutRef<C>;

function Button<C extends React.ElementType = 'button'>(
  { as, ...props }: ButtonProps<C>
) {
  const Component = as || 'button';
  return <Component className="btn" {...props} />;
}

// 使用
<Button>Click</Button>
<Button as="a" href="/link">Link</Button>
<Button as={Link} to="/page">Route Link</Button>

4. Slots 模式

// Card 组件的 Slots 设计
interface CardSlots {
  header?: React.ReactNode;
  media?: React.ReactNode;
  content: React.ReactNode;
  footer?: React.ReactNode;
  actions?: React.ReactNode;
}

function Card({ header, media, content, footer, actions }: CardSlots) {
  return (
    <div className="card">
      {header && <div className="card-header">{header}</div>}
      {media && <div className="card-media">{media}</div>}
      <div className="card-content">{content}</div>
      {footer && <div className="card-footer">{footer}</div>}
      {actions && <div className="card-actions">{actions}</div>}
    </div>
  );
}

多品牌适配

品牌覆盖层

/* 基础 Token(品牌 A) */
:root {
  --brand-primary: #3b82f6;
  --brand-radius: 8px;
  --brand-font: 'Inter', sans-serif;
}

/* 品牌 B 覆盖 */
[data-brand="b"] {
  --brand-primary: #8b5cf6;
  --brand-radius: 12px;
  --brand-font: 'Poppins', sans-serif;
}

/* 品牌 C 覆盖 */
[data-brand="c"] {
  --brand-primary: #f59e0b;
  --brand-radius: 4px;
  --brand-font: 'Roboto', sans-serif;
}

Token 编译管线

设计源文件(Figma)
  ↓
Style Dictionary 编译
  ↓
├── CSS 变量(Web)
├── Tailwind 配置(Tailwind 项目)
├── iOS Swift 文件
├── Android Kotlin 文件
└── JSON(文档站)
// style-dictionary.config.js
const StyleDictionary = require('style-dictionary');

module.exports = {
  source: ['tokens/**/*.json'],
  platforms: {
    css: {
      transformGroup: 'css',
      buildPath: 'dist/css/',
      files: [{ destination: 'variables.css', format: 'css/variables' }],
    },
    tailwind: {
      transformGroup: 'js',
      buildPath: 'dist/tailwind/',
      files: [{ destination: 'theme.js', format: 'javascript/module' }],
    },
  },
};

工具库的设计系统实践

工具库使用 Tailwind CSS v4 + CSS 变量构建设计系统:

/* 工具库主题 Token */
:root {
  --color-primary: #2563eb;
  --color-surface: #ffffff;
  --color-text: #1f2937;
  --radius: 8px;
  --font-sans: 'Inter', system-ui, sans-serif;
}

/* 工具卡片组件 */
.tool-card {
  padding: var(--space-4);
  border-radius: var(--radius);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  transition: transform 0.2s, box-shadow 0.2s;
}

.tool-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

设计系统治理

版本管理

@toolsku/tokens     v2.4.0  → Design Token 包
@toolsku/components v3.1.0  → 组件库
@toolsku/patterns   v1.2.0  → 模式库

依赖关系:patterns → components → tokens

变更流程

1. 设计师在 Figma 修改 Token
2. 自动同步到 Git 仓库(Figma Plugin)
3. CI 运行 Style Dictionary 编译
4. 视觉回归测试(Chromatic/Storybook)
5. 自动发布新版本(Changesets)

文档站

Storybook
├── Tokens      → 颜色、间距、字号展示
├── Components  → 组件交互文档
├── Patterns    → 模式使用指南
└── Guidelines  → 设计规范说明

总结

设计系统不是组件库,而是一套从 Token 到组件到模式的完整架构。三层 Token(原始→语义→组件)是可扩展性的基础,组合模式优于过度封装,CSS 变量 + Tailwind 是 2026 年最实用的技术选型。记住:好的设计系统让设计师和开发者说同一种语言,Token 就是这种语言的词汇表。

本站提供浏览器本地工具,免注册即可试用 →

#设计系统#Design Token#组件库#Tailwind CSS#前端架构