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>;}

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

toggle anatomy
Anatomy perspectiveFlat
Layers
Keep me in the loopA small note when something changes.
toggle.root

The switch itself; the trigger, thumb, label, messages and field go inside it.

  • toggle.root holds whether the switch is on, and what a form receives. It renders a <div>.
  • toggle.trigger is the switch. It is a <button> with role="switch" and aria-checked.
  • toggle.thumb is a <span> for the moving dot. It does nothing on its own. Your CSS moves it.
  • toggle.label names the switch. Clicking it flips the switch.
  • toggle.description is a help line. The switch points at it with aria-describedby.
  • toggle.error is an error message. The switch points at it too, before the help line.
  • toggle.field is 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

checkedboolean
Whether the switch reads as on. Omit it and the switch starts off. Default false.
disabledboolean
Nobody can flip the switch, and toggle.trigger is disabled outright. Default false.
requiredboolean
Marks the hidden toggle.field input required, so a form refuses to submit with the switch off. Default false.
namestring
Submitted under this name by toggle.field. Default ''.
valuestring
Submitted instead of the browser default "on". Default 'on'.
onChange(checked: boolean) => void
Called 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.