mirror of
https://github.com/hanzoai/ui.git
synced 2026-11-20 20:07:34 +00:00
77 lines
3.6 KiB
JavaScript
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`): twenty-six
|
|
// 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,
|
|
)
|
|
// Twenty-six hues, twelve steps, two themes — every one or the file is wrong.
|
|
if (count(data.dark) !== 312 || count(data.light) !== 312)
|
|
throw new Error(`gen-css: the data palette has ${count(data.dark)} dark and ${count(data.light)} light steps, not 312`)
|
|
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)`,
|
|
)
|