設計系統架構實戰:從 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#前端架构