Carousel

Slant / Layout

Slides that snap into place, with previous and next buttons that respect reduced motion.

Demo

Usage

@tuttn/slant
import { Carousel } from '@tuttn/slant'

// Uncontrolled: starts at defaultValue (0 when omitted), the first slide in view.
<Carousel label="Release highlights">
  <Carousel.Slide><Highlight id="checkpoints" /></Carousel.Slide>
  <Carousel.Slide><Highlight id="review" /></Carousel.Slide>
  <Carousel.Slide><Highlight id="handoff" /></Carousel.Slide>
</Carousel>

// Controlled, two at a time, wrapping at the ends, without indicators.
<Carousel label="Screenshots" value={index} onValueChange={setIndex} perView={2} loop indicators={false}>
  {shots.map(shot => <Carousel.Slide key={shot.id} aria-label={shot.caption}><img src={shot.src} alt="" /></Carousel.Slide>)}
</Carousel>

// Autoplay: on to the next slide every 5 seconds (or every n ms), wrapping at
// the end, with a play/pause button. Uncontrolled it starts playing, unless
// defaultPlaying={false} or the reader prefers reduced motion.
<Carousel label="Customer stories" autoplay>{stories}</Carousel>
<Carousel label="Customer stories" autoplay={8000} playing={playing} onPlayingChange={setPlaying}>{stories}</Carousel>

Props

PropTypeDefaultDescription
labelstringmessages.carousel ("Carousel")The carousel's accessible name.
valuenumber—The first slide fully in view (0-based), when controlled. Changing it scrolls the track to that slide.
defaultValuenumber0The first slide in view when uncontrolled (0-based).
onValueChange(index: number) => void—Called with the new index (0-based) when previous, next, an indicator, an arrow key, the reader's own scrolling or autoplay brings another slide to the front.
autoplayboolean | numberfalseMoves on to the next position by itself, wrapping from the last to the first: `true` every 5000 ms, or a number of milliseconds (0 or less is off). It pauses while the pointer is over the carousel, while focus is inside it, and while the page is hidden or the carousel off screen, and adds a play/pause button as the carousel's first control.
playingboolean—Whether autoplay is on, when controlled. A pause for the pointer, focus or a hidden page leaves it as it is.
defaultPlayingbooleantrueWhether autoplay starts on, when uncontrolled. While the reader prefers reduced motion it starts off, until they press play.
onPlayingChange(playing: boolean) => void—Called with the requested state when the play/pause button is pressed.
perViewnumber1How many slides are visible at once. A fraction, such as 1.2, lets the next slide peek in.
indicatorsbooleantrueShows a row of buttons, one per position, that jump to a slide.
loopbooleanfalseNext wraps from the last position to the first, and previous from the first to the last.
childrenReactNode—The `Carousel.Slide`s.
Carousel.Slide …restHTMLAttributes—className, style, other attributes and the ref go on the slide's div. Each slide is a group named "2 of 5" (messages.slideOf) unless you give it aria-label or aria-labelledby.
…restHTMLAttributes—className, style, other attributes and the ref go on the root <section>. The root sets --sl-carousel-per-view in its style; set --sl-carousel-gap to change the space between slides.

Keyboard

Tabmoves through the play/pause button (with autoplay; always first), the track, the slides in view, the indicators, previous and next
Left / Righton the focused track or a focused slide, the previous or next slide (swapped under RTL)
Enter / Spacepresses the focused button: play/pause, an indicator, previous or next

Accessibility

  • The root is a <section> with aria-roledescription="carousel", named by label; each slide is a group with aria-roledescription="slide", named “2 of 5” (messages.slideOf) unless you name it.
  • Slides out of view are inert, and the track is a polite live region, so a slide that comes into view is announced.
  • With autoplay, a play/pause button comes first in the tab order, named by what a press does: messages.stopRotation (“Stop automatic slide show”) while playing, messages.startRotation while stopped. The track’s live region is off while the carousel rotates by itself, so moves it makes alone aren’t read out.
  • Autoplay pauses while the pointer is over the carousel, while focus is anywhere inside it (except on the play/pause button), and while the page is hidden or the carousel off screen. Under prefers-reduced-motion an uncontrolled carousel starts stopped.

The APG carousel: a <section> with aria-roledescription="carousel" and a label, whose slides are groups with aria-roledescription="slide". The value is the first slide fully in view. Previous and next are disabled at the ends unless loop; a focused one that disables itself hands focus to the other. The indicators, one per position, mark the current one with aria-current.

The track is a scroll-snap container, so touch and trackpads scroll it too, and the value follows that scrolling. With focus on the track (or on a slide that takes focus), Left and Right move to the previous and next slide, swapped under right-to-left text. Moving scrolls only the track, smoothly unless the reader prefers reduced motion.

Slides out of view are inert, so Tab, a screen reader's virtual cursor and the track's polite live region only meet the slides on screen; one coming into view is announced.

Autoplay

autoplay moves on to the next position every 5000 ms, or every autoplay ms when it's a number, wrapping from the last to the first as with loop; each move calls onValueChange. Every move, the reader's or its own, starts the wait over, so a slide the reader brings up gets the whole interval. It pauses, without changing playing, while the pointer is over the carousel, while focus is inside it, and while the page is hidden or the carousel scrolled off screen, and goes on (from a fresh interval) when that ends. The play/pause button itself is left out of the pointer and focus pauses, so pressing play starts it at once.

The play/pause button is a ghost icon button, first in the DOM so it's the first control Tab reaches, and drawn at the start of the controls row. playing, defaultPlaying and onPlayingChange hold its state. Under prefers-reduced-motion an uncontrolled carousel starts stopped (without calling onPlayingChange), until the reader presses play; a controlled playing is followed as given. No timer runs before mount or after unmount, and the first render (on the server and in hydration) never rotates.