--- name: design-systems description: Builds and maintains design systems and shared component libraries, covering design tokens, theming and dark mode, component API design, loading, empty, error and disabled states, built-in accessibility, documentation, visual regression tests and semver releases with deprecations. Use when creating or refactoring a UI component library, defining tokens or themes, reviewing a shared component's props, or planning a breaking change to shared UI. Not for styling a single page. license: MIT --- # Design Systems A design system is a product whose users are other developers and designers. Its job is to make the accessible, on-brand choice the easiest one. Encode decisions in tokens, keep component APIs small and consistent, and never break consumers without a deprecation path. ## When to use - Starting a shared component library or extracting one from an app. - Defining or restructuring design tokens, themes, dark mode, or multiple brands. - Designing or reviewing the props of a shared component. - Planning a release with breaking changes, or migrating apps to a new version. Not for: one-off page styling. For accessibility details use web-accessibility; for React specifics use react-patterns; for type design use typescript-patterns. ## Is a full design system warranted? | Situation | Do this | | --- | --- | | One app, one team | A shared `components/` folder plus tokens as CSS custom properties. No separate package yet | | Several apps or teams on one framework | A versioned package with docs, tests and a release process | | Several frameworks (React, Vue, native mobile) | Shared tokens generated for every platform; components per framework, or Web Components where they fit | Promote a component into the system after the same need shows up in about three places. Earlier abstraction usually guesses the API wrong. ## Workflow 1. **Inventory** the existing UI: repeated components, every distinct color, font size, spacing value, radius and shadow. Duplicates and near-duplicates show where tokens are missing. 2. **Define tokens first** (primitive, then semantic). Components come after. 3. **Build the most-used components first** (button, input, select, checkbox, dialog, layout primitives). Each one gets an API review, accessibility checks, docs and tests before release. 4. **Release with semver and a changelog.** Breaking changes ship with a migration guide and, for renames, a codemod where practical. 5. **Measure adoption** and remove what nobody uses. ## Tokens | Tier | Example | Who uses it | | --- | --- | --- | | Primitive | `color.blue.600 = #2563eb`, `space.4 = 16px` | Only semantic tokens | | Semantic | `color.text.primary`, `color.bg.danger`, `color.border.focus`, `space.inset.md` | Components and app code | | Component (optional, sparingly) | `button.primary.bg` | One component | Rules: - Components consume **semantic tokens only**. A theme is a different mapping from semantic to primitive tokens; if a component uses a primitive directly, theming breaks. - Name by purpose, not value: `color.text.muted`, never `color.grey5` or `blue-text`. - Use a predictable pattern such as `category.property.variant.state` (`color.bg.surface.hover`). - Cover color, typography (family, size, line height, weight), spacing scale, radius, elevation/shadow, motion (duration, easing), z-index layers, and breakpoints. - Keep one source of truth (JSON) and generate platform outputs (CSS custom properties, JS, iOS, Android) with a build tool such as Style Dictionary. The W3C Design Tokens Community Group (DTCG) format, stable since version 2025.10, is the common interchange format; check which version your build tool supports. Read [references/tokens.md](references/tokens.md) when creating or restructuring tokens; it has a naming scheme, a JSON example and a dark-theme mapping. ## Theming and dark mode ```css :root { color-scheme: light; --color-bg-surface: #ffffff; --color-text-primary: #111827; --color-border-focus: #2563eb; } [data-theme="dark"] { color-scheme: dark; --color-bg-surface: #111827; --color-text-primary: #f9fafb; --color-border-focus: #60a5fa; } .card { background: var(--color-bg-surface); color: var(--color-text-primary); } ``` - Dark mode is a second palette, not inverted colors. Re-check contrast for every text/background pair in every theme (WCAG 2.2 AA: 4.5:1 for normal text, 3:1 for large text and UI component boundaries). - Respect the OS preference by default (`prefers-color-scheme`) and let users override it. - Prevent a flash of the wrong theme: set `data-theme` before first paint (server-rendered from a cookie, or a tiny inline script in ``). - Multiple brands use the same semantic tokens with different primitive mappings. ## Component API rules - **Consistent names across components**: `size`, `variant`, `disabled`, `value` / `defaultValue` / `onChange`, `open` / `defaultOpen` / `onOpenChange`. - **Variants as one enum prop**, not booleans: `variant="primary" | "secondary" | "danger"` instead of `primary`, `secondary`, `danger` flags that allow impossible combinations. - **Composition over configuration**: compound components or slots (``) instead of `headerTitle`, `headerIcon`, `headerActions` props. - **Controlled and uncontrolled**: accept `value` + `onChange` for control, and `defaultValue` for simple use. - **Pass through native attributes** to the root element (`...rest`), forward refs, and merge `className`/`style` instead of replacing them. - **Polymorphism with care**: an `as` prop is flexible but complicates types and invites broken semantics (`