Switch
An on/off setting that submits with its form, such as Email notifications on a settings page. Renders a real <input type="checkbox" role="switch">. Put it inside aihio-field for a label and description; for a pressed state in a toolbar (Bold, Italic) use aihio-toggle instead.
Intents
form-field
toggle-state
Examples
Each one rendered live, over the markup that produces it.
In a field
Laid out as a row beside its label, with a description beneath.
<aihio-field>
<label slot="label">Email notifications</label>
<aihio-switch name="email-notifications" checked></aihio-switch>
<span slot="description">A digest of activity, sent weekly.</span>
</aihio-field>
Wrapped in a label
<label>
<aihio-switch name="remember"></aihio-switch>
Remember this device
</label>
Mistakes
Each one beside its fix, with what aihio lint says about it. The prompt fragment gives agents the
same pairs, and the build fails if the linter stops catching one or a fix stops passing.
The visible text is not associated with the switch, so it is announced as an unnamed switch.
Use aihio-field with <label slot="label">, or wrap both in a <label>.
Don't
<span>Email notifications</span>
<aihio-switch name="email-notifications"></aihio-switch>
aihio lint reports
-
switch-labelerror
Do
<aihio-field>
<label slot="label">Email notifications</label>
<aihio-switch name="email-notifications"></aihio-switch>
</aihio-field>
A switch named by its state is announced as "On, switch, on".
Name it after the setting it controls.
Don't
<aihio-switch aria-label="On" checked></aihio-switch>
aihio lint reports
-
switch-state-namewarn
Do
<aihio-switch aria-label="Email notifications" checked></aihio-switch>
Boolean attributes are on by presence. checked="false" starts the switch on; omit the attribute for off.
Don't
<aihio-switch checked="false" aria-label="Weekly summary"></aihio-switch>
aihio lint reports
-
boolean-attribute-valueerror
Do
<aihio-switch aria-label="Weekly summary"></aihio-switch>
API
Every attribute, property, method, and event the schema declares for aihio-switch.
Attributes
checked-
boolean
default
false - Whether the switch starts on. Like a native checkbox's checked attribute, this is the reset default; the checked property is live state.
disabled-
boolean
default
false - Disables the switch
required-
boolean
default
false - The form cannot submit until the switch is on
name- string
- Form field name. The switch submits name=value when on and nothing when off.
value-
string
default
on - Value submitted when the switch is on
form- string
- Id of an external form that owns the switch
Properties
checked-
boolean - Live on/off state
defaultChecked-
boolean - Reset state; reflects the checked attribute
control-
HTMLInputElement | nullread-only - The native checkbox the component delegates to
form-
HTMLFormElement | nullread-only - The owning form, or null outside one
validity-
ValidityState | nullread-only - Native constraint validation state
validationMessage-
stringread-only - Native validation message
willValidate-
booleanread-only - Whether the switch takes part in constraint validation
Methods
click(): void- Flips the switch, as a click on it would.
focus(options?: FocusOptions): void- Moves focus to the native checkbox.
checkValidity(): boolean- Runs native constraint validation.
reportValidity(): boolean- Runs native constraint validation and shows the browser's message.
setCustomValidity(message: string): void- Sets a custom validation message; an empty string clears it.
Events
aihio-change-
detail
{ checked: boolean } - Fired when the user turns the switch on or off. The native change and input events also bubble from the inner checkbox.
Composition
- Allowed children
- none
Accessibility
What you have to provide, and what the component already does. The obligations with a rule are checked by
aihio lint and by the dev build's console warnings.
Your obligations
-
error When the switch has no labelName the switch after the setting it controls: put it in
aihio-fieldwith a<label slot="label">, wrap it in a<label>, or give it aria-label. Never label it with its state ("On", "Enabled").Checked as
switch-label -
warn When the switch is inside a<form>and its state should be submittedSet name. A switch without a name is omitted from FormData entirely.
Checked as
switch-form-name -
warn When the switch is named by a state word (On, Off, Enabled)Name the switch after the setting it controls. The switch announces its own state, so a switch named "On" is read as "On, switch, on" and never says what is on.
Checked as
switch-state-name
Handled for you
- The component renders a real
<input type="checkbox" role="switch">, so Space toggles it, it is announced as a switch that is on or off, and focus and disabled semantics are native - Form participation is native: it submits name=value when on, resets with its form, and
<fieldset disabled>disables it - aria-label, aria-labelledby, aria-describedby, and aria-invalid set on the host are forwarded to the checkbox
- Inside
aihio-fieldthe label, description, and error are wired to the checkbox, and the field lays out as a row with the switch beside its label - On and off stay distinguishable under forced colours: the track keeps a border and the on state uses Highlight
In patterns
Canonical compositions that use aihio-switch.