# From design to code

> How a design system lives in code: the token pipeline, building a component library, matching APIs, theming, packaging and versioning, Storybook, visual and accessibility testing, and keeping Figma and code in sync.

Web page: https://odeocreative.me/tutorials/design-systems/handoff-code
Author: Hecham Gazhi (https://odeocreative.me)

Track: [Design System Tutorial](/md/tutorials/design-systems.md)

## Design and code: one system, two places

A design system that exists only in Figma is a UI kit. The real system lives in code too — in a component library that product teams install and use. The challenge is keeping both sides in sync so that what designers place in Figma is exactly what developers can build.

The goal is **parity**: the same tokens, the same components, the same names, the same options, the same states.

## The token pipeline

1. **Source of truth** — tokens are defined in one place: Figma variables, a tokens plugin, or JSON in a Git repository.
2. **Export** — tokens are exported as JSON, ideally in the W3C Design Tokens format.
3. **Transform** — a build tool like **Style Dictionary** converts JSON into platform formats.
4. **Distribute** — outputs are published in a package that apps install.
5. **Consume** — components and apps use token names, never raw values.

```
tokens/*.json  ──▶  Style Dictionary  ──▶  build/
                                           ├── css/variables.css     (--color-action-primary)
                                           ├── js/tokens.ts          (colorActionPrimary)
                                           ├── ios/Tokens.swift      (Color.actionPrimary)
                                           └── android/colors.xml    (@color/action_primary)
```

## Building the component library

- Pick the frameworks your products use (e.g. React for web, SwiftUI for iOS, Jetpack Compose for Android). Many systems also offer **Web Components** to work across frameworks.
- Build components on accessible foundations — native elements, or proven headless libraries (such as Radix UI, React Aria or Headless UI) that handle keyboard and ARIA behaviour.
- Style components only with tokens.
- Support theming through CSS variables or a theme provider.
- Support RTL with logical properties.
- Make components composable and forward refs and native attributes so teams aren't blocked.

## Matching component APIs

Figma properties and code props should be a direct translation:

| Figma property | Code prop |
| --- | --- |
| Variant = Primary / Secondary / Danger | variant="primary" \| "secondary" \| "danger" |
| Size = SM / MD / LG | size="sm" \| "md" \| "lg" |
| Show icon = true (boolean) | iconStart={<Icon />} |
| Label = "Continue" (text) | children="Continue" |
| State = Disabled | disabled |
| State = Hover / Focus / Pressed | Handled by CSS (:hover, :focus-visible, :active) — not props |

```tsx
import { Button } from "@acme/design-system";
import { ArrowRight } from "@acme/icons";

export function CheckoutFooter() {
  return (
    <Button variant="primary" size="md" iconEnd={<ArrowRight />}>
      Continue to payment
    </Button>
  );
}
```

> Tip: Interaction states like hover and focus are designed in Figma as variants, but in code they're usually CSS states, not props. Document this so designers don't expect a `state` prop.

## Packaging and versioning

- Publish the library as a package (e.g. on npm, or a private registry) so products install a specific version.
- Use **semantic versioning**: MAJOR for breaking changes, MINOR for new features, PATCH for fixes.
- Write a **changelog** for every release, with migration notes for breaking changes.
- Consider **codemods** — scripts that automatically update product code for breaking API changes.
- Release regularly and predictably so teams can plan upgrades.

## Documenting and testing in code

| Practice | What it does |
| --- | --- |
| Storybook stories | Every component, variant and state rendered in isolation; interactive props |
| Visual regression tests | Screenshots compared on each change to catch unintended visual changes (e.g. Chromatic, Playwright snapshots) |
| Accessibility tests | Automated axe checks in stories and unit tests; manual keyboard and screen reader checks |
| Unit / interaction tests | Behaviour: clicking, typing, keyboard navigation, focus management |
| Type checking | TypeScript types document the API and prevent invalid props |

## Keeping Figma and code in sync

- Use the **same names** for tokens, components and properties in both places.
- Automate token sync so values can't drift.
- Use **Figma Dev Mode** and **Code Connect** (where available) so inspecting a component in Figma shows the real code snippet.
- Include designers and developers in each other's reviews: a component is "done" only when it exists, matches, and is documented on both sides.
- Track parity in a simple table: component, Figma status, code status, docs status.

| Component | Figma | Code | Docs |
| --- | --- | --- | --- |
| Button | Stable | Stable | Complete |
| Text field | Stable | Stable | Complete |
| Date picker | Beta | In progress | Draft |
| Data table | Planned | — | — |

> Common mistake: Designers creating new variants in Figma that don't exist in code. Product designers use them, developers can't find them, and they build one-offs. New variants should be proposed, built and released together.

## Key takeaways

- A real design system lives in both design and code, with parity between them.
- Tokens flow from one source of truth through tools like Style Dictionary to every platform.
- Build accessible, token-styled, themeable components with APIs that mirror Figma properties.
- Package with semantic versioning, changelogs and migration notes.
- Test visually, for accessibility and for behaviour, and keep Figma and code in sync on purpose.

## Exercise: Mirror a component in code

1. Take the Button you built in Figma.
2. Write its props as a TypeScript type that mirrors the Figma properties.
3. Implement it with CSS variables from your tokens (any framework, or plain HTML/CSS).
4. Add hover, focus-visible, active, disabled and loading styles.
5. Fill in a parity table comparing the Figma and code versions.

## Quiz

1. What does 'parity' mean between design and code?
   - Same price
   - **Same tokens, components, names, options and states in both** ✓
   - Same file format
   - Same team
   - Why: Parity means designers and developers use the same system.

2. What does a tool like Style Dictionary do?
   - Draws icons
   - **Transforms token files into platform outputs like CSS variables** ✓
   - Hosts websites
   - Writes documentation
   - Why: It converts one token source into formats for each platform.

3. Renaming a component prop is which kind of release?
   - Patch
   - Minor
   - **Major** ✓
   - None
   - Why: Breaking API changes require a major version.

4. In code, hover and focus states are usually…
   - Props like state='hover'
   - **CSS states like :hover and :focus-visible** ✓
   - Separate components
   - Not supported
   - Why: They're handled by the browser, not passed as props.

5. What do visual regression tests catch?
   - Spelling mistakes
   - **Unintended visual changes between versions** ✓
   - Slow servers
   - Missing translations
   - Why: They compare screenshots to detect visual changes.
