Skip to content
Frontend & UIChapter 42 / 57Intermediate6 min read

Design Tokens & UI Consistency

Design tokens and UI consistency. Use semantic CSS variables, a Tailwind theme, no raw hex, a spacing scale and shared UI primitives so every screen looks alike.

On this page

1. What is this practice?

A design token is a named design value: primary, surface, destructive, border. A component does not say "this is #1e40af", it says "this is primary". The real color of primary is decided in one place, in globals.css.

It is the same idea as named constants in code, just for design.

2. Why does it matter?

A real story: the brand color changed. The codebase had hex values in 140 places, across 80 files, and three slightly different blues. It took two weeks and some screens still stayed in the old color. Adding dark mode was impossible, because every component had written its own white.

Another story: two developers built different modals. One closed on Escape and the other did not, and keyboard users got trapped inside one modal.

Raw values and ad hoc componentsTokens and shared primitives
A rebrand means search and replace across a hundred filesA rebrand means changing a token in one file
Dark mode means redoing every componentDark values for the tokens, components stay the same
Three slightly different grays and radiiOne scale, every step has a name
Every developer has their own modal and buttonOne primitive that everyone uses
Accessibility differs everywhereFocus ring and contrast are fixed once in the primitive

3. When to follow it

LevelHow importantWhy
Learning projectUse Tailwind's default scale, avoid arbitrary valuesStaying inside the scale is the first step to consistency
Production codeSemantic tokens and primitives are required. Hex in a component gets blocked in reviewRebrand, dark mode and accessibility all stand on this
Senior levelDesign and document token naming, scales and the primitive inventoryThe team knows which primitive exists and what is forbidden

4. Bad vs good code

Bad: raw values and a custom button

tsx
export function DeleteButton({ onDelete }: DeleteButtonProps) {
  return (
    <button
      onClick={onDelete}
      className="rounded-[7px] bg-[#dc2626] px-[13px] py-[9px] text-[#ffffff] outline-none"
    >
      Delete
    </button>
  );
}

A hex color, odd spacing (13px), no dark mode, and outline-none removed the keyboard focus ring entirely.

Good: tokens in one place

css
@import "tailwindcss";

:root {
  --surface: oklch(0.99 0 0);
  --on-surface: oklch(0.21 0 0);
  --primary: oklch(0.52 0.2 262);
  --on-primary: oklch(0.99 0 0);
  --destructive: oklch(0.58 0.22 27);
  --on-destructive: oklch(0.99 0 0);
  --border: oklch(0.92 0 0);
}

.dark {
  --surface: oklch(0.18 0 0);
  --on-surface: oklch(0.96 0 0);
  --border: oklch(0.3 0 0);
}

@theme inline {
  --color-surface: var(--surface);
  --color-on-surface: var(--on-surface);
  --color-primary: var(--primary);
  --color-on-primary: var(--on-primary);
  --color-destructive: var(--destructive);
  --color-on-destructive: var(--on-destructive);
  --color-border: var(--border);
}

A helper for merging classes:

ts
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

Now the feature component only uses a primitive:

tsx
import { Button } from "@/components/ui/button";

export function DeleteButton({ onDelete }: DeleteButtonProps) {
  return (
    <Button variant="destructive" onClick={onDelete}>
      Delete
    </Button>
  );
}

Inside the Button primitive, bg-destructive text-on-destructive rounded-md px-4 py-2 and a visible focus ring are written only once.

What is the difference?

ThingBadGood
Color#dc2626 in the componentThe destructive token
Spacing13px, 9pxSteps of the scale, px-4 py-2
Dark modeImpossibleThe token's dark value
Focusoutline-none, ring goneA visible ring in the primitive
ReuseA new button everywhereOne Button, with variants

5. How to apply it

Setting up tokens and primitives
Name the roles: surface, primary, destructive, border, text tiers
CSS variables in globals.css, light and dark
Map them in the Tailwind theme
Fixed scales for spacing, radius, shadow, type and motion
Primitives in components/ui, and feature components use them
Token groupExample
Colorsurface, on-surface, primary, destructive, border, muted
SpacingA rhythm of 4 or 8: 4, 8, 12, 16, 24, 32
TypeOne scale, like 12, 14, 16, 18, 24, 32, and two or three weights
Radius, shadow, z-indexA small fixed scale for each, every step named
MotionDurations like 150ms for state, 200ms for reveal, 300ms for attention

Before you build a new component, look in components/ui. If you find a similar primitive, add a variant, do not make a new one. Every primitive should handle loading, empty, error and disabled states.

6. Exceptions and common mistakes

Many teams use the Tailwind palette directly in components (bg-red-600), and in small projects or prototypes that works fine. Semantic tokens add a layer. The trade-off: a little work at the start, but at rebrand, dark mode and multi-brand time, that same layer saves weeks.

Chart data colors, a brand logo SVG, or a marketing illustration can sit outside the token system, but even charts should have a fixed palette token set.

Mistake: Naming tokens after colors, like blue-brand

Why? If a rebrand turns blue-brand green, the name starts lying.

Better: Name them by role: primary, accent, destructive, surface.

Mistake: Arbitrary values like p-[13px] and text-[#333]

Why? Every one-off value breaks the scale, and screens slowly start to look different.

Better: Use the nearest step of the scale. If you truly need a new value, add it to the tokens.

Mistake: Removing the focus ring because it does not look good in the design

Why? Keyboard users cannot tell where they are, and this is a blocking accessibility problem.

Better: Style the ring with a token, do not remove it. Get it right once in the primitive.

7. What to learn before and after

Suggested order
Styling with Tailwind
Design Tokens & UI Consistency
Visual Hierarchy & Spacing
Motion & Design Iteration

Final quick revision

CheckYes / No
Raw hex in a componentNo
Tokens named by roleYes
Light and dark values in one placeYes
Arbitrary spacing valuesNo, use the scale
Looked in components/ui before building a new primitiveYes
Focus ring visibleYes