Principle:AGenUI AGenUI Theme And Design Token System
| Knowledge Sources | |
|---|---|
| Domains | UI_Framework, Theming |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
A design token system that decouples visual styling from component code by mapping semantic token names to concrete values, with built-in light/dark mode switching and lazy re-evaluation of dependent components.
Description
Design tokens are named entities that store visual design decisions (colors, spacing, typography) as key-value pairs. They decouple the what (semantic intent: "primary_button_color") from the how (concrete value: "#007AFF"). This allows theme changes without modifying component code — only the token mapping changes.
This principle implements a token resolution pipeline: design token config JSON is loaded into a `TokenParser` singleton. Each token stores a `type` (e.g. "color") and separate `light`/`dark` values. When the theme mode is switched via `setDayNightMode()`, the token parser updates its internal mode and broadcasts `invalidateFunctionCallValues()` to all surface managers, triggering re-evaluation of all components and styles with the new theme.
The resolution logic follows a fallback strategy: in dark mode, the parser prefers the `dark` value and falls back to `light` if `dark` is absent (and vice versa). If a token name is not found at all, the original name is returned unchanged, allowing non-token values to pass through transparently.
Usage
Use this principle when:
- A UI framework needs to support light/dark mode switching without recompilation
- Visual design decisions should be centralized in configuration, not hardcoded
- Theme changes must propagate to all rendered components efficiently
- Design tokens should resolve transparently in component style attributes
Theoretical Basis
The token resolution algorithm:
# Abstract algorithm (NOT real implementation)
def resolve(token_name, theme_mode):
token = design_tokens.get(token_name)
if not token:
return token_name # pass through if not a token
if theme_mode == Dark:
return token.dark if token.dark else token.light
else:
return token.light if token.light else token.dark
The invalidation cascade on theme switch:
setDayNightMode("dark")
→ TokenParser.setThemeMode(Dark)
→ if mode actually changed:
→ for each SurfaceManager:
→ invalidateFunctionCallValues()
→ for each Surface:
→ BatchScope (coalesce diffs)
→ re-evaluate all component attributes & styles
→ emit field-level diffs to native renderer
Practical Guide
- Author design token config JSON: `{"designTokens": {"color_primary": {"type": "color", "light": "#007AFF", "dark": "#0A84FF"}, ...}}`
- Load via `engine->loadDesignTokenConfig(tokenJson, error)`.
- Use tokens in component styles via the `parseToken` functionCall or inline token references.
- Switch modes with `engine->setDayNightMode("dark")`; all surfaces re-evaluate automatically.