Runtime entry
@flexpixels/cortex/runtime: generate the theme CSS and check contrast in the browser, live, without zod. Its CSS is the full engine's, byte for byte.
@flexpixels/cortex/runtime is the entry for theming in the browser while the page runs: a theme configurator, or a page whose users re-theme it live. It generates the theme stylesheet and checks contrast, and nothing else.
import { mergeThemeConfig, generateThemeCSS, checkContrast } from "@flexpixels/cortex/runtime";
const config = mergeThemeConfig({
seeds: { primary: "#4F46E5", secondary: "#7C3AED", neutral: "#334155" },
});
document.querySelector("#theme")!.textContent = generateThemeCSS(config);
checkContrast("#FACC15", "#1F2937", "AA"); // { passes, ratio, required, lc, polarity }Exports
| Export | What it does |
|---|---|
generateThemeCSS(config, options?) | The same function as the full entry’s: the lean stylesheet, or the full one when output.tokens is "full". |
mergeThemeConfig(patch, base?) | patch laid over base (the default config). For a patch of plain values it returns the config the full engine’s mergeThemeConfig returns. |
defaultThemeConfig | The full engine’s default config, resolved when the package was built. |
checkContrast(fg, bg, level?) | WCAG 2 ratio and APCA Lc for two CSS colours, as in Contrast helpers. |
ThemeConfig, Seeds, ConfigPatch | Types. |
What it leaves out
- Full validation. The runtime carries no zod. It does not check that a config is complete or that its numbers are in range: validate configs with
validateThemeConfigfrom@flexpixels/cortexwhile you author them, for example in the configurator’s save step or in CI. It does keep the full engine’s CSS-safety rules, on every patch and every config it is handed: a value that would end its declaration or rule (a;,}or<in a font stack, say), a seed that is not a hex colour, a key that cannot be part of a CSS name or reaches an object’s prototype, and a number that is not finite all throw an error naming the field, before anything reaches a stylesheet. Values typed into a running page are safe to pass. - What only the full engine resolves.
mergeThemeConfigthrows, naming the field, for a typography or elevationscenario, forelevation.levelAliases, and fornull(removing a field). Resolve such a config withmergeThemeConfigfrom@flexpixels/cortexand pass the result to the runtime’sgenerateThemeCSS: a resolved config is plain JSON. - The helpers. Typography, elevation, palette, vision, DTCG and DESIGN.md exports stay in the full entry.
Same CSS as the full engine
For the default config and the two brands of the Cortex eval, under settings.schemes "both", "light" and "dark", the runtime’s stylesheet is byte for byte the full engine’s. A test builds the shipped bundle and compares them, and checks that the bundle contains no zod.
Size
Measured with Bun, minified, with culori and apca-w3 bundled in (the file a browser loads with nothing else):
| Entry | Minified | Gzipped |
|---|---|---|
@flexpixels/cortex/browser | 379 KB | 125 KB |
@flexpixels/cortex/runtime | 166 KB | 58 KB |
A test fails if the runtime bundle grows more than 10% past these sizes.