A toggle, or "switch", is a button control element that allows the user to make a binary selection.
Preview
Usage
The Toggle component acts as a way to allow the User to switch between two mutually exclusive options. While it technically mirrors a Checkbox state, its effect on the system is immediate—much like a light switch immediately turns on or off the lights. In contrast, the checked state of a Checkbox won't be applied until a separate action to confirm the selection is taken.
Do
- When its action has an instantaneous effect.
Best Practices
- A Toggle component should be used as a control within an application and provide a way to toggle between two states like a household light switch.
- An
indeterminateToggle convey a "mixed" state that neither qualifies as toggled or not toggled. An example use case is when a Toggle acts as a "parent" of a collection of child Toggle components:- Toggled: all children are toggled.
- Not toggled: all children are not toggled.
indeterminate: children are a mix of toggled and not toggled.
Variants and Examples
Base Styles
Sizes
With v-model
Vue API
Slots
Props
Events
Classes
Accessibility
This component uses a native button element under the hood that has a role switch and type button to improve accessibility.
See W3C guidelines
for more information.
The best accessibility is semantic HTML. Most screen readers understand how to parse buttons if they’re correctly formatted. When it comes to toggles, there are a few things to keep in mind:
- All toggle buttons should have an
idattribute. - Associate toggle labels with their buttons using the
forattribute. This correlates with the toggle'sid. - If you have a group of related toggles, use a
fieldsetto group them and alegendto title the group. For further information, please visit Gov.UK’s article, "Using the fieldset and legend elements".
Button
The button element should be fully accessible by keyboard. To accomplish this, the DtToggle component automatically
populates several ARIA attributes to the underlying button element depending on the checked and
disabled states. However, if a label default slot is not used with DtToggle
(without including an aria-label), a console warning error will be thrown indicating that this issue exists.
Focus & Keyboard
The button element should capture keyboard focus as long as it is not disabled (disabled prop is true).
ENTER key will
emit a change event with the current value of the toggle and will change its internal checked state.