Hello world
Generate your first cortex theme from a minimal config.
A minimal theme-config.json:
{
"name": "MyApp",
"seeds": {
"primary": "#3b82f6",
"secondary": "#22d3ee",
"neutral": "#475569"
}
}Every field has a default, so mergeThemeConfig({}) and a file holding {} give a theme; in practice you set at least the three brand seeds, and the JSON Schema an editor checks the file against asks for them. success, danger, warning and info are generated at fixed hues (145°, 25°, 85°, 250°) when you leave them out. name appears in the stylesheet’s header comment and as the title of the DESIGN.md export.
Generate the CSS — Node or Bun
loadUserConfig reads the file, merges it over the defaults and validates it. generateThemeCSS turns the result into a stylesheet:
// build-theme.ts
import { mkdirSync, writeFileSync } from "node:fs";
import { generateThemeCSS, loadUserConfig } from "@flexpixels/cortex";
const config = loadUserConfig("theme-config.json");
mkdirSync("public", { recursive: true });
writeFileSync("public/theme.css", generateThemeCSS(config));Run it with bun build-theme.ts, or save it as build-theme.mjs and run node build-theme.mjs. Re-run it whenever theme-config.json changes.
generateThemeCSS writes the lean stylesheet (the tokens in use). For every token the engine computes, including the APCA contrast matrix, call generateFullThemeCSS(config) instead, or set "output": { "tokens": "full" } in the config.
If the file is missing, loadUserConfig returns the default theme without a message. If it exists but is invalid, it prints a CONFIG_LOAD_FAILED warning naming the problem and also returns the defaults. Use loadConfigFromPath(absolutePath) when you would rather it throw.
If you already have a partial config in memory (a request body, editor state), skip the file and use mergeThemeConfig, which runs the same merge and validation:
import { generateThemeCSS, mergeThemeConfig } from "@flexpixels/cortex";
const config = mergeThemeConfig({
seeds: { primary: "#3b82f6", secondary: "#22d3ee", neutral: "#475569" },
});
const css = generateThemeCSS(config);Generate the CSS — browser
The same functions work in the browser, through a bundler. Put an empty <style> element in the page’s <head>, after the stylesheet you link:
<style id="fp-theme"></style>Then write the result into it to switch themes live:
import { generateThemeCSS, mergeThemeConfig } from "@flexpixels/cortex/browser";
const css = generateThemeCSS(mergeThemeConfig(userPartial));
document.getElementById("fp-theme")!.textContent = css;The engine adds about 290 KB of minified JavaScript to the page, so a theme that does not change at runtime is better generated at build time (see Install). With a Content Security Policy, writing CSS into a <style> element needs style-src 'unsafe-inline' or a nonce on that element; the engine needs no unsafe-eval.
mergeThemeConfig throws a ZodError if userPartial is invalid; see Validators.
Use it
Load the stylesheet, then read the tokens anywhere CSS variables work:
@import "./theme.css";
.button {
background: var(--fp-primary-9);
color: var(--fp-primary-9-contrast);
padding: var(--fp-s-3) var(--fp-s-5);
border-radius: var(--fp-corner-control);
}
.button:hover {
background: var(--fp-primary-10);
color: var(--fp-primary-10-contrast);
}--fp-primary-9 is your primary seed, exactly. --fp-primary-9-contrast is black or white, whichever the engine measured to be readable on it. --fp-primary-10 is the hover shade. Light and dark mode follow the operating system through light-dark(), or a data-theme="light" / "dark" attribute on an ancestor.
Next
- Engine only — a fuller walkthrough for projects without
@flexpixels/ui. - theme-config.json reference — every field and its default.
- API overview — the full public surface, grouped by purpose.