0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Style a native checkbox as an on/off toggle switch, in three sizes. The look is pure CSS; a small script adds role='switch'.
Overview
data-switch turns a native <input type="checkbox"> into a sliding toggle switch. There is no wrapper: the attribute goes on the checkbox itself, and the checkbox stays a real form control. It submits with the form, takes part in validation and form.reset(), and fires the native change event.
The track and knob are drawn by CSS (appearance: none plus a ::before knob), so the switch looks right before any JavaScript runs. The only scripted part is the role: switch-init adds role="switch" so screen readers say "switch, on" instead of "checkbox, checked".
Usage
Put the checkbox inside its <label> so the label text names it and is part of the click target.
Switch styling on a checkbox. Track sizes: s 2 × 1.125rem, default 2.75 × 1.5rem, l 3.5 × 2rem. The knob is the track height minus 4px. Only input[type="checkbox"] gets the role.
data-switch-init
boolean
—
Set by the script once the role is applied. Informational; no styles depend on it.
checked, disabled, required
native
—
Work as on any checkbox. A disabled switch is dimmed and not interactive. A required switch that is left off is invalid and gets a dashed error outline after interaction.
The size values follow the VB size vocabulary (s / l, as on <layout-badge>, <progress> and <dialog>). They were renamed from sm / lg; the old values no longer match.
No read-only switch. HTML readonly does not apply to checkboxes, so <input type="checkbox" data-switch readonly> still toggles. Use disabled for a locked switch (note that a disabled control is not submitted), or show the state as text.
Events
data-switch adds no events of its own. Listen for the native change (and input) events on the checkbox and read checked.
Colours come from three public tokens in src/tokens/forms.css. Override them on :root, a theme, or a single switch:
Custom property
Default
Used for
--switch-track-bg
var(--control-border)
Track when off (at least 3:1 against the surface).
--switch-track-checked-bg
var(--color-interactive)
Track when on.
--switch-knob-bg
var(--color-white, white)
Knob.
Geometry is private (--_switch-w, --_switch-h); pick a size with the attribute value instead of setting them. The styles hang off input[data-switch] directly, not off data-switch-init.
The script adds role="switch", so the control is announced as a switch with an on/off state. Without JavaScript it looks like a switch but is announced as a checkbox unless you author the role.
Space toggles it, as for any checkbox.
It needs a label: wrap it in <label>, use <label for>, or aria-labelledby.
Focus shows a 2px outline from :focus-visible. The invalid state is a dashed outline, not only a colour change.
In forced-colours mode the track is outlined in CanvasText, the on state uses Highlight, and disabled switches use GrayText, so on and off stay distinguishable.
The knob's slide is turned off under prefers-reduced-motion: reduce.
In right-to-left text the knob travels toward the inline end (left).
Examples
Sizes
Switch sizes
12345
<layout-stackdata-layout-gap="m"><label><inputtype="checkbox"data-switch="s"name="compact" /> Small switch</label><label><inputtype="checkbox"data-switchname="standard"checked /> Default switch</label><label><inputtype="checkbox"data-switch="l"name="prominent" /> Large switch</label></layout-stack>
Checked and disabled
Switch states
12345
<layout-stackdata-layout-gap="m"><label><inputtype="checkbox"data-switchname="on"checked /> On by default</label><label><inputtype="checkbox"data-switchname="locked-off"disabled /> Disabled, off</label><label><inputtype="checkbox"data-switchname="locked-on"disabledchecked /> Disabled, on</label></layout-stack>
Required, with form-field
A required switch must be on before the form submits. Inside <form-field> the error output appears after the user has interacted.
Required switch
12345678910
<form><layout-stackdata-layout-gap="m"><form-field><label><inputtype="checkbox"data-switchname="terms"id="terms"required /> Accept the terms</label><outputclass="hint"for="terms">You must agree before continuing.</output><outputclass="error"for="terms"aria-live="polite">Turn this on to continue.</output></form-field><buttontype="submit">Continue</button></layout-stack></form>
Settings group
Group related switches in a <fieldset> with a <legend>.
data-switch — a setting that takes effect on its own (on/off), styled the same in every browser.
A plain checkbox — a choice the user confirms with a submit button, or one item in a list of options.
Safari's native <input type="checkbox" switch> — only Safari draws it as a switch (and exposes the switch role natively); other browsers show a checkbox. data-switch gives the same result everywhere. If you use both, the data-switch styles win.