Media Slideshow
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.
Show 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> );}Composition
Section titled “Composition”| 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>Controls
Section titled “Controls”- 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
MediaSlideshowControlswith a filled, neutralIconButtonanddata-color-mode="dark", so they match the arrows on any media. See the custom controls example.
Aspect ratio
Section titled “Aspect ratio”Show 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.
Active slide
Section titled “Active slide”- Start on another slide with
defaultActiveIndex. - To follow a slide picked outside the slideshow, pass
activeIndexand update it fromonSlideChange. See the thumbnails example. - A hidden slideshow, such as in a closed dialog, starts on its active slide when it’s shown.
Animation
Section titled “Animation”Show 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,
goToandactiveIndex. Swipes always follow the user.
Show 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> );}MediaSlideshowPlayButtonplays and pauses the video in the active slide, and is left out on other slides. A video pauses when its slide is left.- Add
autoPlayfor short, silent films, and give the videomuted,loopandplaysInline. 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
autoplayattribute. 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, passisPlayingand play or pause it inonClick. - Give a video without native
controlstabIndex={-1}. Otherwise Firefox adds it to the Tab order, where it does nothing.
Loading and failed media
Section titled “Loading and failed media”Show 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 toMediaSlideshowItemto 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
MediaSlideshowFallbackto show your own content, as on the last slide here. - Each slide with media reports
data-status:loadingwhile its images load,loaded, orerror. - While the slides themselves load, show a
skeletonin the same aspect ratio:
<div className="skeleton aspect-16/9 w-full" aria-busy="true" aria-label="Loading images"/>Slides
Section titled “Slides”- Render each
MediaSlideshowItemdirectly inMediaSlideshowContent. Components and fragments that return items work. - Slides sit 2px apart with
gap-2, so the page shows between them as they move. Pass anothergap-*class toMediaSlideshowContentto change it. - Give images their real
widthandheight, and serve sizes that fit withsrcSetandsizes, 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 imagefetchPriority="high". - Keep slides to media. Put buttons and links in
MediaSlideshowControls, not in a slide.
Keyboard navigation
Section titled “Keyboard navigation”- 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.
Accessibility
Section titled “Accessibility”From the WCAG checklist:
- Labels:
aria-labelonMediaSlideshownames the slideshow. Give imagesalttext, 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.
MediaSlideshowCountis 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> );}MediaSlideshow
Section titled “MediaSlideshow”| Prop | Type | Required | Default |
|---|---|---|---|
children | ReactNode | - | - |
| `MediaSlideshowContent`, followed by any controls such as `MediaSlideshowControls`. Tab order follows this order, and the built-in previous and next arrows come last. | |||
className | string | - | - |
| Merged with the default slideshow root layout classes. | |||
aria-label | string | - | "Slideshow", in the page language |
| Accessible label for the slideshow, also used for its content. | |||
defaultActiveIndex | number | - | 0 |
| Index of the slide to show first. | |||
activeIndex | number | - | - |
| 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. | |||
MediaSlideshowContent
Section titled “MediaSlideshowContent”| Prop | Type | Required | Default |
|---|---|---|---|
className | string | - | - |
| 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. | |||
MediaSlideshowItem
Section titled “MediaSlideshowItem”| Prop | Type | Required | Default |
|---|---|---|---|
className | string | - | - |
| Merged with the default slideshow item layout classes. A `bg-*` class replaces the placeholder surface shown while media loads. | |||
aria-label | string | - | The slide position, such as "2 of 5" |
| Accessible label for the slide. | |||
MediaSlideshowFallback
Section titled “MediaSlideshowFallback”| Prop | Type | Required | Default |
|---|---|---|---|
className | string | - | - |
| Merged with the default fallback layout classes. | |||
MediaSlideshowControls
Section titled “MediaSlideshowControls”| Prop | Type | Required | Default |
|---|---|---|---|
className | string | - | - |
| Merged with the default slideshow controls layout classes. | |||
MediaSlideshowPlayButton
Section titled “MediaSlideshowPlayButton”| Prop | Type | Required | Default |
|---|---|---|---|
dir | string | - | - |
hidden | boolean | - | - |
id | string | - | - |
lang | string | - | - |
slot | string | - | - |
style | CSSProperties | - | - |
tabIndex | number | - | - |
title | string | - | - |
| Tooltip text, which defaults to the accessible label | |||
iconFilled | boolean | - | - |
| 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 | |||
disabled | boolean | - | - |
| Disables the button. Avoid using disabled buttons whenever possible - instead show error messages explaining the next steps the user should take. | |||
className | string | - | - |
| Merged with the default play button layout classes. | |||
aria-label | string | - | "Play video" or "Pause video", in the page lang... |
| Accessible label for the play button. | |||
autoPlay | boolean | - | 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`. | |||
isPlaying | boolean | - | - |
| Whether the video is playing, for a custom player. Leave it out to play and pause the `video` element in the active slide. | |||
onClick | MouseEventHandler<HTMLButtonElement> | - | - |
| Called on click. With `isPlaying`, start or pause your player here. Without it, call `event.preventDefault()` to stop the built-in playback. | |||








