Skip to content

Toggle Button

Edit on GitHub

Toggle buttons switch between two persistent states, selected and unselected, for reversible actions such as enabling features or adding items to a selection.

@volvo-cars/css v3.3.1@volvo-cars/react-icons v4.0.2

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 codeHide 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 codeHide 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 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 codeHide 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>
);
}
  • 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-pressed when the label stays the same.

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.

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

Key consumer responsibilities from the WCAG audit:

  • If the label changes with the state, leave out aria-pressed. If the label stays the same, update aria-pressed when the state changes.
  • Don’t use color alone to show the state. Swap the icon, or change the label.
  • Keep a minimum 40×40px touch target. Small toggles provide it.
  • Announce meaningful results of async changes in application code, for example with a live region.