Toggle
A button that stays pressed until pressed again.
This demo runs the real GPUI component on WebGPU, which this browser does not expose.
Try Chrome or Edge 113+, or Safari 26+. In Firefox, enable dom.webgpu.enabled.
You can also run it natively: cargo run -p showcase toggle
The demo failed to start in this browser.
You can run it natively instead: cargo run -p showcase toggle
Live examples are not supported on most mobile browsers yet — open this page on a desktop or tablet to run the demo.
Recent phones with WebGPU may manage it (downloads ~18 MB):
#Anatomy
Import the component's parts and compose them under its root:
use base_gpui::toggle::{
Toggle,
};
Toggle::new();
The anatomy is a structural overview. Parts with mutually exclusive modes may need separate instances; each part's section below documents its configuration.
#Toggle
Public renderable part of the Toggle component.
use base_gpui::toggle::Toggle;
Toggle::new()
.id("example-id")
.default_pressed(true)
.pressed(None)
.value(/* value: T */)
.group_value()
.own_disabled()
.toggle_id()
.with_toggle_group(/* context: ToggleGroupContext<T> */, 0, /* focus_handle: FocusHandle */)
.disabled(true)
.aria_label("aria label")
.on_pressed_change(|/* callback arguments */| { /* handle change */ })
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a Toggle with its default configuration. |
.id | pub fn id(mut self, id: impl Into<ElementId>) -> Self
Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view. |
.default_pressed | pub fn default_pressed(mut self, default_pressed: bool) -> Self
Sets the initial pressed for uncontrolled state. |
.pressed | pub fn pressed(mut self, pressed: Option<bool>) -> Self
Sets the pressed configuration for this part. |
.value | pub fn value(mut self, value: T) -> Self
Sets the current controlled value or the value represented by this part, depending on the part's role. |
.group_value | pub fn group_value(&self) -> Option<&T>The group-membership identity, consumed by the Toggle Group wiring. Has no standalone behavior. |
.own_disabled | pub fn own_disabled(&self) -> boolThe toggle's own disabled prop, consumed by the Toggle Group wiring when resolving the effective per-item disabled fact. |
.toggle_id | pub fn toggle_id(&self) -> &ElementIdThe toggle's element id, consumed by the Toggle Group wiring to key the roving focus handle. |
.with_toggle_group | pub fn with_toggle_group(mut self, context: ToggleGroupContext<T>, index: usize, focus_handle: FocusHandle,) -> Self
Attaches this toggle to a Toggle Group as a composite item. Called by the Toggle Group child wiring; not intended for direct use. |
.disabled | pub fn disabled(mut self, disabled: bool) -> Self
When true, prevents user interaction with this part. |
.aria_label | pub fn aria_label(mut self, aria_label: impl Into<SharedString>) -> Self
The accessible label announced by assistive technology. Icon-only toggles must set this; there is no |
.on_pressed_change | pub fn on_pressed_change(mut self, on_pressed_change: impl Fn(bool, &mut TogglePressedChangeDetails, &mut Window, &mut App) + 'static,) -> Self
Registers a callback invoked when pressed change occurs. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(ToggleStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. Toggle also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#Accessibility
Keyboard interaction and accessibility semantics are implemented by the component, independently of visual styling. Known limitations caused by missing GPUI accessibility primitives are documented in the module source and are not silently approximated.
The Toggle layer enters the AccessKit tree as Role::Button with aria_toggled reflecting the pressed state (the AccessKit stand-in for aria-pressed; screen readers may phrase it as "toggled"/"checked").
#Stability
Base GPUI is pre-1.0. Builder names and state types may evolve as GPUI and this port mature.