Skip to content

IDs and Keys

GPUI requires an element ID (.id(...)) whenever an element uses stateful behavior such as clicks, focus, tooltips, or scrolling. GPUI-RSX injects deterministic IDs automatically for static markup—so you only need key in loops and explicit id when identity must survive source edits.

Situation What to write Why
Plain layout/styling (<div class="flex p-4">) Nothing Non-stateful elements do not need an ID
Single interactive element outside a loop Nothing (auto ID) Macro derives a deterministic ID from file, line, and column
Stateful element inside {for ...} key={item.id} or id={...} Distinguishes repeated rows across reorders and updates
Helper function called multiple times Explicit id={...} or distinct parent ID Prevents identical auto IDs inside the same GPUI identity scope
Identity must survive line moves / refactors Explicit id="save-button" Auto IDs include source line & column

Outside loops, stateful attributes and scroll classes trigger an automatic ID derived from the call site:

rsx! {
<button onClick={handler}>{"Click"}</button>
}
rsx! {
<div class="overflow-y-scroll">{"Scrollable content"}</div>
}
  • Stateful attributes: onClick, onHover, onDrag, onAuxClick, onA11yAction, active, activeClass, groupActive, tooltip, tooltipShowDelay, focusable, role, accessibilityId, ariaLabel, ariaDescription, ariaKeyShortcuts, ariaActiveDescendant, a11ySyntheticChildren, ariaNumericValueStep, ariaValue, ariaPlaceholder, overflowScroll, overflowXScroll, overflowYScroll, restrictScrollToAxis, trackScroll, externalDragPayload.
  • Static scroll classes: overflow-scroll, overflow-x-scroll, overflow-y-scroll.

Non-stateful (no ID injected): hover, hoverClass, focus, focusClass, group, groupHover, scrollbarWidth, onMouseDown, onMouseExit, onMousePressure, captureMousePressure, onPinch, capturePinch, and captureKeyDown.

For component tags (such as <ui::card onClick={handler} />) and base={...} expressions, automatic ID injection is type-directed:

  • Types implementing both gpui::InteractiveElement and gpui::Element (like functions returning Div) receive an auto ID.
  • Component builders (such as #[component] structs or gpui-component’s Button) keep their own setters without an injected .id().
  • For opaque or generic return types where Element is hidden, pass id={...} explicitly or apply stateful methods inside the constructor where the concrete type is known.

Inside {for ...}, any element with a stateful attribute or scroll class must specify key={...} or a distinct id={...}—otherwise compilation fails:

rsx! {
<div>
{for task in &self.tasks {
<button key={task.id} onClick={handler} class="flex items-center gap-2">
{task.title.clone()}
</button>
}}
</div>
}
  • key is a macro-only attribute and never generates a .key(...) method call.
  • On non-stateful elements (e.g. <div key={task.id} class="flex">), key is ignored so the element stays lightweight.
  • Component tags with stateful attribute names still require key or id in loops because the macro validates loop safety before Rust type resolution.
Key expression Generated ID representation Cost
String literal (key="toolbar") concat!(...) static &str Zero allocation
Owned integer ≤ 64-bit (u64, usize, i32, etc.) GPUI (&'static str, u64) tuple ID Zero allocation
u128 / i128, borrowed refs, generic T: Display Formatted string fallback Allocates per render

An explicit id always takes precedence over both key and auto IDs:

rsx! {
<button id="save-button" key={task.id} onClick={handler}>
{"Save"}
</button>
}

Use an explicit id whenever:

  • Element identity must stay stable across source-line edits or refactors.
  • A helper function containing stateful RSX is invoked more than once within the same parent scope.