Switch
A toggle between an on and an off state.
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 switch
The demo failed to start in this browser.
You can run it natively instead: cargo run -p showcase switch
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::switch::{
SwitchRoot,
SwitchThumb,
};
SwitchRoot::new()
.child(
SwitchThumb::new(),
);
The anatomy is a structural overview. Parts with mutually exclusive modes may need separate instances; each part's section below documents its configuration.
#SwitchRoot
Coordinates the component's state and supplies context to its child parts.
use base_gpui::switch::SwitchRoot;
SwitchRoot::new()
.id("example-id")
.name("name")
.default_checked(true)
.checked(None)
.value(/* value: impl Into<SharedString> */)
.form(/* form: impl Into<SharedString> */)
.unchecked_value(/* unchecked_value: impl Into<SharedString> */)
.disabled(true)
.read_only(true)
.required(true)
.aria_label("aria label")
.on_checked_change(|/* callback arguments */| { /* handle change */ })
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a SwitchRoot with its default configuration. |
.child | pub fn child(mut self, child: impl Into<SwitchChild>) -> Self
Adds one typed child to this part. |
.children | pub fn children(mut self, children: impl IntoIterator<Item = impl Into<SwitchChild>>) -> Self
Adds multiple typed children in iteration order. |
.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. |
.name | pub fn name(mut self, name: impl Into<SharedString>) -> Self
Sets the form field name used when serializing or submitting the value. |
.default_checked | pub fn default_checked(mut self, default_checked: bool) -> Self
Sets the initial checked for uncontrolled state. |
.checked | pub fn checked(mut self, checked: Option<bool>) -> Self
Sets the checked configuration for this part. |
.value | pub fn value(mut self, value: impl Into<SharedString>) -> Self
Sets the current controlled value or the value represented by this part, depending on the part's role. |
.form | pub fn form(mut self, form: impl Into<SharedString>) -> Self
Sets the form configuration for this part. |
.unchecked_value | pub fn unchecked_value(mut self, unchecked_value: impl Into<SharedString>) -> Self
Sets the unchecked value configuration for this part. |
.disabled | pub fn disabled(mut self, disabled: bool) -> Self
When true, prevents user interaction with this part. |
.read_only | pub fn read_only(mut self, read_only: bool) -> Self
When true, allows presentation and focus behavior without permitting value changes. |
.required | pub fn required(mut self, required: bool) -> Self
Marks the control as required for validation and accessibility state. |
.aria_label | pub fn aria_label(mut self, aria_label: impl Into<SharedString>) -> Self
Sets a literal accessible name for this part. |
.on_checked_change | pub fn on_checked_change(mut self, on_checked_change: impl Fn(bool, &mut SwitchCheckedChangeDetails, &mut Window, &mut App) + 'static,) -> Self
Registers a callback invoked when checked change occurs. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(SwitchRootStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. SwitchRoot also supports the GPUI traits implemented in its source, such as standard styling or child composition. |
#SwitchThumb
Interactive or visual handle representing the current value.
use base_gpui::switch::SwitchThumb;
SwitchThumb::new()
.style_with_state(|/* callback arguments */| { /* handle change */ });
#Builders
| Builder | Signature & description |
|---|---|
.new | pub fn new() -> SelfCreates a SwitchThumb with its default configuration. |
.style_with_state | pub fn style_with_state(mut self, style: impl Fn(SwitchThumbStyleState, Div) -> Div + 'static,) -> Self
Styles the part from its current behavioral state while preserving separation between behavior and visual design. SwitchThumb 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.
SwitchRoot exposes an AccessKit node with Role::Switch, aria_toggled derived from the checked style state, and an accessible name via the .aria_label(...) builder. Action::Click and Action::Focus are auto-registered by the existing .on_click(...) / .track_focus(...) wiring. SwitchThumb is decorative and has no role, so it stays out of the accessibility tree.
#Stability
Base GPUI is pre-1.0. Builder names and state types may evolve as GPUI and this port mature.