Page MenuHomePhabricator

Publish design tokens as JS variables
Closed, ResolvedPublic

Description

Background/Goal

Style Dictionary (the library used for managing our design tokens) supports the ability to export tokens as ES6 variables. The output looks something like this:

export const ColorBackgroundBase = '#ffffff';
export const ColorBackgroundAlt = '#fcfcfcfc';

It's also possible to provide custom prefixes like Cdx if desired.

Is this something we should consider including in the public @wikimedia/codex-design-tokens package on NPM?

It sounds like the Language Team may have a use-case for this, where they are accessing breakpoint token values in JS to help dictate component behavior (i.e. not just styles that could be handled in Less or Sass).

Development considerations

Having access to named exports for each individual token would allow for a more efficient build when using a JS build tool like Vite because the unused tokens could be tree-shaken out. Not sure if this is possible when importing JSON.

Acceptance Criteria
  • Figure out if this is something we want to do (I believe at least one use-case exists per above)
  • Add a new build target with "format": "javascript/es6" in the tokens build configuration if so
Supporting documentation

Event Timeline

Should we instead (or also) export them in the json/flat format? That looks like this:

{
    "color-background-base": "#ffffff",
    "color-background-alt": "#fcfcfcfc"
}

In most systems JSON can be imported just as easily as JS, but this way the tokens would still have the same names (in kebab-case, rather than being transformed to lowerCamelCase), and they'd be usable in e.g. PHP as well.

Using lowerCamelCase for token names would make it easier to import them in JS, because you could simply do import { colorBackgroundBase } from '@wikimedia/codex-design-tokens'; However, it is possible to import things with kebab-case names: the syntax for that is import { "color-background-base" as colorBackgroundBase } from '@wikimedia/codex-design-tokens';, and that works as long as the exporting code does something like const colorBackgroundBase = '#ffffff'; export colorBackgroundBase as "color-background-base";. Unfortunately, the transformation that Rollup (and by extension, Vite) uses to internally transform JSON files to JS to allow keys to be imported does not support this, so it would probably be useful to build javascript/es6 output and make that the entry point for the package.

Regarding json vs js format, some considerations

  • If there is a potential usecase to use these variables in php, json might be a good candidate since it can be imported in non-js programming languages.
  • Since these are just key-value pairs and there no logic involved, a data format like json fits the purpose.

... Vite because the unused tokens could be tree-shaken out. Not sure if this is possible when importing JSON.

Treeshaking is supported with json and vite. See https://vitejs.dev/guide/features.html#json

Having index.json as main entrypoint(it will be picked up even if not explicitly defined, import { colorBackgroundBase } from '@wikimedia/codex-design-tokens'; should work

Yes, that will work as long as the keys in the JSON file have lowerCamelCase names. If they have kebab-case names, importing them individually doesn't work in Vite.

Discussed at Dec 12 refinement: this task does not block T293127

AnneT changed the task status from Open to In Progress.Jan 3 2024, 6:26 PM
AnneT claimed this task.

Claiming this so I can get some experience working with Style Dictionary

Change 987492 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] build, tokens: Export design tokens as ES6 variables

https://gerrit.wikimedia.org/r/987492

Change 987492 merged by jenkins-bot:

[design/codex@main] build, tokens: Export design tokens as ES6 variables

https://gerrit.wikimedia.org/r/987492

Change 992533 had a related patch set uploaded (by Eric Gardner; author: Eric Gardner):

[mediawiki/core@master] Update Codex from v1.2.1 to v1.3.0

https://gerrit.wikimedia.org/r/992533

Change 992533 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from v1.2.1 to v1.3.0

https://gerrit.wikimedia.org/r/992533