The Open Standard for Generative UI
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { defaultDarkTheme, defaultLightTheme } from "../components/ThemeProvider/defaultTheme.js";
import { camelToKebab } from "../components/ThemeProvider/utils.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const sectionHeaders: Record<string, string> = {
background: "// Surface / Background Colors",
textNeutralPrimary: "// Text Colors",
chatUserResponseBg: "// Chat Colors",
interactiveAccentDefault: "// Interactive Colors",
borderDefault: "// Border Colors",
space000: "// Spacing",
radiusNone: "// Border Radius",
fontBody: "// Typography Tokens",
shadow0: "// Shadows",
};
const lines: string[] = [];
lines.push("// Auto-generated by src/scripts/generate-css-utils.ts — do not edit manually.");
lines.push('@use "sass:map";');
lines.push("");
const entries = Object.entries(defaultLightTheme).filter(
([, value]) => typeof value === "string",
) as [string, string][];
for (const [key] of entries) {
if (sectionHeaders[key]) {
if (lines.length > 3) lines.push("");
lines.push(sectionHeaders[key]);
lines.push("");
}
const kebab = camelToKebab(key);
lines.push(`$${kebab}: var(--openui-${kebab});`);
}
// $chat-container-bg is an SCSS-level alias, not a CSS variable
lines.push("");
lines.push("$chat-container-bg: $background;");
// ─── Static typography system ────────────────────────────────────────────────
lines.push("");
lines.push(`\
// $variation picks the Inter "wght" axis token: regular or medium for the
// theme's Regular/Medium styles, null for fixed weights (600/700, code), where
// the font shorthand's reset leaves font-weight in charge.
@function typography($font-name, $variation: null) {
$axis: null;
@if $variation {
$axis: var(--openui-font-variation-#{$variation});
}
@return (
font: var(--#{$font-name}),
letter-spacing: var(--#{$font-name}-letter-spacing),
variation: $axis
);
}
$typography: (
body: (
default: typography(openui-text-body-default, regular),
heavy: typography(openui-text-body-default-heavy, medium),
small: typography(openui-text-body-sm, regular),
small-heavy: typography(openui-text-body-sm-heavy, medium),
large: typography(openui-text-body-lg, regular),
large-heavy: typography(openui-text-body-lg-heavy, medium),
link: typography(openui-text-body-default, regular),
medium: typography(openui-text-body-default, regular),
),
label: (
default: typography(openui-text-label-default, regular),
heavy: typography(openui-text-label-default-heavy, medium),
large: typography(openui-text-label-lg, regular),
large-heavy: typography(openui-text-label-lg-heavy, medium),
medium: typography(openui-text-label-default, regular),
medium-heavy: typography(openui-text-label-default-heavy, medium),
small: typography(openui-text-label-sm, regular),
small-heavy: typography(openui-text-label-sm-heavy, medium),
extra-small: typography(openui-text-label-xs, regular),
extra-small-heavy: typography(openui-text-label-xs-heavy, medium),
2-extra-small: typography(openui-text-label-xs, regular),
2-extra-small-heavy: typography(openui-text-label-xs-heavy, medium),
),
heading: (
xl: typography(openui-text-heading-xl),
large: typography(openui-text-heading-lg),
medium: typography(openui-text-heading-md),
small: typography(openui-text-heading-sm),
extra-small: typography(openui-text-heading-xs),
),
number: (
large: typography(openui-text-numbers-lg, regular),
large-heavy: typography(openui-text-numbers-lg-heavy, medium),
heavy: typography(openui-text-numbers-default-heavy, medium),
default: typography(openui-text-numbers-default, regular),
small: typography(openui-text-numbers-sm, regular),
small-heavy: typography(openui-text-numbers-sm-heavy, medium),
extra-small: typography(openui-text-numbers-xs, regular),
extra-small-heavy: typography(openui-text-numbers-xs-heavy, medium),
heading: typography(openui-text-numbers-heading-lg),
heading-small: typography(openui-text-numbers-heading-sm),
heading-medium: typography(openui-text-numbers-heading-md),
title: typography(openui-text-numbers-heading-lg),
title-medium: typography(openui-text-numbers-heading-md),
heading-large: typography(openui-text-numbers-heading-lg),
heading-xl: typography(openui-text-numbers-heading-xl),
),
code: (
default: typography(openui-text-code-default),
heavy: typography(openui-text-code-default-heavy),
small: typography(openui-text-code-sm),
small-heavy: typography(openui-text-code-sm-heavy),
),
);
@mixin typography($font-name, $font-variant) {
font: map.get($typography, $font-name, $font-variant, font);
letter-spacing: map.get($typography, $font-name, $font-variant, letter-spacing);
// The font shorthand resets font-variation-settings, so set the axis after it.
font-variation-settings: map.get($typography, $font-name, $font-variant, variation);
}
@mixin button-reset {
padding: 0;
border: 0;
background: transparent;
}`);
lines.push("");
const outputPath = path.resolve(__dirname, "../cssUtils.scss");
fs.writeFileSync(outputPath, lines.join("\n"));
console.info(`Generated cssUtils.scss at ${outputPath}`);
// ─── Generate openui-defaults.scss with :root custom property defaults ───────
const defaultsLines: string[] = [];
defaultsLines.push(
"// Auto-generated by src/scripts/generate-css-utils.ts — do not edit manually.",
);
defaultsLines.push("");
defaultsLines.push(":root {");
for (const [key, value] of entries) {
if (sectionHeaders[key]) {
if (defaultsLines.length > 3) defaultsLines.push("");
defaultsLines.push(` ${sectionHeaders[key]}`);
defaultsLines.push("");
}
const kebab = camelToKebab(key);
defaultsLines.push(` --openui-${kebab}: ${value};`);
}
defaultsLines.push("}");
defaultsLines.push("");
// ─── Dark mode defaults ──────────────────────────────────────────────────────
// Dark follows prefers-color-scheme unless the page pins a mode with
// `data-openui-theme="light" | "dark"` on <html> (set before paint by apps with
// a theme switcher, so the first frame is already in the chosen mode).
const darkEntries = Object.entries(defaultDarkTheme).filter(
([, value]) => typeof value === "string",
) as [string, string][];
const pushDarkBlock = (selector: string, indent: string) => {
defaultsLines.push(`${indent}${selector} {`);
for (const [key, value] of darkEntries) {
if (sectionHeaders[key]) {
defaultsLines.push("");
defaultsLines.push(`${indent} ${sectionHeaders[key]}`);
defaultsLines.push("");
}
const kebab = camelToKebab(key);
defaultsLines.push(`${indent} --openui-${kebab}: ${value};`);
}
defaultsLines.push(`${indent}}`);
};
defaultsLines.push("@media (prefers-color-scheme: dark) {");
pushDarkBlock(':root:not([data-openui-theme="light"])', " ");
defaultsLines.push("}");
defaultsLines.push("");
pushDarkBlock(':root[data-openui-theme="dark"]', "");
defaultsLines.push("");
const defaultsOutputPath = path.resolve(__dirname, "../openui-defaults.scss");
fs.writeFileSync(defaultsOutputPath, defaultsLines.join("\n"));
console.info(`Generated openui-defaults.scss at ${defaultsOutputPath}`);