# 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.

**Dependencies:** npm: 1 direct, 2 total; registry: 3 direct, 3 total.

Native: iOS stable, Android stable.

## 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.

### Native

Swipe between slides and tap any dot on the glass pill.

**Places**

```text
WerewolfCarousel(count: 4, label: "Places") { index in
    Text("Slide \(index + 1)")
        .frame(maxWidth: .infinity, maxHeight: .infinity)
}
```

```text
WerewolfCarousel(count = 4, label = "Places") { index ->
    WerewolfText("Slide ${index + 1}")
}
```

With loop on, swiping past the last slide wraps to the first.

**Wraps around**

```text
WerewolfCarousel(count: 3, label: "Places", loop: true) { index in
    Text("Slide \(index + 1)")
        .frame(maxWidth: .infinity, maxHeight: .infinity)
}
```

```text
WerewolfCarousel(count = 3, label = "Places", loop = true) { index ->
    WerewolfText("Slide ${index + 1}")
}
```

## Examples

### Places

Previous, next and a dot for each photo, on a glass pill.

```tsx
<Carousel label="Places" className="w-full">
  <Image src={places.coast.src} alt={places.coast.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
  <Image src={places.ridge.src} alt={places.ridge.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
  <Image src={places.lake.src} alt={places.lake.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
  <Image src={places.cabin.src} alt={places.cabin.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
</Carousel>
```

### Stops at the ends

With loop off, Previous is disabled on the first photo and Next on the last.

```tsx
<Carousel label="Places" loop={false} className="w-full">
  <Image src={places.coast.src} alt={places.coast.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
  <Image src={places.ridge.src} alt={places.ridge.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
  <Image src={places.lake.src} alt={places.lake.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
</Carousel>
```

## 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

```bash
npx werewolf-ui add carousel
```

Run `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](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add carousel --platform ios
npx werewolf-ui add carousel --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
