Skip to content

Style Dictionary JSON ​

CSS Forge can generate a separate token file for Style Dictionary and other tools that read the same JSON shape. This output does not change the CSS, TypeScript, or regular JSON files you already generate.

Generate the file ​

Use style-dictionary mode to generate only the token file:

bash
cssforge --mode style-dictionary --style-dictionary ./.cssforge/tokens.json

Use --mode all to generate it together with the CSS, TypeScript, and regular JSON outputs. The --style-dictionary option controls where the token file is written.

A generated token looks like this:

json
{
  "palette": {
    "neutral": {
      "900": {
        "value": "oklch(17.764% 0 0)",
        "type": "color",
        "$tier": "primitive",
        "$resolvedValue": "oklch(17.764% 0 0)",
        "attributes": {
          "cssVariable": "--palette-neutral-900",
          "cssVariableReference": "var(--palette-neutral-900)",
          "tailwindVariable": "--palette-neutral-900",
          "resolvedValue": "oklch(17.764% 0 0)",
          "sourcePath": "palette.neutral.900"
        }
      }
    }
  }
}

Semantic tokens also include $reference and attributes.referencePaths. These paths match the keys in the generated file, so consumers can connect a semantic token to its source.

Token fields ​

FieldContainsUse it for
valueThe token value in the selected value modeRendering and Style Dictionary transforms
typeThe value kind, falling back to the CSS Forge module when the value kind is not narrowerGrouping and previews that depend on what the token holds
$tierprimitive or semanticSeparating base scales from intent tokens
$referenceThe token path this token was built from, when it has oneFollowing a semantic token back to its source
attributes.cssVariableThe token's CSS custom property, such as --palette-neutral-900Declaring or overriding the token in CSS
attributes.tailwindVariableThe same custom property name, without the var() wrapperTools that match authored var(--token) usage to tokens
attributes.resolvedValueThe final value, even in css-reference modeShowing a value without following references
$resolvedValueThe same final value as a top-level DTCG-style fieldTools that read $resolvedValue before falling back to value

type narrows fontSize, lineHeight, fontWeight, fontFamily, borderRadius, letterSpacing, shadow, opacity, zIndex, and number when the token's name and value agree, and stays color, spacing, gradient, typography, primitive, or component otherwise.

A narrowed kind is also matched by the leaf name aliases font-size, text-size, line-height, leading, font-weight, font-family, radius, rounded, tracking, box-shadow, text-shadow, shadow, alpha, z-index, gap, duration, and delay.

Choose the value mode ​

Modevalue containsUse it for
resolved (default)The final value, such as oklch(...), 1rem, or clamp(...)Style Dictionary transforms and token previews
css-referenceThe token's own CSS variable, such as var(--palette-neutral-900)Tools that match CSS variable usage in source files

The default resolved mode recursively resolves references to other CSS Forge tokens. Cycles and unknown CSS variables remain as var(...) instead of causing generation to fail.

css-reference values are CSS custom-property references, not Style Dictionary aliases. Style Dictionary aliases use {path.to.token}. Use the default resolved mode when Style Dictionary will transform the file.

bash
# Keep CSS variables as values for usage matching
cssforge --mode style-dictionary --style-dictionary ./.cssforge/tokens.json --style-dictionary-value-mode css-reference

Programmatic API ​

typescript
import { generateStyleDictionaryJSON } from "@hebilicious/cssforge";

const resolvedTokens = generateStyleDictionaryJSON(config);
const usageTokens = generateStyleDictionaryJSON(config, {
  valueMode: "css-reference",
});

Example: Musea ​

Musea can use the generated file as its token source:

typescript
import { musea } from "@vizejs/vite-plugin-musea";

musea({
  tokensPath: ".cssforge/tokens.json",
});

Musea reads value, type, and $reference from this file. Keep the default resolved value mode: previews and token swatches render from value, and attributes.tailwindVariable is what lets Musea attribute a var(--token) written in an art file back to its token.

Released under the MIT License.