A toggle is a form element that allows users to select between two mutually exclusive states.
Usage
When to use
- To allow users to turn on and off two mutually exclusive options providing an immediate response.
When not to use
- When users could check and uncheck an option, consider Checkbox.
- When users need to select more than one option from a list, consider Checkbox.
- When only one choice can be selected, consider Radio buttons.
Layout
We recommend using vertical Toggle groups, especially with short option lists.
Error validation
For error validation recommendations, refer to the Form patterns documentation.
How to use this component
There are two ways to use the Toggle component, which is based on the HTML checkbox input. Both are the <i80-toggle> element:
- the base control: an
<i80-toggle>with your own<input>written inside it - the field: an
<i80-toggle>with alabelattribute — the<input>control, with label, helper text, and error messaging (in a wrapping container)
The group
Use a group — <i80-toggle-group> — when there are multiple related options to choose from, or a single one that needs to be presented with an extra legend. If there’s a single choice and no need for an extra legend, use a single field.
It’s unlikely you’ll need to use a Toggle group, instead consider using a Checkbox group.
The field
Use a single field when the user has a single choice to make. If there are multiple related choices, use a group.
The basic invocation creates:
- a
<label>element with aforattribute automatically associated with the inputIDattribute. - a
<input type="checkbox">control with an automatically generatedIDattribute.
<i80-toggle label="Enable cost estimation" name="demo-cost-estimate"></i80-toggle>
Input value
Pass a value.
<i80-toggle label="Enable cost estimation" name="demo-cost-estimate" value="enable"></i80-toggle>
Checked
Pass the standard HTML checked attribute to set the Toggle to “checked”.
<i80-toggle label="Enable cost estimation" name="demo-cost-estimate" value="enable" checked></i80-toggle>
Required vs. optional
Use the required and optional attributes to add a visual indication next to the legend text that the field is "required" or "optional".
Note: While the Toggle component is already normally required by the nature of its use for on/off selection, there may be rare instances in which a Toggle is turned off by default but requires the user to interact with it before submitting a form. For example, for accepting terms and conditions.
<i80-toggle label="Private" name="demo-private" required></i80-toggle>
<i80-toggle label="Private" name="demo-private" optional></i80-toggle>
Validation
To indicate a field is invalid, provide an error message: the error attribute.
Unlike the Text Input, Textarea and Select components, you don’t need to mark the control invalid as well — the error attribute is enough — because the Toggle doesn’t have an “invalid” visual state.
<i80-toggle label="I approve the changes." name="demo-approve-change" error="Error: it is necessary to explicitly approve the changes to continue."></i80-toggle>
Custom control ID
If needing a custom ID for the control instead of the one automatically generated by the component, write the <input> yourself inside the element with that id.
In this case all the internal references (id/for/aria-describedby) between the different parts of the field are still automatically generated and will use the custom ID provided.
<i80-toggle label="Enable cost estimation" helper-text="With this option enabled you will receive an approximate cost estimation.">
<input type="checkbox" name="demo-cost-estimate" id="my-control">
</i80-toggle>
HTML native attributes
The element passes name, value, checked, required, disabled and readonly on to the <input type="checkbox"> it creates. For any other native behavior, write the <input> yourself inside the element and it is kept as it is, with all its attributes.
<i80-toggle label="Enable cost estimation" name="demo-cost-estimate"></i80-toggle>
The base control
The base control is intended for rare cases where the field or the group can’t be used and a custom implementation is needed. Most of the details for the field also apply to it, but see the Component API for more details.
Write the <input type="checkbox"> inside the element with its own aria-label.
<i80-toggle>
<input type="checkbox" name="demo-cost-estimate" value="enable" aria-label="Enable cost estimation">
</i80-toggle>
Attributes
<i80-toggle> renders this component. Everything it understands:
| Attribute | Values | Default | Notes |
|---|---|---|---|
label, helper-text, error, invalid, name, value, checked, required, optional, disabled, readonly |
On i80-checkbox, i80-radio and i80-toggle. | ||
legend, helper-text, error, required, optional |
On i80-checkbox-group, i80-radio-group and i80-toggle-group. | ||
layout |
vertical · horizontal | vertical |
On i80-checkbox-group, i80-radio-group and i80-toggle-group. |
name |
text | On i80-radio-group: given to radios without a name. |
The tag keeps any native element you write inside it, so a server-rendered form, link or button works as it is. See Web Components for loading i80.js and the reference for these examples running.
Anatomy
A single toggle

