Custom Themes
How to build your own design language with theme-config.json — the cortex engine turns one config file into a complete token system.
FlexPixels ships one default look; cortex is the engine you use to replace it with your own. The main parts of a design language are set in config: colour seeds, spacing rhythm, radius and border character, elevation style (flat, neumorphic, brutalism or liquid-glass via elevation.style), glow and gradient borders, typography scenarios and motion timings. You change them in one file instead of overriding component CSS.
Quick start
- Create a
theme-config.jsonin your project. - Generate the CSS from it with a small build script that calls
generateThemeCSS— see Build & Import. - Load the generated CSS on your pages.
The whole theme is one CSS file of custom properties.
Configuration File
The theme config is validated against a strict Zod schema: unknown keys are errors. The schema requires seeds (primary, secondary, neutral): validateThemeConfig throws without it. loadConfigFromPath and mergeThemeConfig fill anything missing from the defaults, seeds included, so a file without seeds gets the default colours without an error. The theme-config.json reference lists every key, including the sections this page only touches on (sizes, layout, inset, scroll, dataviz, output).
Minimal Config
{
"seeds": {
"primary": "#4f46e5",
"secondary": "#c026d3",
"neutral": "#94a3b8"
}
}Three colours is all you need. The engine generates success, danger, warning and info at fixed hue anchors (145° / 25° / 85° / 250°), using the primary’s chroma, raised to at least 0.12 and capped to what sRGB holds at each anchor hue (warning ends up near 0.10).
Full Config
{
"name": "MyBrandSystem",
"settings": {
"prefix": "fp",
"accessibility": "AA",
"schemes": "both"
},
"seeds": {
"primary": "#4f46e5",
"secondary": "#c026d3",
"neutral": "#94a3b8",
"success": "#22c55e",
"danger": "#ef4444",
"warning": "#eab308",
"info": "#3b82f6"
},
"spacing": {
"mode": "hybrid",
"base": 4
},
"border": {
"base": 1,
"scale": [1, 2, 3, 4],
"gradient": {
"preset": "none",
"angle": 135
}
},
"radius": {
"base": 4,
"scale": [2, 4, 6, 8, 12, 16]
},
"typography": {
"base": 16,
"scale": [12, 14, 16, 18, 20, 24, 32, 40],
"lineHeightFormula": "ratio-1.5",
"fluid": { "minViewport": 320, "maxViewport": 1280 },
"fontFamilies": {
"display": "InterVariable, Inter, system-ui, sans-serif",
"body": "InterVariable, Inter, system-ui, sans-serif",
"mono": "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace"
}
},
"elevation": {
"style": "auto",
"shadowIntensity": 0.12,
"shadowIntensityDark": 0.4,
"tintChroma": 0.01
},
"interaction": {
"opacityDisabled": 0.5,
"durations": { "xfast": 75, "fast": 100, "normal": 150, "slow": 250, "xslow": 400 },
"focusRing": { "width": 2, "offset": 2, "color": "primary" },
"scaleActive": 0.98
}
}Config Reference
settings
| Property | Type | Default | Description |
|---|---|---|---|
prefix | string | "fp" | CSS custom property prefix. All tokens will use --{prefix}-* |
accessibility | "AA" | "AAA" | "AA" | AAA raises the floors the engine solves for: -text reaches 7:1, -border and the icon tier 4.5:1; the matrix text tiers and -contrast reach 7:1 wherever black or white can (on mid-tone steps they keep the better of the two, at least 4.5:1). It also sets the level ThemeEngine.getContrast and the warnings use, and the legacy --fp-contrast-threshold token (0.45 for AAA, 0.6 for AA). |
contrastAlgorithm | "apca" | "wcag2" | "apca" | How -contrast picks black or white: apca prefers white while it clears 4.5:1, wcag2 takes whichever contrasts more. |
schemes | "both" | "light" | "dark" | "both" | Which colour schemes the CSS carries. both ships every colour as a light-dark() pair that follows the OS preference or [data-theme]. light or dark ships that scheme alone, with nothing of the other, which makes the default stylesheet 16% smaller for the theme @flexpixels/ui ships (the full one 26%; 27% and 40% with output.legacyFallback). A one-scheme build gives fp-theme-switch and dark islands nothing to switch to. |
seeds
Each seed is a hex colour (#RRGGBB or #RGB; an opaque #RRGGBBFF is read too) that generates a 12-step OKLCH scale. For primary, secondary and any semantic seed you set, step 9 of the scale is the seed colour itself.
| Property | Required | Hue Anchor | Description |
|---|---|---|---|
primary | Yes | — | Main brand colour. Step 9 (buttons, CTAs) is this colour; the other steps take its hue and chroma. |
secondary | Yes | — | Supporting brand color. Used for secondary actions, accents, and visual variety. |
neutral | Yes | — | Structural color — grays, borders, backgrounds, body text. The skeleton of your UI. |
success | No | 145° | Positive feedback (confirmations, completed states). Auto-generated if null. |
danger | No | 25° | Destructive actions, errors. Auto-generated if null. |
warning | No | 85° | Caution, potential issues. Auto-generated if null. |
info | No | 250° | Informational, neutral context. Auto-generated if null. |
An optional surface seed (hex) anchors dark-mode neutral steps 1–5 to a deliberately tinted dark ground — step 1 becomes exactly that colour, blending back to the standard dark ramp by step 6, with light mode untouched.
The engine converts your hex seeds to OKLCH and builds the scale on OKLCH lightness ramps, which are more perceptually even than HSL.
interaction
| Property | Type | Default | Description |
|---|---|---|---|
opacityDisabled | number | 0.5 | Disabled-state opacity (0–1) |
durations.xfast | number | 75 | Extra-fast transition in ms |
durations.fast | number | 100 | Fast transition in ms |
durations.normal | number | 150 | Normal transition in ms |
durations.slow | number | 250 | Slow transition in ms |
durations.xslow | number | 400 | Extra-slow transition in ms |
focusRing.width | number | 2 | Focus ring outline width in px |
focusRing.offset | number | 2 | Focus ring outline offset in px |
focusRing.color | color role | "primary" | Colour role for --fp-focus-ring-color: any of the seven roles. The ring uses that role’s -text colour. |
scaleActive | number | 0.98 | Scale on :active press (0–1) |
Reduced motion is not a duration: it is handled by the motion switches (see Motion). An old durations.reduced key is accepted and ignored. Easings, state layers, spring and stagger settings are on the Interaction page.
elevation
Controls the shadow model and the four-level shadow ladder. The most used fields:
| Property | Type | Default | Description |
|---|---|---|---|
style | string | "auto" | auto (acts as flat), flat, neumorphic, liquid-glass, brutalism |
shadowIntensity | number | 0.12 | Light-mode shadow opacity (0–1). Higher = darker shadows. |
shadowIntensityDark | number | 0.4 | Dark-mode shadow opacity (0–1). |
tintChroma | number | 0.01 | Chroma of the shadow tint (0–0.05). 0 = pure grey, 0.05 = vivid. |
See Elevation for every style and option.
spacing
| Property | Type | Default | Description |
|---|---|---|---|
mode | "linear" | "geometric" | "hybrid" | "hybrid" | Scale generation mode |
base | number | 4 | Base unit in logical pixels. Outputs as precomputed rem |
scale | number[] | — | Custom primitive (--fp-dim-*) scale; replaces the mode’s scale |
Spacing modes:
"linear"— Even increments (--fp-s-1…12at base 4: 2, 4, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48px)"geometric"— Faster growth (--fp-s-1…12at base 4: 1, 2, 4, 8, 12, 16, 24, 32, 48, 64, 96, 128px)"hybrid"— Linear for the small steps (tight UI spacing), geometric for the large steps (generous layout spacing). Default and recommended.
The mode also picks the default --fp-dim-* primitive list, unless you give scale.
All spacing primitives are output as precomputed rem values (px / 16). For example, --fp-dim-4 resolves to 0.25rem (4px at a 16px root font size).
radius
| Property | Type | Default | Description |
|---|---|---|---|
base | number | 4 | Base radius in logical pixels. Maps to --fp-radius-base |
scale | number[] | [2,4,6,8,12,16] | Radius primitives (--fp-radius-1 … in logical px) |
cornerShape | string | "round" | Corner character: round, squircle, bevel, scoop, notch, square (see Shape) |
The engine also generates --fp-radius-full: 9999px and a semantic --fp-corner-circle alias for full-circle elements like avatars. The scale is written smallest first. By default --fp-corner-control is the base radius, --fp-corner-surface the step nearest base × 2 and --fp-corner-overlay the step nearest base × 3. radius.corners states any of them instead, in px (control also takes "full"): { "control": "full", "surface": 24 } gives pill controls with 24px cards.
border
| Property | Type | Default | Description |
|---|---|---|---|
base | number | 1 | Base border width in logical pixels. Maps to --fp-border-base |
scale | number[] | [1,2,3,4] | Border width primitives (steps 1-4 in logical px) |
gradient.preset | "none" | "primary-secondary" | "primary-neutral" | "secondary-neutral" | "none" | Gradient preset derived from seed colors |
gradient.angle | number | 135 | Gradient angle in degrees (0-360) |
gradient.custom | string? | — | Custom CSS gradient value. Overrides preset. |
The engine generates a Three-Layer border API:
- Primitives:
--fp-border-1through--fp-border-4(from scale),--fp-border-none(0),--fp-border-base(alias to scale entry matching base) - Named widths:
--fp-border-w-{hairline,thin,medium,thick}(fromwidthScale) - Semantics:
--fp-stroke-control(buttons/inputs),--fp-stroke-divider(separators),--fp-stroke-strong(focus rings),--fp-stroke-accent(decorative),--fp-stroke-hairline - Gradients:
--fp-border-gradient-brandand--fp-border-gradient-subtlealways;--fp-border-gradient-activeonly whengradient.presetis not"none"orgradient.customis set
Gradient presets use seed-derived CSS variable references so they adapt when seeds change:
{
"border": {
"base": 2,
"gradient": {
"preset": "primary-secondary",
"angle": 135
}
}
}Components consume border width via --fp-stroke-control. To give a FlexPixels component a gradient border, add the border-gradient attribute; set --fp-border-gradient on it (for example to var(--fp-border-gradient-active)) to choose the gradient.
typography
| Property | Type | Default | Description |
|---|---|---|---|
base | number | 16 | Base font size. Must be 16 — validation rejects anything else. |
strictGrid | boolean | true | Even-integer scale + 2px-snapped line heights. Set false to allow odd sizes (13, 15…). |
scale | number[] | [12, 14, 16, 18, 20, 24, 32, 40] | Font size scale (px). Even integers when strictGrid is true. |
lineHeights | number[] | auto-computed | Optional sparse overrides aligned 1:1 with scale; missing entries are filled in by the formula. |
lineHeightFormula | "ratio-1.5" | "golden" | "compact" | "ratio-1.5" | Algorithm for auto-computed line heights. See table below. |
fluid | false | { minViewport, maxViewport, roles? } | { minViewport: 320, maxViewport: 1280 } | Utopia-style clamp() for upper-half-of-scale roles. Pass false to disable globally. |
fontFamilies | { display, body, mono } | Inter / Inter / "Geist Mono", ui-monospace, … | Three family slots. Type styles pick one per role. |
fontWeights | { regular?, medium?, semibold?, bold? } (each 1–1000) | { regular: 400, medium: 500, semibold: 600, bold: 700 } | Named weight primitives. Exactly these four names; any other key is an error. |
letterSpacings | Record<string, string> | { tighter: -0.03em, tight: -0.02em, snug: -0.01em, normal: 0em, wide: 0.04em } | Named letter-spacing primitives. Type-style letterSpacing can reference these by key. |
metrics | { display?, body?, mono? } | Inter / Inter / Geist Mono | Per-family font metrics, in the font’s own units. Enables --fp-text-{role}-trim-top/-bottom leading-trim tokens. A slot you set needs all of familyName, unitsPerEm (above 0), ascent, descent, lineGap, capHeight (not above ascent) and xHeight. |
comfort | { lineLength?, minSize?, maxLineHeightRatio? } | { 65ch, 1rem, 1.6 } | Reading-comfort caps emitted as --fp-text-comfort-*; the fp-prose element applies them. |
variableAxes | { wght?, opsz?, slnt? } | none | Variable-font axis triples [min, max, default]. Emits --fp-font-axis-* when configured. |
scenario | "marketing" | "app" | "editorial" | "dashboard" | none | Curated bundle of scale + lineHeightFormula + fluid range. Your explicit fields still override. |
typeStyles | Record<string, TypeStyleDef> | 14 built-in roles | Per-role overrides + custom roles. See the MCP resource flexpixels://typography/roles/{role} for the built-in role intent metadata. |
Line-height formulas
The engine auto-computes line heights using one of three formulas when lineHeights is omitted:
| Formula | Multiplier | Best for |
|---|---|---|
ratio-1.5 | 1.5× | UI (default). Snapped to 2px grid. |
golden | 1.618× | Editorial / long-form reading. |
compact | 1.3× | Data-dense dashboards and tables. |
For the default ratio-1.5 and the 8-step default scale:
| Font Size | Line Height | Ratio |
|---|---|---|
| 12px | 18px | 150% |
| 14px | 22px | 157% |
| 16px | 24px | 150% |
| 18px | 28px | 156% |
| 20px | 30px | 150% |
| 24px | 36px | 150% |
| 32px | 48px | 150% |
| 40px | 60px | 150% |
Scenario shortcut
typography.scenario swaps multiple defaults in one line — handy when you know the product type but don’t want to hand-tune scale and leading. See Typography System for the full table.
You can always override with explicit values if your design requires different proportions.
Building your theme
Programmatic (Node or Bun)
import { mkdirSync, writeFileSync } from "node:fs";
import { generateThemeCSS, loadConfigFromPath } from "@flexpixels/cortex";
const config = loadConfigFromPath(`${process.cwd()}/theme-config.json`);
mkdirSync("src/styles", { recursive: true });
writeFileSync("src/styles/theme.css", generateThemeCSS(config));generateThemeCSS writes the stylesheet output asks for (the lean set by default); generateFullThemeCSS always writes every token. loadConfigFromPath throws on a missing or invalid file; loadUserConfig instead falls back to the default theme. Build & Import covers wiring this into a build and loading the result.
In the browser
The engine also runs in the browser, so you can generate a theme on the fly and swap it in:
import { generateThemeCSS, mergeThemeConfig } from "@flexpixels/cortex";
const css = generateThemeCSS(mergeThemeConfig(myPartialConfig));
const style = document.getElementById("fp-theme") ?? document.createElement("style");
style.id = "fp-theme";
style.textContent = css;
document.head.appendChild(style);Example: dark-only brand
{
"name": "DarkHighContrast",
"settings": {
"schemes": "dark",
"accessibility": "AAA"
},
"seeds": {
"primary": "#818cf8",
"secondary": "#f472b6",
"neutral": "#475569"
},
"spacing": {
"base": 8
},
"radius": {
"base": 8,
"scale": [4, 8, 12, 16, 24, 32]
},
"elevation": {
"shadowIntensity": 0.18,
"tintChroma": 0.02
}
}This produces a dark-only theme with 8px base spacing and larger border radii. accessibility: "AAA" solves -text to 7:1 and -border to 4.5:1, so brand text colours move further from the seed; it also changes the level ThemeEngine.getContrast checks against and the warnings you get. In dark mode the shadow opacity comes from shadowIntensityDark (default 0.4), so shadowIntensity here only matters if you later add the light scheme.
Validation
The config is validated with Zod when it is loaded or merged. Common errors:
| Error message (start) | Fix |
|---|---|
… is not a hex colour / A seed must be opaque | Use #RRGGBB or #RGB for seeds, without transparency |
Font size 13px must be an even integer when strictGrid is true | Use 12 or 14, or set typography.strictGrid: false |
Line height 19px must be an even integer … | Same — use the 2px grid |
typography.base must be 16 | Set typography.base to 16; vary sizes with scale |
settings.mode was replaced by settings.schemes | Rename mode ("system" → "both") |
You can validate programmatically:
import { validateThemeConfig } from "@flexpixels/cortex";
try {
const config = validateThemeConfig(rawInput);
} catch (error) {
// Zod error with specific field paths
}