Skip to content
Visual explainer 3h to learn 4 steps

Component libraries & design tokens

Build a small set of reusable, accessible UI primitives with consistent tokens.

By Invicly Tech, , in Frontend with React & Next.js. About 10 minutes to read.

Topic 28 of 36

In short

A design system is a shared set of tokens and components that keeps UI consistent. Tokens define values like colors and spacing; primitives like Button and Input use them and handle accessibility once.

Explained simply

Imagine every page in your app has a slightly different button: one is blue with round corners, one is a little darker, one forgets the focus outline. Users notice, even if they cannot say why. A design system stops this by giving the whole team one shared set of building blocks.

It has two parts. Design tokens are named values: the brand color, the spacing steps, the corner radius, the font sizes. Components are the reusable pieces built from those tokens, like Button, Input and Card.

The real win is that hard things are solved once. The Button already has the right focus ring, the disabled style and the loading state. The Input already connects its label and its error message for screen readers. Every new page gets that for free.

You do not need a huge library to start. Five well-made components and a short list of tokens will make a small app look and feel professional.

Press Play on a diagram to walk through it one step at a time.

LayersLayers of a design system
  1. 1
    Pages

    Screens assembled from patterns

  2. 2
    Patterns

    Forms, cards, dialogs built from primitives

  3. 3
    Primitives

    Button, Input, Card with variants and states

  4. 4
    Design tokens

    Color, spacing, radius and type scale values

MapA starter design system
ui/
  • tokens
    • Colors (semantic)
    • Spacing scale
    • Radius and shadow
    • Type scale
  • Button
    • primary, secondary, ghost, danger
    • sm, md, lg
    • loading and disabled
  • Input
    • Label and hint
    • Error state
    • Linked with useId
  • Card
    • Header, body, footer
    • Uses surface tokens
  • Docs
    • Story per variant
    • Do and don't notes

In depth

Three tiers of tokens

Good token systems have layers. Base tokens hold raw values: blue-600 is a specific color, space-4 is 1rem. Semantic tokens give those values a job: color-primary points to blue-600, color-danger points to red-600, color-surface is the card background. Component tokens, used only when needed, point at semantic ones: button-bg is color-primary.

Components should use semantic tokens, never base ones. Then a rebrand changes one mapping, and dark mode simply maps color-surface to a dark value. Keep scales small and spaced on purpose, for example spacing in steps of 4px, and a type scale of five or six sizes. A small, strict menu is what makes the UI look consistent.

FlowHow a token reaches a button
  1. Step 1
    Base token

    blue-600 = oklch(0.55 0.2 260)

  2. Step 2
    Semantic token

    color-primary = blue-600

  3. Step 3
    Component token

    button-bg = color-primary (optional)

  4. Step 4
    Component

    Button uses bg-primary

Designing a primitive's API

A primitive should feel like the native element it wraps, with a few extra options. Accept every native prop by typing it as React.ComponentProps<"button"> and spreading the rest onto the element, so type, disabled, onClick and aria attributes all just work. In React 19, ref is a normal prop for function components, so you no longer need forwardRef to let parents focus your Input.

Limit choices on purpose: a variant prop (primary, secondary, ghost, danger) and a size prop (sm, md, lg) cover most needs. Avoid props like color or padding that let every caller invent new styles. Libraries such as class-variance-authority (cva) map variant combinations to class strings neatly. Default a Button's type to "button", because a button inside a form submits it by default.

  • Spread native props so the component behaves like the element
  • Allow className for layout tweaks, merged with tailwind-merge
  • Default type="button" to avoid surprise form submits

Accessibility baked into each primitive

Use the real element whenever one exists. A native button already handles Tab focus and activates on Enter and Space; a div with onClick does none of that. Show focus with a visible ring on focus-visible, and keep text contrast at least 4.5:1 (3:1 for large text and for borders of controls), as WCAG asks.

For Input, generate an id with useId and link the label with htmlFor. When there is an error, set aria-invalid="true" and point aria-describedby at the error text so screen readers read it out. Prefer showing a disabled button with an explanation, or use aria-disabled, because a disabled button cannot be focused and users may not learn why. For complex widgets like menus, dialogs and tabs, follow the WAI-ARIA Authoring Practices or build on a tested library like Radix or React Aria.

  • Native elements first: button, a, input, select, dialog
  • useId() links label, input, hint and error
  • WCAG 2.2 AA asks for touch targets of at least 24 by 24 CSS pixels
SequenceHow a screen reader meets an Input with an error
User
Screen reader
Input primitive
  1. 1Presses Tab to the field
    User to Screen reader: Presses Tab to the field
  2. 2Reads label via htmlFor
    Screen reader to Input primitive: Reads label via htmlFor
  3. 3aria-invalid=true
    Input primitive to Screen reader: aria-invalid=true
  4. 4Follows aria-describedby
    Screen reader to Input primitive: Follows aria-describedby
  5. 5Email, invalid, Enter a valid email
    Screen reader to User: Email, invalid, Enter a valid email

Documenting variants so people use them

