Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Switch

An input where the user toggles a boolean value between on and off.

When To Use It

Use Switch for immediate binary settings such as enabling notifications, dark mode, or realtime updates. Prefer switches for settings panels and quick toggles where state change is applied immediately.

Constructing a Switch

A Switch binds to a bool and emits actions through on_toggle:

let enabled = Signal::new(false);

Switch::new(cx, enabled)
	.on_toggle(|cx| cx.emit(AppEvent::ToggleEnabled));

With a visible label:

HStack::new(cx, |cx| {
	Switch::new(cx, enabled)
		.id("notifications_switch")
		.on_toggle(|cx| cx.emit(AppEvent::ToggleEnabled));

	Label::new(cx, "Enable notifications")
		.describing("notifications_switch");
})
.gap(Pixels(8.0))
.height(Auto);

Switch Modifiers

ModifierTypeDescriptionDefault
on_toggleFn(&mut EventContext)Called when the switch is toggled.-

Components and Styling

Switch uses a root switch element and a thumb child.

SelectorDescription
switchRoot switch element.
switch .thumbThumb/handle element.
switch:checkedChecked/on state.
switch:disabledDisabled state.

Accessibility

Switch sets role Switch and is keyboard navigable.

  • Use name("...") when there is no visible label.
  • Use id(...) and describing(...) to associate visible label text.

Keyboard interaction:

KeyAction
Space / EnterToggles the switch when focused.