toggle
toggle is an on/off switch. It is either on or off, never half-way. The family's name is
toggle because switch is a reserved word in JavaScript.
onChange called 0 time(s)
Off: no emails.
import { state } from '@markless/core';import { toggle } from '@markless/ui';export default function ToggleBasic() @{ let on = state(false); <toggle.root class="row" name="notifications" checked={on} onChange={(next: boolean) => { on = next; }} > <div class="words"> <toggle.label class="label">Email me when an order ships</toggle.label> <p class="now">{on ? 'On: we send one email per order.' : 'Off: no emails.'}</p> </div> <toggle.trigger class="track"> <toggle.thumb class="thumb" /> </toggle.trigger> <toggle.field /> </toggle.root>;}
.row { display: flex; gap: 1.5em; align-items: center; justify-content: space-between; box-sizing: border-box; width: min(26rem, 100%); padding: 0.85em 1em; border: 1px solid var(--ink); border-radius: 6px; font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink); transition: background 140ms ease;}.row[ui-checked] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.words { display: grid; gap: 0.15em;}.label { font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step-0); cursor: pointer;}.row:has(:required) .label::after { content: ' (required)'; color: var(--muted-ink); font-size: var(--step--1);}.now { margin: 0; font-family: 'Shantell Sans', system-ui, sans-serif;}.track { position: relative; flex: none; box-sizing: border-box; width: 3em; height: 1.65em; padding: 0; border: 1px solid var(--ink); border-radius: 999px; background: color-mix(in srgb, var(--switch-off) 20%, var(--paper)); cursor: pointer; transition: background 140ms ease;}.track[ui-checked] { background: color-mix(in srgb, var(--switch-on) 65%, var(--cream)); html.dark & { --ink: var(--on-accent); --paper: var(--cream); color: var(--on-accent); }}.thumb { position: absolute; inset-block: -1px; inset-inline-start: -1px; box-sizing: border-box; width: 1.65em; height: 1.65em; border: 1px solid var(--ink); border-radius: 999px; background: var(--paper); transition: translate 130ms ease;}.track[ui-checked] .thumb { translate: 1.35em 0;}.track:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.row[ui-disabled] { opacity: 0.55;}.row[ui-disabled] .track,.row[ui-disabled] .label { cursor: not-allowed;}@media (prefers-reduced-motion: reduce) { .row, .track, .thumb { transition: none; }}
Click the switch, or Tab to it and press Space. The line under the label follows the switch.
Clicking the words flips the switch too, because the label's for points at the trigger. A
screen reader hears a switch named "Email me when an order ships", and whether it is on.
When to use it
Use toggle for a setting that takes effect at once, such as notifications. If the choice only
counts when a form is sent, or it can be mixed, use checkbox instead.
Parts
The switch itself; the trigger, thumb, label, messages and field go inside it.
toggle.rootholds whether the switch is on, and what a form receives. It renders a<div>.toggle.triggeris the switch. It is a<button>withrole="switch"andaria-checked.toggle.thumbis a<span>for the moving dot. It does nothing on its own. Your CSS moves it.toggle.labelnames the switch. Clicking it flips the switch.toggle.descriptionis a help line. The switch points at it witharia-describedby.toggle.erroris an error message. The switch points at it too, before the help line.toggle.fieldis a hidden checkbox that sends the switch to a form. Tab never reaches it.
name, value and required go on the root, and toggle.field reads them from there. A form
receives value under name while the switch is on. If you leave value out, the form gets
on. A switch that is off or disabled sends nothing.
Styling hooks
| Attribute | On | Present when |
|---|---|---|
ui-checked |
root, trigger | The switch is on. |
ui-disabled |
root, trigger | disabled is set on the root. |
The thumb writes no attributes. Style it from its parent, such as
.track[ui-checked] .thumb { translate: 1.4em 0 }.
Keyboard
toggle.trigger is a native button, so it takes focus with Tab.
| Key | What it does |
|---|---|
| Space or Enter | Flips the switch. |
A disabled switch ignores clicks, Space and Enter. It never calls onChange.
Gotchas
API reference
toggle.root
checkedbooleanWhether the switch reads as on. Omit it and the switch starts off.Defaultfalse.disabledbooleanNobody can flip the switch, andtoggle.triggeris disabled outright.Defaultfalse.requiredbooleanMarks the hiddentoggle.fieldinput required, so a form refuses to submit with the switch off.Defaultfalse.namestringSubmitted under this name bytoggle.field.Default''.valuestringSubmitted instead of the browser default"on".Default'on'.onChange(checked: boolean) => voidCalled with the new value when a person flips the switch. Omit it and the switch still works; the call site simply does nothing.
toggle.label
No props of its own.
toggle.trigger
No props of its own.
toggle.thumb
No props of its own.
toggle.description
No props of its own.
toggle.error
No props of its own.
toggle.field
No props of its own.