Files
ui/pkg/tokens/scripts/gen-css.mjs
zeekay 403e60db1c feat(tokens): a data palette for what a person colours, a warning state, the faint ink and two small radii; 1.1.0
Thirty 12-step hues, light and dark, as --data-<hue>-<step> in their own
dist/data.css and the full bundle, never in colors.css: data colour is not
chrome. hues, scales, next() and hueOf() name the same set in TypeScript;
hueOf is twenty-ui's string hash, so an imported record keeps its colour.

--state-warning, --state-success-bg, --text-faint, --radius-xs (4px) and
--radius-2xs (2px), icon sizes, blur radii, a 1.5s loop and the keycap
underline. font-size-xs is 12px, as design 0.5.35 publishes it.
2026-10-19 01:32:08 -07:00

77 lines
3.6 KiB
JavaScript

// gen-css.mjs — the ONE TS→CSS bridge for @hanzo/tokens.
//
// The token VALUES live in src/theme.ts (dark, light, scale). This script reads
// them and emits the CSS custom-property bundle that non-React surfaces embed:
//
// dist/colors.css — the colour tokens only (:root dark + .light). This is the
// contract @hanzo/design's tokens/colors.css republishes,
// byte-for-byte in values, so its existing consumers (the
// git/ci/cd surfaces, and any app linking styles.css) keep
// working with one edit here instead of three hand-copies.
// dist/tokens.css — the full bundle: the same colours PLUS radius, type and
// spacing, and the data palette. This is the `@hanzo/tokens/css`
// export and what @hanzo/brand re-emits for Go surfaces.
// dist/data.css — the data palette alone (`@hanzo/tokens/css/data`): thirty
// 12-step hues for what a person colours. Not in colors.css,
// which design republishes as chrome.
//
// Runs after tsup, importing the built ./dist/theme.mjs — so the numbers the CSS
// carries are exactly the numbers the TypeScript exports; they cannot drift.
import { writeFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { dark, light, scale, data } from '../dist/theme.mjs'
const dist = join(dirname(fileURLToPath(import.meta.url)), '..', 'dist')
const decl = (v) => ` --${v.name}:${v.value};${v.comment ? ` /* ${v.comment} */` : ''}`
const banner = (title, note) =>
note?.length
? `\n /* ——— ${title} ———\n${note.map((l) => ` ${l}`).join('\n')} */`
: `\n /* ——— ${title} ——— */`
const block = (sections) => sections.map((s) => `${banner(s.title, s.note)}\n${s.vars.map(decl).join('\n')}`).join('\n')
/** :root{ color-scheme:dark; …dark… } .light{ color-scheme:light; …light… } */
function sheet(header, rootSections, lightSections) {
return (
`${header}\n\n` +
`:root{\n color-scheme:dark;\n${block(rootSections)}\n}\n\n` +
`.light{\n color-scheme:light;\n${block(lightSections)}\n}\n`
)
}
const count = (s) => s.reduce((n, g) => n + g.vars.length, 0)
const GENERATED =
'/* AUTO-GENERATED by @hanzo/tokens scripts/gen-css.mjs from src/theme.ts — DO NOT EDIT.\n' +
' * Edit the token in @hanzo/tokens src/theme.ts and re-run `pnpm --filter @hanzo/tokens gen-css`. */'
const colorsCss = sheet(
`${GENERATED}\n/* Hanzo colour tokens — monochrome, dark-default. One hue, an opacity ladder. */`,
dark,
light,
)
writeFileSync(join(dist, 'colors.css'), colorsCss)
const tokensCss = sheet(
`${GENERATED}\n/* Hanzo design tokens — colour + radius + type + spacing + data. The full bundle. */`,
[...dark, ...scale, ...data.dark],
[...light, ...data.light],
)
writeFileSync(join(dist, 'tokens.css'), tokensCss)
const dataCss = sheet(
`${GENERATED}\n/* Hanzo data palette — colour a person chose for a record, never chrome. src/data.ts. */`,
data.dark,
data.light,
)
// Thirty hues, twelve steps, two themes — every one or the file is wrong.
if (count(data.dark) !== 360 || count(data.light) !== 360)
throw new Error(`gen-css: the data palette has ${count(data.dark)} dark and ${count(data.light)} light steps, not 360`)
writeFileSync(join(dist, 'data.css'), dataCss)
console.log(
`gen-css: wrote dist/colors.css (${count(dark)} dark + ${count(light)} light) ` +
`and dist/tokens.css (+${count(scale)} scale) and dist/data.css (${count(data.dark)} steps a theme)`,
)