Mapping Design Tokens to Tailwind via CSS Variables
1. Background and Motivation
Design Token Source:
Our design team produces tokens in Figma/Token Studio. For example, tokens such asprimary-button-bg_hover
,primary-button-bg
,primary-button-text
, and more are exported as JSON payloads. These tokens include values for color, spacing, typography, and dimensions.Consistent Language:
We aim to use the same naming as the design team to reduce miscommunication. By preserving names likeprimary-button-bg_hover
as CSS variable names, we create a common vocabulary.Dynamic Theming:
Using CSS variables allows our app to refresh styles dynamically. Changes in design tokens (e.g., a new shade for hover states) automatically propagate when the tokens update.Tailwind Integration:
We plan to use Tailwind CSS for our utility-first styling. The key is to integrate our CSS variables into Tailwind’s setup so that developers can use classes likebg-primary-button-bg
orhover:bg-primary-button-bg_hover
directly.Tooling:
We are leveraging Styledictionary v4 as part of our token build process to generate the appropriate output from the token JSON.
2. Objectives
Preserve Design Language:
Keep the token names as defined by the design team when mapping to CSS variables (e.g.,primary-button-bg_hover
remains unchanged).Generate CSS Variables:
Create a build step (using a TypeScript-based utility) that recursively processes the token JSON and produces a CSS file (e.g.,design-tokens.css
).Integrate with Tailwind:
Extend the Tailwind setup (intailwind.config.js
) to reference these CSS variables, so tokens are used in spacing, colors, borderRadius, etc.Maintain Flexibility:
Allow for computed expressions within tokens (e.g.,"{dimension.xs} * {dimension.scale}"
), even if initial implementation just passes these as raw strings.
3. Proposed Approach
Token JSON Structure
The design tokens JSON might resemble:
CSS Variable Generation
The idea is to create a TypeScript utility that:
Traverses the entire token JSON recursively.
Uses the token’s key names to generate CSS variables.
For example, a token with the key
primary-button-bg_hover
is mapped to a CSS variable named--primary-button-bg_hover
.
Pseudocode Example:
The generated CSS might look like:
Tailwind Theme Extension
The generated design-tokens.css
file is imported globally. In our tailwind.config.js
, we extend the setup by referencing these variables:
Component Usage Example
A React button component then uses Tailwind classes referencing these setup extensions:
Now when the design team updates tokens like "primary-button-bg_hover"
in Figma, we regenerate our CSS file, and Tailwind automatically uses the new values without modifying component class names.
4. Challenges & Next Steps
Expression Evaluation:
Tokens with expressions (e.g.,"{dimension.xs} * {dimension.scale}"
) currently aren’t computed. Future work could integrate a lightweight evaluator.Build Integration:
Integrate the token processing script into our build pipeline (possibly using Styledictionary v4) to automatically generate the CSS file on token changes.UI Customization:
With tokens available as CSS variables, a UI could later be developed for dynamically updating the design system.