🎁 Get the FREE AI Skills Starter GuideSubscribe →
BytesAgainBytesAgain
🦀 ClawHub

Design System Patterns

by @wpank

Foundational design system architecture — token hierarchies, theming infrastructure, token pipelines, and governance. Use when creating design tokens, implementing theme switching, setting up Style Dictionary, or establishing multi-brand theming. Triggers on design tokens, theme provider, Style Dictionary, token pipeline, multi-brand theming, CSS custom properties architecture.

Versionv1.0.0
Downloads1,501
Installs8
Stars2
TERMINAL
clawhub install design-system-patterns

📖 About This Skill


name: design-system-patterns model: standard description: Foundational design system architecture — token hierarchies, theming infrastructure, token pipelines, and governance. Use when creating design tokens, implementing theme switching, setting up Style Dictionary, or establishing multi-brand theming. Triggers on design tokens, theme provider, Style Dictionary, token pipeline, multi-brand theming, CSS custom properties architecture.

Design System Patterns

Foundational architecture for scalable design systems: token hierarchies, theming infrastructure, token pipelines, and governance patterns.


When to Use

  • Defining token architecture (primitive → semantic → component layers)
  • Implementing light/dark/system theme switching with React
  • Setting up Style Dictionary or Figma-to-code token pipelines
  • Building multi-brand theming systems
  • Establishing token naming conventions and governance
  • Preventing flash of unstyled content (FOUC) in SSR

  • Pattern 1: Token Hierarchy

    Three-layer token architecture separates raw values from meaning from usage.

    /* Layer 1: Primitive tokens — raw values, never used directly in components */
    :root {
      --color-blue-500: #3b82f6;
      --color-blue-600: #2563eb;
      --color-gray-50: #fafafa;
      --color-gray-900: #171717;

    --space-1: 0.25rem; --space-2: 0.5rem; --space-4: 1rem;

    --font-size-sm: 0.875rem; --font-size-base: 1rem; --radius-sm: 0.25rem; --radius-md: 0.5rem; }

    /* Layer 2: Semantic tokens — contextual meaning, theme-aware */ :root { --text-primary: var(--color-gray-900); --text-secondary: var(--color-gray-600); --surface-default: white; --surface-elevated: var(--color-gray-50); --border-default: var(--color-gray-200); --interactive-primary: var(--color-blue-500); --interactive-primary-hover: var(--color-blue-600); }

    /* Layer 3: Component tokens — specific usage, optional */ :root { --button-bg: var(--interactive-primary); --button-bg-hover: var(--interactive-primary-hover); --button-text: white; --button-radius: var(--radius-md); --button-padding-x: var(--space-4); --button-padding-y: var(--space-2); }

    > Semantic tokens are the most important layer — they enable theming. Component tokens are optional and useful for complex component libraries.


    Pattern 2: Theme Switching with React

    Key capabilities: theme (user selection), resolvedTheme (actual light/dark), setTheme, system preference detection, localStorage persistence, DOM attribute application.

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

    export function ThemeProvider({ children, defaultTheme = "system", storageKey = "theme", attribute = "data-theme" }: { children: React.ReactNode; defaultTheme?: Theme; storageKey?: string; attribute?: "class" | "data-theme" }) { const [theme, setThemeState] = useState(() => typeof window === "undefined" ? defaultTheme : (localStorage.getItem(storageKey) as Theme) || defaultTheme); const [resolvedTheme, setResolvedTheme] = useState<"light" | "dark">("light");

    const getSystem = useCallback(() => matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light" as const, []);

    const apply = useCallback((r: "light" | "dark") => { const root = document.documentElement; attribute === "class" ? (root.classList.remove("light", "dark"), root.classList.add(r)) : root.setAttribute(attribute, r); root.style.colorScheme = r; setResolvedTheme(r); }, [attribute]);

    useEffect(() => { apply(theme === "system" ? getSystem() : theme); }, [theme, apply, getSystem]);

    useEffect(() => { // Listen for system preference changes if (theme !== "system") return; const mq = matchMedia("(prefers-color-scheme: dark)"); const handler = () => apply(getSystem()); mq.addEventListener("change", handler); return () => mq.removeEventListener("change", handler); }, [theme, apply, getSystem]);

    const setTheme = useCallback((t: Theme) => { localStorage.setItem(storageKey, t); setThemeState(t); }, [storageKey]);

    return {children} ; }

    Full implementation with toggleTheme, disableTransitionOnChange, and testing patterns in references/theming-architecture.md.

    Preventing FOUC in SSR (Next.js)

    Inline script in runs before paint:

    const themeScript = (function(){
      var t=localStorage.getItem('theme')||'system';
      var d=t==='dark'||(t==='system'&&matchMedia('(prefers-color-scheme:dark)').matches);
      document.documentElement.setAttribute('data-theme',d?'dark':'light');
      document.documentElement.style.colorScheme=d?'dark':'light';
    })();

    // In layout.tsx