3.3 KiB
3.3 KiB
Tailwind with the tokens
Tailwind is fine as long as the tokens stay the single source of truth. Never use Tailwind's stock palette (bg-blue-600, text-gray-500) in components. Map the tokens into the theme and use only those names.
Tailwind v4 (CSS-first config)
/* src/styles/app.css */
@import "tailwindcss";
@import "./tokens.css"; /* the file from assets/tokens.css */
/* Explicit toggle. System dark mode needs no variant: the tokens already switch. */
@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));
@theme inline {
--color-*: initial; /* drop the stock palette */
--color-bg: var(--bg);
--color-surface: var(--surface);
--color-surface-2: var(--surface-2);
--color-border: var(--border);
--color-border-strong: var(--border-strong);
--color-fg: var(--fg);
--color-fg-muted: var(--fg-muted);
--color-fg-subtle: var(--fg-subtle);
--color-accent: var(--accent);
--color-accent-hover: var(--accent-hover);
--color-accent-fg: var(--accent-fg);
--color-accent-soft: var(--accent-soft);
--color-accent-text: var(--accent-text);
--color-ring: var(--ring);
--color-ok: var(--ok); --color-ok-soft: var(--ok-soft);
--color-warn: var(--warn); --color-warn-soft: var(--warn-soft);
--color-danger: var(--danger); --color-danger-soft: var(--danger-soft);
--color-info: var(--info); --color-info-soft: var(--info-soft);
--color-transparent: transparent;
--color-current: currentColor;
/* Names must differ from the token they read, or the variable refers to itself and becomes invalid. */
--font-heading: var(--font-display); /* class: font-heading */
--font-sans: var(--font-body);
--shadow-card: var(--shadow-raised); /* class: shadow-card */
--shadow-menu: var(--shadow-popover);
--shadow-dialog: var(--shadow-modal);
/* --radius-sm/md/lg, --font-mono and --ease-out need no mapping: tokens.css declares them
with Tailwind's own names, unlayered, so they override Tailwind's defaults. */
}
Because the colors resolve through CSS variables that already switch with the theme, components rarely need dark: variants at all: bg-surface text-fg border-border is correct in both themes.
Usage examples:
<button class="inline-flex h-10 items-center gap-2 rounded-md bg-accent px-4 text-sm font-medium text-accent-fg hover:bg-accent-hover focus-visible:outline-2 focus-visible:outline-ring">Save changes</button>
<div class="rounded-md border border-border bg-surface p-5">…</div>
<span class="text-xs font-semibold uppercase tracking-[0.06em] text-fg-muted">Revenue</span>
Tailwind v3 projects
Put the same mapping in tailwind.config under theme.extend.colors (bg: 'var(--bg)', fg: 'var(--fg)', …), plus fontFamily, borderRadius and boxShadow. Set darkMode: ['selector', '[data-theme="dark"]'] for the toggle. As in v4, the variables already follow the system theme, so dark: classes are rarely needed.
Habits to avoid in Tailwind output
rounded-xl shadow-lgas the default on every container.bg-gradient-to-r from-purple-500 to-blue-500anywhere.text-centeron whole sections.- Arbitrary values (
p-[13px],text-[#333]) instead of tokens. - Very long class strings repeated across files: extract a component instead.