Fragiola

API: components

Switch

An on/off control the user toggles, with native form support and a value while off, following the APG's switch pattern, in each framework.

An on/off control (WAI-ARIA APG switch pattern): a switch role whose state is on or off, never mixed. It works in a native <form>: it submits, resets and validates as an <input type="checkbox"> would, and can submit a value while off (uncheckedValue). A read-only switch is focusable and submits, but does not toggle.

Quick settings
Gallery

Behaviour

  • Toggle: a click on the Root or on its <label>, Space or Enter (the APG's optional key).
  • Read-only: readOnly keeps it focusable and submitted; a press or a key does not toggle it.
  • Forms: a visually hidden checkbox input right after the Root submits name=value while on (value defaults to "on"). While off, nothing, or name=uncheckedValue when you give uncheckedValue (a type="hidden" input, rendered only then). required blocks the submission while off and focuses the Root; a form reset returns an uncontrolled switch to its default, and a controlled one reports the default through onCheckedChange.
  • Thumb: always rendered; your CSS moves it from data-checked.
  • In a Field, as a Checkbox: the Root takes the field's ids, name, disabled and states, the Field's Label names it, and validate receives checked.

Anatomy

partelementdata-primitives-partrole
Rootbuttonswitch-rootthe switch; holds its state
Thumbspanswitch-thumbthe moving part
(hidden input)input type="checkbox"switch-inputwhat the form submits while on
(unchecked input)input type="hidden"switch-unchecked-inputwhile off, with uncheckedValue: submits it

Usage

import { Switch } from "@fragiola/primitives-react/switch";

<label>
    <Switch.Root name="bluetooth" value="yes" uncheckedValue="no" onCheckedChange={(on) => console.log(on)}>
        <Switch.Thumb />
    </Switch.Root>
    Bluetooth
</label>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue binds them the same way (v-model:checked, @checked-change), and Angular as inputs and outputs ([(checked)], (checkedChangeDetails)).

Root

proptypedefaultdescription
checkedbooleanthe state, controlled
defaultCheckedbooleanfalsethe initial state, uncontrolled
onCheckedChange(checked: boolean, details: { reason }) => voidevery change (reason none)
disabledbooleanfalseignores clicks and keys
requiredbooleanfalsethe form cannot submit while off
readOnlybooleanfalsefocusable and submitted, but a press or a key does not toggle it
namestringthe form name (a field's wins)
valuestring"on"submitted while on
uncheckedValuestringsubmitted while off; without it, nothing is
idstringgeneratedthe button's id

Thumb

No props of its own.

State and part attributes

partattributes
Rootrole="switch", type="button", aria-checked ("true" or "false"), aria-required="true" when required, aria-readonly="true" when read-only, disabled and data-disabled when disabled, data-checked / data-unchecked (exactly one), data-readonly, data-required; in a Field, the field's states
Thumbdata-checked / data-unchecked, data-disabled, data-readonly, data-required
hidden inputtype="checkbox", name, value, checked, required, disabled, aria-hidden="true", tabindex="-1", visually hidden
unchecked inputtype="hidden", name, value (the uncheckedValue), disabled when disabled

Keyboard

keyaction
Spacetoggles
Entertoggles
Tabmoves on; every switch is a tab stop

A read-only switch ignores Space and Enter.

Exports

entryexports
@fragiola/primitives-react/switchChangeDetails, ChangeReason, RenderProp, Switch, SwitchRootProps, SwitchState, SwitchThumbProps
@fragiola/primitives-vue/switchChangeDetails, ChangeReason, RenderSlotProps, Switch, SwitchRootEmits, SwitchRootProps, SwitchState
@fragiola/primitives-angular/switchChangeDetails, ChangeReason, SwitchRoot, SwitchState, SwitchThumb, ValueChangeEvent