Switch
@codeui/styles v0.3.0
An on and off toggle in plain HTML, with a track, a thumb, and a label.
Load @codeui/styles/switch.css, or all.css. The Solid component is documented in Switch.
Demo
<div class="cui-switch" data-cui="switch" data-size="md" data-checked> <input id="line-numbers" type="checkbox" role="switch" checked aria-describedby="line-numbers-hint" class="cui-switch-input" data-cui="switch-input" data-size="md" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" /> <div class="cui-switch-control" data-cui="switch-control" data-size="md" data-checked aria-hidden="true"> <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md" data-checked></div> </div> <label for="line-numbers" class="cui-switch-label cui-field-label" data-cui="switch-label" data-size="md">Show line numbers</label> <div id="line-numbers-hint" class="cui-switch-description" data-cui="switch-description" data-size="md">Include line numbers in exported snippets.</div></div>Markup
| Part | Element | Class | data-cui |
|---|---|---|---|
| Root | div | cui-switch | switch |
| Input | input type="checkbox" role="switch" | cui-switch-input | switch-input |
| Track | div | cui-switch-control | switch-control |
| Thumb | div | cui-switch-thumb | switch-thumb |
| Label | label | cui-switch-label cui-field-label | switch-label |
| Hint | div | cui-switch-description | switch-description |
| Error | div | cui-switch-error-message | switch-error-message |
The root is a two-column grid. The track sits in the first column, and the label, the hint, and the error sit in the second. cui-switch-input has no rules, so hide the input visually with your own style, as the demos do inline. The focus ring selects [data-cui="switch-input"]:focus-visible ~ .cui-switch-control, so the input keeps its data-cui and comes before the track.
| Attribute | Element | Values | Default |
|---|---|---|---|
data-size | Root or track | sm, md | md |
Variants
Sizes
<div class="cui-stack" data-gap="4" data-align="start"> <div class="cui-switch" data-cui="switch" data-size="sm" data-checked> <input id="size-sm" type="checkbox" role="switch" checked class="cui-switch-input" data-cui="switch-input" data-size="sm" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" /> <div class="cui-switch-control" data-cui="switch-control" data-size="sm" data-checked aria-hidden="true"> <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="sm" data-checked></div> </div> <label for="size-sm" class="cui-switch-label cui-field-label" data-cui="switch-label" data-size="sm">Line numbers (sm)</label> </div> <div class="cui-switch" data-cui="switch" data-size="md" data-checked> <input id="size-md" type="checkbox" role="switch" checked class="cui-switch-input" data-cui="switch-input" data-size="md" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" /> <div class="cui-switch-control" data-cui="switch-control" data-size="md" data-checked aria-hidden="true"> <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md" data-checked></div> </div> <label for="size-md" class="cui-switch-label cui-field-label" data-cui="switch-label" data-size="md">Line numbers (md)</label> </div></div>md is a 36×22 track with a 16px thumb. sm is a 28×16 track with a 12px thumb. On a touch screen both grow to 44×26 with a 20px thumb, so the target is at least 40px. data-size="sm" works on the track, or on a root with data-cui="switch".
States
Checked
<div class="cui-stack" data-gap="4" data-align="start"> <div class="cui-switch" data-cui="switch" data-size="md"> <input id="state-off" type="checkbox" role="switch" class="cui-switch-input" data-cui="switch-input" data-size="md" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" /> <div class="cui-switch-control" data-cui="switch-control" data-size="md" aria-hidden="true"> <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md"></div> </div> <label for="state-off" class="cui-switch-label cui-field-label" data-cui="switch-label" data-size="md">Show window header</label> </div> <div class="cui-switch" data-cui="switch" data-size="md" data-checked> <input id="state-on" type="checkbox" role="switch" checked class="cui-switch-input" data-cui="switch-input" data-size="md" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" /> <div class="cui-switch-control" data-cui="switch-control" data-size="md" data-checked aria-hidden="true"> <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md" data-checked></div> </div> <label for="state-on" class="cui-switch-label cui-field-label" data-cui="switch-label" data-size="md">Show line numbers</label> </div></div>Set data-checked on both the track and the thumb. The track takes the brand fill and the thumb slides to the end. Keep the native checked property in sync.
Disabled
<div class="cui-stack" data-gap="4" data-align="start"> <div class="cui-switch" data-cui="switch" data-size="md" data-checked data-disabled> <input id="disabled-on" type="checkbox" role="switch" checked disabled aria-describedby="disabled-on-hint" class="cui-switch-input" data-cui="switch-input" data-size="md" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" /> <div class="cui-switch-control" data-cui="switch-control" data-size="md" data-checked data-disabled aria-hidden="true"> <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md" data-checked></div> </div> <label for="disabled-on" class="cui-switch-label cui-field-label" data-cui="switch-label" data-size="md">Show line numbers</label> <div id="disabled-on-hint" class="cui-switch-description" data-cui="switch-description" data-size="md">Include line numbers in the export.</div> </div> <div class="cui-switch" data-cui="switch" data-size="md" data-disabled> <input id="disabled-off" type="checkbox" role="switch" disabled class="cui-switch-input" data-cui="switch-input" data-size="md" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" /> <div class="cui-switch-control" data-cui="switch-control" data-size="md" data-disabled aria-hidden="true"> <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md"></div> </div> <label for="disabled-off" class="cui-switch-label cui-field-label" data-cui="switch-label" data-size="md">Show window header</label> </div></div>data-disabled on the root dims the switch to 40% opacity. On the track it sets the not-allowed cursor. Set the native disabled attribute on the input too.
Invalid
<div class="cui-switch" data-cui="switch" data-size="md" data-checked> <input id="invalid" type="checkbox" role="switch" checked aria-invalid="true" aria-describedby="invalid-hint invalid-error" class="cui-switch-input" data-cui="switch-input" data-size="md" style="position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap" /> <div class="cui-switch-control" data-cui="switch-control" data-size="md" data-checked data-invalid aria-hidden="true"> <div class="cui-switch-thumb" data-cui="switch-thumb" data-size="md" data-checked></div> </div> <label for="invalid" class="cui-switch-label cui-field-label" data-cui="switch-label" data-size="md">Show line numbers</label> <div id="invalid-hint" class="cui-switch-description" data-cui="switch-description" data-size="md">Include line numbers in the export.</div> <div id="invalid-error" class="cui-switch-error-message" data-cui="switch-error-message" data-size="md">Enable line numbers before you export.</div></div>data-invalid on the track draws a 2px --cui-text-danger outline. Render the error only while the switch is invalid, and set aria-invalid="true" on the input.
What you supply
- A click handler on the track that toggles the input. The label toggles it natively through
for. data-checkedon the track and the thumb whenever the input'scheckedchanges.html[data-cui-input]set tokeyboardorpointer. Withpointer, the focus ring stays hidden after a click. Withkeyboard, the toggle is instant.- Matching
idandfor, andaria-describedbyfor the hint and the error.
Helper
import "@codeui/styles/switch.css";
import { applyStyleProps } from "@codeui/styles";
import { switchStyleProps } from "@codeui/styles/switch";
applyStyleProps(document.querySelector("[data-cui=switch-control]")!, switchStyleProps.control({ size: "sm" }));
applyStyleProps(document.querySelector("[data-cui=switch-thumb]")!, switchStyleProps.thumb({ size: "sm" }));
// class="cui-switch-control" data-cui="switch-control" data-size="sm"