| name | design-tokens |
| description | Parameterized design token system. Dynamically generates CSS variables for colors, border-radius, spacing, and typography based on brand parameters. |
Design Tokens
Purpose: UI design foundation layer that dynamically generates design variables from style parameters.
Core Value: Automatic mapping from style dimension values to concrete CSS variables.
Design Philosophy
Core Principle: Parameter-driven, not hardcoded presets.
| Principle | Description |
|---|
| Parameter mapping | 0-100 dimension values automatically convert to concrete CSS values |
| Transparent formulas | Mapping logic is clear and predictable |
| Fine-tunable | Generated values can still be manually adjusted |
| Backward compatible | Default values used when parameters are unspecified |
Parameter to Token Mapping
Input: Style Parameters
interface DesignTokenInput {
primaryHue?: number;
mode?: "light" | "dark";
density?: "compact" | "normal" | "spacious";
borderRadius?: "sharp" | "rounded" | "pill";
}
Output: CSS Variables
:root {
--color-primary: ...;
--color-surface: ...;
--color-text: ...;
--space-0 through --space-9: ...;
--radius-sm: ...;
--radius-md: ...;
--radius-lg: ...;
--radius-full: ...;
--font-sans: ...;
--font-mono: ...;
--text-xs through --text-3xl: ...;
--shadow-sm through --shadow-xl: ...;
}
Mapping Formula Details
1. Color Palette Generation (primaryHue + mode → CSS)
function generatePalette(hue: number, dark: boolean) {
return {
'--color-bg': hsl(hue, saturation, lightness),
'--color-surface': ...,
'--color-primary': hsl(hue, 70, dark ? 55 : 45),
'--color-accent': hsl((hue + 30) % 360, 60, ...),
};
}
2. Spacing Scale (density → CSS)
function generateSpacing(density: string) {
}
| Density | Base | --space-2 | --space-4 | --space-6 |
|---|
| compact | 4px | 4px | 12px | 24px |
| normal | 6px | 6px | 18px | 36px |
| spacious | 8px | 8px | 24px | 48px |
3. Border Radius (borderRadius → CSS)
| Style | --radius-sm | --radius-md | --radius-lg | --radius-full |
|---|
| sharp | 2px | 4px | 6px | 8px |
| rounded | 4px | 8px | 12px | 9999px |
| pill | 9999px | 9999px | 9999px | 9999px |
4. Typography (fixed, does not vary with parameters)
:root {
--font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
--font-mono: 'JetBrains Mono', 'Fira Code', monospace;
--text-xs: 0.75rem;
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: 1.125rem;
--text-xl: 1.25rem;
--text-2xl: 1.5rem;
--text-3xl: 2rem;
--line-height-tight: 1.25;
--line-height-normal: 1.5;
--line-height-relaxed: 1.75;
}
5. Shadows (mode → CSS)
Shadow opacity scales with light/dark mode:
function generateShadows(dark: boolean) {
const opacity = dark ? 0.5 : 0.1;
return {
'--shadow-sm': `0 1px 2px rgba(0,0,0,${opacity})`,
'--shadow-md': `0 4px 6px rgba(0,0,0,${opacity})`,
'--shadow-lg': `0 10px 15px rgba(0,0,0,${opacity})`,
'--shadow-xl': `0 20px 25px rgba(0,0,0,${opacity})`,
};
}
Usage Examples
Generate from Parameters
import { express } from './index';
const result = await express({
primaryHue: 220,
mode: 'dark',
density: 'normal',
borderRadius: 'rounded'
});
console.log(result.css);
console.log(result.totalTokens);
Use in Components
<button style="
background: var(--color-primary);
color: var(--color-primary-fg);
border-radius: var(--radius-md);
padding: var(--space-2) var(--space-4);
font-family: var(--font-sans);
font-size: var(--text-sm);
box-shadow: var(--shadow-md);
">
Button
</button>
Default Tokens
When no parameters are specified, defaults are used:
primaryHue: 220 (blue)
mode: "dark"
density: "normal"
borderRadius: "rounded"
This generates approximately 48 CSS custom properties covering colors, spacing, radii, typography, and shadows.
Tailwind CSS Integration
module.exports = {
theme: {
extend: {
colors: {
primary: 'var(--color-primary)',
'primary-hover': 'var(--color-primary-hover)',
surface: 'var(--color-surface)',
accent: 'var(--color-accent)',
},
borderRadius: {
sm: 'var(--radius-sm)',
DEFAULT: 'var(--radius-md)',
lg: 'var(--radius-lg)',
full: 'var(--radius-full)',
},
},
},
}