Design tokens
@theme: colours, spacing and type as CSS variables, the design system this site runs on.
What it is
Tailwind v4 is configured in CSS, not JavaScript. Inside @theme you declare design tokens as CSS variables in named namespaces (--color-*, --font-*, --text-*, --radius-*, --spacing…), and Tailwind generates the matching utilities: declare --color-accent and you get bg-accent, text-accent, border-accent, ring-accent and the rest. Because they're real CSS variables, tokens can change at runtime: per colour scheme, per component, per user. This site keeps its raw values in :root (swapped for dark mode) and points the theme at them with @theme inline, so every utility follows the current scheme.
When to use it
- Every project: name the colours and sizes your design uses, once
- Theming: dark mode, brands, or user-chosen accents from one set of utilities
- Sharing a design system across sites (the plan for these labs)
1. The tokens this site runs on
| Token | Value now | Utility | Used for |
|---|---|---|---|
| --bg | … | bg-bg | Page background |
| --surface | … | bg-surface | Cards and panels |
| --line | … | border-line | Borders |
| --fg | … | text-fg | Text |
| --muted | … | text-muted | Secondary text |
| --accent | … | bg-accent | Buttons, highlights |
| --accent-ink | … | text-accent-ink | Links |
| --code | … | bg-code | Code and chips |
| --danger | … | text-danger | Errors |
| --font-sans | … | font-sans | Body text |
| --font-mono | … | font-mono | Code |
| --text-sm | … | text-sm | Small text |
| --spacing | … | p-1, gap-1, m-1… | One spacing unit |
| --radius-xl | … | rounded-xl | Card corners |
2. Change a token, everything follows
- --accent
- oklch(50% 0.130 237) ≈ #006ba3
- White text on it
- ✓ 5.75:1 passes WCAG AA for normal text (4.5:1)
- Gamut
- Outside sRGB: a wide-gamut screen shows it as set; others clip it to the nearest colour they can show.
OKLCH describes a colour as lightness, chroma (colourfulness) and hue, so moving the hue keeps the perceived brightness steady; that's why Tailwind v4's whole palette is defined in it. Some of those colours are richer than ordinary (sRGB) screens can show: Tailwind's own sky-700 is one, by a hair.
3. One spacing token
Padding p-4 = 16px · gaps gap-3 = 12px, gap-2 = 8px
Show code · node_modules/@djmiskey/lab-tokens/tokens.css
/* The shared tokens of the djmiskey.com labs, as plain CSS custom properties:
no build step needed, so a lab without Tailwind can use them too.
Each lab supplies its own accent, the one colour that tells the labs apart:
:root {
--lab-accent: #0369a1; buttons and highlights; white text on it ≥ 4.5:1
--lab-accent-dark: #0369a1; optional, if dark mode needs a different one
--lab-accent-ink: #075985; accent-coloured text, ≥ 4.5:1 on --bg, --surface, --code
--lab-accent-ink-dark: #38bdf8; the same, on the dark backgrounds
}
The theme: <html data-theme="light|dark"> decides (set before the first
paint by the script in theme.ts); without it (no JavaScript), the device
setting does. The dark values appear twice because a selector can't sit
both inside and outside a media query. */
:root {
--bg: #f7f7f5;
--surface: #ffffff;
--line: #e3e2de;
--fg: #1d1d1b;
--muted: #64625d;
--code: #f1f0ec;
--danger: #c0362c;
--accent: var(--lab-accent);
--accent-ink: var(--lab-accent-ink);
color-scheme: light;
}
:root[data-theme="dark"] {
--bg: #131312;
--surface: #1c1c1a;
--line: #2f2e2b;
--fg: #ecebe7;
--muted: #a09e97;
--code: #232321;
--danger: #ff7b6e;
--accent: var(--lab-accent-dark, var(--lab-accent));
--accent-ink: var(--lab-accent-ink-dark);
color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme]) {
--bg: #131312;
--surface: #1c1c1a;
--line: #2f2e2b;
--fg: #ecebe7;
--muted: #a09e97;
--code: #232321;
--danger: #ff7b6e;
--accent: var(--lab-accent-dark, var(--lab-accent));
--accent-ink: var(--lab-accent-ink-dark);
color-scheme: dark;
}
}
Show code · src/app/globals.css
@import "tailwindcss";
@import "@djmiskey/lab-tokens/tailwind.css";
@plugin "@tailwindcss/typography";
/* The shared look of every djmiskey.com lab (neutral tokens, light/dark,
fluid type, element defaults) comes from @djmiskey/lab-tokens. Each lab
sets only its own accent: this one is sky. */
:root {
--lab-accent: #0369a1; /* white text on it 5.9:1, and ≥ 3:1 against the dark backgrounds */
--lab-accent-ink: #075985; /* clears 4.5:1 on the surface and the code background */
--lab-accent-ink-dark: #38bdf8;
}
/* Extending Tailwind in CSS (see /tailwind/utilities). */
/* A variant: hover for a mouse, focus-visible for a keyboard, in one word. */
@custom-variant hocus (&:hover, &:focus-visible);
/* A plain utility: hide the scrollbar but keep the scrolling. */
@utility scrollbar-hidden {
scrollbar-width: none;
&::-webkit-scrollbar {
display: none;
}
}
/* Functional utilities: the * is the value. fade-x-8 fades both edges over
calc(var(--spacing) * 8), like any spacing utility; fade-x-[3rem] takes any length. */
@utility fade-x-* {
--fade: calc(var(--spacing) * --value(integer));
--fade: --value([length]);
mask-image: linear-gradient(to right, transparent, #000 var(--fade), #000 calc(100% - var(--fade)), transparent);
}
/* tab-4 takes a number; tab-github a named size from @theme below. */
@utility tab-* {
tab-size: --value(integer, --tab-size-*);
}
/* A utility that uses a variant inside: @variant applies hocus to its own CSS.
The underline grows from the left, and simply appears with reduced motion. */
@utility link-grow {
text-decoration: none;
background: linear-gradient(currentColor 0 0) 0 100% / 0 2px no-repeat;
transition: background-size 0.2s ease-out;
@variant hocus {
background-size: 100% 2px;
}
@variant motion-reduce {
transition: none;
}
}
/* Typography (prose), coloured by this site's tokens, so light and dark mode
come free: no prose-invert needed. */
@utility prose-site {
--tw-prose-body: var(--color-fg);
--tw-prose-headings: var(--color-fg);
--tw-prose-lead: var(--color-muted);
--tw-prose-links: var(--color-accent-ink);
--tw-prose-bold: var(--color-fg);
--tw-prose-counters: var(--color-muted);
--tw-prose-bullets: var(--color-muted);
--tw-prose-hr: var(--color-line);
--tw-prose-quotes: var(--color-fg);
--tw-prose-quote-borders: var(--color-accent);
--tw-prose-captions: var(--color-muted);
--tw-prose-kbd: var(--color-fg);
--tw-prose-kbd-shadows: color-mix(in oklab, var(--color-fg) 15%, transparent);
--tw-prose-code: var(--color-fg);
--tw-prose-pre-code: var(--color-fg);
--tw-prose-pre-bg: var(--color-code);
--tw-prose-th-borders: var(--color-line);
--tw-prose-td-borders: var(--color-line);
}
@theme {
--tab-size-github: 8;
}
Show code · src/app/tailwind/theme/AccentPlayground.tsx
"use client";
import { type CSSProperties, useState } from "react";
import { Button } from "@/components/ui";
import { contrast, oklchToRgb, rgbToHex } from "@/lib/color";
// Change one token, --accent, and everything built on it follows: the button,
// the badge, the focus ring. The tokens are set on this box only (custom
// properties inherit), so the rest of the page keeps its colours.
const START = { l: 0.5, c: 0.13, h: 237 }; // close to the site's own sky accent
export function AccentPlayground() {
const [{ l, c, h }, set] = useState(START);
const { rgb, inGamut } = oklchToRgb(l, c, h);
const ratio = contrast(rgb, [255, 255, 255]);
const passes = ratio >= 4.5;
const accent = `oklch(${(l * 100).toFixed(0)}% ${c.toFixed(3)} ${h.toFixed(0)})`;
const slider = (key: "l" | "c" | "h", label: string, min: number, max: number, step: number, value: number, show: string) => (
<label className="grid grid-cols-[6.5rem_minmax(0,1fr)_4.5rem] items-center gap-2 text-sm">
<span>{label}</span>
<input type="range" min={min} max={max} step={step} value={value} onChange={(e) => set((v) => ({ ...v, [key]: Number(e.target.value) }))} className="w-full accent-[var(--accent)]" />
<span className="text-right font-mono text-xs tabular-nums">{show}</span>
</label>
);
return (
<div className="grid grid-cols-[minmax(0,1fr)] gap-4">
<div className="grid gap-2">
{slider("l", "Lightness", 0.3, 0.8, 0.01, l, `${(l * 100).toFixed(0)}%`)}
{slider("c", "Chroma", 0, 0.25, 0.005, c, c.toFixed(3))}
{slider("h", "Hue", 0, 360, 1, h, `${h.toFixed(0)}°`)}
</div>
{/* The preview: same components as the rest of the site, with --accent overridden here. */}
<div className="rounded-xl border border-line bg-bg p-4" style={{ "--accent": accent } as CSSProperties} data-testid="accent-preview">
<div className="flex flex-wrap items-center gap-3">
<Button>Primary action</Button>
<span className="rounded-full bg-accent px-2.5 py-0.5 text-xs font-semibold text-white">Live</span>
<span className="inline-block size-8 rounded-lg bg-accent" aria-hidden="true" />
<input type="text" defaultValue="Focus me" aria-label="An input with an accent focus ring" className="max-w-40" />
</div>
</div>
<dl className="m-0 grid grid-cols-[max-content_minmax(0,1fr)] gap-x-3 gap-y-1 text-sm">
<dt className="text-muted">--accent</dt><dd className="m-0 font-mono text-xs">{accent} <span className="text-muted">≈ {rgbToHex(rgb)}</span></dd>
<dt className="text-muted">White text on it</dt>
<dd className="m-0" data-testid="contrast">
{/* A word and an icon, not only a colour. */}
<strong className={passes ? "text-fg" : "text-danger"}>{passes ? "✓" : "✕"} {ratio.toFixed(2)}:1</strong>{" "}
<span className="text-muted">{passes ? "passes WCAG AA for normal text (4.5:1)" : ratio >= 3 ? "only enough for large text (3:1), not body text" : "fails: too little contrast to read"}</span>
</dd>
{!inGamut && (<><dt className="text-muted">Gamut</dt><dd className="m-0 text-muted">Outside sRGB: a wide-gamut screen shows it as set; others clip it to the nearest colour they can show.</dd></>)}
</dl>
<div><Button variant="ghost" onClick={() => set(START)}>Reset</Button></div>
</div>
);
}