Getting Started: Engine Only
Use FlexPixels tokens in your own components — React, Vue, Svelte, or vanilla CSS.
What This Path Is For
This guide is for developers who want FlexPixels design tokens without the pre-built Lit component library. You’ll install the cortex token engine, generate CSS custom properties, and use them in your own React, Vue, Svelte, or vanilla CSS components.
By the end, you’ll have a working themed button styled entirely with --fp-* tokens.
When to Choose This Path
Engine-only is the right choice when:
- You already have a component library (React, Vue, Svelte, etc.)
- You want token-first styling without adopting web components
- You want to generate the theme at build time and ship plain CSS, with no theme library on the page
- You want to control your own component APIs and accessibility
The FlexPixels web components (@flexpixels/ui) read the same --fp-* tokens, but that package is not published yet.
Prerequisites
- Node.js 20+ or Bun installed. The
theme:buildscript below runs with Bun; with Node, save the script asscripts/build-theme.mjsand runnode scripts/build-theme.mjs. - An existing project to add tokens to (or a fresh one —
mkdir my-app && cd my-app && npm init -y)
Setup
Step 1: Install the Token Engine
npm install @flexpixels/cortex@betaOr with your preferred package manager:
pnpm add @flexpixels/cortex@beta
yarn add @flexpixels/cortex@beta
bun add @flexpixels/cortex@betaStep 2: Create Your Theme Config
Create a theme-config.json in your project root. Every field has a default; the three brand seeds are where you start. The seeds below are not the defaults, so you can tell your config was read:
{
"seeds": {
"primary": "#0d9488",
"secondary": "#ea580c",
"neutral": "#78716c"
}
}The engine generates success, danger, warning, and info at fixed hues (145°, 25°, 85°, 250°) when you leave them out. Set any of them to use your own colour; a seed you set becomes step 9 of its role exactly:
{
"seeds": {
"primary": "#0d9488",
"secondary": "#ea580c",
"neutral": "#78716c",
"success": "#22c55e",
"danger": "#ef4444",
"warning": "#eab308",
"info": "#3b82f6"
}
}Step 3: Generate the Theme CSS
Cortex is a library, not a CLI — you own the build step. Write a tiny Node/Bun script that reads theme-config.json, generates the CSS, and writes it wherever your bundler expects:
// scripts/build-theme.ts
import { writeFileSync, mkdirSync } from "node:fs";
import { dirname } from "node:path";
import { generateThemeCSS, loadUserConfig } from "@flexpixels/cortex";
const OUTPUT = "src/styles/theme.css";
const css = generateThemeCSS(loadUserConfig("theme-config.json"));
mkdirSync(dirname(OUTPUT), { recursive: true });
writeFileSync(OUTPUT, css);
console.log(`Wrote ${OUTPUT}`);Wire it into package.json:
{
"scripts": {
"theme:build": "bun scripts/build-theme.ts"
}
}Run it once now (and again whenever theme-config.json changes):
bun run theme:buildYou’ll get a single CSS file at src/styles/theme.css: the lean token set (563 --fp-* custom properties for this config; 599 for the theme @flexpixels/ui ships), with light and dark values in light-dark() pairs. For every token the engine computes, use generateFullThemeCSS instead — see Build & Import.
loadUserConfig validates the file. If it is invalid, the script prints a CONFIG_LOAD_FAILED warning and writes the default theme; use loadConfigFromPath if you want the build to fail instead.
Step 4: Import the CSS
Import it in your bundler entry point:
import "./styles/theme.css";Without a bundler, set OUTPUT to a file in the folder your server publishes (for example public/theme.css) and link it:
<link rel="stylesheet" href="/theme.css" />Step 5: Use Tokens in Your CSS
Create a button styled with --fp-* tokens:
.btn-primary {
display: inline-flex;
align-items: center;
gap: var(--fp-s-2);
padding: var(--fp-s-2) var(--fp-s-6);
border-radius: var(--fp-corner-control);
border: var(--fp-stroke-control) solid transparent;
background: var(--fp-primary-9);
color: var(--fp-primary-9-contrast);
font-family: var(--fp-font-family-body);
font-size: var(--fp-text-label-font-size);
line-height: var(--fp-text-label-line-height);
cursor: pointer;
transition: background var(--fp-transition-duration) var(--fp-transition-easing);
}
.btn-primary:hover {
background: var(--fp-primary-10);
color: var(--fp-primary-10-contrast);
}
.btn-primary:active {
background: var(--fp-primary-11);
color: var(--fp-primary-11-contrast);
}
.btn-primary:focus-visible {
outline: var(--fp-focus-ring-width) solid var(--fp-focus-ring-color);
outline-offset: var(--fp-focus-ring-offset);
}<button class="btn-primary">Click me</button>Verify It Works
You should see a teal button with a black label: --fp-primary-9 is the seed, exactly, and --fp-primary-9-contrast is black because black reads better than white on #0d9488. To check that your config was read, run getComputedStyle(document.documentElement).getPropertyValue("--fp-primary-seed") in the browser console: it gives #0d9488. If it gives #4f46e5 (an indigo button with a white label), the stylesheet holds the default theme: the config was not found or was invalid, and the build printed a CONFIG_LOAD_FAILED warning. Hovering moves the button to step 10, a visibly different shade of the seed: for a mid-tone seed like this one it is darker in light mode and lighter in dark mode; a seed near black or white moves the way that leaves room for a visible change. The button should have rounded corners from --fp-corner-control and consistent spacing from --fp-s-* tokens. If you change the primary seed in theme-config.json and rebuild, the button color should update accordingly.
Token Naming Convention
All tokens follow --fp-{category}-{name}:
| Pattern | Example | Description |
|---|---|---|
--fp-{role}-{step} | --fp-primary-9 | Color step (1–12) |
--fp-{role}-{step}-contrast | --fp-primary-9-contrast | Black or white text on that step, measured to WCAG AA at build time (steps 8–11 in the lean stylesheet) |
--fp-{role}-{step}-on-text | --fp-primary-9-on-text | Same-hue text on that step (APCA Lc 90 target); full stylesheet, or lean with output.contrastMatrix |
--fp-{role}-text | --fp-primary-text | The role’s colour for text and icons on the page (4.5:1, 7:1 under AAA) |
--fp-{role}-a{n} | --fp-primary-a4 | Alpha stop of step 9 (a1–a5) |
--fp-{role}-seed | --fp-primary-seed | The seed hex, for primary, secondary and any status role you set (not neutral); step 9 reads it |
--fp-{role}-h / -c | --fp-primary-h / --fp-primary-c | Hue and chroma the other steps are computed from |
--fp-s-{n} | --fp-s-4 | Semantic spacing (s-1..s-12) |
--fp-corner-{name} | --fp-corner-control | Semantic radius |
--fp-stroke-{name} | --fp-stroke-control | Semantic border |
--fp-elevation-{name} | --fp-elevation-lifted | Shadow level (rest, lifted, overlay, modal, or 1–4) |
--fp-text-{role}-{prop} | --fp-text-body-font-size | Type-style role properties (font-size, line-height, font-weight, …) |
See the Token API Reference for the complete list.
Use the tokens from Tailwind CSS v4
Tailwind v4 builds its utilities from theme variables declared in CSS. Point OUTPUT in the build script at the folder of your Tailwind entry stylesheet, import the generated theme.css after Tailwind, and map the tokens you use in an @theme inline block:
/* app.css */
@import "tailwindcss";
@import "./theme.css";
@theme inline {
--color-brand: var(--fp-primary-9);
--color-on-brand: var(--fp-primary-9-contrast);
}<button class="bg-brand text-on-brand">Save</button>inline makes each utility read the token itself (.bg-brand { background-color: var(--fp-primary-9) }), so the utilities follow the theme’s light and dark values and a regenerated theme.css. Map the other tokens the same way: --spacing-*, --radius-* and --shadow-* for spacing, corners and elevation.
Runtime Theme Switching
Since the theme is pure CSS custom properties, you can switch themes at runtime. Put an empty <style> element in the page’s <head>, after the stylesheet you link, for the new theme to go into:
<style id="fp-theme"></style>import { generateThemeCSS, mergeThemeConfig } from "@flexpixels/cortex/browser";
function switchTheme(primaryColor) {
const config = mergeThemeConfig({
seeds: { primary: primaryColor, secondary: "#ea580c", neutral: "#78716c" },
});
document.querySelector("style#fp-theme").textContent = generateThemeCSS(config);
}This runs the engine in the browser, which needs a bundler and adds about 290 KB of minified JavaScript to the page (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.
This regenerates everything, including the measured black/white -contrast foregrounds, so it is the safe way to change a brand colour.
For a quick tint without regenerating, override the variables the ramp is computed from. Steps 1–8 and 11–12 follow the hue and chroma; steps 9 and 10 follow the seed:
const root = document.documentElement.style;
root.setProperty("--fp-primary-h", "280");
root.setProperty("--fp-primary-c", "0.18");
root.setProperty("--fp-primary-seed", "#7c3aed");Values measured at build time do not follow these overrides: -contrast, --fp-primary-border and the contrast-matrix choices keep the values computed for the original seed. --fp-primary-text follows the new seed only in a scheme where it was the seed itself, and is not re-measured there. Check text contrast, or regenerate.
Programmatic Build (Alternative)
If you don’t want a wrapper script, call the engine directly from your build / SSR pipeline:
import { generateThemeCSS, loadUserConfig } from "@flexpixels/cortex";
const config = loadUserConfig("theme-config.json");
const css = generateThemeCSS(config);
// Write to file, inject into a server response, push into a CSS-in-JS layer, etc.This is the same call scripts/build-theme.ts above makes — only the surrounding plumbing differs.
Troubleshooting
Tokens aren’t applying
Check that the generated theme.css is actually loaded. Verify the import path is correct and that the stylesheet appears in your browser’s DevTools under the “Sources” or “Network” tab. If using a bundler, ensure the CSS import is in your entry file and not tree-shaken.
Colors look wrong
Seeds must be hex values with a #: 3 or 6 digits, or the opaque 4- and 8-digit forms design tools copy ("#4f46e5" or "#4f46e5ff", not "4f46e5", "#4f46e580" or "rgb(79,70,229)"). An invalid config makes loadUserConfig print a CONFIG_LOAD_FAILED warning and fall back to the default theme, so check the build output. Re-run bun run theme:build after fixing the config.
Dark mode isn’t working
With settings.schemes: "both" (the default) every colour is a light-dark() pair and the page follows the operating system. To force a scheme, set data-theme="dark" (or "light") on <html>; the stylesheet sets color-scheme for those attributes. If you set settings.schemes to "light" or "dark", the stylesheet carries only that scheme and there is nothing to switch to.
Next Steps
- Core Concepts — Understand the Three-Layer API architecture
- Custom Themes — Full schema reference for
theme-config.json - Token API Reference — Every token the engine generates
- API overview — Every exported function, schema, helper, and constant