Skip to content

Class Handling

The class attribute maps Tailwind-like utility tokens to native GPUI builder methods. Whenever possible, keep classes as static string literals or literal conditional arrays so expansion happens entirely at compile time.

Form Example When expanded Best for
Static literal class="flex items-center gap-4 p-4" Compile time (zero cost) Stable layout, colors, state variants, rsx.toml tokens
Conditional literal array / branch class={["flex", active && "bg-blue-500"]} Compile time (zero cost) Toggling styles based on Rust booleans or enums
Dynamic runtime expression class={format!("w-[{}px]", width)} Runtime (StyleRefinement matcher) Values computed externally as strings at runtime

Static string literals expand directly into GPUI method calls:

rsx! {
<div class="flex items-center gap-2 px-4 py-2 rounded-md" />
}
// Expands to:
// div().flex().items_center().gap(px(2.0)).px(px(4.0)).py(px(2.0)).rounded_md()
  • Permissive (rsx!, rsx_permissive!): Unsupported static classes emit compile-time warnings (with typo suggestions where possible). Unknown identifier-like classes warn and pass through as GPUI methods—except CSS-only families (z-*, space-x-*, space-y-*, divide-*, transition*, duration-*, ease-*, delay-*, animate-*), which always warn and are ignored. Invalid arbitrary values (e.g. w-[oops]) remain compile errors.
  • Strict (rsx_strict!): Every unsupported static class is a compile-time error. For third-party extension methods like h_flex, use an explicit flag attribute (<div h_flex />) or base={...}.

if, match, and clsx-style arrays whose leaves are string literals are compiled into direct builder branches—no runtime string parser is emitted:

rsx! {
<div class={[
"flex items-center p-2 rounded-md",
if active { "bg-blue-500 text-white" } else { "bg-gray-100 text-gray-900" },
disabled && "opacity-50",
selected.then_some("border-1 border-blue-600"),
(compact, "gap-2"),
]} />
}

Watch out: Assigning the expression to a let cls = ...; variable first turns it into a runtime dynamic class string. Keep the conditional directly inside class={...} to retain compile-time expansion.

Static literals support inline state prefixes: hover:, focus:, focus-visible:, active:, group-hover:, and group-active: (including named groups like group-hover/menu:).

You can also group state styles into dedicated attributes:

rsx! {
<button
class="px-4 py-2 rounded-md bg-blue-500 text-white"
hoverClass="bg-blue-600"
focusClass="border-blue-500"
activeClass="opacity-75"
/>
}
  • hoverClass, focusClass, and activeClass accept string literals only and reject element-level stateful classes such as overflow-scroll or debug-outline.
  • active: and activeClass automatically inject a GPUI ID because GPUI’s active hook is stateful; hoverClass and focusClass do not.

When a class string is constructed at runtime (class={row_class}), GPUI-RSX generates a compact StyleRefinement matcher:

let class_name = format!("w-[{}px] bg-{}", width, tone);
rsx! { <div class={class_name} /> }
  • Permissive mode: Unknown dynamic tokens are ignored; debug builds log each unknown token once per call site (or aggregated at exit with the dynamic-class-report feature).
  • Strict mode: Unknown dynamic tokens panic unless you supply onUnknownClass={|class: &str| ...}.
Capability Static class Dynamic class
Layout & alignment Supported Supported subset
Numeric spacing & sizing Supported Supported
Arbitrary lengths (w-[280px], max-w-[40%]) Supported Supported
Fractions (w-6/24, w-1/2) Supported Supported
Full Tailwind color palette + hex / RGB / RGBA Supported Supported
Static rsx.toml tokens ([colors] hex, [radius], [spacing], [opacity]) Supported Supported
Render-time rsx.toml { expr = "cx..." } tokens Supported Not supported
Static stateful scroll classes (overflow-y-scroll) Supported (auto ID) Not supported
State variants (hover:, focus:, active:, group-*) Supported Not supported
Responsive / dark: prefixes Warning / strict error Debug report / strict panic
Area Examples
Layout flex, flex-col, flex-row, flex-wrap, grid, hidden, absolute, relative, grow/shrink
Alignment items-*, justify-*, content-*, self-*
Spacing gap-*, gap-x-*, gap-y-*, p-*, px-*, m-*, directional variants, negative spacing (-m-4, -mt-1.5)
Sizing w-*, h-*, size-*, min-*, max-*, *-full, *-auto, percentages, fractions, arbitrary lengths
Colors Full Tailwind palette (slate..rose 50–950, black, white), #hex, rgb(...), rgba(...)
Typography Text sizes, font weights, alignment, decoration, truncate, whitespace, line clamp
Borders Width, directional width, color, rounded-*, dashed border helpers
Misc Cursor helpers, debug-outline, shadows, opacity, grid placement (col-span-full)

Numeric utilities use direct pixel units by default: w-64 gap-4 expands to .w(px(64.0)).gap(px(4.0)).

  • Opt into Tailwind’s 4px scale (gap-4 → 16px) per block with rsx_tw!, via the tailwind-scale Cargo feature, or crate-wide with [options] spacing-scale = "tailwind" in rsx.toml.
  • Arbitrary lengths (w-[280px], h-[18rem], max-w-[37.5%]) and fractions (w-6/24) always keep their exact declared values.
  • Percentage spacing like gap-[10%] is rejected because GPUI spacing APIs require definite lengths.

Spacing and sizing families with GPUI *_neg_* presets accept a leading minus sign (-m-2, -mt-1.5, -ml-[13px], hover:-gap-1):

Negative family GPUI layout behavior
-m-*, -mx-*, -my-*, -mt-*, -ml-* Shifts the element and following siblings toward the negative edge (overlapping neighbors)
-mb-*, -mr-* Pulls the next sibling closer along the layout axis
-gap-*, -gap-x-*, -gap-y-* Pulls siblings closer together along the matching axis
-p-*, -px-*, -py-*, -pt-*, -pl-* Shifts content outward past the container edge
-w-*, -h-*, -size-*, -max-w-*, -max-h-* Clamped to 0 (emits a compile-time warning)
-min-w-*, -min-h-* Ignored by GPUI (emits a compile-time warning)

-auto, -full, rounded-*, and leading-* cannot be negated. Preset names like -mt-px require a static literal (not supported in the dynamic runtime matcher).

Choose error policy and spacing scale independently:

Macro Error policy Spacing scale
rsx! Permissive Pixel (or rsx.toml / feature default)
rsx_strict! Strict compile errors Pixel (or rsx.toml / feature default)
rsx_permissive! Explicitly permissive Pixel (or rsx.toml / feature default)
rsx_tw! Permissive Tailwind 4px scale
rsx_expand! Debug string preview (no type check) Pixel (or rsx.toml / feature default)

Combine strict checking with Tailwind scale by using rsx_strict! together with tailwind-scale or [options] spacing-scale = "tailwind".

For render-loop tips and shared runtime measurements, see the Performance Guide.