Carousel
Shows one slide at a time. Previous and next sit over the frame, and a dot for each slide sits on a glass pill. `loop` wraps from the last slide to the first.
React
Preview
Usage
When to use
- Show a few images or panels one at a time, such as photos of a place.
- Let the reader move through a short sequence with previous, next and a dot for each item.
When not to use
- A long list the reader scans. Use a list or Table.
- Steps of a flow the reader moves through. Use Stepper.
- One image. Use Image.
Platform support
What each platform has, from the spec. A selected platform is highlighted.
| Name | Web | iOS | Android | Note |
|---|---|---|---|---|
| label | Yes | Yes | Yes | |
| count | No | Yes | Yes | Only on iOS, Android. |
| index | Yes | Yes | Yes | |
| defaultIndex | Yes | Yes | Yes | |
| onIndexChange | Yes | Yes | Yes | |
| loop | Yes | Yes | Yes | |
| labels | Yes | No | No | Only on web. Not generated for iOS and Android. |
| value of keyboard: Tab | Yes | Deferred | Deferred | |
| value of keyboard: ArrowLeft, ArrowRight | Yes | Deferred | Deferred | |
| value of keyboard: Home, End | Yes | Deferred | Deferred | |
| value of keyboard: Enter, Space | Yes | Deferred | Deferred |
Examples
Places
Previous, next and a dot for each photo, on a glass pill.
Stops at the ends
With loop off, Previous is disabled on the first photo and Next on the last.
Do and don't
Do
- Name the region with label, in the reader's language.
- Keep the sequence short, about three to eight slides.
- Give each slide the same frame, such as one ratio, so the page does not jump.
Don't
- Don't use it for the steps of a flow.
- Don't put another carousel inside a slide.
- Don't autoplay. The reader moves the slides.
Props
| Name | Type | Values | Default | Description |
|---|---|---|---|---|
| label required | string | - | - | Names the carousel region for assistive technology. |
| count required | number | - | - | Native only: how many slides the carousel holds. Calls the slide slot with each index from 0 to count - 1. Only on iOS, Android. |
| index | number | - | - | The slide on screen, from 0. Set it, with onIndexChange, to control which slide shows. |
| defaultIndex | number | - | 0 | The slide shown first, from 0, when index is not set. |
| onIndexChange | (index: number) => void | - | - | Called with the next index when the slide changes. |
| loop | boolean | - | true | Wraps from the last slide to the first, and from the first to the last. Off, Previous is disabled on the first slide and Next on the last. |
| labels | { previous?: string; next?: string; indicators?: string; slide?: (current: number, total: number) => string; indicator?: (current: number) => string } | - | - | Words for the buttons and the slide names. Defaults: Previous slide, Next slide, Slides, 2 of 4, Show slide 2. Only on web. Not generated for iOS and Android. |
Slots
| Name | Required | Description |
|---|---|---|
| slides | yes | One node per slide, in order. Each is shown on its own. |
| slide | yes | Native only: the slide for one index, called for 0 up to count - 1. |
Anatomy
| Part | Description |
|---|---|
| root | The region named by label. |
| viewport | The frame that clips the slides, with container corners. |
| track | The row of slides. It moves by one frame width. |
| item | One slide. |
| step | Previous and next: round glass buttons over the frame. |
| hitArea | Transparent ::before on Previous and Next, never smaller than size-target-min. |
| pill | The glass pill along the bottom that holds the dots, with no gap between them. |
| dot | A fixed 16px button, on mouse and on touch. The 8px mark sits in the middle, so the space around it is the hit area. |
| icon | The dot drawn inside that button. |
Accessibility
Role: region
| Keys | Action |
|---|---|
| Tab | Moves through Previous, Next and the current dot. |
| ArrowLeft, ArrowRight | Shows the previous or next slide. In right to left, the arrows swap. |
| Home, End | Shows the first or last slide. |
| Enter, Space | Activates the focused button. |
- The region is named by label, with aria-roledescription carousel.
- Each slide is a group named like 2 of 4. A slide that is not showing is hidden and inert, so its contents are not in the tab order.
- The current dot has aria-current. The other dots stay in the group and are left out of the tab order.
- With loop off, Previous is disabled on the first slide and Next on the last.
- Previous and Next keep a size-target-min hit area. Each dot is a fixed 16px button with an 8px mark, on mouse and on touch, so the pill does not grow on a coarse pointer.
Install
npx werewolf-ui add carouselRun npx werewolf-ui init once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See Installation for requirements and the alternative registry setup.