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
  • Tinted rear windows

    £445

  • Dark exterior theme

    £195

  • Charging station cable, 6 m

    £270

Show codeHide code
import { Icon } from '@volvo-cars/react-icons';
import { useState } from 'react';
const accessories = [
{ name: 'Tinted rear windows', price: '£445' },
{ name: 'Dark exterior theme', price: '£195' },
{ name: 'Charging station cable, 6 m', price: '£270' },
];
function AccessoryRow({ name, price }: { name: string; price: string }) {
const [added, setAdded] = useState(false);
return (
<li className="list-item">
<div>
<div>
<h3 slot="title">{name}</h3>
<p slot="body">{added ? 'Added' : price}</p>
</div>
<button
slot="after"
type="button"
className="icon-toggle-button"
data-size="small"
aria-label={`Add ${name}`}
aria-pressed={added}
onClick={() => setAdded((value) => !value)}
>
<Icon
icon={added ? 'checkmark' : 'plus'}
size={16}
color="currentColor"
/>
</button>
</div>
</li>
);
}
export function IconToggleButtonList() {
return (
<ul className="w-xs max-w-full">
{accessories.map((accessory) => (
<AccessoryRow key={accessory.name} {...accessory} />
))}
</ul>
);
}
  • EX30

  • EX90

  • ES90

Show codeHide code
import { Icon } from '@volvo-cars/react-icons';
import { useState } from 'react';
const models = ['EX30', 'EX90', 'ES90'];
function FavoriteRow({ model }: { model: string }) {
const [favorite, setFavorite] = useState(false);
const [loading, setLoading] = useState(false);
function toggle() {
if (loading) return;
setLoading(true);
// Stands in for a request; the state only changes once it succeeds.
setTimeout(() => {
setFavorite((value) => !value);
setLoading(false);
}, 1000);
}
return (
<li className="list-item">
<div>
<p slot="body">{model}</p>
<button
slot="after"
type="button"
className="icon-toggle-button-clear"
aria-label={`Favorite ${model}`}
aria-pressed={favorite}
aria-disabled={loading || undefined}
data-loading={loading || undefined}
onClick={toggle}
>
<Icon icon="heart" filled={favorite} size={16} color="currentColor" />
{loading && (
<progress className="spinner" aria-label="Saving favorite" />
)}
</button>
</div>
</li>
);
}
export function IconToggleButtonClearList() {
return (
<ul className="w-xs max-w-full">
{models.map((model) => (
<FavoriteRow key={model} model={model} />
))}
</ul>
);
}