Switch
A switch is an on/off control that represents a binary setting. <cinq-switch>
provides an accessible, keyboard-navigable interface following the
WAI-ARIA Authoring Practices switch pattern,
reads its state from the markup, takes part in forms like a checkbox, and
dispatches events when toggled.
Inspired by @19h47/switch.
Install
Section titled “Install”pnpm add @agencecinq/switchImport once:
import "@agencecinq/switch";Then write the WAI-ARIA markup. The host <cinq-switch> is the focusable switch
element. Set role="switch", aria-checked, and tabindex explicitly.
HTML is the source of truth. The component will not auto-set
role, auto-migrate attributes, or warn about missing labels. Run an a11y linter (axe, Lighthouse) to catch invalid markup.
<cinq-switch name="infravision" role="switch" aria-checked="false" tabindex="0"> <span class="label">Infravision</span> <span class="switch" aria-hidden="true"><span></span></span> <span class="on" aria-hidden="true">On</span> <span class="off" aria-hidden="true">Off</span></cinq-switch>When disabled, add disabled. As on a native field, the switch leaves the tab
order and the form, and is announced as disabled:
<cinq-switch role="switch" aria-checked="false" disabled tabindex="0"> <span class="label">Cursed item</span> <span class="switch" aria-hidden="true"><span></span></span></cinq-switch>The component upgrades automatically on connectedCallback. No manual
initialization is needed.
Required markup
Section titled “Required markup”| Attribute / element | Required | Role |
|---|---|---|
<cinq-switch> |
Yes | Host element with role="switch". |
role="switch" |
Yes | Identifies the control as a switch. |
aria-checked |
Yes | The on/off state ("true" or "false"), and the only one: style with it, and write it to change the state without events. |
tabindex="0" |
Yes | Makes the switch keyboard-focusable. disabled takes it out of the tab order. |
| Accessible name | Yes | Via aria-label, aria-labelledby, a <label>, or visible label text. |
name |
Optional | Submits the switch with its form. See Forms. |
aria-hidden="true" on On/Off text |
Recommended | Decorative state labels must not appear in the accessible name. |
Per the APG switch pattern,
the switch label must not change when its state changes. Style On/Off
decorative text with CSS and mark it aria-hidden="true".
The content of the host is presentational, as for any role="switch"
(ARIA): its text becomes the
accessible name, and it must hold nothing interactive. Put a help link next to
the switch, tied with aria-describedby.
For multiple switches, use a <fieldset> / <legend> (see Forms) or
role="group" with aria-labelledby.
Markup variants
Section titled “Markup variants”The playground below covers every supported markup pattern:
| Variant | When to use |
|---|---|
| Visible label text | Default. Text inside <cinq-switch> becomes the accessible name. |
aria-label |
Icon-only or compact switches with no visible label. |
aria-labelledby |
Name lives in a separate visible element (heading, table cell, etc.). |
<label> |
<label for> or a wrapping <label>, as on a native field. |
aria-describedby |
Extra static help text announced with the switch. |
| Minimal | No slider. UI-only toggle outside a form. |
| Pre-checked | aria-checked="true". |
| Form field | name (+ optional value) on the host. See Forms. |
| Switch group | <fieldset> / <legend> wrapping several switches. See Forms. |
| Disabled | disabled, or aria-disabled="true" alone to stay focusable and submitted. |
aria-label
Section titled “aria-label”<cinq-switch role="switch" aria-checked="false" tabindex="0" aria-label="Battle cries"> <span class="slider" aria-hidden="true"></span></cinq-switch>aria-labelledby
Section titled “aria-labelledby”<span id="prefs-reduced-motion">Show THAC0</span>
<cinq-switch role="switch" aria-checked="false" tabindex="0" aria-labelledby="prefs-reduced-motion"> <span class="slider" aria-hidden="true"></span></cinq-switch>aria-describedby
Section titled “aria-describedby”<cinq-switch role="switch" aria-checked="false" tabindex="0" aria-describedby="push-desc"> <span class="label">Torch light</span> <span class="slider" aria-hidden="true"></span></cinq-switch>
<p id="push-desc">Keeps a dim light source active while the party explores underground.</p>Minimal (no form)
Section titled “Minimal (no form)”<cinq-switch class="autoplay-switch" role="switch" aria-checked="false" tabindex="0"> Auto-map corridors</cinq-switch>.autoplay-switch { padding: 0.5rem 0.75rem; border: 1px solid #d1d5db; border-radius: 0.5rem; color: #6b7280;}
.autoplay-switch[aria-checked="true"] { border-color: #111827; background-color: #111827; color: #fff; font-weight: 600;}Pre-checked
Section titled “Pre-checked”<cinq-switch role="switch" aria-checked="true" tabindex="0"> <span class="label">Sage's gazette</span> <span class="slider" aria-hidden="true"></span></cinq-switch>aria-checked is the only state: style the on switch with
cinq-switch[aria-checked="true"]. It is also the state form.reset() brings back.
<cinq-switch> is a form-associated custom element, and behaves like a
checkbox. Inside a <form>, a switch with a name submits its value while
on ("on" without a value attribute), and nothing while off. form.reset()
brings back the aria-checked of the markup, and the browser restores the
state when the user comes back to the page. disabled, on the switch or on a
<fieldset> around it, leaves it out of the form.
Name it and group it as any field: a <label for> or a wrapping <label>
names the switch and toggles it on click, and a <fieldset disabled> disables
every switch inside.
<form> <fieldset> <legend>Notifications</legend>
<label for="loot">Loot found</label> <cinq-switch id="loot" name="loot" role="switch" aria-checked="true" tabindex="0"> <span class="switch" aria-hidden="true"><span></span></span> </cinq-switch>
<label> Level up <cinq-switch name="level_up" value="yes" role="switch" aria-checked="false" tabindex="0"> <span class="switch" aria-hidden="true"><span></span></span> </cinq-switch> </label> </fieldset></form>new FormData(form); // loot=on, then level_up=yes once it is onThe Like a native field playground demo runs this form, with a reset and a disabled fieldset.
Styling
Section titled “Styling”Style the on state with its attributes, and the rest with native pseudo-classes:
| Selector | When |
|---|---|
[aria-checked="true"] |
The switch is on |
:disabled |
disabled on the switch, or on a <fieldset> around it |
:focus-visible |
Keyboard focus |
cinq-switch:disabled { opacity: 0.5; cursor: not-allowed;}:disabled does not match aria-disabled="true" alone: add
[aria-disabled="true"] to the selector if you use it.
The switch has no custom state:
on, off and disabled are already in the markup or in :disabled, and a change
is synchronous, so nothing waits in between.
Accessibility
Section titled “Accessibility”Accessible label is required
Section titled “Accessible label is required”The switch must have an accessible name. Use one of:
aria-label="Battle cries"on<cinq-switch>aria-labelledby="some-id"referencing a visible label- a
<label for>pointing to the switch, or a<label>around it - Visible label text inside the slotted content (when it provides the name)
The component does not check this. Rely on axe-core / Lighthouse / your CI.
Keyboard support
Section titled “Keyboard support”| Key | Function |
|---|---|
Tab |
Moves keyboard focus to the switch (tabindex="0" in markup). |
Space |
Toggles between on and off when focused. |
Enter |
Toggles between on and off when focused (optional per APG). |
Style focus with native pseudo-classes:
cinq-switch:focus-visible { outline: 2px solid currentColor; outline-offset: 2px;}Attributes
Section titled “Attributes”| Attribute | Required | Description |
|---|---|---|
disabled |
No | Disables the switch: out of the tab order and the form, announced as disabled, blurred when set while focused. A disabled <fieldset> does the same. |
name |
No | Form field name. Without it, the switch is not submitted. |
value |
No | Submitted while on. Defaults to "on". |
Methods
Section titled “Methods”| Method | Description |
|---|---|
init() |
Binds listeners. Runs on connect. Call destroy() first if already bound. |
activate(emit?) |
Turns the switch on. Dispatches an event by default. |
deactivate(emit?) |
Turns the switch off. Dispatches an event by default. |
toggle() |
Toggles on/off. Returns false if disabled or a cancelable event was aborted. |
destroy() |
Detaches listeners. Runs on disconnect. |
Properties
Section titled “Properties”| Property | Description |
|---|---|
checked |
Whether the switch is currently on. |
disabled |
Whether the switch is disabled, by disabled, aria-disabled="true" or a disabled <fieldset>. |
value |
The value submitted while on. |
Events
Section titled “Events”| Event | Cancelable | Detail | Description |
|---|---|---|---|
switch:activate |
Yes | { el } |
Fired before the switch turns on. Cancel to abort. |
switch:deactivate |
Yes | { el } |
Fired before the switch turns off. Cancel to abort. |
import { EVENTS } from "@agencecinq/utils";
$switch.addEventListener(EVENTS.SWITCH_ACTIVATE, (event) => { console.log("activated", event.detail.el);});
$switch.addEventListener(EVENTS.SWITCH_DEACTIVATE, (event) => { console.log("deactivated", event.detail.el);});Call event.preventDefault() to abort before the state changes:
$switch.addEventListener(EVENTS.SWITCH_ACTIVATE, (event) => { if (!userMayEnable()) { event.preventDefault(); }});Programmatic API
Section titled “Programmatic API”const $switch = document.querySelector("cinq-switch");
$switch.activate();$switch.deactivate();$switch.toggle();activate() and deactivate() accept an optional emit flag (default true).
Pass false to update state without dispatching an event. The Programmatic API
playground section includes a silent sync button.
Advanced settings panel
Section titled “Advanced settings panel”The Campaign settings (advanced) playground demo combines:
- form submit with multiple switches, and
form.reset()for the defaults - a master switch that enables/disables a child via
disabled - preset loading with
activate(false)(no events) - locking the whole panel with a disabled
<fieldset>
Examples
Section titled “Examples”Basic switch
Click or press Space / Enter to toggle. Activate and deactivate events are logged below.
Last event: -
On / Off labels
A larger track can reveal On and Off text inside the slider. Style with [aria-checked="true"] on the host.
State: off
Accessible naming
Per the APG, the switch needs an accessible name via visible text, aria-label,aria-labelledby, or a <label> (see Like a native field).
aria-labelNo visible label. Name comes from aria-label.
aria-labelledbyThe visible heading above is referenced by aria-labelledby.
Minimal markup
No slider. The visible text inside the host provides the accessible name. Style the on state with [aria-checked="true"].
Pre-checked state
Set aria-checked="true" in markup for an initially on switch. It is also the state form.reset() brings back.
Description
Use aria-describedby when additional static help text should be announced with the switch.
Keeps a dim light source active while the party explores underground.
Like a native field
Plain HTML, no script: with a name, the switch submits itsvalue while on ("on" by default), and Reset brings back the markup state. A <label for> or a wrapping <label>names the switch and toggles it on click, a <fieldset> with a<legend> groups it, and <fieldset disabled>disables every switch inside. The output lists new FormData(form).
Submit the form to read FormData.
Disabled
disabled works as on a native field: the switch leaves the tab order and the form, matches :disabled, and is announced as disabled.aria-disabled="true" alone keeps it focusable and submitted.
disabledaria-disabled onlyProgrammatic API
Call activate(), deactivate(), or toggle() from external controls.
State: off
Events:none(silent sync skips them)
Cancellable events
Activate and deactivate events fire before the state changes. Call event.preventDefault() to abort. Useful for confirmations or gating access.
Last attempt: -
Campaign settings (advanced)
Form submit, a master switch that enables/disables a child, preset loading via activate(false), form.reset(), and a disabled<fieldset> that locks the whole panel.
Toggle house rules, load a preset, or submit the form.