A component nobody can find gets rebuilt. Storybook is the common tool: each component gets a stories file showing every variant and state (default, hover, disabled, loading, error), and the team browses them in one place. Its accessibility addon runs automatic checks on every story. Smaller projects can use a single /ui page in the app that renders every component.

For each component, write down when to use it and when not to, for example use the danger variant only for destructive actions. Keep a short changelog. When you change a primitive, every screen that uses it changes too, so visual review of the stories catches surprises before release.

  • npx storybook@latest init sets Storybook up in an existing project
  • One story per variant and state
  • Document do and don't usage next to each example

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Define design tokens for color and spacing~45m
  2. 2
    Build Button, Input and Card primitives~45m
  3. 3
    Make primitives keyboard accessible~45m
  4. 4
    Document component variants~45m

Key points

  • Name tokens by purpose, like color-danger, not by value, like red-500.
  • Give primitives a small set of variants and sizes instead of endless props.
  • Build focus styles, labels and keyboard support into primitives once.
  • Document each variant with an example so the team uses them consistently.

Key terms

Tap a card to flip it. 0/4 known

Examples

tsx
type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & {
  variant?: 'primary' | 'secondary';
};

const styles = {
  primary: 'bg-brand text-white hover:bg-brand-dark',
  secondary: 'border border-gray-300 text-gray-900',
};

export function Button({ variant = 'primary', className = '', ...rest }: ButtonProps) {
  const base = 'rounded px-4 py-2 focus-visible:outline-2';
  return <button className={`${base} ${styles[variant]} ${className}`} {...rest} />;
}

A typed Button primitive with variants and native props.

An accessible Input primitive

Your forms keep forgetting to connect labels and errors. Step 1: build one Input that takes label, hint and error props. Step 2: create a stable id with useId. Step 3: link the label with htmlFor and the hint or error with aria-describedby. Step 4: set aria-invalid when there is an error, and give the error role="alert" so it is announced when it appears. Now every form gets correct screen reader behavior by default.

tsx
import { useId } from "react";
type Props = React.ComponentProps<"input"> & { label: string; error?: string };

export function Input({ label, error, ...rest }: Props) {
  const id = useId();
  const errId = `${id}-err`;
  return (
    <div className="grid gap-1">
      <label htmlFor={id} className="text-sm font-medium">{label}</label>
      <input id={id} aria-invalid={!!error} aria-describedby={error ? errId : undefined}
        className="rounded border px-3 py-2 aria-[invalid=true]:border-red-600" {...rest} />
      {error && <p id={errId} role="alert" className="text-sm text-red-700">{error}</p>}
    </div>
  );
}

Semantic tokens that survive a rebrand

Marketing changes the brand from blue to green. Step 1: your components use bg-primary and text-on-primary, never bg-blue-600. Step 2: those utilities come from semantic variables in @theme that point at base values. Step 3: change the mapping of --color-primary from the blue value to the green one. Step 4: every Button, link and badge updates in one commit, and nobody greps the codebase for blue-600.

css
@import "tailwindcss";

@theme {
  /* semantic tokens used by components */
  --color-primary: oklch(0.6 0.17 150);    /* was a blue */
  --color-on-primary: oklch(1 0 0);
  --color-danger: oklch(0.58 0.22 27);
  --color-surface: oklch(0.99 0 0);
  --radius-control: 0.5rem;
}

/* Button: bg-primary text-on-primary rounded-control */

Quick reference

  • Name tokens by job (primary, danger, surface), not by value (blue-600)
  • Components use semantic tokens; semantic tokens point at base values
  • Type primitives as React.ComponentProps<"button"> and spread ...rest
  • React 19: ref is a regular prop, forwardRef is no longer required
  • Default Button type to "button" so it does not submit forms by accident
  • Text contrast 4.5:1 (3:1 for large text and control borders) per WCAG
  • aria-invalid plus aria-describedby connect an input to its error
  • Use Radix, React Aria or the WAI-ARIA patterns for menus, dialogs and tabs

Common mistakes

  • Creating a new one-off button style on every page.
  • Building custom controls from divs and losing keyboard support.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which token name is best for a component to use?

Question 2

2A Button inside a form submits the form when clicked, even though it only opens a menu. Why?

Question 3

3How should an Input tell screen readers about its error message?

Question 4

4Why is a div with onClick a poor replacement for a button?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write three semantic color tokens (primary, danger, surface) in an @theme block and use them in a Button class list.

  2. Exercise 2medium

    Build a typed Button with variant (primary, ghost, danger) and size (sm, md) props, native props passed through, and type defaulting to button.

  3. Exercise 3hard

    Add a loading state to your Button that keeps the button width, shows a spinner, blocks repeat clicks and tells screen readers it is busy.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I build my own components or use a library like shadcn/ui?
For most apps, start from a library. shadcn/ui copies Radix-based components into your project so you own and restyle them, which gives you tested accessibility without starting from zero.
What is the difference between a design system and a component library?
A component library is the code. A design system also includes the tokens, the usage rules and the documentation that tell people when and how to use each piece.
How many variants should a Button have?
Usually three to five, such as primary, secondary, ghost and danger, plus two or three sizes. More than that is a sign people are inventing styles instead of following the system.
ShareXLinkedInWhatsApp