Skip to content

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.

Start with a concise token palette:

rsx.toml
[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>
}
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 conflicting text-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:

rsx.toml
[options]
preset = "gpui-component"
# context = "cx" # default; change if your render context uses another identifier

Import 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.

Any token can evaluate a Rust expression at render time using { expr = "..." }:

rsx.toml
[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.

With cargo-rsx installed, inspect or validate your theme from the terminal:

Terminal window
cargo rsx theme check --manifest-path demo/Cargo.toml
cargo rsx theme list --manifest-path demo/Cargo.toml
cargo rsx theme explain hover:opacity-muted --manifest-path demo/Cargo.toml

All three subcommands support --json (exit code 1 for validation failures, 2 for usage errors). The editor LSP uses the exact same configuration parser.

Beyond preset, rsx.toml centralizes three crate-wide macro options:

rsx.toml
[options]
preset = "gpui-component"
spacing-scale = "tailwind" # 4px per numeric unit (gap-4 -> 16px)
dynamic-runtime = "rsx_runtime" # Share runtime matcher across call sites
gpui-path = "::engine" # Optional: override default ::gpui path
  • spacing-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 to gpui_rsx::rsx_runtime!(); at the crate root. See Performance.
  • gpui-path: Overrides ::gpui when your crate aliases GPUI under another name or re-exports it at crate::ui. See Compatibility for build.rs tracking and function-component caveats.