Icon Toggle Button
Icon toggle buttons are compact controls that use only an icon to switch between two persistent states, selected and unselected.
@volvo-cars/css v3.3.1@volvo-cars/react-icons v4.0.2
Use icon-toggle-button on a <button> with an aria-label, and set aria-pressed. Selected and unselected toggles share the same backplate, so swap the icon to show the state.
Show code
import { Icon } from '@volvo-cars/react-icons';import { useState } from 'react';
export function IconToggleButton() { const [favorite, setFavorite] = useState(false);
return ( <button type="button" className="icon-toggle-button" aria-label="Favorite" aria-pressed={favorite} onClick={() => setFavorite((value) => !value)} > <Icon icon="heart" filled={favorite} size={16} color="currentColor" /> </button> );}Use icon-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 IconToggleButtonClear() { const [favorite, setFavorite] = useState(false);
return ( <button type="button" className="icon-toggle-button-clear" aria-label="Favorite" aria-pressed={favorite} onClick={() => setFavorite((value) => !value)} > <Icon icon="heart" filled={favorite} size={16} color="currentColor" /> </button> );}Size system
Section titled “Size system”| Size | Attribute | Size | 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. icon-toggle-button-clear is always 40px.
Show code
import { Icon } from '@volvo-cars/react-icons';
export function IconToggleButtonSizes() { return ( <div className="flex flex-wrap items-center gap-16"> <button type="button" className="icon-toggle-button" aria-label="Favorite" aria-pressed="false" > <Icon icon="heart" size={16} color="currentColor" /> </button>
<button type="button" className="icon-toggle-button" aria-label="Favorite" aria-pressed="false" data-size="medium" > <Icon icon="heart" size={16} color="currentColor" /> </button>
<button type="button" className="icon-toggle-button" aria-label="Favorite" aria-pressed="false" data-size="small" > <Icon icon="heart" size={16} color="currentColor" /> </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.
- Stays selected until the user activates it again.
- Supports reversible actions without confirmation.
- Announces the state to assistive technologies through
aria-pressed.
Loading state behavior
Section titled “Loading state behavior”Set data-loading="true" and aria-disabled="true", and add a <progress class="spinner">.
- 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="icon-toggle-button" aria-label="Favorite" aria-pressed="false" aria-disabled="true" data-loading="true" > <progress class="spinner" aria-label="Saving favorite"></progress> </button>
<button type="button" class="icon-toggle-button-clear" aria-label="Favorite" aria-pressed="false" aria-disabled="true" data-loading="true" > <progress class="spinner" aria-label="Saving favorite"></progress> </button></div>CSS reference
Section titled “CSS reference”| API | Description |
|---|---|
icon-toggle-button |
Icon toggle button with a backplate. |
icon-toggle-button-clear |
Icon toggle button without a backplate. |
aria-pressed |
Selected state. |
data-size |
Sets Medium or Small on icon-toggle-button. Unset is Large. |
data-loading="true" |
Hides the icon 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:
- Give every icon toggle an accessible name with
aria-label. Keep the name the same in both states. - Update
aria-pressedwhen the state changes. - Don’t use color alone to show the state. Swap the icon.
- Keep a minimum
40×40pxtouch target. Small toggles provide it. - Add a tooltip when the icon’s meaning isn’t obvious.