ToggleSwitch
A sliding boolean input used to enable or disable options.
ToggleSwitch is similar to Checkbox, but meant for circumstances where the target can be "on" or "off" rather than "checked" or "unchecked."
For a single switch, the v-model
value will be a boolean true
when the switch is "on" and false
when "off".
For multiple switches, the v-model
value will be an array of the inputValue
props of any currently "on" switches (or an empty array if all switches are "off").
Attributes passed to input
This component will pass any HTML attributes applied to it, except for CSS class, to the <input>
element within the component.
Demos
Configurable
Name | Value |
---|---|
Props | |
alignSwitch | |
hideLabel | |
disabled | |
Slots | |
default | |
View | |
Reading direction |
Single switch with label and description
Always include label text via the default slot. You can also add description text via the #description
slot.
A single switch does not need an inputValue
prop. v-model
is bound to a boolean value: true
for "on", false
for "off".
Toggle switch value: false
In rare cases, a visible label is not necessary. You can use the hideLabel
prop to visually hide the label, which will still appear in the markup for screen reader accessibility.
Form field
When used in a form, a single ToggleSwitch or group of ToggleSwitches can be wrapped in the Field component to add features like a semantic label, description and help text, validation messages, and more. See the Field page for more information.
When building a ToggleSwitch field, always set isFieldset
to true
, even for a single ToggleSwitch, to ensure proper accessibility support. This wraps the group in a <fieldset>
element and labels it with an associated <legend>
.
If using a ToggleSwitch or ToggleSwitch group outside of a form, follow the instructions in the next demo.
ToggleSwitch group
For a group of related toggle switches, each ToggleSwitch component's v-model
will be bound to an array of the inputValue
props of the toggle switches that are currently "on".
Use the alignSwitch
prop to align all of the switches to the end of the container, creating a more streamlined look.
This demo shows what to do when using a ToggleSwitch group outside of a form:
- Wrap the group in an element with
role="group"
- Connect the group with a label via the
aria-labelledby
attribute
ToggleSwitch group value: [ "switch-1" ]
Usage
Props
Prop name | Description | Type | Default |
---|---|---|---|
modelValue | Current value of the toggle switch or toggle switch group. Provided by v-model binding in the parent component. | boolean|string[]|number[] | false |
inputValue | HTML "value" attribute to assign to the input element. Required for groups of ToggleSwitches. Can be omitted for single true/false switches. | string|number|boolean | false |
alignSwitch | Whether to align the switch to the end of the container. Useful for ToggleSwitch groups, where each switch should be aligned regardless of label length. | boolean | false |
hideLabel | Whether the label should be visually hidden. Note that this will also hide the description. | boolean | false |
disabled | Whether the disabled attribute should be added to the input. | boolean | false |
Events
Event name | Properties | Description |
---|---|---|
update:modelValue | modelValue boolean - The new model value | Emitted when modelValue changes. |
Slots
Name | Description | Bindings |
---|---|---|
default | Label text. | |
description | Short description text. |
CSS-only version
Markup structure
The structure below can be used for most cases. If you need a description or a visually-hidden label, see the sections on those features below.
With description
To add a description below the label:
- Add a span after the
<label>
element with an ID and classcdx-label__description
. Include the description text here. - Add class
cdx-label__label
to the<label>
element - Wrap the label and description in a div with classes
cdx-toggle-switch__label
andcdx-label
- Add an
aria-describedby
attribute to the<input>
element with the value of the ID of the description
With visually-hidden label
For a visually-hidden label:
- Add class
cdx-label__label
to the<label>
element - Wrap the label in a div with classes
cdx-toggle-switch__label
,cdx-label
, andcdx-label--visually-hidden
Initially on
The toggle switch appears "on" when the hidden checkbox is checked. To initially set the switch to "on", add the checked
attribute to the <input>
.
Disabled
To disable the toggle switch, add the disabled
attribute to the <input>
element.