Use Cortex in Angular
Generate the theme stylesheet before each Angular build, load it as a global style, use the tokens in components, and switch light and dark.
Cortex runs at build time, so an Angular app needs no runtime package: a small Node script writes the theme stylesheet, and the Angular CLI bundles it like any other global style. Every step below was checked in a new Angular 21 project (ng new, which uses the application builder).
1. Install
npm install --save-dev @flexpixels/cortex@betaIt is a dev dependency: the browser receives the generated CSS, not the engine.
2. Write the theme config
theme-config.json at the project root. Every field has a default; the three brand seeds are where you start, and the editor schema expects them. The reference lists every field.
{
"$schema": "./node_modules/@flexpixels/cortex/theme-config.schema.json",
"name": "MyApp",
"seeds": {
"primary": "#1D4ED8",
"secondary": "#7C3AED",
"neutral": "#334155"
}
}The $schema line gives completion and validation in VS Code and other editors.
3. Generate the stylesheet
scripts/build-theme.mjs:
import { mkdirSync, writeFileSync } from "node:fs";
import { generateThemeCSS, loadConfigFromPath } from "@flexpixels/cortex";
const css = generateThemeCSS(loadConfigFromPath("theme-config.json"));
mkdirSync("src/styles", { recursive: true });
writeFileSync("src/styles/theme.css", css);
console.log(`theme.css: ${(css.length / 1024).toFixed(1)} KB`);loadConfigFromPath throws on a missing file or an invalid config, so a typo in a seed stops the build instead of shipping a fallback theme. Run the script before the CLI in package.json:
{
"scripts": {
"theme": "node scripts/build-theme.mjs",
"start": "node scripts/build-theme.mjs && ng serve",
"build": "node scripts/build-theme.mjs && ng build"
}
}The generated file is build output: add src/styles/theme.css to .gitignore. After editing theme-config.json while ng serve runs, run npm run theme and the dev server reloads the new stylesheet.
4. Load it as a global style
In angular.json, list the theme before your own styles, so your rules can use its tokens:
"styles": ["src/styles/theme.css", "src/styles.css"]The array sits under projects.<name>.architect.build.options.
5. Use the tokens
Custom properties inherit through Angular’s emulated view encapsulation, so component styles read them directly:
/* card.component.css */
:host {
display: block;
padding: var(--fp-s-4);
border: 1px solid var(--fp-border-subtle-color);
border-radius: var(--fp-radius-2);
background: var(--fp-surface-element);
color: var(--fp-text-default);
box-shadow: var(--fp-elevation-1);
}
button {
padding: var(--fp-s-2) var(--fp-s-4);
border: 0;
border-radius: var(--fp-radius-2);
background: var(--fp-primary-9);
color: var(--fp-primary-9-contrast);
}
button:hover {
background: var(--fp-primary-10);
}
button:focus-visible {
outline: 2px solid var(--fp-focus-ring-color);
outline-offset: 2px;
}--fp-primary-9-contrast is the text colour measured for that background, so the label stays readable whatever the seed. Token usage explains which step to use where.
6. Light and dark
With no other setting the page follows the operating system: every colour is a light-dark() pair. To let people choose, set data-theme="light" or data-theme="dark" on <html>, and remove it to follow the system again. A small service:
// theme.service.ts
import { DOCUMENT, Injectable, effect, inject, signal } from "@angular/core";
export type Scheme = "light" | "dark" | "system";
@Injectable({ providedIn: "root" })
export class ThemeService {
private readonly root = inject(DOCUMENT).documentElement;
readonly scheme = signal<Scheme>((localStorage.getItem("scheme") as Scheme | null) ?? "system");
constructor() {
effect(() => {
const scheme = this.scheme();
if (scheme === "system") {
this.root.removeAttribute("data-theme");
localStorage.removeItem("scheme");
} else {
this.root.setAttribute("data-theme", scheme);
localStorage.setItem("scheme", scheme);
}
});
}
}A component calls theme.scheme.set("dark"). The service runs once Angular has started, so a page reloaded in dark mode would show its light colours for a moment first. To avoid that, apply the saved choice in src/index.html before the app loads:
<script>
const scheme = localStorage.getItem("scheme");
if (scheme) document.documentElement.dataset.theme = scheme;
</script>The service reads localStorage, which only exists in the browser. With server-side rendering, read it only when isPlatformBrowser(inject(PLATFORM_ID)) is true.
Next
- Token usage: which token to reach for.
- Build & Import: the lean and full stylesheets, and other build tools.
- Custom Themes: shape, type, spacing and elevation from the same config.