| Element | Usage |
|---|---|
| Base control | Required |
| Label | Required |
| Helper text | Optional |
| Error message | Triggered by system |
A group of toggles

| Element | Usage |
|---|---|
| Legend | Optional |
| Helper text | Optional |
| Fields | At least one is required |
States

For general content recommendations, refer to the Primitives documentation.
Conformance rating
A toggle without a label
A toggle is not conformant until it has an accessible name. Give <i80-toggle> a label — a real <label for> pointing at the control — or, if the name is already somewhere else on the page, an aria-label.
The control itself is an <input type="checkbox"> with role="switch", and the element keeps its aria-checked in step, so a screen reader announces it as on or off rather than as checked or unchecked. Several toggles under one name belong in an <i80-toggle-group>, which renders a <fieldset> with the legend attribute as its <legend>.
Known issues
The helper text and the error are announced through aria-describedby, and a screen reader reads only their text. A link inside them is not announced as a link, so a keyboard user has no way to reach it from the announcement. Put links in the surrounding page content rather than in a label, helper text or error message.
Applicable WCAG Success Criteria
This section is for reference only, some descriptions have been truncated for brevity. This component intends to conform to the following WCAG Success Criteria:
-
1.3.1
Info and Relationships (Level A):
Information, structure, and relationships conveyed through presentation can be programmatically determined or are available in text. -
1.3.2
Meaningful Sequence (Level A):
When the sequence in which content is presented affects its meaning, a correct reading sequence can be programmatically determined. -
1.3.4
Orientation (Level AA):
Content does not restrict its view and operation to a single display orientation, such as portrait or landscape. -
1.3.5
Identify Input Purpose (Level AA):
The purpose of each input field collecting information about the user can be programmatically determined when the input field serves a purpose identified in the Input Purposes for User Interface Components section; and the content is implemented using technologies with support for identifying the expected meaning for form input data. -
1.4.1
Use of Color (Level A):
Color is not used as the only visual means of conveying information, indicating an action, prompting a response, or distinguishing a visual element. -
1.4.10
Reflow (Level AA):
Content can be presented without loss of information or functionality, and without requiring scrolling in two dimensions. -
1.4.11
Non-text Contrast (Level AA):
The visual presentation of the following have a contrast ratio of at least 3:1 against adjacent color(s): user interface components; graphical objects. -
1.4.12
Text Spacing (Level AA):
No loss of content or functionality occurs by setting all of the following and by changing no other style property: line height set to 1.5; spacing following paragraphs set to at least 2x the font size; letter-spacing set at least 0.12x of the font size, word spacing set to at least 0.16 times the font size. -
1.4.3
Minimum Contrast (Level AA):
The visual presentation of text and images of text has a contrast ratio of at least 4.5:1 -
1.4.4
Resize Text (Level AA):
Except for captions and images of text, text can be resized without assistive technology up to 200 percent without loss of content or functionality. -
2.4.6
Headings and Labels (Level AA):
Headings and labels describe topic or purpose. -
2.4.7
Focus Visible (Level AA):
Any keyboard operable user interface has a mode of operation where the keyboard focus indicator is visible. -
3.2.1
On Focus (Level A):
When any user interface component receives focus, it does not initiate a change of context. -
3.2.2
On Input (Level A):
Changing the setting of any user interface component does not automatically cause a change of context unless the user has been advised of the behavior before using the component. -
3.2.4
Consistent Identification (Level AA):
Components that have the same functionality within a set of Web pages are identified consistently. -
3.3.2
Labels or Instructions (Level A):
Labels or instructions are provided when content requires user input. -
4.1.2
Name, Role, Value (Level A):
For all user interface components, the name and role can be programmatically determined; states, properties, and values that can be set by the user can be programmatically set; and notification of changes to these items is available to user agents, including assistive technologies.
Support
If any accessibility issues have been found within this component, let us know by submitting an issue.
0.1.0
Første i80-version (fork af upstream, se core/UPSTREAM.md).