Skip to content

Media Slideshow

Edit on GitHub

Browse one image or piece of content at a time with the Media Slideshow component.

@volvo-cars/css v3.3.1@volvo-cars/react-headless v0.27.2@volvo-cars/react-slideshow v0.0.2
Volvo EX60 parked by a wall in soft evening light, seen from the front at an angle
Volvo EX60 parked by a wall, seen from the side
Volvo EX60 parked by a wall, seen from the rear
Volvo EX60 parked by a wall, seen from the front
Show codeHide code
import {
MediaSlideshow,
MediaSlideshowContent,
MediaSlideshowItem,
} from '@volvo-cars/react-slideshow';
import { useState } from 'react';
const images = [
{
src: '/images/cars/slideshow/ex60-front-three-quarter.avif',
thumbnail: '/images/cars/slideshow/ex60-front-three-quarter-960.avif',
alt: 'Volvo EX60 parked by a wall in soft evening light, seen from the front at an angle',
},
{
src: '/images/cars/slideshow/ex60-side.avif',
thumbnail: '/images/cars/slideshow/ex60-side-960.avif',
alt: 'Volvo EX60 parked by a wall, seen from the side',
},
{
src: '/images/cars/slideshow/ex60-rear.avif',
thumbnail: '/images/cars/slideshow/ex60-rear-960.avif',
alt: 'Volvo EX60 parked by a wall, seen from the rear',
},
{
src: '/images/cars/slideshow/ex60-front.avif',
thumbnail: '/images/cars/slideshow/ex60-front-960.avif',
alt: 'Volvo EX60 parked by a wall, seen from the front',
},
];
export function MediaSlideshowWithThumbnails() {
const [activeIndex, setActiveIndex] = useState(1);
return (
<div>
<MediaSlideshow
aria-label="Volvo EX60 exterior"
activeIndex={activeIndex}
onSlideChange={({ activeIndex }) => setActiveIndex(activeIndex)}
>
<MediaSlideshowContent className="aspect-4/3">
{images.map((image, index) => (
<MediaSlideshowItem key={image.src}>
<img
className="img object-cover w-full h-full"
src={image.src}
alt={image.alt}
width="1920"
height="1080"
loading={index === activeIndex ? undefined : 'lazy'}
/>
</MediaSlideshowItem>
))}
</MediaSlideshowContent>
</MediaSlideshow>
{/* Thumbnails sit 2px apart. Focus fills that gap, so it shows against
the page and not over the images */}
<div className="flex flex-wrap gap-2 mt-2">
{images.map((image, index) => {
const isActive = index === activeIndex;
return (
<button
key={image.src}
type="button"
className="relative w-64"
style={{ outlineOffset: 0 }}
aria-current={isActive || undefined}
onClick={() => setActiveIndex(index)}
>
{/* The smaller file, so thumbnails don't download every full size slide */}
<img
className="img object-cover w-full aspect-4/3"
src={image.thumbnail}
alt={image.alt}
width="960"
height="540"
/>
{isActive && (
// Two rings inside the edge, so it shows on any image and
// stays apart from the single focus ring in the gap
<span className="absolute top-0 start-0 w-full h-full border-2 border-primary">
<span className="block w-full h-full border-2 border-always-white" />
</span>
)}
</button>
);
})}
</div>
</div>
);
}

Keep thumbnails below the slideshow in sync with activeIndex and onSlideChange. This example starts on the second image and marks the active thumbnail with aria-current. Thumbnails use small image files, so they don’t download every slide.

Thumbnails sit 2px apart with gap-2. The focus ring fills that gap, so it shows against the page and not over the images. The selected thumbnail has two rings inside its edge, so it stays distinct from focus.

Volvo EX60 seen from the front at an angle
Volvo EX60 seen from the side
Volvo EX60 seen from the rear at an angle

Volvo EX60 from every angle

Show codeHide code
import { useDialog } from '@volvo-cars/react-headless';
import { IconButton } from '@volvo-cars/react-icons';
import {
MediaSlideshow,
MediaSlideshowContent,
MediaSlideshowControls,
MediaSlideshowCount,
MediaSlideshowItem,
} from '@volvo-cars/react-slideshow';
import { useCallback, useId, useState } from 'react';
const title = 'Volvo EX60 from every angle';
const images = [
{
src: '/images/cars/slideshow/ex60-studio-front-three-quarter.avif',
alt: 'Volvo EX60 seen from the front at an angle',
},
{
src: '/images/cars/slideshow/ex60-studio-side.avif',
alt: 'Volvo EX60 seen from the side',
},
{
src: '/images/cars/slideshow/ex60-studio-rear-three-quarter.avif',
alt: 'Volvo EX60 seen from the rear at an angle',
},
];
function Slides({
startIndex,
className,
}: {
startIndex: number;
className?: string;
}) {
return (
<MediaSlideshowContent className={className}>
{images.map((image, index) => (
<MediaSlideshowItem key={image.src}>
<img
className="img object-contain w-full h-full"
src={image.src}
alt={image.alt}
width="1920"
height="1080"
loading={index === startIndex ? undefined : 'lazy'}
/>
</MediaSlideshowItem>
))}
</MediaSlideshowContent>
);
}
export function MediaSlideshowCustomControls() {
const titleId = useId();
const [activeIndex, setActiveIndex] = useState(0);
const [isExpanded, setIsExpanded] = useState(false);
const onToggle = useCallback(
({ open }: { open: boolean }) => setIsExpanded(open),
[],
);
const { closeDialog, dialogProps, showDialog } = useDialog({ onToggle });
return (
<>
<MediaSlideshow
aria-label={title}
activeIndex={activeIndex}
onSlideChange={({ activeIndex }) => setActiveIndex(activeIndex)}
>
<Slides startIndex={0} />
<MediaSlideshowControls className="items-center justify-between">
<MediaSlideshowCount />
<IconButton
icon="maximise"
variant="filled"
color="neutral"
data-color-mode="dark"
aria-label="View larger"
title="View larger"
onClick={showDialog}
/>
</MediaSlideshowControls>
</MediaSlideshow>
<dialog
className="dialog-large w-xl"
aria-labelledby={titleId}
{...dialogProps}
>
<header slot="header">
<div slot="close">
<IconButton
bleed
icon="x"
aria-label="Close"
variant="clear"
onClick={closeDialog}
/>
</div>
<h2 id={titleId} className="font-16">
{title}
</h2>
</header>
{/* The slideshow fills the space under the header, so slides fit
any viewport */}
<div slot="main" className="flex-col py-0">
{/* Mounted once the dialog is open, so it can measure its slides
and start on the active one */}
{isExpanded && (
<MediaSlideshow
className="flex-grow min-h-0"
aria-label={title}
defaultActiveIndex={activeIndex}
onSlideChange={({ activeIndex }) => setActiveIndex(activeIndex)}
>
<Slides startIndex={activeIndex} className="h-full" />
<MediaSlideshowControls className="justify-end">
<MediaSlideshowCount />
</MediaSlideshowControls>
</MediaSlideshow>
)}
</div>
</dialog>
</>
);
}

Add your own controls to MediaSlideshowControls, such as a View larger button next to the count. It opens a dialog-large w-xl with a larger slideshow that fills the dialog and starts on the active slide. Both slideshows share activeIndex, so closing the dialog keeps your place. The larger slideshow mounts when the dialog opens, so its media loads only then.