Skip to content

Icon Toggle Button

Edit on GitHub

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 codeHide 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 codeHide 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 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 codeHide 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>
);
}
  • 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.

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 codeHide 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>
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.

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-pressed when the state changes.
  • Don’t use color alone to show the state. Swap the icon.
  • Keep a minimum 40×40px touch target. Small toggles provide it.
  • Add a tooltip when the icon’s meaning isn’t obvious.