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

Use the Media Slideshow to show images and videos one at a time. Slides snap into place as users swipe, scroll or use the arrows.

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,
MediaSlideshowControls,
MediaSlideshowCount,
MediaSlideshowItem,
} from '@volvo-cars/react-slideshow';
const images = [
{
src: '/images/cars/slideshow/ex60-front-three-quarter.avif',
srcSet:
'/images/cars/slideshow/ex60-front-three-quarter-960.avif 960w, /images/cars/slideshow/ex60-front-three-quarter.avif 1920w',
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',
srcSet:
'/images/cars/slideshow/ex60-side-960.avif 960w, /images/cars/slideshow/ex60-side.avif 1920w',
alt: 'Volvo EX60 parked by a wall, seen from the side',
},
{
src: '/images/cars/slideshow/ex60-rear.avif',
srcSet:
'/images/cars/slideshow/ex60-rear-960.avif 960w, /images/cars/slideshow/ex60-rear.avif 1920w',
alt: 'Volvo EX60 parked by a wall, seen from the rear',
},
{
src: '/images/cars/slideshow/ex60-front.avif',
srcSet:
'/images/cars/slideshow/ex60-front-960.avif 960w, /images/cars/slideshow/ex60-front.avif 1920w',
alt: 'Volvo EX60 parked by a wall, seen from the front',
},
];
export function BasicMediaSlideshow() {
return (
<MediaSlideshow aria-label="Volvo EX60 exterior">
<MediaSlideshowContent>
{images.map((image, index) => (
<MediaSlideshowItem key={image.src}>
<img
className="img object-contain w-full h-full"
src={image.src}
srcSet={image.srcSet}
sizes="auto, (min-width: 1024px) 720px, 100vw"
alt={image.alt}
width="1920"
height="1080"
// The first slide is the main image at the top of the page
fetchPriority={index === 0 ? 'high' : undefined}
loading={index === 0 ? undefined : 'lazy'}
/>
</MediaSlideshowItem>
))}
</MediaSlideshowContent>
<MediaSlideshowControls className="justify-end">
<MediaSlideshowCount />
</MediaSlideshowControls>
</MediaSlideshow>
);
}
Name Description
MediaSlideshow Root. Built-in arrows and shared state.
MediaSlideshowContent Scrollable container for the slides. Sets the aspect ratio.
MediaSlideshowItem One slide.
MediaSlideshowFallback Your own content for a slide whose media fails to load.
MediaSlideshowControls Bar for controls along the top of the frame.
MediaSlideshowCount Visible count, such as 2/5.
MediaSlideshowPlayButton Plays and pauses the video in the active slide.

Put the content first and controls after it, so Tab reaches the slides, then your controls, then the arrows.

<MediaSlideshow aria-label="Volvo EX60 exterior">
<MediaSlideshowContent>
<MediaSlideshowItem>{/* Image or video */}</MediaSlideshowItem>
</MediaSlideshowContent>
<MediaSlideshowControls>
<MediaSlideshowCount />
</MediaSlideshowControls>
<MediaSlideshowPlayButton />
</MediaSlideshow>
  • The previous and next arrows are built in. They’re left out with a single slide, and an arrow with nowhere to go is disabled.
  • Add your own controls to MediaSlideshowControls with a filled, neutral IconButton and data-color-mode="dark", so they match the arrows on any media. See the custom controls example.
Volvo EX60 Cross Country driving on a forest road
Volvo EX60 Cross Country parked by the sea, with three people nearby
Volvo EX60 Cross Country parked on a wet city street at dusk
Show codeHide code
import {
MediaSlideshow,
MediaSlideshowContent,
MediaSlideshowItem,
} from '@volvo-cars/react-slideshow';
const images = [
{
src: '/images/cars/slideshow/ex60-cross-country-forest.avif',
alt: 'Volvo EX60 Cross Country driving on a forest road',
},
{
src: '/images/cars/slideshow/ex60-cross-country-coast.avif',
alt: 'Volvo EX60 Cross Country parked by the sea, with three people nearby',
},
{
src: '/images/cars/slideshow/ex60-cross-country-dusk.avif',
alt: 'Volvo EX60 Cross Country parked on a wet city street at dusk',
},
];
export function MediaSlideshowAspectRatio() {
return (
<MediaSlideshow aria-label="Volvo EX60 Cross Country">
<MediaSlideshowContent className="aspect-4/5 md:aspect-21/9">
{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 === 0 ? undefined : 'lazy'}
/>
</MediaSlideshowItem>
))}
</MediaSlideshowContent>
</MediaSlideshow>
);
}

