Themes & Design Tokens
Place an rsx.toml file beside your crate’s Cargo.toml to define project-wide design tokens and macro defaults. Static class literals resolve tokens at compile time and expand straight into ordinary GPUI builder calls—no runtime lookup overhead.
1. Define Your Tokens
Section titled “1. Define Your Tokens”Start with a concise token palette:
[colors]brand = "#2563eb"
[radius]card = "12px"
[spacing]gutter = "18px"
[text-size]body = "14px"
[line-height]body = "20px"
[opacity]muted = "60%"Then reference them directly in any static class attribute:
rsx! { <div class="flex flex-col gap-gutter p-gutter rounded-card"> <h1 class="text-brand font-bold">{"Project overview"}</h1> <p class="text-size-body leading-body opacity-muted"> {"A shared vocabulary for your views."} </p> </div>}Token-to-GPUI Mapping
Section titled “Token-to-GPUI Mapping”rsx.toml section |
Example utility classes | Expanded GPUI method |
|---|---|---|
colors |
bg-brand, text-brand, border-brand |
.bg(), .text_color(), .border_color() |
radius |
rounded-card |
.rounded() |
spacing |
p-gutter, gap-gutter |
.p(), .gap() and directional variants |
text-size |
text-size-body |
.text_size() |
line-height |
leading-body |
.line_height() |
opacity |
opacity-muted |
.opacity() |
shadow |
shadow-card |
.shadow() |
Validation rules: Lengths require explicit units (e.g.
"12px"). Opacity accepts a quoted fraction ("0.6") or percentage ("60%"). Duplicate keys, malformed values, and conflictingtext-size-*entries fail configuration validation.
2. Follow the Application Theme (gpui-component Preset)
Section titled “2. Follow the Application Theme (gpui-component Preset)”Enable the built-in gpui-component preset to unlock semantic classes such as bg-background, text-foreground, and bg-primary:
[options]preset = "gpui-component"# context = "cx" # default; change if your render context uses another identifierImport ActiveTheme and keep cx in scope wherever the macro expands:
use gpui::component::ActiveTheme as _;use gpui::prelude::*;use gpui::*;use gpui_rsx::rsx;
// Inside Render::render with cx: &mut Context<Self>:rsx! { <div class="bg-background text-foreground p-[24px]"> {"Follows the active application theme automatically."} </div>}The preset expands to expressions like cx.theme().colors.background. Defining a custom color with the same name overrides the preset entry; literal hex tokens remain fixed colors. For component setup, see gpui-component integration.
3. Rust Expression Tokens & Shadows
Section titled “3. Rust Expression Tokens & Shadows”Any token can evaluate a Rust expression at render time using { expr = "..." }:
[colors]surface = { expr = "cx.theme().colors.background" }
[shadow]card = { expr = "vec![BoxShadow::new(px(0.0), px(1.0), hsla(0.0, 0.0, 0.0, 0.2))]" }Use class="bg-surface shadow-card". Every identifier referenced inside expr (such as BoxShadow, px, hsla, or cx) must be in scope at the macro call site.
4. Inspect & Validate Configuration
Section titled “4. Inspect & Validate Configuration”With cargo-rsx installed, inspect or validate your theme from the terminal:
cargo rsx theme check --manifest-path demo/Cargo.tomlcargo rsx theme list --manifest-path demo/Cargo.tomlcargo rsx theme explain hover:opacity-muted --manifest-path demo/Cargo.tomlAll three subcommands support --json (exit code 1 for validation failures, 2 for usage errors). The editor LSP uses the exact same configuration parser.
5. Crate-Wide [options]
Section titled “5. Crate-Wide [options]”Beyond preset, rsx.toml centralizes three crate-wide macro options:
[options]preset = "gpui-component"spacing-scale = "tailwind" # 4px per numeric unit (gap-4 -> 16px)dynamic-runtime = "rsx_runtime" # Share runtime matcher across call sitesgpui-path = "::engine" # Optional: override default ::gpui pathspacing-scale = "tailwind": Switches numeric spacing/sizing from 1px to Tailwind’s 4px scale. Explicit lengths (gap-[14px]) and named tokens (gap-gutter) are unaffected. See the spacing migration guide before flipping this on an existing codebase.dynamic-runtime = "rsx_runtime": Deduplicates generated dynamic class matchers by delegating togpui_rsx::rsx_runtime!();at the crate root. See Performance.gpui-path: Overrides::gpuiwhen your crate aliases GPUI under another name or re-exports it atcrate::ui. See Compatibility forbuild.rstracking and function-component caveats.