Navigation
  • Home
  • Work
  • About
  • Writing
  • Contact
Socials
LinkedInGitHubEmailResume
Writing
Code by AsjadAsjad Anwar
  • Home
  • Work
  • About
  • Writing
  • Contact
5 November 2026 · 5 min read

Design tokens in Tailwind CSS v4: three layers, dark mode

I owned the front end of Indexed, an AI news reader with a card feed filtered by reader intent. It needed light and dark modes from the first screen, and I did not want every component to carry two sets of colour classes. A card written as bg-white dark:bg-neutral-900 text-black dark:text-white works, but each new component is another chance to forget one half.

The answer was three layers of CSS custom properties. Primitives hold raw values. Semantic tokens name a role, such as "canvas" or "ink", and point at a primitive. A Tailwind CSS v4 @theme block maps each semantic token to a utility class. Dark mode is one [data-theme="dark"] selector that re-points the semantic layer, so a component writes bg-canvas text-ink once and never mentions the theme.

The whole system is three files in a Next.js 15 app: primitives.css (91 lines), semantic.css (62 lines) and the @theme block at the top of globals.css. There is no tailwind.config file.

Primitive tokens: the raw palette

A design token is a named design value, stored once and referenced everywhere else. A primitive token is the lowest level: a name for a literal value, with no opinion about where it is used.

The primitives file declares 30 colours on :root: four surfaces (--paper-0, --paper-50, --night-900, --night-950), six inks, six alpha values, eight violets and six story-type tints. Violet is the only brand colour. Everything else is achromatic.

