Toggle

A button that stays pressed until pressed again.

EXAMPLEwasm · webgpu

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.

Source

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

BuilderSignature & description
.newpub fn new() -> Self

Creates a Toggle with its default configuration.

.idpub fn id(mut self, id: impl Into<ElementId>) -> Self
  • id: impl Into<ElementId>

Sets the stable GPUI element identity. Use a unique value when multiple instances can appear in the same view.

.default_pressedpub fn default_pressed(mut self, default_pressed: bool) -> Self
  • default_pressed: bool

Sets the initial pressed for uncontrolled state.

.pressedpub fn pressed(mut self, pressed: Option<bool>) -> Self
  • pressed: Option<bool>

Sets the pressed configuration for this part.

.valuepub fn value(mut self, value: T) -> Self
  • value: T

Sets the current controlled value or the value represented by this part, depending on the part's role.

.group_valuepub fn group_value(&self) -> Option<&T>

The group-membership identity, consumed by the Toggle Group wiring. Has no standalone behavior.

.own_disabledpub fn own_disabled(&self) -> bool

The toggle's own disabled prop, consumed by the Toggle Group wiring when resolving the effective per-item disabled fact.

.toggle_idpub fn toggle_id(&self) -> &ElementId

The toggle's element id, consumed by the Toggle Group wiring to key the roving focus handle.

.with_toggle_grouppub fn with_toggle_group(mut self, context: ToggleGroupContext<T>, index: usize, focus_handle: FocusHandle,) -> Self
  • context: ToggleGroupContext<T>
  • index: usize
  • focus_handle: FocusHandle

Attaches this toggle to a Toggle Group as a composite item. Called by the Toggle Group child wiring; not intended for direct use.

.disabledpub fn disabled(mut self, disabled: bool) -> Self
  • disabled: bool

When true, prevents user interaction with this part.

.aria_labelpub fn aria_label(mut self, aria_label: impl Into<SharedString>) -> Self
  • aria_label: impl Into<SharedString>

The accessible label announced by assistive technology. Icon-only toggles must set this; there is no aria-labelledby id-reference wiring in this gpui revision, so the literal string is the only labelling mechanism.

.on_pressed_changepub fn on_pressed_change(mut self, on_pressed_change: impl Fn(bool, &mut TogglePressedChangeDetails, &mut Window, &mut App) + 'static,) -> Self
  • on_pressed_change: impl Fn(bool, &mut TogglePressedChangeDetails, &mut Window, &mut App) + 'static

Registers a callback invoked when pressed change occurs.

.style_with_statepub fn style_with_state(mut self, style: impl Fn(ToggleStyleState, Div) -> Div + 'static,) -> Self
  • style: impl Fn(ToggleStyleState, Div) -> Div + 'static,

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.