Reference
Switch
On/off toggle bound to a boolean state.
Switch(bind: State, checked: Bool, label: String, …)
Before 5.0
checked
label
disabled
Replies on, digest off
Example
state replies = true
state digest = false
Stack(gap: .sm) {
Switch(bind: replies, label: "Email me when someone replies")
Switch(bind: digest, label: "Send a weekly digest")
Text("Replies {if replies { "on" } else { "off" }}, digest {if digest { "on" } else { "off" }}").muted
}
Disabled
state sso = true
Switch(bind: sso, label: "Single sign-on (set by your organisation)").disabled
Props
| Prop | Type | Unset | Meaning |
|---|---|---|---|
bind: | State | The | |
checked: | Bool | false | Whether it is on, when no state is bound: shown, not written back — handle |
label: | String | none | The words after the switch, which a press on also flips; also its accessible name |
disabled: | Bool | false | Whether the control is inert: it cannot be focused or changed, and a form does not send it. Written as a flag, |
Attribute families: global, aria, data, input. Plus the 22 universal props every element takes: animate:, exit:, speed:, duration:, delay:, easing:, stagger:, on:, count:, shared:, class:, ref:, drag:, dragGroup:, dropValue:, dropAxis:, dropInside:, roving:, trapFocus:, mount:, update:, cleanup:.
Flags and cases
A flag is written after the parentheses with a dot: a Bool prop, or a case of one of the element's enum props. A case the enum lacks is an error with a hint.
.checked
.disabled
Events
The DOM events it is drawn to answer. Any other DOM event works too, in an on … { } handler.
| Event | Sends | Meaning |
|---|---|---|
on change | DOM | The value was committed |
Every element also takes the props, the events and the attribute families listed on every element.
Slots
It takes no slots.
Renders with the class wf-switch, stable across releases, so a stylesheet may target it.