Toggle Group

A row of toggles where one or several can be pressed.

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-group

The demo failed to start in this browser.

You can run it natively instead: cargo run -p showcase toggle-group

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_group::{
    ToggleGroup,
};

ToggleGroup::new();
The anatomy is a structural overview. Parts with mutually exclusive modes may need separate instances; each part's section below documents its configuration.

#ToggleGroup

Groups related child parts and coordinates their shared behavior.

Source

use base_gpui::toggle_group::ToggleGroup;

ToggleGroup::new()
    .id("example-id")
    .aria_label("aria label")
    .default_value(/* default_value: Vec<T> */)
    .value(/* value: Vec<T> */)
    .disabled(true)
    .orientation(/* orientation: ToggleGroupOrientation */)
    .multiple(true)
    .loop_focus(true)
    .on_value_change(|/* callback arguments */| { /* handle change */ })
    .style_with_state(|/* callback arguments */| { /* handle change */ });

#Builders

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

Creates a ToggleGroup with its default configuration.

.childpub fn child(mut self, child: impl Into<ToggleGroupChild<T>>) -> Self
  • child: impl Into<ToggleGroupChild<T>>

Adds one typed child to this part.

.childrenpub fn children(mut self, children: impl IntoIterator<Item = impl Into<ToggleGroupChild<T>>>,) -> Self
  • children: impl IntoIterator<Item = impl Into<ToggleGroupChild<T>>>

Adds multiple typed children in iteration order.

.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.

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

Sets a literal accessible name for this part.

.default_valuepub fn default_value(mut self, default_value: Vec<T>) -> Self
  • default_value: Vec<T>

Sets the initial value for uncontrolled state. Later user changes are retained by the component.

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

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

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

When true, prevents user interaction with this part.

.orientationpub fn orientation(mut self, orientation: ToggleGroupOrientation) -> Self
  • orientation: ToggleGroupOrientation

Sets the component's horizontal or vertical orientation and corresponding keyboard behavior.

.multiplepub fn multiple(mut self, multiple: bool) -> Self
  • multiple: bool

Controls whether more than one value may be selected at the same time.

.loop_focuspub fn loop_focus(mut self, loop_focus: bool) -> Self
  • loop_focus: bool

Controls whether keyboard focus wraps from the last enabled item to the first and vice versa.

.on_value_changepub fn on_value_change(mut self, on_value_change: impl Fn(&[T], &mut ToggleGroupValueChangeDetails, &mut Window, &mut App) + 'static,) -> Self
  • on_value_change: impl Fn(&[T], &mut ToggleGroupValueChangeDetails, &mut Window, &mut App) + 'static

Registers a callback invoked when value change occurs.

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

Styles the part from its current behavioral state while preserving separation between behavior and visual design.

ToggleGroup 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.

#Stability

Base GPUI is pre-1.0. Builder names and state types may evolve as GPUI and this port mature.