Switch
An on/off control that takes effect at once. The switch is a native checkbox with the switch role.
Basic
checked is two-way. A wrapping label toggles it.
Code
<Label>
<Switch checked={state.airplane} />
Airplane mode
</Label>Usage
template.ehtmlimport { Switch } from "@elements/ui/switch"; <Label><Switch checked={settings.airplane} /> Airplane mode</Label> <Switch checked={settings.wifi} size="sm" attrs={{ "aria-label": "Wi-Fi" }} />
Examples
With a description
The label and description sit beside the switch. The label stays the same in both states.
Focus is shared across devices, and turns off when you leave the app.
Code
<div class="option">
<div>
<Label htmlFor="share">Share across devices</Label>
<p class="hint" id="share-hint">Focus is shared across devices, and turns off when you leave the app.</p>
</div>
<Switch checked={state.share} attrs={{ id: "share", "aria-describedby": "share-hint" }} />
</div>Size
sm and the default size.
Code
<Label><Switch size="sm" checked={state.small} /> Small</Label>
<Label><Switch checked={state.medium} /> Default</Label>Disabled and invalid
States change color, never size.
Code
<Switch disabled />
<Switch disabled checked />
<Switch invalid />Group
A FieldSet and its legend name a set of switches (APG). attrs.form joins them to a form they sit outside.
Code
<FieldSet>
<FieldLegend variant="label">Email notifications</FieldLegend>
<Field orientation="horizontal" label="Product updates" reserveError={false}>
<Switch checked={state.updates} name="updates" attrs={{ form: "signup" }} />
</Field>
<Field orientation="horizontal" label="Weekly digest" reserveError={false}>
<Switch checked={state.digest} name="digest" uncheckedValue="off" attrs={{ form: "signup" }} />
</Field>
</FieldSet>Read only
A read-only switch is focusable and announced. A click, Space or Enter does not change its state.
Code
<Label><Switch checked readOnly /> Backups (managed by your administrator)</Label>Required in a form
The browser will not submit until it is on. Enter toggles it like Space. uncheckedValue submits a value while a switch is off.
Code
<form onsubmit={() => submit(state)}>
<Label><Switch checked={state.terms} required name="terms" /> Accept terms and conditions</Label>
<Label><Switch name="marketing" value="yes" uncheckedValue="no" /> Marketing email</Label>
<Button type="submit" variant="primary">Continue</Button>
<Button type="reset">Reset</Button>
</form>Choice card
A FieldLabel wrapping a Field makes the whole card toggle the switch.
Code
<FieldLabel>
<Field orientation="horizontal" reserveError={false}>
<FieldContent>
<FieldTitle>Share across devices</FieldTitle>
<FieldDescription>Focus is shared across devices, and turns off when you leave the app.</FieldDescription>
</FieldContent>
<Switch checked={state.share} />
</Field>
</FieldLabel>Cancel a change
onCheckedChange runs before the switch changes and gets details as its second argument. details.cancel() keeps the current state for a click, Space or Enter.
Code
<Switch checked={guard.value} onCheckedChange={(checked, details) => {
if (!guard.allowed) details.cancel();
}} />Right to left
The thumb starts at the inline start and slides toward the end.