Toggle Button
Toggle buttons switch between two persistent states, selected and unselected, for reversible actions such as enabling features or adding items to a selection.
Use toggle-button on a <button>. Selected and unselected toggles share the same backplate, so change the label and swap the icon to show the state.
Show code
import { Icon } from '@volvo-cars/react-icons';import { useState } from 'react';
export function ToggleButton() { const [added, setAdded] = useState(false);
return ( <button type="button" className="toggle-button" onClick={() => setAdded((value) => !value)} > <Icon icon={added ? 'checkmark' : 'plus'} size={16} color="currentColor" /> <span>{added ? 'Added' : 'Add'}</span> </button> );}Use toggle-button-clear when less emphasis is needed. It has no backplate, shows a state layer on hover and press, and comes in one size.
Show code
import { Icon } from '@volvo-cars/react-icons';import { useState } from 'react';
export function ToggleButtonClear() { const [saved, setSaved] = useState(false);
return ( <button type="button" className="toggle-button-clear" aria-pressed={saved} onClick={() => setSaved((value) => !value)} > <Icon icon="heart" filled={saved} size={16} color="currentColor" /> <span>Save</span> </button> );}For toggles with only an icon, use Icon Toggle Button.
Size system
Section titled “Size system”| Size | Attribute | Height | Usage |
|---|---|---|---|
| Large | Unset | 48px |
Touch-first interfaces or primary controls |
| Medium | data-size="medium" |
40px |
Standard UI usage |
| Small | data-size="small" |
32px |
Dense layouts, such as tables and toolbars |
Small keeps a 40px interaction target. toggle-button-clear is always 40px.
Show code
import { Icon } from '@volvo-cars/react-icons';
export function ToggleButtonSizes() { return ( <div className="flex flex-wrap items-center gap-16"> <button type="button" className="toggle-button" aria-pressed="false"> <Icon icon="plus" size={16} color="currentColor" /> <span>Large</span> </button>
<button type="button" className="toggle-button" aria-pressed="false" data-size="medium" > <Icon icon="plus" size={16} color="currentColor" /> <span>Medium</span> </button>
<button type="button" className="toggle-button" aria-pressed="false" data-size="small" > <Icon icon="plus" size={16} color="currentColor" /> <span>Small</span> </button> </div> );}Behavior
Section titled “Behavior”- Switches between two persistent states: selected and unselected.
- Updates immediately when activated.
- Shows the current state through a change of icon, or of label and icon.
- Stays selected until the user activates it again.
- Supports reversible actions without confirmation.
- Announces the state to assistive technologies through
aria-pressedwhen the label stays the same.
Label and pressed state
Section titled “Label and pressed state”If the label changes with the state, such as Add and Added, leave out aria-pressed. The label already describes the state, and screen readers would otherwise announce both, such as “Added, pressed”.
If the label stays the same, such as Save in the clear example, set aria-pressed and swap the icon.
Loading state behavior
Section titled “Loading state behavior”Set data-loading="true" and aria-disabled="true", and add a <progress class="spinner">. Wrap the label in an element so it hides while the spinner shows.
- The button is non-interactive while loading.
- The visual state stays the same until the request completes.
- On success, update the state.
- On failure, revert to the previous state.
Show code
<div class="flex flex-wrap gap-8 items-center"> <button type="button" class="toggle-button" aria-disabled="true" data-loading="true" > <img class="icon-sprite" width="16" height="16" role="presentation" aria-hidden="true" alt="" src="https://www.volvocars.com/static/shared/icons/v5/plus-16.svg" /> <span>Add</span> <progress class="spinner" aria-label="Loading"></progress> </button>
<button type="button" class="toggle-button-clear" aria-pressed="false" aria-disabled="true" data-loading="true" > <img class="icon-sprite" width="16" height="16" role="presentation" aria-hidden="true" alt="" src="https://www.volvocars.com/static/shared/icons/v5/heart-16.svg" /> <span>Save</span> <progress class="spinner" aria-label="Loading"></progress> </button></div>CSS reference
Section titled “CSS reference”| API | Description |
|---|---|
toggle-button |
Toggle button with a backplate. |
toggle-button-clear |
Toggle button without a backplate. |
aria-pressed |
Selected state, when the label stays the same. |
data-size |
Sets Medium or Small on toggle-button. Unset is Large. |
data-loading="true" |
Hides the content and shows the spinner. |
aria-disabled="true" |
Shows the disabled state. Suppress activation in application code. |
Accessibility
Section titled “Accessibility”Key consumer responsibilities from the WCAG audit:
- If the label changes with the state, leave out
aria-pressed. If the label stays the same, updatearia-pressedwhen the state changes. - Don’t use color alone to show the state. Swap the icon, or change the label.
- Keep a minimum
40×40pxtouch target. Small toggles provide it. - Announce meaningful results of async changes in application code, for example with a live region.