Skip to content

FE-M0-15 · The token theme for Tailwind

ID FE-M0-15 · Level L0 · Run order row 26, after FE-M0-09 and before FE-M0-10 · Why D35

pnpm --filter web tokens also writes design/opleet/theme.css, Tailwind's names for every token in tokens.json, and tokens --check fails when either generated file is stale. app/app.css imports theme.css and maps by hand the values only opleet.css carries. A build generates utilities such as gap-space-4 and bg-brand. Nothing in design/opleet or components/opleet is edited; theme.css is a new generated file next to tokens.css. Committed on the branch web/shell.

  • TL-M0-29 is Done (timeline row 25a): grep -c tailwind apps/web/package.json prints 2.
  • FE-M0-09 is Done: you are on the branch web/shell and git status --short prints nothing.
  • This step needs no file from fe-pack-c.zip. Everything it writes is below.

1 · The tokens script writes theme.css too

Section titled “1 · The tokens script writes theme.css too”

tokens.css comes out exactly as before; only the second file is new. Read the comments in the script, they say how a class is named.

Terminal window
cat > apps/web/scripts/tokens.mjs <<'EOF'
// Writes design/opleet/tokens.css and design/opleet/theme.css from design/opleet/tokens.json (D8, D35).
// Run from apps/web: pnpm tokens. With --check it writes nothing and fails when either file is stale.
import { readFileSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
const path = (name) => fileURLToPath(new URL(`../design/opleet/${name}`, import.meta.url));
const tokens = JSON.parse(readFileSync(path("tokens.json"), "utf8"));
const from = `${tokens.name} v${tokens.version}`;
// tokens.css: the CSS variables. "{green-500}" refers to another token; it becomes
// var(--green-500), so a theme change follows it.
const resolve = (value) => String(value).replace(/\{([a-z0-9-]+)\}/g, "var(--$1)");
const modeValue = (value, mode) =>
typeof value === "object" && value !== null
? resolve(value[mode] ?? value.light)
: resolve(value);
const light = [];
const dark = [];
const add = (name, value) => {
const lightValue = modeValue(value, "light");
const darkValue = modeValue(value, "dark");
light.push(` --${name}: ${lightValue};`);
if (darkValue !== lightValue) dark.push(` --${name}: ${darkValue};`);
};
for (const token of tokens.color.tokens) add(token.name, token.value);
for (const group of ["spacing", "radius", "shadow", "size"]) {
for (const token of tokens[group].tokens) add(token.name, token.value);
}
for (const [family, stack] of Object.entries(tokens.type.families)) add(`font-${family}`, stack);
const tokensCss = `/* Generated by pnpm tokens from tokens.json (${from}). Don't edit by hand. */
:root,
[data-theme="light"] {
${light.join("\n")}
}
[data-theme="dark"] {
${dark.join("\n")}
}
`;
// theme.css: Tailwind's names for the same tokens (D35). A class is the utility's prefix plus the
// token's name: bg-green-500, text-text-strong, gap-space-4, w-sidebar-w, rounded-radius-md,
// shadow-shadow-glass. Each value is the variable tokens.css defines, read as the page runs, so
// dark mode follows data-theme and no dark: variant is needed. Sizes join the spacing scale so
// they work for width and height. Breakpoints are literal because a media query can't read a
// variable; they drop the bp- prefix, as in md:flex.
const theme = [];
const map = (namespace, name) => theme.push(` --${namespace}-${name}: var(--${name});`);
for (const token of tokens.color.tokens) map("color", token.name);
for (const token of tokens.spacing.tokens) map("spacing", token.name);
for (const token of tokens.size.tokens) map("spacing", token.name);
for (const token of tokens.radius.tokens) map("radius", token.name);
for (const token of tokens.shadow.tokens) map("shadow", token.name);
for (const token of tokens.breakpoint.tokens) {
theme.push(` --breakpoint-${token.name.replace(/^bp-/, "")}: ${token.value};`);
}
const themeCss = `/* Generated by pnpm tokens from tokens.json (${from}). Don't edit by hand. */
@theme inline {
${theme.join("\n")}
}
`;
const files = [
["tokens.css", tokensCss],
["theme.css", themeCss],
];
if (process.argv.includes("--check")) {
const stale = files.filter(([name, css]) => {
try {
return readFileSync(path(name), "utf8") !== css;
} catch {
return true;
}
});
if (stale.length > 0) {
const names = stale.map(([name]) => `design/opleet/${name}`).join(" and ");
console.error(`${names} out of date: run pnpm --filter web tokens`);
process.exit(1);
}
console.log("design/opleet/tokens.css and theme.css are up to date");
} else {
for (const [name, css] of files) writeFileSync(path(name), css);
console.log(`design/opleet/tokens.css: ${light.length} tokens, ${dark.length} with a dark value`);
console.log(`design/opleet/theme.css: ${theme.length} names for Tailwind`);
}
EOF

2 · app.css imports theme.css, and maps what only opleet.css carries

Section titled “2 · app.css imports theme.css, and maps what only opleet.css carries”

This replaces the file TL-M0-29 wrote. The four breakpoints that step set by hand are gone: theme.css now supplies them from tokens.json, so sm: starts at 480px (the design system's bp-sm) instead of 640px.

Terminal window
cat > apps/web/app/app.css <<'EOF'
/* App-level layout only. Colours, type and components come from the design system (@ui/design).
Tailwind adds layout utilities only (D35): no reset, no default theme. */
@layer theme, base, components, utilities;
/* Tailwind's names for the tokens and the breakpoints, written by pnpm --filter web tokens. */
@import "../design/opleet/theme.css" layer(theme);
@import "tailwindcss/utilities.css" layer(utilities);
@theme {
--spacing: 0.25rem;
}
/* Values that only opleet.css carries, so they are not in tokens.json (D35). Each one reads the
variable named beside it. */
@theme inline {
/* type scale: --fs-*, --lh-*, --ls-* */
--text-fs-display: var(--fs-display);
--text-fs-display--line-height: var(--lh-display);
--text-fs-display--letter-spacing: var(--ls-display);
--text-fs-h1: var(--fs-h1);
--text-fs-h1--line-height: var(--lh-h1);
--text-fs-h1--letter-spacing: var(--ls-tight);
--text-fs-h2: var(--fs-h2);
--text-fs-h2--line-height: var(--lh-h2);
--text-fs-h2--letter-spacing: var(--ls-tight);
--text-fs-h3: var(--fs-h3);
--text-fs-h3--line-height: var(--lh-h3);
--text-fs-body: var(--fs-body);
--text-fs-body--line-height: var(--lh-body);
--text-fs-small: var(--fs-small);
--text-fs-small--line-height: var(--lh-small);
--text-fs-caption: var(--fs-caption);
--text-fs-caption--line-height: var(--lh-caption);
--text-fs-eyebrow: var(--fs-eyebrow);
--text-fs-eyebrow--letter-spacing: var(--ls-eyebrow);
--text-fs-metric: var(--fs-metric);
--text-fs-metric--line-height: var(--lh-metric);
/* font weights: --fw-* */
--font-weight-regular: var(--fw-regular);
--font-weight-medium: var(--fw-medium);
--font-weight-semibold: var(--fw-semibold);
--font-weight-bold: var(--fw-bold);
}
/* Glass backdrops: --blur-glass and --blur-glass-thin are whole backdrop-filter values (a saturate
and a blur), which Tailwind's blur names can't hold, so they are utilities of their own. */
@utility backdrop-glass {
-webkit-backdrop-filter: var(--blur-glass);
backdrop-filter: var(--blur-glass);
}
@utility backdrop-glass-thin {
-webkit-backdrop-filter: var(--blur-glass-thin);
backdrop-filter: var(--blur-glass-thin);
}
html,
body {
min-height: 100dvh;
}
EOF

3 · Generate the theme, then check both files

Section titled “3 · Generate the theme, then check both files”
Terminal window
pnpm --filter web tokens
pnpm --filter web tokens --check
git status --short

4 · A canary file, then typecheck, lint and a build

Section titled “4 · A canary file, then typecheck, lint and a build”

The canary holds one class from each part of the theme, so the build has to generate them. It is removed in the same block, before anything is committed.

Terminal window
echo 'flex gap-space-4 md:p-space-6 bg-brand text-text-strong rounded-radius-md w-sidebar-w text-fs-h1 font-semibold backdrop-glass' > apps/web/app/canary.txt
pnpm --filter web typecheck
pnpm lint
pnpm --filter web build
for c in '.gap-space-4' '.md\:p-space-6' '.bg-brand' '.text-text-strong' '.rounded-radius-md' '.w-sidebar-w' '.text-fs-h1' '.font-semibold' '.backdrop-glass'; do printf '%s ' "$c"; grep -rlF -e "$c" apps/web/.next/static --include='*.css' | wc -l; done
rm apps/web/app/canary.txt
Terminal window
git add -A apps/web
git commit -m "feat(web): the token theme for tailwind (d35)"
  • Block 1 and 2: nothing printed.
  • Block 3: “design/opleet/tokens.css: 107 tokens, 40 with a dark value”, then “design/opleet/theme.css: 108 names for Tailwind”; the check prints “design/opleet/tokens.css and theme.css are up to date”; git status lists exactly M apps/web/app/app.css, M apps/web/scripts/tokens.mjs and ?? apps/web/design/opleet/theme.css. tokens.css is not listed: it did not change.
  • Block 4: typecheck and lint pass (“No fixes applied.”); the build passes; the loop prints nine lines, each ending in 1, the number of CSS files that hold the class.
  • Block 5: the commit line with 3 files changed.
Terminal window
pnpm --filter web tokens --check
git diff --stat --diff-filter=M HEAD~1 -- apps/web/design apps/web/components
git diff --name-only --diff-filter=A HEAD~1 -- apps/web/design apps/web/components

The first prints “up to date”. The second prints nothing: no existing design-system file changed. The third prints apps/web/design/opleet/theme.css and nothing else.

  • A line of the loop ends in 0: that class was not generated. For .md\:p-space-6, check that app.css has no --breakpoint lines of its own; for the others, check that app.css imports theme.css and that theme.css exists (block 3). Paste the line.
  • tokens --check says a file is out of date straight after block 3: something wrote to it in between. Run pnpm --filter web tokens again and paste git status --short.
  • git status lists tokens.css as modified: stop and paste git diff apps/web/design/opleet/tokens.css. The script must leave it byte for byte as it was.
  • Biome fails to parse @theme, @utility or layer(theme): the "css" line from TL-M0-29 block 4 is missing in biome.json. Paste the Biome output.
  • Typecheck fails in scripts/tokens.mjs: the heredoc was cut short or pasted twice. wc -l apps/web/scripts/tokens.mjs should print 91.
Terminal window
git checkout -- apps/web/app/app.css apps/web/scripts/tokens.mjs
rm -f apps/web/design/opleet/theme.css apps/web/app/canary.txt

Before the commit. After it: git reset --hard HEAD~1. Nothing has been pushed.

Not run yet. Log each run as a comment on #17.