Publish design tokens#
Description#
Beta: The --token-* CSS custom properties are in beta. We encourage you to start using them and welcome your feedback. The token API may still change, but we will communicate any breaking changes.
The design tokens are synchronized from Figma with the "Design Tokens" GitHub Action and converted to CSS variables with the make-properties script. We generate color and radius tokens, and sync typography into Eufemia's existing CSS properties.
Overview#
- Design tokens are synchronized from Figma by a GitHub Action, which opens a draft pull request for review.
- We generate color tokens and radius values. Font families, body weights, font sizes and line heights use the existing typography properties, without an additional
--token-font-*layer. Other string values are skipped. - We export Foundation colors, plus light and dark brand token modes for DNB and Sbanken (Carnegie is light-only for now)
How to update#
1. Run the "Design Tokens" workflow#
- Open Actions → Design Tokens, then select Run workflow on
main. - The workflow reads the Figma variables with the variables REST API, writes the
*.tokens.jsonfiles listed below, regenerates the CSS files and their test snapshots, builds the library and Portal, updates the visual snapshots and opens a draft pull request from a newtokens/automation-figma-sync-YYYYMMDD-HHMMSSbranch. If Figma and the Eufemia design tokens are already synchronized, it reports that no changes were found. - Review the token values, removals and visual snapshots before marking the pull request ready.
- Review and (squash) merge the pull request.
Run on any other branch, the tokens are committed to that branch instead. The Output branch input overrides where the commit goes, and can name an existing branch or a new one, but not main or a release branch. An existing branch is built on as it is, without being rebased. A draft pull request is opened for the branch unless it already has one, and an open pull request is never modified. A new PR targets the branch it was created from.
The tokens and their test snapshots are committed separately from the screenshots, so the screenshot commit can be reviewed, or dropped, on its own. The commits are attributed to whoever started the run, and a new pull request is assigned to them. The pull request itself, and its comments, come from the GitHub Actions bot.
The Generate screenshots input is on by default. Turning it off skips the screenshot run, so the image snapshots are left untouched and have to be updated separately.
Every mode of the exported collections has to be listed in TOKEN_EXPORTS (packages/dnb-eufemia/scripts/figma/tasks/tokensExtractor.ts). A mode that is in Figma but not there is skipped with a warning, and only becomes CSS once it is added both to TOKEN_EXPORTS and to the token files in makePropertiesFile.ts. A mode that is listed but no longer in Figma fails the run.
The workflow writes color.tokens.json with only the foundation colors the brand collection actually uses, because the API returns the remote variables used in the file. A manual export contains the whole collection instead, so switching between the two shows up as a large diff in that one file without changing the generated CSS.
The workflow needs the repository secret FIGMA_TOKEN (a personal access token with the file_variables:read scope) and the repository variable FIGMA_TOKENS_FILE (the key of the "💻 Eufemia - Web" file). The Figma variables API is only available to Enterprise organizations.
2. Exporting design tokens manually#
As a fallback, the same files can be exported by hand.
We currently export the Figma variable collections for:
- "Foundation" collection "colors" (
color.tokens.json) - "💻 Eufemia - Web" collection "brand" with modes:
- "dnb-light" (
brand/dnb-light.tokens.json) - "dnb-dark" (
brand/dnb-dark.tokens.json) - "sbanken-light" (
brand/sbanken-light.tokens.json) - "sbanken-dark" (
brand/sbanken-dark.tokens.json) - "dnbcarnegie-light" (
brand/dnbcarnegie-light.tokens.json) - "dnbeiendom-light" (
brand/birdhouse-light.tokens.json) - "dnbeiendom-dark" (
brand/birdhouse-dark.tokens.json)
- "dnb-light" (
These modes generate the opt-in Birdhouse preview. The legacy Eiendom theme continues to use the DNB tokens.
Birdhouse dark uses Eufemia accessibility overrides for error text, focus rings and control boundaries until the corresponding Figma colors meet contrast requirements. Keep an icon or another non-color cue alongside errors, since the accessible error text is close to the neutral text color.
Place color.tokens.json directly in packages/dnb-eufemia/src/style/themes/figma, and place the brand-mode exports in its brand subfolder.
Detailed steps:
- Go to the "💻 Eufemia - Web" file in Figma. (You need edit permissions to see sub-collections)
- Active "Design" mode.
- Click the "Open variables" icon next to the label "Variables" in the right side-menu (must deselect elements to see the side-menu).
- Click the "Collections options" icon next to the "Collections" heading and ensure "All collections" is selected.
- Right click the collections "brand" and "colors" and select "Export Modes".
- Unzip the downloaded JSON files and place them in
packages/dnb-eufemia/src/style/themes/figma, using the relative filenames listed above.
We export the collections from the same Figma file in order to ensure the collections are in sync. Each brand-mode export is a complete snapshot of the collection, so light and dark files also contain shared font and radius values. The generator reads typography from the light export. Keep families, weights, sizes and line heights identical in light and dark modes.
3. Generate CSS files#
The CSS files are automatically generated during the "Prebuild" step by the makePropertiesFile.ts script (packages/dnb-eufemia/scripts/prebuild/tasks/makePropertiesFile.ts).
Generate locally (manual)#
To generate the files manually you can run the command yarn workspace @dnb/eufemia make-properties.
To fetch the tokens from Figma locally, add FIGMA_TOKEN and FIGMA_TOKENS_FILE to your .env file and run yarn workspace @dnb/eufemia figma:tokens.
Typography mapping#
Typography uses an explicit mapping in makePropertiesFile.ts, rather than the generic --token-* naming transform. This preserves existing application overrides, brand aliases, JavaScript exports and Tailwind properties. The generator updates existing declarations, so this mapping adds no CSS declarations or font files.
font/family/bodysupplies--font-family-default;font/family/headingsupplies--font-family-heading. Figma's installed family names map to the registered web family names and their fallback stacks. For example,Maison Neuemaps toMaisonNeueHeadings. Families not registered in the selected theme fail generation. New families need a code mapping and the appropriate font assets registered in the theme.font/weight/text-basissupplies the existing default, basis and regular weight aliases. It must remainRegularuntil the web's regular font rules and brand mappings are migrated together.text-basis-mediumsupplies--font-weight-medium. Thetext-smandtext-xsweights must match their corresponding basis weight.Regularmaps tonormal, andMediumto500. Unknown values fail generation.- Sizes and line heights use the public typography scale. Regular and medium variants share sizes and line heights.
- Carnegie inherits its body family and weight scale from DNB. The generator checks that its exported values still match. Sbanken and the transitional Coconut theme keep their existing
--sb-*aliases, and Carnegie keeps--ca-font-family-heading. - Coconut uses the DNB body family. Until its Figma export provides it, the generator replaces the exported body family with DNB and logs a warning. Once Figma exports DNB, the warning will ask for the replacement to be removed.
Heading and lead weights keep their existing theme rules. Sbanken exports Book Sbanken for headings and Demi Sbanken for lead, while the web uses regular weight for both and registers one Maison heading face. Coconut exports medium for headings and lead, while its web rules also use regular. Carnegie exports regular headings, while the web uses medium. Confirm the intended web appearance and available font files with UX before changing those rules. Bold, light, display, monospace and native sizes also retain their existing code values because the brand exports do not provide matching roles.
Typography changes in Figma#
Use the published CSS property names for Web code syntax. Keep the existing Figma paths until their mappings and exports can change together.
| Figma variable | Web code syntax |
|---|---|
font/family/body | --font-family-default |
font/family/heading | --font-family-heading |
font/size/text-2xs | --font-size-xx-small |
font/size/text-xs | --font-size-x-small |
font/size/text-sm | --font-size-small |
font/size/text-basis | --font-size-basis |
font/size/text-lead | --font-size-medium |
font/size/heading-lg | --font-size-large |
font/size/heading-xl | --font-size-x-large |
font/size/heading-2xl | --font-size-xx-large |
font/height/* | The matching --line-height-* property |
font/weight/text-basis, text-sm, text-xs | --font-weight-regular |
Their -medium weight variants | --font-weight-medium |
Medium size and height variants use the same Web code syntax as their regular counterparts. The current exports incorrectly give both text-xs (14px) and text-2xs (13px) the same size code name; correct these and their medium variants using the table. Use CSS's line-height terminology for Web code syntax. The current height path heading-large maps to the size path heading-lg; coordinate any path cleanup with the code mapping.
Naming conventions/transforms#
Since Figma has different rules and limitations than CSS we have established some naming conventions to avoid errors.
- only use alphanumeric characters. (a-z and 0-9)
- Variables are not case-sensitive. (
onDarkandondarkare considered the same variable) - We use dashes (
-) to separate groups and words.
Transforms#
During the CSS generation, Figma variables are converted to lower case. And groups are separated by dashes (-).
Only characters a-z A-Z 0-9. and - are supported. Any unsupported characters will throw an error.
We also add the prefix token to the variables from the Brand collection.
Example#
Figma variable: Color/Dimmer/Action-Pressed-Subtle-OnDark
CSS variable: --token-color-dimmer-action-pressed-subtle-ondark
Potential issues#
Even following all the rules, we still risk naming overlap since groups and words use the same separator in CSS color/action/pressed and color/action-pressed would map to the same variable.
But these will at least be caught on build.