# Carousel

Pages through a set of images with previous/next and optional thumbnails; never auto-advances.

- Family: display
- Import: `import { Carousel, CarouselSlide } from "@senvenli/react/display";`
- Page: https://design.senvenli.com/docs/components/carousel
- JSON: https://design.senvenli.com/docs/components/carousel.json

## Example: Product photos

Previous and next, dots, arrow keys and swipe; it never advances by itself.

```tsx
import { Carousel } from "@senvenli/react/display";

// Flat placeholder photos drawn in the file, so the example needs no network.
const photo = (label: string, hue: number) =>
  `data:image/svg+xml,${encodeURIComponent(
    `<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900"><rect width="100%" height="100%" fill="hsl(${hue} 30% 62%)"/><text x="50%" y="50%" fill="hsl(${hue} 30% 20%)" font-family="system-ui" font-size="72" text-anchor="middle" dominant-baseline="middle">${label}</text></svg>`,
  )}`;

const PHOTOS = [
  { src: photo("Front view", 210), alt: "Front view" },
  { src: photo("Side view", 150), alt: "Side view" },
  { src: photo("Detail", 30), alt: "Detail of the oak top" },
];

export default function Example() {
  return (
    <div className="max-w-xl">
      <Carousel aria-label="Product photos" items={PHOTOS} />
    </div>
  );
}
```

## Examples

### With thumbnails

A strip under the frame picks an image directly; the dots then only indicate.

```tsx
import { Carousel } from "@senvenli/react/display";

// Flat placeholder photos drawn in the file, so the example needs no network.
const photo = (label: string, hue: number) =>
  `data:image/svg+xml,${encodeURIComponent(
    `<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900"><rect width="100%" height="100%" fill="hsl(${hue} 30% 62%)"/><text x="50%" y="50%" fill="hsl(${hue} 30% 20%)" font-family="system-ui" font-size="72" text-anchor="middle" dominant-baseline="middle">${label}</text></svg>`,
  )}`;

const PHOTOS = [
  { src: photo("Front view", 210), alt: "Front view" },
  { src: photo("Side view", 150), alt: "Side view" },
  { src: photo("Detail", 30), alt: "Detail of the oak top" },
];

export default function Example() {
  return (
    <div className="max-w-xl">
      <Carousel aria-label="Product photos" items={PHOTOS} showThumbnails aspectRatio="4/3" />
    </div>
  );
}
```

### Controlled

The page owns the shown image, here to caption it.

```tsx
import { Carousel, Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

// Flat placeholder photos drawn in the file, so the example needs no network.
const photo = (label: string, hue: number) =>
  `data:image/svg+xml,${encodeURIComponent(
    `<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900"><rect width="100%" height="100%" fill="hsl(${hue} 30% 62%)"/><text x="50%" y="50%" fill="hsl(${hue} 30% 20%)" font-family="system-ui" font-size="72" text-anchor="middle" dominant-baseline="middle">${label}</text></svg>`,
  )}`;

const PHOTOS = [
  { src: photo("Front view", 210), alt: "Front view" },
  { src: photo("Side view", 150), alt: "Side view" },
  { src: photo("Detail", 30), alt: "Detail of the oak top" },
];

export default function Example() {
  const [index, setIndex] = useState(1);
  return (
    <div className="max-w-xl">
      <Stack space="space-100">
        <Carousel aria-label="Product photos" items={PHOTOS} index={index} onIndexChange={setIndex} />
        <Text tone="subtle">{PHOTOS[index]?.alt}</Text>
      </Stack>
    </div>
  );
}
```

### Any content per slide

`CarouselSlide` children: cards with a heading and text instead of photos.

```tsx
import { Carousel, CarouselSlide, Heading, Text } from "@senvenli/react/display";

const QUOTES = [
  { id: "ada", quote: "We closed the month two days earlier than before.", name: "Ada Lovelace, Finance lead" },
  { id: "grace", quote: "Our support queue finally fits on one screen.", name: "Grace Hopper, Support" },
  { id: "alan", quote: "Setting it up took one afternoon.", name: "Alan Turing, Operations" },
];

export default function Example() {
  return (
    <div className="w-full max-w-lg">
      <Carousel aria-label="What customers say" aspectRatio="16/9">
        {QUOTES.map((item) => (
          <CarouselSlide key={item.id} id={item.id}>
            <div className="flex size-full flex-col items-center justify-center gap-ds-150 px-ds-600 text-center">
              <Heading level={3} size="heading-medium">
                “{item.quote}”
              </Heading>
              <Text tone="subtle">{item.name}</Text>
            </div>
          </CarouselSlide>
        ))}
      </Carousel>
    </div>
  );
}
```

## Guidelines

### When to use it

- A few photos of one item.

### Use something else when

- [image-viewer](https://design.senvenli.com/docs/components/image-viewer.md): Photos need to be seen full screen.

## Accessibility

Keyboard:

- Arrow left / right: Move to the previous or next image.

Built in:

- The position is announced as "Image N of M".

You provide:

- Meaningful alt text for each image.
- With CarouselSlide children: real alt text on images inside slides, and a label per slide when thumbnails are shown.

## API reference

### Carousel

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| items | `CarouselItem[]` |  | no | Images as data. Leave it out to compose `CarouselSlide`s as children instead (any content: a card, a quote). |
| children | `React.ReactNode` |  | no | With no `items`: `CarouselSlide`s, one per page. |
| aria-label | `string` |  | yes | Names the carousel ("Product photos"). |
| index | `number` |  | no | The shown image, controlled. The carousel calls `onIndexChange` and expects the new value back. |
| defaultIndex | `number` |  | no | The first image shown when uncontrolled. |
| onIndexChange | `(index: number) => void` |  | no |  |
| showThumbnails | `boolean` |  | no | A strip of thumbnails under the frame; the current one outlined. The dots then only indicate. With `CarouselSlide`s, shown only when every slide has a `thumbnail`. |
| aspectRatio | `"16/9" \| "4/3"` |  | no |  |
| isLandmark | `boolean` |  | no | A `region` landmark (default) for a gallery that is part of the page's outline; `false` inside repeated cards, so a listing does not fill the landmark list. |
| motion | `ComponentMotion` |  | no | `auto` (follow the device), `reduce` or `none`, for this component; the provider's setting otherwise. |

### CarouselSlide

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `string` |  | no | Stable key; the position by default. |
| label | `string` |  | no | The slide's name for its thumbnail ("Front view"); the position by default. |
| thumbnail | `string` |  | no | An image URL for the thumbnail strip. |
| children | `React.ReactNode` |  | yes | What the page shows: an image, a card, a quote. It fills the frame. |

### Tokens it reads

`--ds-background-neutral-bold`, `--ds-border-focused`, `--ds-border-selected`, `--ds-icon`, `--ds-icon-brand`, `--ds-icon-danger`, `--ds-icon-information`, `--ds-icon-subtle`, `--ds-icon-success`, `--ds-icon-warning`, `--ds-radius-full`, `--ds-radius-medium`, `--ds-radius-xlarge`, `--ds-shadow-overlay`, `--ds-size-icon`, `--ds-size-icon-large`, `--ds-size-icon-small`, `--ds-space-025`, `--ds-space-100`, `--ds-space-150`, `--ds-surface-hovered`, `--ds-surface-overlay`, `--ds-surface-sunken`, `--ds-text-inverse`

## Related

- [image-viewer](https://design.senvenli.com/docs/components/image-viewer.md)
- [grid-list](https://design.senvenli.com/docs/components/grid-list.md)
