Build & Import
How to generate cortex theme CSS from your config and import it across Astro, Vite, Webpack, Next.js, Bun, and plain HTML.
What This Guide Is For
Cortex is a library, not a CLI — you own the build step. This page covers what generateThemeCSS(config) and generateFullThemeCSS(config) produce, where to write the output, and how to load it with common bundlers and SSR setups. For the theme this site uses (the one @flexpixels/ui ships), run generateThemeCSS on the theme-config.json at the root of the FlexPixels repository.
Prerequisites
bun add @flexpixels/cortex@beta. (@flexpixels/ui, the component library with a pre-builttheme.css, is not part of the beta and is not on npm yet.)- For your own theme, a
theme-config.jsonin your project — see Custom Themes and the theme-config.json reference.
Generate the stylesheet
A build script
You write a small build script that calls generateThemeCSS and writes the output where your bundler expects it:
// scripts/build-theme.ts
import { writeFileSync, mkdirSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { generateThemeCSS, loadConfigFromPath } from "@flexpixels/cortex";
const OUTPUT = "src/styles/cortex.css";
const config = loadConfigFromPath(resolve("theme-config.json")); // throws if missing or invalid
mkdirSync(dirname(OUTPUT), { recursive: true });
writeFileSync(OUTPUT, generateThemeCSS(config));Wire it into package.json:
{
"scripts": {
"theme:build": "bun scripts/build-theme.ts",
"prebuild": "bun run theme:build",
},
}The output is a single CSS file. Besides the --fp-* custom properties on :root (colour scales, spacing, typography, radius, border, elevation, interaction, layout, size axis, scrollbars), it holds the [data-size] / size="" rules of the size axis, a coarse-pointer touch-target rule, P3 wide-gamut overrides, the registered motion switches (@property), ultrawide viewport scaling, forced-colours mappings, and [data-theme="light|dark"] rules when settings.schemes is "both" (the default). Set it to "light" or "dark" to ship one scheme: for the theme @flexpixels/ui ships, 16% fewer bytes for the default stylesheet and 26% for the full one.
The stylesheet is written for current browsers: it needs light-dark(), relative colour syntax and round() (roughly Chrome 125, Safari 18 and Firefox 128 on). Set output.legacyFallback: true to add static rgb() fallbacks for older engines; for the theme @flexpixels/ui ships that makes the default stylesheet 39% larger and the full one 72% larger, and with it a one-scheme build saves 27% and 40%. The figures are measured from that theme when the docs are generated; another theme’s differ by a few points.
The stylesheet @flexpixels/ui ships
@flexpixels/ui, the component library, is not published yet. In the repository, its theme.css is an entry stylesheet that declares the cascade layers in order and puts the cortex output in the first one. An excerpt:
@layer fp.tokens, fp.structure, fp.presets;
@import url("./surface.css") layer(fp.tokens);
@import url("./generated/theme.css") layer(fp.tokens); /* cortex tokens */
@import url("./modal.css") layer(fp.structure); /* native popover infra */
/* …shape, border, density, layout primitives… */generated/theme.css is generateThemeCSS run on the repository’s theme-config.json. The fp.presets layer is declared but ships empty: it is where your own overrides (::part() selectors, token re-declarations) go, and there they beat fp.tokens and fp.structure without specificity fights. Any CSS outside @layer wins over all three.
To use your own theme with the components, load your generated file after that entry stylesheet, unlayered or in @layer fp.presets, so your tokens override the defaults. See Custom Themes for the config side.
Lean or full
The engine always computes every token (1,034 for the theme @flexpixels/ui ships). The stylesheet a page loads carries the lean set by default: 599 tokens, 49 KB instead of 97 KB (7 KB instead of 11 KB gzipped). It keeps:
- every token a FlexPixels component reads
- the public tier (the tokens DESIGN.md, llms.txt and the MCP token tools offer)
- the families promised to authors, whole: the 12-step color ramps with their alphas and seeds,
-contraston the fill steps 9–11 (step 8 is in too, because components read it), the static alpha ramps, the type roles and named line heights, the size axis, the stroke / container / touch-target scales, and the global knobs - everything those read
On these pages, theme.css means the lean stylesheet and theme-full.css the full one, the names of the two files @flexpixels/ui ships. With cortex you get them from generateThemeCSS(config) and generateFullThemeCSS(config).
It leaves out what no component reads: the APCA contrast matrix (--fp-{role}-{n}-on-text, -on-text-soft, -on-icon), -contrast on steps 1–7 and 12, the chart palette (@flexpixels/flexcharts, not published yet, ships its own tokens.css), and unreferenced primitives. All of it is still computed, and one setting adds it back.
Charts with your own theme
The chart palette (--fp-chart-*) is in the full stylesheet: generateFullThemeCSS(config), or output.tokens: "full". It comes from your seeds, so the series colours match your brand. A lean theme has no --fp-chart-cat-* at all, and a chart that reads them draws nothing. (@flexpixels/flexcharts, which can write the chart tokens alone, is not published yet.)
| You want | With cortex |
|---|---|
| The default | generateThemeCSS(config) |
| Every token | "output": { "tokens": "full" } in theme-config.json, or generateFullThemeCSS(config) |
| Lean, plus the APCA matrix | "output": { "contrastMatrix": true } |
// theme-config.json
{
"output": {
"tokens": "lean", // "lean" (default) | "full"
"contrastMatrix": false, // true: add -on-text / -on-text-soft / -on-icon and every -contrast
},
}generateThemeCSS(config) honours output; generateFullThemeCSS(config) always writes every token.
Importing the result
Astro
---
import "../styles/cortex.css";
---Or via a <link> in your layout’s <head>.
Vite
// main.ts
import "./styles/cortex.css";Vite handles CSS imports natively. In dev the tokens are injected as a <style> tag; vite build extracts them into a linked stylesheet.
Webpack
// entry.js
import "./styles/cortex.css";Requires css-loader and style-loader (or MiniCssExtractPlugin for production).
Next.js / Nuxt
Import in your top-level layout (app/layout.tsx, app.vue, etc.). The framework handles injection into the HTML automatically.
Plain HTML
<link rel="stylesheet" href="/styles/cortex.css" />SSR & static export
The generated CSS is a static file — it works in any SSR or static-site environment. Tokens live on :root, so they’re available to every element once the stylesheet loads. With schemes: "both" every colour that differs between the schemes is a light-dark() pair, so dark mode follows color-scheme without any JS.
The stylesheet sets color-scheme on :root (light dark for both schemes), which light-dark() needs. It also tells the browser to draw the page canvas, scrollbars and form controls for the active scheme, so on a page that had no dark mode of its own those follow the OS as soon as the theme loads; a data-theme="light" or "dark" on <html> pins one. In engines without light-dark() the static fallback (output.legacyFallback) switches on <html> only: a data-theme on an inner element makes a dark island in modern engines, and has no effect there.
For per-request theming (multi-tenant apps, brand overrides), run the engine on the server:
import { generateThemeCSS, mergeThemeConfig } from "@flexpixels/cortex";
const tenantCss = generateThemeCSS(mergeThemeConfig(tenantPartial));
// Stream into your SSR response as a `<style>` tag.Generating a theme takes real work (the contrast solving), so cache the result per tenant rather than regenerating it on every request.
Regenerating during development
Re-run your build script after any change to theme-config.json. For a watch workflow, run it from a file watcher (for example chokidar) or your bundler’s plugin API.
Runtime overrides vs the generated file
- The generated CSS is the baseline — it sets the
--fp-*tokens on:root. - Override a token itself on
:rootor any ancestor. The inputs--fp-{role}-h,-cand-seedre-tint the ramp only when set on:root, e.g.document.documentElement.style.setProperty("--fp-primary-h", "280"): ramp steps are declared on:rootasoklch(L calc(var(--fp-{role}-c) * var(--fp-C-n)) var(--fp-{role}-h))and resolve there. Step 9 of a brand role (and its hover and alpha stops) follows--fp-{role}-seedinstead; withseeds.surface, neutral’s dark steps 1–5 are fixed anchor colours that ignore-hand-c; and the measured-contrast,-textand-bordertokens are not recomputed. - For a full theme swap, call
generateThemeCSS(newConfig)and put the result in a<style id="fp-theme">tag.
Verify
Open DevTools and inspect :root: you should see the --fp-* custom properties. From the console:
getComputedStyle(document.documentElement).getPropertyValue("--fp-primary-seed");
// your primary seed, e.g. "#4f46e5"If it returns a value, the theme is loaded.
Troubleshooting
--fp-* properties missing in DevTools. The CSS file isn’t loaded. Verify the bundler resolved the import; check the Network tab.
The theme looks like the default one. loadUserConfig resolves its path against process.cwd() and returns the default config when the file is missing (with a CONFIG_NOT_FOUND warning, unless the path is the default theme-config.json); when the file is invalid it logs a CONFIG_LOAD_FAILED warning and also falls back. Use loadConfigFromPath with an absolute path in build scripts so a missing or broken config fails the build.
Build throws a validation error. Zod found an invalid field. The message names the file and the field path — fix the config and re-run. See validators for the difference between validateThemeConfig, mergeThemeConfig and mergeWithDefaults.
Next Steps
- Custom Themes — Full config schema and all available options.
- Token API Reference — The token families the engine generates.
- API overview — Every public export grouped by purpose.