A simple, flexible library for managing design tokens in Elixir.
Jetons generates compile-time token accessor functions from parsed JSON maps, with support for single or multiple themes (light/dark/custom).
Generated Functions
Single theme modules generate:
token/1- Get token by path (returns nil if not found)token!/1- Get token by path (raises if not found)list_<category>/0- List all tokens in a categorygroup_by_path/2- Group tokens by path depthget_groups/1- Get top-level groups for a categoryget_in_group/2- Get all tokens in a specific group
Multi-theme modules additionally generate:
token/2- Get token by path for specific themetoken!/2- Get token by path for specific theme (raises if not found)list_<category>/1- List tokens in category for specific themelist/0- List all tokens (uses default theme)list/1- List all tokens for specific themethemes/0- List all available theme names
Extending Jetons
Jetons supports custom transformers for generating output in any format. See the "Extending Jetons" guide for creating iOS, Android, or custom transformers.
What use Jetons Injects
When you use Jetons, it generates the following functions in your module:
Single Theme Mode:
token/1- Retrieves token value by path (returns nil if not found)token!/1- Retrieves token value by path (raises KeyError if not found)list_<category>/0- One function per category (e.g.,list_colors/0)group_by_path/2- Groups tokens by path depthget_groups/1- Returns sorted list of top-level groupsget_in_group/2- Returns all tokens in a specific group
Multi-Theme Mode (when 2+ themes provided): All of the above, plus:
token/2- Retrieves token with explicit theme parametertoken!/2- Retrieves token with theme (raises if not found)list_<category>/1- List category tokens for specific themethemes/0- Returns list of all available theme names- All helper functions with
/3variants accepting theme parameter
Compile-Time Dependencies: Note that token files must exist at compile time. Changing token files requires recompiling any modules that use them.
Summary
Functions
Generates token accessor functions at compile time.
Functions
Generates token accessor functions at compile time.
Single Theme (No Theme Switching)
For single-theme tokens, provide one theme key:
defmodule MyTokens do
use Jetons,
main: File.read!("tokens.json") |> Jason.decode!()
end
MyTokens.token("color.text.primary") # No theme parameter needed
MyTokens.list_color() # List all color tokensMultiple Themes (With Theme Switching)
For multi-theme support, provide multiple theme keys:
defmodule MyTokens do
use Jetons,
light: File.read!("tokens/light.json") |> Jason.decode!(),
dark: File.read!("tokens/dark.json") |> Jason.decode!(),
high_contrast: File.read!("tokens/high-contrast.json") |> Jason.decode!()
end
MyTokens.token("color.text.primary") # Uses default (first theme: :light)
MyTokens.token("color.text.primary", :dark) # Explicit dark theme
MyTokens.token("color.text.primary", :high_contrast) # High contrast theme
MyTokens.themes() # [:light, :dark, :high_contrast]
MyTokens.list_color(:dark) # List colors for dark themeRequirements
- All theme values must be parsed JSON maps (use
Jason.decode!/1) - Token files must follow the Design Tokens Community Group (DTCG) format
- For multi-theme setups, all themes should have the same token structure
See Jetons.Ref.expand/1.