Token dictionary processing
The token-dictionary package turns theme token input into a shared token model for Panda's compiler and theme tools. It keeps token names and source values linked to CSS variable references, conditions, aliases, and generated views so that style resolution, CSS output, generated theme data, and token documentation use the same token meaning.
Package role
The dictionary is the processing layer below the design token system. It accepts base tokens, semantic tokens, breakpoints, and theme variants, then exposes one consistent model to compiler and theme consumers. Reusable token foundations enter this layer through the preset library, while the dictionary makes their values available to the rest of Panda.
Token representation
home/…/token.ts defines a Token as a named value with its path, category, type, description, deprecation state, and processing extensions. The model keeps the original value as well as the current value. This lets later work preserve source meaning while producing a value that is ready for CSS or runtime lookup.
The representation separates the main token forms:
| Token form | Role in the system |
|---|---|
| Base token | Supplies a direct design value such as a color, size, or font. |
| Semantic token | Supplies a meaning-based value that can change by condition. |
| Theme token | Supplies a value for a named theme condition. |
| Virtual token | Provides a derived lookup path such as a color-palette value. |
| Conditional token | Represents one named value from a token with several condition values. |
References: packages/…/token.ts• L8-35 packages/…/token.ts• L48-113 packages/…/dictionary.ts• L140-218
Dictionary lifecycle
The TokenDictionary collects token input and builds the shared model before compiler consumers use it. The build order matters because each stage prepares data for the next stage: token values need names before references can resolve, and CSS variable extensions need to exist before conditional values can point to them.
The main lifecycle moves from source input to consumer views:
flowchart LR
A[Theme token input] --> B[Named token model]
B --> C[Pre-processing]
C --> D[Reference and condition model]
D --> E[Value transforms]
E --> F[CSS variable and theme data]
F --> G[Computed dictionary views]
G --> H[Compiler]
G --> I[Theme and Studio consumers]
The lifecycle has these broad stages:
- The dictionary registers regular tokens, semantic tokens, breakpoints, and theme variants.
- Pre-processing adds derived entries such as negative spacing and virtual color-palette tokens.
- The dictionary records token references and expands references that can resolve to other token values.
- Value transforms turn structured values into CSS-ready values and attach CSS variable data.
- Post-processing adds conditional values, pixel metadata, and removes empty entries.
- A computed view groups the result by category, condition, variable, and color palette.
References: packages/…/dictionary.ts• L68-81 packages/…/dictionary.ts• L104-221 packages/…/dictionary.ts• L463-485 packages/…/middleware.ts• L6-45 packages/…/middleware.ts• L64-158
Reference resolution
Token references connect semantic values to base values. The package accepts curly-brace references such as {colors.blue.500} and token-function expressions with fallbacks. It resolves known references to CSS variable references for runtime styling, while direct resolution can also return the underlying token value for compiler work.
Reference handling has two useful results:
- Variable resolution keeps a relationship such as
{colors.blue.500}connected tovar(--colors-blue-500). - Deep value resolution follows references recursively when a consumer needs the raw value instead of a CSS variable.
The reference parser also preserves ordinary CSS variable fallbacks and supports nested token functions. An unknown token is escaped and reported rather than silently treated as a known design token.
References: packages/…/utils.ts• L12-79 packages/…/expand-token-references.ts• L5-37 packages/…/expand-token-references.ts• L65-184 packages/…/dictionary.ts• L322-347 packages/…/dictionary.ts• L404-457
Value transformation
Value transformation converts token data into forms that CSS and runtime consumers can use. The package supports structured values for shadows, gradients, borders, assets, fonts, and easing curves. It also handles color mixes and adds the CSS variable identity that links a token name to its emitted value.
Transformations are conditional on token category. This keeps the source model expressive while giving each consumer a stable string value or variable reference. The same transform rules apply to condition values when a semantic token has more than one value.
References: packages/…/transform.ts• L19-137 packages/…/transform.ts• L140-224 packages/…/transform.ts• L368-379 packages/…/dictionary.ts• L261-301
Computed token views
The computed view is the dictionary's consumer-facing index. It presents the processed tokens as category maps, flat variable values, raw values, condition groups, theme variable groups, and color-palette mappings. Consumers use these views instead of rebuilding token relationships from the source theme.
The view supports the main lookup needs:
| View | Consumer need |
|---|---|
| Category map | Enumerate tokens by token category and inspect token metadata. |
| Flat values | Resolve a token path to its CSS variable reference. |
| Raw values | Read the value that matches runtime token behavior. |
| Condition and variable maps | Emit conditional and theme-specific CSS. |
| Color-palette map | Register palette names and their derived color paths. |
| Category JSON | Expose grouped values to property and documentation consumers. |
References: packages/…/dictionary.ts• L499-551 packages/…/dictionary.ts• L553-649
Compiler consumers
The compiler creates one dictionary from the configured theme before it creates utilities, patterns, and other style engines. Utility resolution reads the computed views to turn token paths into CSS variables and to expose token category values to style properties.
Generators use the same views for different output goals. CSS generation reads condition-grouped variables, JavaScript theme generation reads theme variable groups, and token specifications read category maps. These outputs stay aligned because they all depend on the same processed dictionary.
References: packages/…/context.ts• L58-105 packages/…/context.ts• L253-263 packages/…/utility.ts• L161-171 packages/…/utility.ts• L238-267 packages/…/token-css.ts• L7-48 packages/…/themes.ts• L12-44 packages/…/tokens.ts• L13-107
Theme and palette consumers
Theme variants enter the dictionary as conditional token data. The CSS generator uses the condition map to emit base and theme-specific variable rules, while generated theme files reuse those rules for runtime theme loading. The Studio token view can also rebuild the dictionary for a selected theme and inspect the resulting category values.
Color palettes are a derived consumer feature. The dictionary identifies palette roots and creates virtual lookup paths, then the compiler registers those paths as values for the colorPalette style property. Palette documentation and generated token types read the same palette view.
References: packages/…/dictionary.ts• L193-219 packages/…/middleware.ts• L64-148 packages/…/token-css.ts• L15-37 packages/…/themes.ts• L12-26 packages/…/utility.ts• L161-171 packages/…/color-palette.ts• L24-71 packages/…/panda-context.ts• L45-73
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to token names or CSS variable formatting must preserve lookups from
TokenDictionary.viewand the generated CSS, JavaScript theme, and token-spec outputs. - Changes to semantic conditions or theme variants must preserve the condition names shared by the dictionary, the compiler condition registry, and theme CSS filtering.
- Changes to reference syntax must keep curly references, token-function fallbacks, nested CSS variables, and unresolved-reference handling consistent.
- Changes to color-palette derivation must keep virtual token paths aligned with the compiler's
colorPaletteproperty and generated palette documentation. - Changes to transform order must preserve the dependency between CSS variable creation, reference expansion, and conditional value conversion.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A token object has no value field. | Dictionary construction stops with an invalid-token error. | Keep every leaf in token input in the supported token-entry shape. |
| A reference names a token that is not in the dictionary. | The value keeps an escaped token path and a warning is logged. | Define referenced tokens in the merged theme or treat the value as ordinary CSS. |
| A conditional token is referenced as if it had one fixed value. | Reference expansion leaves that relationship in place instead of flattening it to one value. | Resolve conditional values through their condition-aware view. |
| Color-palette generation is disabled or a path is excluded. | No virtual palette lookup is available for that color path. | Align colorPalette settings with the palette names used by styles. |
A theme is not allowed by staticCss.themes. | Its variable rules are omitted from the generated static CSS. | Include the needed theme name or wildcard in the static CSS theme selection. |
Key files
| File | Why to read it |
|---|---|
| packages/…/dictionary.ts | Defines dictionary construction, processing order, reference access, and computed consumer views. |
| packages/…/token.ts | Defines the shared token model and condition-aware token behavior. |
| packages/…/transform.ts | Shows how token values become CSS-ready values and variable metadata. |
| packages/…/middleware.ts | Shows the main derived token features such as negative spacing and virtual palettes. |
| packages/…/utils.ts | Defines token reference discovery, replacement, validation, and warning behavior. |
| packages/…/context.ts | Shows where the compiler creates and shares the dictionary. |
| packages/…/token-css.ts | Shows how computed condition and theme views become CSS variable rules. |
| packages/…/tokens.ts | Shows how category views become token and semantic-token consumer data. |