DTCG export
Export a cortex theme as W3C DTCG 2025.10 JSON for Figma Tokens, Style Dictionary and other token pipelines, and what Style Dictionary needs on top.
import { exportDTCGTokens, type DTCGExportOptions } from "@flexpixels/cortex";
const json: Record<string, unknown> = exportDTCGTokens(config, {
includeExtensions: true, // default
});exportDTCGTokens(config, options?) walks the same ThemeEngine token tree that drives generateFullThemeCSS and returns a plain object that follows the W3C DTCG 2025.10 format. It always exports every token, whatever output says. The config is validated first, as in ThemeEngine.
Options
| Option | Default | Description |
|---|---|---|
includeExtensions | true | When true, groups and tokens carry $extensions["com.flexpixels"]: the CSS expression, the dark-mode value, intent metadata. Set false for tools that read only the standard DTCG fields. |
Values
Every value is a DTCG type, not CSS — the value a browser renders for the token:
$type | $value |
|---|---|
color | { colorSpace: "srgb", components: [r, g, b], alpha?, hex } |
dimension | { value, unit } in px or rem. A fluid size gives its upper bound. A length in em is written in px, with the em itself in its extensions (em: 0.04): under a text role (text.<role>.letter-spacing, trim-top, trim-bottom) and inside a typography composite it is the px it comes to at that role’s font size; a free-standing em token (letter-spacing.wide) is the px it comes to at 16px. A letter spacing of normal is 0px. |
duration | { value, unit: "ms" } |
shadow | { color, offsetX, offsetY, blur, spread, inset? }, or a list of them for a layered shadow |
border | { color, width, style } |
gradient | a list of { color, position } stops |
fontFamily | a list of family names |
fontWeight | a number |
typography | { fontFamily, fontSize, fontWeight, letterSpacing, lineHeight } — lineHeight a unitless ratio |
cubicBezier | [x1, y1, x2, y2] |
number | a plain number (hue, ramp lightness, multipliers, ratios) |
A reference in a $value ("{color.neutral.1}") names a token in the file, and an alias takes the $type of the token it points at (a size tier’s title line height is a number when its role’s is a ratio). References inside $extensions may point at values kept only in extensions: the elevation bundles point at the surface tints.
- One set of values. They are read from the stylesheet for current browsers, so
output.legacyFallbackdoes not change the export. The returned object is the caller’s own: nothing in it is frozen or shared between calls. - Schemes.
$valueholds the theme’s scheme — light forsettings.schemes: "both". Aboththeme’s dark value is in$extensions["com.flexpixels"].modes.dark. - The CSS.
$extensions["com.flexpixels"].csskeeps the expression the stylesheet uses (relativeoklch(),light-dark(),calc()over the density axis) when it is not the value itself. - No token is also a group. A step’s contrast tokens are its siblings, named like the CSS:
color.primary.9-contrast,color.primary.9-on-text(--fp-primary-9-contrast). - Values DTCG has no type for ride on their group’s
$extensions["com.flexpixels"]: the springlinear()easing, thecorner-shapekeyword, the tabular-figures keywords, the surface tints, the comfort line length, the scrollbar width, the focus ring’s colour as the CSS writes it (avar()string such asvar(--fp-primary-text); the resolved colour is the tokeninteraction.focus-ring-color) and the per-level elevation bundle (a shadow plus a surface tint).
Top-level groups
Each top-level key is a DTCG group with a $description; some also carry a $type. Every token states its own $type, except in semantic-color, where the group’s color applies. With the default config the export has these groups:
| Group | Contents |
|---|---|
color | Per role: steps 1–12, their -contrast and matrix siblings (9-contrast, 9-on-text, …), h, c, seed (primary, secondary and each status role you set a seed for; neutral has none), text, border, a1–a5. Also the static white / black alpha ramps. |
semantic-color | The surface, border and text roles (surface-app, text-default, …), each a reference to a neutral step. |
lightness-ramp-light, lightness-ramp-dark, chroma-curve | The per-step OKLCH lightness of each scheme, and the chroma multipliers (--fp-C-1…12). |
chart | The chart palette: categorical, sequential and diverging colours. |
dim, spacing, spacing-semantic, spacing-roles, inset | Spacing primitives, the global spacing tokens, the semantic s-1…s-12 scale, gap roles and corner-safe insets. |
global, density, icon-size, size, size-class, viewport | Global axes, icon sizes, the size tiers and the responsive scale tokens. |
radius, corner | Radius primitives and the semantic corners (control, surface, overlay, circle). |
border, stroke, glow | Border widths, semantic strokes, glow shadows. |
shadow, elevation, elevation-alias, surface-tint, overlay, composite-elevation, glass | Shadow building blocks, the levels 1–4 and their aliases, overlay strengths, glass tokens, and (in $extensions only, as DTCG has no type for them) the surface tints and each level’s bundle. glass.backdrop-blur is the blur radius, a dimension (12px); the CSS custom property of the same name is a whole backdrop-filter value (blur(12px) saturate(140%)), kept in its css extension. |
font-family, font-size, line-height, font-weight, letter-spacing, font-numeric | Typography primitives. |
text, text-composite, text-comfort | Per-role type properties, one DTCG typography composite per role (the CSS has no equivalent), and the reading-comfort tokens. |
interaction | Durations, easings (each with a default alias: what a transition takes when it names none), state layers, spring, stagger, the focus ring with its resolved colour (focus-ring-color), opacity and active scale. |
breakpoint, container, grid, layout, scrollbar | Layout and scrollbar tokens. |
Some groups appear only when the config produces tokens for them: surface (the liquid-glass fills and borders; the backdrop filter and elevation.ambient’s page image are in its extensions) and glow.size (with liquid glass or elevation.glow above 0), elevation-surface (with elevation.surfaceAware, in the flat, liquid-glass or auto style), elevation-perceptual (with elevation.perceptual, in the same styles), elevation-inset (neumorphic style) and font-axis (with typography.variableAxes).
Vision palettes (vision.variants) are not exported as token values. The file’s top-level $extensions["com.flexpixels"].vision holds, for each variant, its seeds, settings, selector and report (met, minDeltaE, minContrast, shortfalls). To get a variant’s tokens, export that variant’s config:
import { exportDTCGTokens, visionPalettes } from "@flexpixels/cortex";
const [redGreen] = visionPalettes(config, ["red-green"]);
const redGreenTokens = exportDTCGTokens(redGreen.config);Example pipeline
Generate a tokens.json your design tool of choice can ingest:
// scripts/export-tokens.ts
import { mkdirSync, writeFileSync } from "node:fs";
import { exportDTCGTokens, loadUserConfig } from "@flexpixels/cortex";
const config = loadUserConfig("theme-config.json");
const tokens = exportDTCGTokens(config, { includeExtensions: false });
mkdirSync("./dist", { recursive: true });
writeFileSync("./dist/tokens.json", JSON.stringify(tokens, null, 2));With includeExtensions: false the dark-scheme values go too, and so do the values kept only in $extensions (surface tints, the spring easing, corner shape, the elevation bundles). Keep the default if your tool needs them.
Write the CSS and the DTCG file from the same config:
import { mkdirSync, writeFileSync } from "node:fs";
import { generateThemeCSS, exportDTCGTokens, loadUserConfig } from "@flexpixels/cortex";
const config = loadUserConfig("theme-config.json");
mkdirSync("./dist", { recursive: true });
writeFileSync("./dist/theme.css", generateThemeCSS(config));
writeFileSync("./dist/tokens.json", JSON.stringify(exportDTCGTokens(config), null, 2));Style Dictionary
Measured with Style Dictionary 5 on the default export (usesDtcg: true):
- The
css,scssandjstransform groups build it as is, except the two border gradients ($type: "gradient"), which come out as[object Object]without a custom format. - The stock Android and iOS size transforms (
size/remToDp,size/remToPt) treat every dimension as rem and multiply it by 16, so apxtoken, such as the1pxborder width, becomes16dp. Give those platforms a transform that reads each dimension’sunit. - Composite types (
shadow,border,gradient,typography) need custom formats on native platforms; the stock Swift and Android formats write them as[object Object]. - Dark values are in
$extensions["com.flexpixels"].modes.dark, which Style Dictionary does not read: every platform gets the light scheme unless a custom transform picks the dark value.
Browser-safe
exportDTCGTokens is re-exported from @flexpixels/cortex/browser — you can produce DTCG JSON from a live web playground without a build step.