Every slide shares the frame of MediaSlideshowContent, 16:9 by default. Pass an aspect-* class to change it, such as aspect-4/5 md:aspect-21/9 here. Fill the frame with w-full h-full on the media, and object-cover or object-contain.

  • Start on another slide with defaultActiveIndex.
  • To follow a slide picked outside the slideshow, pass activeIndex and update it from onSlideChange. See the thumbnails example.
  • A hidden slideshow, such as in a closed dialog, starts on its active slide when it’s shown.
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,
MediaSlideshowControls,
MediaSlideshowCount,
MediaSlideshowItem,
} from '@volvo-cars/react-slideshow';
const images = [
{
src: '/images/cars/slideshow/ex60-front-three-quarter.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',
alt: 'Volvo EX60 parked by a wall, seen from the side',
},
{
src: '/images/cars/slideshow/ex60-rear.avif',
alt: 'Volvo EX60 parked by a wall, seen from the rear',
},
{
src: '/images/cars/slideshow/ex60-front.avif',
alt: 'Volvo EX60 parked by a wall, seen from the front',
},
];
export function MediaSlideshowWithoutAnimation() {
return (
<MediaSlideshow aria-label="Volvo EX60 views" scrollBehavior="instant">
<MediaSlideshowContent>
{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 === 0 ? undefined : 'lazy'}
/>
</MediaSlideshowItem>
))}
</MediaSlideshowContent>
<MediaSlideshowControls className="justify-end">
<MediaSlideshowCount />
</MediaSlideshowControls>
</MediaSlideshow>
);
}
  • Slides scroll smoothly into place, unless the user prefers reduced motion.
  • Pass scrollBehavior="instant" to change slides without animation, as here. Use it when slides jump far apart, such as from thumbnails.
  • It applies to the arrows, the arrow keys, goTo and activeIndex. Swipes always follow the user.
Volvo EX60 parked by a wall, seen from the side
Volvo EX60 parked by a wall, seen from the rear
Show codeHide code
import {
MediaSlideshow,
MediaSlideshowContent,
MediaSlideshowControls,
MediaSlideshowCount,
MediaSlideshowItem,
MediaSlideshowPlayButton,
} from '@volvo-cars/react-slideshow';
const slides = [
{
id: 'ex60-film',
type: 'video',
src: '/videos/ex60-exterior-film.mp4',
poster: '/images/cars/slideshow/ex60-film-poster.avif',
label: 'Volvo EX60 exterior film',
},
{
id: 'ex60-side',
type: 'image',
src: '/images/cars/slideshow/ex60-side.avif',
label: 'Volvo EX60 parked by a wall, seen from the side',
},
{
id: 'ex60-rear',
type: 'image',
src: '/images/cars/slideshow/ex60-rear.avif',
label: 'Volvo EX60 parked by a wall, seen from the rear',
},
];
export function MediaSlideshowWithImagesAndVideo() {
return (
<MediaSlideshow aria-label="Volvo EX60 film and photos">
<MediaSlideshowContent>
{slides.map((slide, index) => (
<MediaSlideshowItem key={slide.id}>
{slide.type === 'video' ? (
<video
className="block object-contain w-full h-full"
src={slide.src}
poster={slide.poster}
aria-label={slide.label}
// Without native controls the video does nothing on focus
tabIndex={-1}
preload="metadata"
playsInline
muted
loop
/>
) : (
<img
className="img object-contain w-full h-full"
src={slide.src}
alt={slide.label}
width="1920"
height="1080"
loading={index === 0 ? undefined : 'lazy'}
/>
)}
</MediaSlideshowItem>
))}
</MediaSlideshowContent>
<MediaSlideshowControls className="justify-end">
<MediaSlideshowCount />
</MediaSlideshowControls>
<MediaSlideshowPlayButton autoPlay />
</MediaSlideshow>
);
}
  • MediaSlideshowPlayButton plays and pauses the video in the active slide, and is left out on other slides. A video pauses when its slide is left.
  • Add autoPlay for short, silent films, and give the video muted, loop and playsInline. It plays while the slide is on screen, doesn’t start when the user prefers reduced motion, and stays paused once paused.
  • Don’t use the autoplay attribute. It ignores the slide and the user’s motion preference.
  • Don’t autoplay video with speech. Give it captions and the native controls.
  • Give videos a poster. For a custom player, pass isPlaying and play or pause it in onClick.
  • Give a video without native controls tabIndex={-1}. Otherwise Firefox adds it to the Tab order, where it does nothing.