Two decisions here made the next layer simple. Light and dark values sit side by side as separate primitives (--ink-light-1 is #0a0a0a, --ink-dark-1 is #f3f3f3), so the primitives file never changes with the theme. And borders are not grey. They are black or white at low alpha: --alpha-light-line is rgba(0, 0, 0, 0.1) and --alpha-dark-line is rgba(255, 255, 255, 0.1). A border defined this way sits correctly on any surface, so I never had to pick a grey per background.

The same file holds the non-colour primitives: a spacing scale, five radii, three shadows, two font stacks, three layout widths and motion timings.

Semantic tokens: one name per role

A semantic token names a job in the interface instead of a colour. It is the only layer that knows about themes.

:root {
  --bg-canvas: var(--paper-0);
  --bg-surface: var(--paper-0);
  --fg-ink: var(--ink-light-1);
  --fg-ink-2: var(--ink-light-2);
  --line: var(--alpha-light-line);
  --accent: var(--violet-600);
  --on-accent: #ffffff;
}

[data-theme="dark"] {
  --bg-canvas: var(--night-950);
  --bg-surface: var(--night-900);
  --fg-ink: var(--ink-dark-1);
  --fg-ink-2: var(--ink-dark-2);
  --line: var(--alpha-dark-line);
  --accent: var(--violet-400);
  --on-accent: var(--ink-light-1);
}

That is trimmed. The full file has 17 semantic tokens: four surfaces, three ink levels, two lines and a hover fill, five accent tokens and two bar shadows. The dark block redefines all 17 and nothing else.

Dark mode is not a plain inversion. The accent moves from --violet-600 to the lighter --violet-400, and --on-accent, the text colour on an accent fill, flips from white to near-black to keep contrast. The bar shadows change shape as well as colour: on a near-black canvas a soft black shadow is invisible, so the dark value adds a 1px line of white at 8% opacity to give the sticky header and bottom nav an edge.

Mapping tokens to utilities with Tailwind v4 @theme

In Tailwind CSS v4, @theme is the CSS directive that replaces the JavaScript config. A variable declared in a namespace such as --color-* generates the matching utilities.

@import "tailwindcss";
@import "./styles/tokens/primitives.css";
@import "./styles/tokens/semantic.css";

@source "../../../packages/ui/src";

@theme {
  --color-canvas: var(--bg-canvas);
  --color-surface: var(--bg-surface);
  --color-ink: var(--fg-ink);
  --color-ink-2: var(--fg-ink-2);
  --color-line: var(--line);
  --color-accent: var(--accent);
  --color-on-accent: var(--on-accent);
}

The theme values are var() references, not literals. In the built CSS the utility is .bg-canvas{background-color:var(--color-canvas)}, and --color-canvas is var(--bg-canvas). The colour is resolved in the browser, so changing the attribute on <html> repaints every utility with no rebuild and no second set of classes.

The @source line matters in a monorepo. The shared component package lives outside the app, and without that line Tailwind would not scan it for class names.

Components then read only semantic names. This is the shared button:

variant: {
  // Indexed primary — ink bg, canvas fg
  primary: 'bg-ink text-canvas hover:opacity-90',
  // Accent — violet
  accent: 'bg-accent text-on-accent hover:opacity-90',
  // Outline — line border, transparent bg
  outline: 'bg-transparent text-ink border border-line-strong hover:bg-hover',
  // Ghost — invisible until hover
  ghost: 'bg-transparent text-ink-2 font-medium hover:bg-hover hover:text-ink',
},

There is no dark: class in it. The primary button is ink on canvas, which is black on white in light mode and near-white on near-black in dark mode, from the same two class names.

Switching light and dark mode with a data attribute

The switch is the next-themes library, configured to write an attribute instead of a class:

export function ThemeProvider(props: ComponentProps<typeof NextThemesProvider>) {
  return (
    <NextThemesProvider
      attribute="data-theme"
      defaultTheme="system"
      enableSystem
      disableTransitionOnChange
      {...props}
    />
  );
}

The default follows the operating system. disableTransitionOnChange stops every element animating its colours at the moment of the switch. The <html> element carries suppressHydrationWarning, because the attribute is set before React hydrates and would otherwise be reported as a mismatch.

The toggles in the side rail and on the settings page read resolvedTheme and only trust it after mount, so the server render and the first client render agree.

Trade-offs and what I'd do differently

Reading the code back, the layers leak in a few places.

  • Raw hex still exists in components. Three badge variants use literal colours such as text-[#c2410c], with no dark value. The media placeholder gradients are hex pairs with a hand-written dark selector:
const gradientStyle = {
  '--thumb-light': GRADIENTS_LIGHT[type],
  '--thumb-dark': GRADIENTS_DARK[type],
} as CSSProperties;
// ...
'[background:var(--thumb-light)] [[data-theme=dark]_&]:[background:var(--thumb-dark)]',

It works, but it is a second theming mechanism. These should have been semantic tokens.

  • Tints skip the semantic layer. @theme maps them straight from primitives, so they cannot change per theme.

  • Some @theme entries reference themselves. --radius-sm: var(--radius-sm) reuses the primitive's own name. The build output contains both that declaration, inside Tailwind's theme cascade layer, and the unlayered --radius-sm: 8px from the primitives file. Unlayered rules win, so it resolves, but it depends on cascade order. Distinct primitive names would be safer.

  • Two theme-colour sources. The browser themeColor values in the root layout are hard-coded #ffffff and #101010, duplicating --paper-0 and --night-950.

Takeaways

  • Keep raw values, role names and utility mapping in three separate layers, and let only the middle layer know about themes.
  • In Tailwind CSS v4, point @theme colours at var() references so utilities resolve at runtime and one attribute switches the theme.
  • Define borders and hover fills as alpha on black or white so they work on any surface.
  • Dark mode needs its own decisions for accent, on-accent text and shadows, not an inversion.
  • Audit for raw hex; every literal colour in a component is a second theming system.
Tailwind CSSDesign TokensDark ModeNext.js

From the project: Indexed

Keep reading

  • 3 December 2026 · 7 min read

    Offline GPS route tracking on iOS that survives a crash

    How I record GPS routes on iOS with no signal: every point goes to SQLite first, syncs in acknowledged batches, and a killed recording resumes on relaunch.

  • 28 November 2026 · 7 min read

    RTK Query caching for sensitive data: what never to cache

    How I set up RTK Query caching in a React admin console that shows patient data: memory only, 30-second revalidation, and reads that are never repeated.

All writing

Let’s work
together

Get in touch
theasjadanwar@gmail.com+92 321 4010734Resume (PDF)LinkedInGitHub
Based inLahore, Pakistan (UTC+5)
SocialsLinkedInGitHub