Design Patterns
Visual design patterns for creating consistent, accessible interfaces with proper design tokens and component specifications.
Purpose
Provides patterns for:
- Design token systems (colors, typography, spacing)
- Component visual specifications
- WCAG 2.1 AA accessibility compliance
- Responsive design across breakpoints
Core Patterns
Pattern 1: Semantic Color Tokens
When to use: Defining color systems that work across themes.
Implementation:
/* Base palette (reference only) */
--blue-500: #3b82f6;
--blue-700: #1d4ed8;
/* Semantic tokens (use these) */
--color-primary: var(--blue-500);
--color-primary-hover: var(--blue-700);
--color-text: #111827;
--color-text-muted: #6b7280;
--color-background: #ffffff;
--color-error: #ef4444;
--color-success: #10b981;
Benefits:
- Theme switching via semantic tokens
- Consistent color usage across components
- Easier maintenance and updates
Pattern 2: 8px Spacing Scale
When to use: All spacing decisions (padding, margins, gaps).
Implementation:
--space-1: 0.25rem; /* 4px - tight */
--space-2: 0.5rem; /* 8px - compact */
--space-3: 0.75rem; /* 12px */
--space-4: 1rem; /* 16px - default */
--space-6: 1.5rem; /* 24px - medium */
--space-8: 2rem; /* 32px - large */
--space-12: 3rem; /* 48px - section */
Benefits:
- Visual rhythm and consistency
- Predictable spacing relationships
- Easier to maintain
Pattern 3: Component State Matrix
When to use: Defining all interactive component states.
Implementation:
| State |
Background |
Border |
Text |
Cursor |
| Default |
--color-bg |
--color-border |
--color-text |
default |
| Hover |
--color-bg-hover |
--color-border |
--color-text |
pointer |
| Focus |
--color-bg |
--color-focus-ring |
--color-text |
— |
| Active |
--color-bg-active |
--color-border |
--color-text |
pointer |
| Disabled |
--color-bg |
--color-border |
--color-text-muted |
not-allowed |
| Loading |
--color-bg |
--color-border |
--color-text-muted |
wait |
| Error |
--color-bg |
--color-error |
--color-error |
— |
Anti-Patterns
Anti-Pattern 1: Hard-Coded Values
| Aspect |
Description |
| WHY |
Breaks design system consistency, makes theme switching impossible |
| DETECTION |
Raw hex colors, pixel values without tokens in CSS |
| FIX |
Always use design tokens for colors, spacing, typography |
Bad Example:
.button {
background: #3b82f6;
padding: 12px 16px;
color: white;
}
Good Example:
.button {
background: var(--color-primary);
padding: var(--space-3) var(--space-4);
color: var(--color-on-primary);
}
Anti-Pattern 2: Missing States
| Aspect |
Description |
| WHY |
Creates incomplete interactions, accessibility failures |
| DETECTION |
Components only define default + hover, missing focus/disabled/error |
| FIX |
Define ALL states: default, hover, focus, active, disabled, loading, error |
Anti-Pattern 3: Insufficient Contrast
| Aspect |
Description |
| WHY |
WCAG 2.1 AA violation, excludes users with visual impairments |
| DETECTION |
Text contrast < 4.5:1, UI component contrast < 3:1 |
| FIX |
Use contrast checker, ensure text 4.5:1, UI elements 3:1 minimum |
WCAG 2.1 AA Requirements
| Requirement |
Threshold |
Applies To |
| Text contrast |
4.5:1 |
All text |
| Large text contrast |
3:1 |
18px+ or 14px bold |
| UI component contrast |
3:1 |
Borders, icons, focus rings |
| Touch target |
44x44px |
Mobile interactive elements |
| Focus visible |
2px outline |
All focusable elements |
Validation Checklist
Color System
Components
Responsive
Output Templates
Design Tokens Spec
## Design Tokens: [System Name]
### Colors
| Token | Value | Usage |
|-------|-------|-------|
| --color-primary | #3b82f6 | Primary actions |
| --color-text | #111827 | Body text (4.54:1) |
### Typography
| Token | Value | Usage |
|-------|-------|-------|
| --text-base | 1rem | Body text |
| --font-sans | 'Inter', sans-serif | Primary font |
### Spacing
| Token | Value | Usage |
|-------|-------|-------|
| --space-4 | 1rem | Default padding |
Component Spec
## Component: [Name]
### Variants
| Variant | Use Case | Treatment |
|---------|----------|-----------|
| Primary | Main actions | Filled, brand |
| Secondary | Alternative | Outlined |
### States
| State | Background | Border | Text |
|-------|------------|--------|------|
| Default | --color-bg | --color-border | --color-text |
### Accessibility
- Contrast: X.X:1 (meets AA)
- Touch target: 44x44px
- Focus: 2px outline
1---2name: design-patterns3description: Visual design patterns for design tokens, component specifications, color systems, typography, spacing, and WCAG accessibility compliance. Use proactively when working on design systems, UI components, CSS/SCSS styles, or reviewing visual design decisions for accessibility and consistency.4---56# Design Patterns78Visual design patterns for creating consistent, accessible interfaces with proper design tokens and component specifications.910## Purpose1112Provides patterns for:13- Design token systems (colors, typography, spacing)14- Component visual specifications15- WCAG 2.1 AA accessibility compliance16- Responsive design across breakpoints1718---1920## Core Patterns2122### Pattern 1: Semantic Color Tokens2324**When to use:** Defining color systems that work across themes.2526**Implementation:**27```css28/* Base palette (reference only) */29--blue-500: #3b82f6;30--blue-700: #1d4ed8;3132/* Semantic tokens (use these) */33--color-primary: var(--blue-500);34--color-primary-hover: var(--blue-700);35--color-text: #111827;36--color-text-muted: #6b7280;37--color-background: #ffffff;38--color-error: #ef4444;39--color-success: #10b981;40```4142**Benefits:**43- Theme switching via semantic tokens44- Consistent color usage across components45- Easier maintenance and updates4647### Pattern 2: 8px Spacing Scale4849**When to use:** All spacing decisions (padding, margins, gaps).5051**Implementation:**52```css53--space-1: 0.25rem; /* 4px - tight */54--space-2: 0.5rem; /* 8px - compact */55--space-3: 0.75rem; /* 12px */56--space-4: 1rem; /* 16px - default */57--space-6: 1.5rem; /* 24px - medium */58--space-8: 2rem; /* 32px - large */59--space-12: 3rem; /* 48px - section */60```6162**Benefits:**63- Visual rhythm and consistency64- Predictable spacing relationships65- Easier to maintain6667### Pattern 3: Component State Matrix6869**When to use:** Defining all interactive component states.7071**Implementation:**72| State | Background | Border | Text | Cursor |73|-------|------------|--------|------|--------|74| Default | `--color-bg` | `--color-border` | `--color-text` | default |75| Hover | `--color-bg-hover` | `--color-border` | `--color-text` | pointer |76| Focus | `--color-bg` | `--color-focus-ring` | `--color-text` | — |77| Active | `--color-bg-active` | `--color-border` | `--color-text` | pointer |78| Disabled | `--color-bg` | `--color-border` | `--color-text-muted` | not-allowed |79| Loading | `--color-bg` | `--color-border` | `--color-text-muted` | wait |80| Error | `--color-bg` | `--color-error` | `--color-error` | — |8182---8384## Anti-Patterns8586### Anti-Pattern 1: Hard-Coded Values8788| Aspect | Description |89|--------|-------------|90| **WHY** | Breaks design system consistency, makes theme switching impossible |91| **DETECTION** | Raw hex colors, pixel values without tokens in CSS |92| **FIX** | Always use design tokens for colors, spacing, typography |9394**Bad Example:**95```css96.button {97 background: #3b82f6;98 padding: 12px 16px;99 color: white;100}101```102103**Good Example:**104```css105.button {106 background: var(--color-primary);107 padding: var(--space-3) var(--space-4);108 color: var(--color-on-primary);109}110```111112### Anti-Pattern 2: Missing States113114| Aspect | Description |115|--------|-------------|116| **WHY** | Creates incomplete interactions, accessibility failures |117| **DETECTION** | Components only define default + hover, missing focus/disabled/error |118| **FIX** | Define ALL states: default, hover, focus, active, disabled, loading, error |119120### Anti-Pattern 3: Insufficient Contrast121122| Aspect | Description |123|--------|-------------|124| **WHY** | WCAG 2.1 AA violation, excludes users with visual impairments |125| **DETECTION** | Text contrast < 4.5:1, UI component contrast < 3:1 |126| **FIX** | Use contrast checker, ensure text 4.5:1, UI elements 3:1 minimum |127128---129130## WCAG 2.1 AA Requirements131132| Requirement | Threshold | Applies To |133|-------------|-----------|------------|134| Text contrast | 4.5:1 | All text |135| Large text contrast | 3:1 | 18px+ or 14px bold |136| UI component contrast | 3:1 | Borders, icons, focus rings |137| Touch target | 44x44px | Mobile interactive elements |138| Focus visible | 2px outline | All focusable elements |139140---141142## Validation Checklist143144### Color System145- [ ] All colors use semantic tokens, not raw values146- [ ] Text meets 4.5:1 contrast ratio147- [ ] UI components meet 3:1 contrast ratio148- [ ] Color is not sole indicator (add icons/text)149150### Components151- [ ] All states defined (default, hover, focus, active, disabled, loading, error)152- [ ] Touch targets minimum 44x44px153- [ ] Focus ring visible with 2px minimum154- [ ] Tokens used for all values155156### Responsive157- [ ] Works on mobile, tablet, desktop158- [ ] Typography scales appropriately159- [ ] Spacing adjusts for viewport160- [ ] No horizontal scroll on mobile161162---163164## Output Templates165166### Design Tokens Spec167```markdown168## Design Tokens: [System Name]169170### Colors171| Token | Value | Usage |172|-------|-------|-------|173| --color-primary | #3b82f6 | Primary actions |174| --color-text | #111827 | Body text (4.54:1) |175176### Typography177| Token | Value | Usage |178|-------|-------|-------|179| --text-base | 1rem | Body text |180| --font-sans | 'Inter', sans-serif | Primary font |181182### Spacing183| Token | Value | Usage |184|-------|-------|-------|185| --space-4 | 1rem | Default padding |186```187188### Component Spec189```markdown190## Component: [Name]191192### Variants193| Variant | Use Case | Treatment |194|---------|----------|-----------|195| Primary | Main actions | Filled, brand |196| Secondary | Alternative | Outlined |197198### States199| State | Background | Border | Text |200|-------|------------|--------|------|201| Default | --color-bg | --color-border | --color-text |202203### Accessibility204- Contrast: X.X:1 (meets AA)205- Touch target: 44x44px206- Focus: 2px outline207```