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

    £445

  • Dark exterior theme

    £195

Show codeHide code
import { Icon } from '@volvo-cars/react-icons';
import { useId, useState } from 'react';
const accessories = [
{
name: 'Tinted rear windows',
price: '£445',
image: '/images/cars/thumbnails/tinted-rear-windows-ex60.avif',
},
{
name: 'Dark exterior theme',
price: '£195',
image: '/images/cars/thumbnails/dark-exterior-design-theme-ex60.avif',
},
];
function AccessoryRow({
name,
price,
image,
}: {
name: string;
price: string;
image: string;
}) {
const [added, setAdded] = useState(false);
const titleId = useId();
const labelId = useId();
return (
<li className="list-item">
<img
slot="before"
src={image}
alt=""
className="img aspect-1/1 object-cover"
/>
<div>
<div>
<h3 slot="title" id={titleId}>
{name}
</h3>
<p slot="body">{price}</p>
</div>
<button
slot="after"
type="button"
className="toggle-button"
data-size="small"
aria-labelledby={`${labelId} ${titleId}`}
onClick={() => setAdded((value) => !value)}
>
<Icon
icon={added ? 'checkmark' : 'plus'}
size={16}
color="currentColor"
/>
<span id={labelId}>{added ? 'Added' : 'Add'}</span>
</button>
</div>
</li>
);
}
export function ToggleButtonSmallList() {
return (
<ul className="w-xs max-w-full">
{accessories.map((accessory) => (
<AccessoryRow key={accessory.name} {...accessory} />
))}
</ul>
);
}

Charging station cable, 6 m

£270

22" 5-Y spoke wheels

£1,800

Show codeHide code
import { Icon } from '@volvo-cars/react-icons';
import { useId, useState } from 'react';
const accessories = [
{
name: 'Charging station cable, 6 m',
price: '£270',
image: '/images/cars/thumbnails/charging-cable-transparent.avif',
},
{
name: '22" 5-Y spoke wheels',
price: '£1,800',
image: '/images/cars/thumbnails/wheel-with-padding.avif',
},
];
function AccessoryCard({
name,
price,
image,
}: {
name: string;
price: string;
image: string;
}) {
const [added, setAdded] = useState(false);
const titleId = useId();
const labelId = useId();
return (
<div className="flex-col items-start gap-16">
<img
src={image}
alt=""
className="img aspect-4/3 object-contain bg-secondary w-full"
/>
<div>
<h3 className="font-medium" id={titleId}>
{name}
</h3>
<p className="text-secondary">{price}</p>
</div>
<button
type="button"
className="toggle-button"
data-size="medium"
aria-labelledby={`${labelId} ${titleId}`}
onClick={() => setAdded((value) => !value)}
>
<Icon
icon={added ? 'checkmark' : 'plus'}
size={16}
color="currentColor"
/>
<span id={labelId}>{added ? 'Added' : 'Add'}</span>
</button>
</div>
);
}
export function ToggleButtonMediumCards() {
return (
<div className="grid-cols-2 gap-x-8 w-sm max-w-full">
{accessories.map((accessory) => (
<AccessoryCard key={accessory.name} {...accessory} />
))}
</div>
);
}

Charging station cable, 6 m

£270

Connect your car to public charging stations.

Show codeHide code
import { Icon } from '@volvo-cars/react-icons';
import { useId, useState } from 'react';
export function ToggleButtonLargeDetails() {
const [added, setAdded] = useState(false);
const titleId = useId();
const labelId = useId();
return (
<div className="flex-col items-start gap-16 w-xs max-w-full">
<img
src="/images/cars/thumbnails/charging-cable-transparent.avif"
alt=""
className="img aspect-4/3 object-contain bg-secondary w-full"
/>
<div>
<h3 className="font-20" id={titleId}>
Charging station cable, 6 m
</h3>
<p className="text-secondary">£270</p>
</div>
<p>Connect your car to public charging stations.</p>
<button
type="button"
className="toggle-button"
aria-labelledby={`${labelId} ${titleId}`}
onClick={() => setAdded((value) => !value)}
>
<Icon
icon={added ? 'checkmark' : 'plus'}
size={16}
color="currentColor"
/>
<span id={labelId}>{added ? 'Added' : 'Add'}</span>
</button>
</div>
);
}
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>
);
}
Show codeHide code
<div class="flex flex-wrap gap-8 items-center">
<button type="button" class="toggle-button" aria-disabled="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"
/>
Add
</button>
<button
type="button"
class="toggle-button-clear"
aria-pressed="false"
aria-disabled="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"
/>
Save
</button>
</div>