Volvo EX60 parked by a wall in soft evening light, seen from the front at an angle
Volvo EX60 seen from the side
Volvo EX60 seen from the rear
Show codeHide code
import { Icon } from '@volvo-cars/react-icons';
import {
MediaSlideshow,
MediaSlideshowContent,
MediaSlideshowControls,
MediaSlideshowCount,
MediaSlideshowFallback,
MediaSlideshowItem,
MediaSlideshowPlayButton,
} from '@volvo-cars/react-slideshow';
export function MediaSlideshowFailedMedia() {
return (
<MediaSlideshow aria-label="Volvo EX60 with missing media">
<MediaSlideshowContent>
<MediaSlideshowItem>
<img
className="img object-cover w-full h-full"
src="/images/cars/slideshow/ex60-front-three-quarter.avif"
alt="Volvo EX60 parked by a wall in soft evening light, seen from the front at an angle"
width="1920"
height="1080"
/>
</MediaSlideshowItem>
{/* These files don't exist, to show what failed media looks like */}
<MediaSlideshowItem>
<img
className="img object-cover w-full h-full"
src="/images/cars/slideshow/ex60-missing.avif"
alt="Volvo EX60 seen from the side"
width="1920"
height="1080"
loading="lazy"
/>
</MediaSlideshowItem>
<MediaSlideshowItem>
<video
className="block object-cover w-full h-full"
src="/videos/ex60-missing.mp4"
aria-label="Volvo EX60 driving film"
tabIndex={-1}
preload="metadata"
playsInline
muted
/>
</MediaSlideshowItem>
<MediaSlideshowItem>
<img
className="img object-cover w-full h-full"
src="/images/cars/slideshow/ex60-missing-rear.avif"
alt="Volvo EX60 seen from the rear"
width="1920"
height="1080"
loading="lazy"
/>
<MediaSlideshowFallback className="flex-col gap-8 p-16 text-center">
<Icon icon="image-framed" size={48} />
<p className="font-14 text-secondary">Image not available</p>
</MediaSlideshowFallback>
</MediaSlideshowItem>
</MediaSlideshowContent>
<MediaSlideshowControls className="justify-end">
<MediaSlideshowCount />
</MediaSlideshowControls>
<MediaSlideshowPlayButton />
</MediaSlideshow>
);
}
  • Each slide shows a gray surface while its media loads. Pass a bg-* class to MediaSlideshowItem to change it. Don’t add a spinner to each slide.
  • Media that fails keeps its place and shows the surface with an image or video icon. Add MediaSlideshowFallback to show your own content, as on the last slide here.
  • Each slide with media reports data-status: loading while its images load, loaded, or error.
  • While the slides themselves load, show a skeleton in the same aspect ratio:
<div
className="skeleton aspect-16/9 w-full"
aria-busy="true"
aria-label="Loading images"
/>
  • Render each MediaSlideshowItem directly in MediaSlideshowContent. Components and fragments that return items work.
  • Slides sit 2px apart with gap-2, so the page shows between them as they move. Pass another gap-* class to MediaSlideshowContent to change it.
  • Give images their real width and height, and serve sizes that fit with srcSet and sizes, as in the example at the top of this page.
  • Load images after the first with loading="lazy". When the slideshow is the main image at the top of the page, give the first image fetchPriority="high".
  • Keep slides to media. Put buttons and links in MediaSlideshowControls, not in a slide.
  • Tab: moves to the slides, your controls and the arrows, skipping a disabled arrow.
  • Enter / Space on an arrow: changes the slide and keeps focus on the arrow.
  • Arrow Left / Right: changes the slide when the slides or an arrow have focus. On right-to-left pages, Arrow Left moves forward.

