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.
Quick Decision Guide
Section titled “Quick Decision Guide”| 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 |
Auto IDs
Section titled “Auto IDs”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>}What triggers an automatic ID?
Section titled “What triggers an automatic ID?”- 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, andcaptureKeyDown.
Auto IDs on component tags
Section titled “Auto IDs on component tags”For component tags (such as <ui::card onClick={handler} />) and base={...} expressions, automatic ID injection is type-directed:
- Types implementing both
gpui::InteractiveElementandgpui::Element(like functions returningDiv) receive an auto ID. - Component builders (such as
#[component]structs orgpui-component’sButton) keep their own setters without an injected.id(). - For opaque or generic return types where
Elementis hidden, passid={...}explicitly or apply stateful methods inside the constructor where the concrete type is known.
Keys in Loops
Section titled “Keys in Loops”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>}keyis a macro-only attribute and never generates a.key(...)method call.- On non-stateful elements (e.g.
<div key={task.id} class="flex">),keyis ignored so the element stays lightweight. - Component tags with stateful attribute names still require
keyoridin loops because the macro validates loop safety before Rust type resolution.
Performance of key types
Section titled “Performance of key types”| 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 |
Explicit IDs
Section titled “Explicit IDs”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.