Design System Architecture in Practice: The Complete Build Path from Design Tokens to Component Libraries
前端工程(Updated Jun 2, 2026)
The Three-Layer Architecture of Design Systems
┌─────────────────────────────────┐
│ Component Layer │ Button, Card, Modal...
├─────────────────────────────────┤
│ Pattern Layer │ Forms, Navigation, Data Tables
├─────────────────────────────────┤
│ Token Layer (Design Tokens) │ Colors, Spacing, Font Sizes, Shadows
└─────────────────────────────────┘
Tokens are the foundation, patterns are the composition, components are the endpoint. Build bottom-up, consume top-down.
The Design Token System
1. Color Tokens
/* Three-layer semantic tokens */
/* Layer 1: Primitive Values */
:root {
--blue-50: #eff6ff;
--blue-500: #3b82f6;
--blue-700: #1d4ed8;
--gray-50: #f9fafb;
--gray-900: #111827;
--red-500: #ef4444;
--green-500: #22c55e;
}
/* Layer 2: Semantic Mapping */
: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);
}
/* Layer 3: Component-Specific */
: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. Spacing Tokens (8pt Grid System)
: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. Typography Tokens
:root {
/* Font sizes */
--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 */
/* Line heights */
--leading-tight: 1.25;
--leading-normal: 1.5;
--leading-relaxed: 1.75;
/* Font weights */
--font-normal: 400;
--font-medium: 500;
--font-semibold: 600;
--font-bold: 700;
}
4. Shadow Tokens
: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);
}
Theme Switching Mechanism
CSS Variables Approach
/* Light theme (default) */
:root {
--color-surface: #ffffff;
--color-text: #111827;
--color-border: #e5e7eb;
--color-primary: #3b82f6;
}
/* Dark theme */
[data-theme="dark"] {
--color-surface: #1f2937;
--color-text: #f9fafb;
--color-border: #374151;
--color-primary: #60a5fa;
}
/* High-contrast theme */
[data-theme="high-contrast"] {
--color-surface: #000000;
--color-text: #ffffff;
--color-border: #ffffff;
--color-primary: #ffff00;
}
React Theme Switching
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 Dark Mode
/* tailwind.config.ts using CSS variable mode */
@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));
<!-- Auto-responsive to theme -->
<div class="bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100">
Content auto-adapts to the current theme
</div>
Component API Design Principles
1. Single Responsibility + Composition First
// ❌ Over-encapsulation: All features stuffed into one component
<SuperButton variant="primary" size="lg" icon="save" loading disabled />
// ✅ Composition pattern: Assemble via child components
<Button variant="primary" size="lg">
<Icon name="save" />
<Button.Label>Save</Button.Label>
<Button.Loading />
</Button>
2. Controlled vs Uncontrolled
// Controlled mode: State managed externally
<Input value={value} onChange={setValue} />
// Uncontrolled mode: State managed internally by the component
<Input defaultValue="hello" onChange={handleChange} />
// Compatible with both
interface InputProps {
value?: string; // Controlled
defaultValue?: string; // Uncontrolled
onChange?: (value: string) => void;
}
3. Polymorphic Components
// Button can render as button, a, or 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} />;
}
// Usage
<Button>Click</Button>
<Button as="a" href="/link">Link</Button>
<Button as={Link} to="/page">Route Link</Button>
4. Slots Pattern
// Card component Slots design
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>
);
}
Multi-Brand Adaptation
Brand Overlay Layer
/* Base tokens (Brand A) */
:root {
--brand-primary: #3b82f6;
--brand-radius: 8px;
--brand-font: 'Inter', sans-serif;
}
/* Brand B override */
[data-brand="b"] {
--brand-primary: #8b5cf6;
--brand-radius: 12px;
--brand-font: 'Poppins', sans-serif;
}
/* Brand C override */
[data-brand="c"] {
--brand-primary: #f59e0b;
--brand-radius: 4px;
--brand-font: 'Roboto', sans-serif;
}
Token Compilation Pipeline
Design Source Files (Figma)
↓
Style Dictionary Compilation
↓
├── CSS Variables (Web)
├── Tailwind Config (Tailwind projects)
├── iOS Swift Files
├── Android Kotlin Files
└── JSON (Documentation site)
// 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' }],
},
},
};
ToolsKu's Design System Practices
ToolsKu uses Tailwind CSS v4 + CSS variables to build its design system:
/* ToolsKu theme tokens */
:root {
--color-primary: #2563eb;
--color-surface: #ffffff;
--color-text: #1f2937;
--radius: 8px;
--font-sans: 'Inter', system-ui, sans-serif;
}
/* Tool card component */
.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);
}
Design System Governance
Version Management
@toolsku/tokens v2.4.0 → Design Token package
@toolsku/components v3.1.0 → Component library
@toolsku/patterns v1.2.0 → Pattern library
Dependency chain: patterns → components → tokens
Change Workflow
1. Designer modifies tokens in Figma
2. Auto-sync to Git repository (Figma Plugin)
3. CI runs Style Dictionary compilation
4. Visual regression testing (Chromatic/Storybook)
5. Auto-publish new version (Changesets)
Documentation Site
Storybook
├── Tokens → Color, spacing, font size showcase
├── Components → Component interaction documentation
├── Patterns → Pattern usage guide
└── Guidelines → Design specification documentation
Summary
A design system is not just a component library, but a complete architecture from Tokens to Components to Patterns. Three-layer tokens (Primitive → Semantic → Component) are the foundation of scalability. Composition patterns beat over-encapsulation, and CSS variables + Tailwind is the most practical tech choice in 2026. Remember: a good design system enables designers and developers to speak the same language, and tokens are the vocabulary of that language.
Try these browser-local tools — no sign-up required →
#设计系统#Design Token#组件库#Tailwind CSS#前端架构