GrepBoard/Manuals
605 manuals ·

Build design tokens into separate light and dark CSS files

·Tested on Node.js 24 · style-dictionary 5.7.0·8 steps

Build a palette and per-mode semantic token JSON into a :root file and a [data-theme="dark"] file with Style Dictionary, and check that aliases follow the mode.

Mixing light and dark values in one token JSON leaves no clean way to split them into CSS. Keep one palette of raw colors, keep one set of semantic tokens (background, text, accent) per mode, and run the build once per mode to get a :root file and a [data-theme="dark"] file. This assumes the basics from Build design tokens JSON into CSS variables. The example JSON is hand-written.

[1] Install.

console
$ npm install style-dictionary$ npx style-dictionary --version5.7.0

[2] Put the palette in tokens/core/palette.json. These are raw colors, independent of mode.

tokens/core/palette.json
{  "palette": {    "$type": "color",    "white": { "$value": "#ffffff" },    "gray": {      "100": { "$value": "#f4f5f7" },      "800": { "$value": "#1f2430" },      "900": { "$value": "#14171f" }    },    "blue": {      "500": { "$value": "#0b5fff" },      "300": { "$value": "#7aa5ff" }    }  }}

[3] Create one semantic file per mode. Both files define the same names (color.bg and so on) and differ only in which palette entry they point to.

tokens/semantic/light.json
{  "color": {    "$type": "color",    "bg": { "$value": "{palette.white}" },    "surface": { "$value": "{palette.gray.100}" },    "text": { "$value": "{palette.gray.900}" },    "accent": { "$value": "{palette.blue.500}" }  }}
tokens/semantic/dark.json
{  "color": {    "$type": "color",    "bg": { "$value": "{palette.gray.900}" },    "surface": { "$value": "{palette.gray.800}" },    "text": { "$value": "{palette.gray.100}" },    "accent": { "$value": "{palette.blue.300}" }  }}

[4] Write a build script, build.mjs. Create a separate StyleDictionary instance per mode, with only the palette and that mode's semantic file in source. Set the output selector with the selector option of the css/variables format, and use filter on the dark side to keep only semantic tokens. The palette only needs to appear once, in the light (:root) file.

build.mjs
import StyleDictionary from 'style-dictionary'; const modes = [  { name: 'light', selector: ':root', keepCore: true },  { name: 'dark', selector: '[data-theme="dark"]', keepCore: false },]; for (const m of modes) {  const sd = new StyleDictionary({    source: ['tokens/core/*.json', `tokens/semantic/${m.name}.json`],    platforms: {      css: {        transformGroup: 'css',        buildPath: 'build/',        files: [{          destination: `${m.name}.css`,          format: 'css/variables',          filter: (t) => m.keepCore || t.filePath.includes('semantic'),          options: { selector: m.selector },        }],      },    },  });  await sd.buildAllPlatforms();}

[5] Build.

console
$ node build.mjscss✔︎ build/light.css css✔︎ build/dark.css$ cat build/light.css build/dark.css/** * Do not edit directly, this file was auto-generated. */ :root {  --palette-white: #ffffff;  --palette-gray-100: #f4f5f7;  --palette-gray-800: #1f2430;  --palette-gray-900: #14171f;  --palette-blue-300: #7aa5ff;  --palette-blue-500: #0b5fff;  --color-bg: #ffffff;  --color-surface: #f4f5f7;  --color-text: #14171f;  --color-accent: #0b5fff;}/** * Do not edit directly, this file was auto-generated. */ [data-theme="dark"] {  --color-bg: #14171f;  --color-surface: #1f2430;  --color-text: #f4f5f7;  --color-accent: #7aa5ff;}

The same names, such as --color-bg, differ only in value between the two files. Each alias was resolved inside its own mode's build, so the semantic tokens followed the mode.

[6] Load dark.css after light.css and set the attribute on <html>. :root and [data-theme="dark"] have equal selector specificity, so the later file wins. Swap the order and dark mode never applies.

index.html
<html data-theme="dark"><head>  <link rel="stylesheet" href="light.css">  <link rel="stylesheet" href="dark.css"></head>

[7] To emit references instead of values, add outputReferences: true to options. The same input produced this:

output
[data-theme="dark"] {  --color-bg: var(--palette-gray-900);  --color-surface: var(--palette-gray-800);  --color-text: var(--palette-gray-100);  --color-accent: var(--palette-blue-300);}

The light file came out as --color-bg: var(--palette-white); and so on, with the palette definitions still in :root. The --palette-* variables the dark file points to come from :root, so load both files.

[8] See what happens when only one mode has a bad reference. In dark.json, change {palette.blue.300} to {palette.blue.350} and rebuild.

console
$ node build.mjscss✔︎ build/light.css…Error: Reference Errors:Some token references (1) could not be found.Use log.verbosity "verbose" or use CLI option --verbose for more details.Refer to: https://styledictionary.com/reference/logging/$ echo $?1

Light builds, dark stops. The exit code is 1, so CI fails on it directly. Because each mode builds separately, a typo present in only one file shows up when that mode is built.

#design-tokens#style-dictionary#dark-mode