Carousel

A row of slides that scrolls and snaps into place. It is plain scrolling with scroll-snap, so touch, trackpad and keyboard work as the browser does them, and the buttons and dots move the same scroll.

General

Each child is a slide. Name the carousel with aria-label.

Several slides

slidesToShow sets how many are in view, and gap the space between them. The dots count the positions the first slide can take.

Use Image with a fixed height and objectFit: 'cover' so every photo fills its slide the same way, and describe each one in alt.

Several photos

Show a few photos at once and let the rest scroll in. A smaller height keeps the row compact.

Destination cards

A photo, a name and a price in each Card, with two cards in view. Put the name and the price on separate lines.

Controlled

Use value for the first slide in view and onChange to hear about it, both from the buttons and from the scroll.

Customer quotes

One quote at a time, with who said it. Hide the buttons with controls when the dots are enough.

APIs

Carousel.Props

AttributeDescriptionTypeAccepted valuesDefault
slidesToShowslides in view at oncenumber-1
gapspace between the slidesstringany CSS length1rem
controlsshow the previous and next buttonsboolean-true
indicatorsshow a dot for each positionboolean-true
valueindex of the first slide in view, to control itnumber--
initialValueindex of the first slide in view on the first rendernumber-0
onChangecalled with the index of the slide the scroll rests on(index: number) => void--
previousLabelname of the previous buttonstring-Previous slide
nextLabelname of the next buttonstring-Next slide
slideLabelname of each slide for screen readers(index: number, count: number) => string-1 of 5
indicatorLabelname of each dot(index: number) => string-Go to slide 1
...native props, passed to the root divHTMLAttributes'aria-label', 'id', ...-

Behavior

  • • Scroll: swiping, the trackpad, the arrow keys on the focused list and the buttons all move the same scroll. onChange is called once the scroll stops, not on every pixel.
  • • Motion: the scroll is smooth, unless the user asked for less motion in the system.
  • • Ends: the buttons are disabled on the first and the last position. There is no loop.
  • • Controls: when every slide fits, the buttons and dots are not shown.

Ref

The ref points at the root div.

Accessibility

The root is a region with aria-roledescription="carousel", and each slide is a group named by its position, like "2 of 5". Name the carousel with aria-label. The scrolling list can be focused, so the keyboard can scroll it, and the buttons and dots are real buttons with names you can translate. The dot of the current position has aria-current.

Edit this page on GitHub