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

wf

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

wf

state sso = true Switch(bind: sso, label: "Single sign-on (set by your organisation)").disabled

Props

Props of Switch
PropTypeUnsetMeaning

bind:

State

The Bool state it reads and writes: true while on

checked:

Bool

false

Whether it is on, when no state is bound: shown, not written back — handle on change to keep it. Written as a flag, .checked, or checked: true.

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, .disabled, or disabled: true.

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.

DOM events Switch answers
EventSendsMeaning
on changeDOMThe 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.