From the WCAG checklist:

  • Labels: aria-label on MediaSlideshow names the slideshow. Give images alt text, and videos captions where they have speech.
  • Slides: each slide is named by its position, such as “2 of 5”. Inactive slides are hidden from assistive technology and can’t be focused.
  • Announcements: slide changes are announced politely without moving focus. MediaSlideshowCount is hidden from screen readers.
  • Motion: an autoplaying video always has the play button to pause it. When the user prefers reduced motion, slides change without animation and videos don’t autoplay. scrollBehavior="instant" turns the animation off for everyone.
  • Language: built-in labels follow the page language. See Localization.

Use useMediaSlideshow in your own controls inside MediaSlideshow. It returns activeIndex, totalCount and goTo.

function SlideDots() {
const { activeIndex, totalCount, goTo } = useMediaSlideshow();
return (
<ul className="pagination-dots">
{Array.from({ length: totalCount }, (_, index) => (
<li key={index}>
<button
type="button"
aria-current={index === activeIndex}
onClick={() => goTo(index)}
>
<span className="sr-only">Show slide {index + 1}</span>
</button>
</li>
))}
</ul>
);
}
PropTypeRequiredDefault
childrenReactNode--
`MediaSlideshowContent`, followed by any controls such as `MediaSlideshowControls`. Tab order follows this order, and the built-in previous and next arrows come last.
classNamestring--
Merged with the default slideshow root layout classes.
aria-labelstring-"Slideshow", in the page language
Accessible label for the slideshow, also used for its content.
defaultActiveIndexnumber-0
Index of the slide to show first.
activeIndexnumber--
Index of the slide to show. When it changes, the slideshow shows that slide. Navigation inside the slideshow still happens and is reported through `onSlideChange`, so update `activeIndex` from it to stay in sync.
onSlideChange((event: { activeIndex: number; target: HTMLEle...--
Callback fired when navigation inside the slideshow changes the active slide. Changes to `activeIndex` do not call it.
scrollBehavior"smooth" | "instant"-smooth
How the arrows, the arrow keys, `goTo` and `activeIndex` move to a slide. `smooth` animates unless the user prefers reduced motion, and `instant` shows the slide without animating. Swipes always follow the user.
PropTypeRequiredDefault
classNamestring--
Merged with the default slideshow content layout classes. The content sets the aspect ratio of every slide, `aspect-16/9` by default, so pass another `aspect-*` class to change it for all slides. Slides sit 2px apart with `gap-2`, which another `gap-*` class replaces, and a `bg-*` class colors the gap.
PropTypeRequiredDefault
classNamestring--
Merged with the default slideshow item layout classes. A `bg-*` class replaces the placeholder surface shown while media loads.
aria-labelstring-The slide position, such as "2 of 5"
Accessible label for the slide.
PropTypeRequiredDefault
classNamestring--
Merged with the default fallback layout classes.
PropTypeRequiredDefault
classNamestring--
Merged with the default slideshow controls layout classes.
PropTypeRequiredDefault
dirstring--
hiddenboolean--
idstring--
langstring--
slotstring--
styleCSSProperties--
tabIndexnumber--
titlestring--
Tooltip text, which defaults to the accessible label
iconFilledboolean--
Use the filled variant of the icon, typically for active or selected states.
iconLoading"eager" | "lazy"-lazy
The loading strategy for the icon image.
size"medium" | "large"-medium
The size of the IconButton. - `'medium'` — 40 px button, 16 px icon - `'large'` — 48 px button, 24 px icon
disabledboolean--
Disables the button. Avoid using disabled buttons whenever possible - instead show error messages explaining the next steps the user should take.
classNamestring--
Merged with the default play button layout classes.
aria-labelstring-"Play video" or "Pause video", in the page lang...
Accessible label for the play button.
autoPlayboolean-false
Plays the muted video in the active slide by itself while the slide is on screen. It does not start when the user prefers reduced motion, and stops for good once the user pauses. Ignored with `isPlaying`.
isPlayingboolean--
Whether the video is playing, for a custom player. Leave it out to play and pause the `video` element in the active slide.
onClickMouseEventHandler<HTMLButtonElement>--
Called on click. With `isPlaying`, start or pause your player here. Without it, call `event.preventDefault()` to stop the built-in playback.