---
name: Carousel
type: ATM
section: data-display
why: Native scroll-snap track with prev/next controls - scrolling, swiping, and snapping stay browser features.
when: Presenting a finite set of large items (hero slides, galleries) one or a few at a time.
where: dist/components/carousel/carousel.css + dist/components/carousel/carousel.js
supportedStates: default
---

# Carousel

## Native basis

CSS `scroll-snap` on an overflow container, with `IntersectionObserver` for active-slide tracking and native `<button>` elements for navigation.

## Native Web APIs

- [`scroll-snap-type`](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type) - mandatory snap scrolling for horizontal and vertical axes
- [`scroll-snap-align`](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-align) - snap-point alignment per slide
- [`scroll-snap-stop`](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-stop) - forces snap to each slide (prevents fast-scroll skip)
- [`overscroll-behavior`](https://developer.mozilla.org/en-US/docs/Web/CSS/overscroll-behavior) - prevents scroll chaining to parent
- [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver) - tracks which slide is currently visible without scroll listeners
- [`:focus-visible`](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible) - keyboard-only focus rings on buttons and dots
- [`prefers-reduced-motion`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion) - suppresses smooth scroll and transitions
- [`prefers-contrast`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-contrast) - increases border widths for high-contrast mode
- [`forced-colors`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/forced-colors) - Windows High Contrast Mode support
- [WAI-ARIA Carousel pattern](https://www.w3.org/WAI/ARIA/apg/patterns/carousel/) - `role="region"`, `aria-roledescription="carousel"`, slide groups

## Structure

### Default (horizontal, single slide)
```html
<div class="carousel" role="region" aria-roledescription="carousel" aria-label="Featured slides">
  <div class="carousel-viewport" aria-live="polite">
    <div class="carousel-slide" role="group" aria-roledescription="slide" aria-label="1 of 5">
      <!-- slide content -->
    </div>
    <div class="carousel-slide" role="group" aria-roledescription="slide" aria-label="2 of 5">
      <!-- slide content -->
    </div>
    <!-- more slides -->
  </div>
  <button class="carousel-prev" aria-label="Previous slide">
    <i data-lucide="chevron-left"></i>
  </button>
  <button class="carousel-next" aria-label="Next slide">
    <i data-lucide="chevron-right"></i>
  </button>
</div>
```

### With dot indicators
```html
<div class="carousel" role="region" aria-roledescription="carousel" aria-label="Gallery">
  <div class="carousel-viewport" aria-live="polite">
    <div class="carousel-slide" role="group" aria-roledescription="slide" aria-label="1 of 3">…</div>
    <div class="carousel-slide" role="group" aria-roledescription="slide" aria-label="2 of 3">…</div>
    <div class="carousel-slide" role="group" aria-roledescription="slide" aria-label="3 of 3">…</div>
  </div>
  <button class="carousel-prev" aria-label="Previous slide"><i data-lucide="chevron-left"></i></button>
  <button class="carousel-next" aria-label="Next slide"><i data-lucide="chevron-right"></i></button>
  <div class="carousel-dots" role="group" aria-label="Choose slide to display">
    <!-- dots are auto-generated by JS if this container is empty -->
  </div>
</div>
```

### With counter
```html
<div class="carousel" role="region" aria-roledescription="carousel" aria-label="Slides">
  <div class="carousel-viewport" aria-live="polite">…</div>
  <button class="carousel-prev" aria-label="Previous slide"><i data-lucide="chevron-left"></i></button>
  <button class="carousel-next" aria-label="Next slide"><i data-lucide="chevron-right"></i></button>
  <div class="carousel-counter" aria-live="polite">Slide 1 of 5</div>
</div>
```

### Vertical
```html
<div class="carousel" data-orientation="vertical" role="region" aria-roledescription="carousel" aria-label="Vertical slides">
  <div class="carousel-viewport" aria-live="polite" style="height: 16rem;">
    <div class="carousel-slide" role="group" aria-roledescription="slide" aria-label="1 of 3">…</div>
    <div class="carousel-slide" role="group" aria-roledescription="slide" aria-label="2 of 3">…</div>
    <div class="carousel-slide" role="group" aria-roledescription="slide" aria-label="3 of 3">…</div>
  </div>
  <button class="carousel-prev" aria-label="Previous slide"><i data-lucide="chevron-up"></i></button>
  <button class="carousel-next" aria-label="Next slide"><i data-lucide="chevron-down"></i></button>
</div>
```

### Loop
Add `data-loop` to the carousel container to enable infinite circular navigation:
```html
<div class="carousel" data-loop role="region" aria-roledescription="carousel" aria-label="Looping slides">
  …
</div>
```

### Autoplay
Add `data-autoplay="3000"` (milliseconds) to auto-advance slides. Auto-rotation pauses on hover and focus per WAI-ARIA requirements:
```html
<div class="carousel" data-autoplay="3000" data-loop role="region" aria-roledescription="carousel" aria-label="Auto-rotating slides">
  …
</div>
```

### Multi-slide sizing
Set `flex-basis` on `.carousel-slide` to control per-slide width:
```html
<!-- 3 slides visible -->
<div class="carousel-slide" style="flex: 0 0 calc(33.333% - 0.667rem);" role="group" aria-roledescription="slide" aria-label="1 of 9">…</div>

<!-- 2 slides visible -->
<div class="carousel-slide" style="flex: 0 0 calc(50% - 0.5rem);" role="group" aria-roledescription="slide" aria-label="1 of 6">…</div>
```

With several slides in view, Prev / Next (and the arrow keys) move by the number of slides in view, the current
slide (`getState().config.index`, the active dot) is the first one shown, Next disables once the last slide shows,
and `.carousel-counter` reads "Slides 1-3 of 9".

## Variants

| Attribute | Value | Behavior |
| --- | --- | --- |
| `data-orientation` | *(omitted)* | Horizontal scrolling; Left/Right arrows |
| `data-orientation` | `vertical` | Vertical scrolling; Up/Down arrows |
| `data-loop` | *(presence)* | Enables circular/infinite navigation |
| `data-autoplay` | `"3000"` | Auto-advances every N ms; pauses on hover/focus |

## ARIA

| Attribute | Element | Purpose |
| --- | --- | --- |
| `role="region"` | `.carousel` | Identifies the carousel landmark |
| `aria-roledescription="carousel"` | `.carousel` | Announces as "carousel" instead of "region" |
| `aria-label` | `.carousel` | Accessible name (should not include "carousel") |
| `role="group"` | `.carousel-slide` | Groups slide content |
| `aria-roledescription="slide"` | `.carousel-slide` | Announces as "slide" instead of "group" |
| `aria-label="X of Y"` | `.carousel-slide` | Positional label for each slide |
| `aria-live="polite"` | `.carousel-viewport` | Announces slide changes (set to `off` during autoplay) |
| `aria-label` | `.carousel-prev` | "Previous slide" |
| `aria-label` | `.carousel-next` | "Next slide" |
| `disabled` | `.carousel-prev/.next` | Set when at boundary (non-loop only) |
| `aria-current="true"` | `.carousel-dot` | Indicates active dot |
| `aria-label="Go to slide N"` | `.carousel-dot` | Purpose of each dot button |

## Keyboard

| Key | Action |
| --- | --- |
| `←` / `→` | Previous / Next slide (horizontal) |
| `↑` / `↓` | Previous / Next slide (vertical) |
| `Home` | Jump to first slide |
| `End` | Jump to last slide |
| `Tab` | Moves focus through interactive elements (buttons, dots) |

## States

The carousel's observable state is the visible slide. Declared states:
`default` (`{ index }` scrolls to that slide, loop-aware).
`getState().config.index` reports the live index.

```js
document.querySelector('#gallery').api.setState('default', { index: 2 });
document.querySelector('#gallery').api.getState(); // { name: 'default', config: { index: 2 } }
```

The api is bound per carousel; the registry global is
`df$.shadcn.carouselApi` / `df$.shadcn.carouselStates`.

## API

<!-- generated from the source by `bun run api-docs` - edit the JSDoc and the types in the .ts, not this section -->

### States

<code>type CarouselState = 'default'</code> - `setState(name, config)` takes the config of the state it names (`CarouselStateConfigs[name]`).

| State | Description |
|---|---|
| `default` | The carousel showing one slide. <b>config</b> <table><tr><th>Field</th><th>Type</th><th>Description</th></tr><tr><td><code>index?</code></td><td><code>number</code></td><td>the slide to show, 0-based (default 0)</td></tr></table> |

### Every element

| Member | Description |
|---|---|
| <code>el.api.setState&lt;S extends CarouselState&gt;(name: S, config?: CarouselStateConfigs[S]): unknown</code> | Enter a state: the DOM work runs (also when it is the current state), the store records it. <table><tr><th>Argument</th><th>Type</th><th>Description</th></tr><tr><td><code>name</code></td><td><code>S</code></td><td>a declared state (an unknown name throws)</td></tr><tr><td><code>config?</code></td><td><code>CarouselStateConfigs[S]</code></td><td>that state's config (merged into the stored one when the component merges)</td></tr></table> <b>Returns</b> <code>unknown</code> - what the state's DOM work returned - a Promise for an async state (or await settled()) |
| <code>el.api.getState(): { name: CarouselState; config: CarouselStateConfigs[CarouselState]; model?: ElementModel }</code> | The state the element shows now - read back from the DOM, so it includes what the user changed. <b>Returns</b> <code>{ name: CarouselState; config: CarouselStateConfigs[CarouselState]; model?: ElementModel }</code> - the state's name, its config and the authored markup model render() starts from |
| <code>el.api.render(state?: { name: CarouselState; config: CarouselStateConfigs[CarouselState]; model?: ElementModel }): string</code> | The element's markup in a state - the authored markup with that state applied; a pure function of the state. <table><tr><th>Argument</th><th>Type</th><th>Description</th></tr><tr><td><code>state?</code></td><td><code>{ name: CarouselState; config: CarouselStateConfigs[CarouselState]; model?: ElementModel }</code></td><td>a state as getState() returns it (default: the current one)</td></tr></table> <b>Returns</b> <code>string</code> - the element's outer HTML in that state |
| <code>el.api.settled(): Promise&lt;void&gt;</code> | Wait for the last state's DOM work (async states: a diagram rendering, a chart mounting). <b>Returns</b> <code>Promise&lt;void&gt;</code> - resolves when nothing is pending |
| <code>el.store: Store&lt;{ name: CarouselState; config: CarouselStateConfigs[CarouselState] }&gt;</code> | A defuss-store store of the element's state - subscribe to follow every change (also the user's), set it to drive the component. |

### Registry

| Member | Description |
|---|---|
| <code>df$.shadcn.carouselApi.setState&lt;S extends CarouselState&gt;(el: HTMLElement, name: S, config?: CarouselStateConfigs[S]): unknown</code> | Enter a state: the DOM work runs (also when it is the current state), the store records it. <table><tr><th>Argument</th><th>Type</th><th>Description</th></tr><tr><td><code>el</code></td><td><code>HTMLElement</code></td><td>the component's element</td></tr><tr><td><code>name</code></td><td><code>S</code></td><td>a declared state (an unknown name throws)</td></tr><tr><td><code>config?</code></td><td><code>CarouselStateConfigs[S]</code></td><td>that state's config (merged into the stored one when the component merges)</td></tr></table> <b>Returns</b> <code>unknown</code> - what the state's DOM work returned - a Promise for an async state (await it, or el.api.settled()) |
| <code>df$.shadcn.carouselApi.getState(el: HTMLElement): { name: CarouselState; config: CarouselStateConfigs[CarouselState]; model?: ElementModel }</code> | The state the element shows now - read back from the DOM, so it includes what the user changed. <table><tr><th>Argument</th><th>Type</th><th>Description</th></tr><tr><td><code>el</code></td><td><code>HTMLElement</code></td><td>the component's element</td></tr></table> <b>Returns</b> <code>{ name: CarouselState; config: CarouselStateConfigs[CarouselState]; model?: ElementModel }</code> - the state's name, its config and the authored markup model render() starts from |
| <code>df$.shadcn.carouselApi.render(state: { name: CarouselState; config: CarouselStateConfigs[CarouselState]; model?: ElementModel }): string</code> | The element's markup in a state - the authored markup with that state applied; a pure function of the state. <table><tr><th>Argument</th><th>Type</th><th>Description</th></tr><tr><td><code>state</code></td><td><code>{ name: CarouselState; config: CarouselStateConfigs[CarouselState]; model?: ElementModel }</code></td><td>a state as getState() returns it (with its model)</td></tr></table> <b>Returns</b> <code>string</code> - the element's outer HTML in that state |
| <code>df$.shadcn.carouselApi.store(el: HTMLElement): Store&lt;{ name: CarouselState; config: CarouselStateConfigs[CarouselState] }&gt;</code> | The element's store (bindComponent made it). <table><tr><th>Argument</th><th>Type</th><th>Description</th></tr><tr><td><code>el</code></td><td><code>HTMLElement</code></td><td>the component's element</td></tr></table> <b>Returns</b> <code>Store&lt;{ name: CarouselState; config: CarouselStateConfigs[CarouselState] }&gt;</code> - a defuss-store store of { name, config } - subscribe to follow every change, set it to drive the component |
| <code>df$.shadcn.carouselApi.commit&lt;S extends CarouselState&gt;(el: HTMLElement, name: S, config?: CarouselStateConfigs[S]): void</code> | Record a state the element reached on its own (no DOM work) - for a component's own handlers. <table><tr><th>Argument</th><th>Type</th><th>Description</th></tr><tr><td><code>el</code></td><td><code>HTMLElement</code></td><td>the component's element</td></tr><tr><td><code>name</code></td><td><code>S</code></td><td>the state it is in</td></tr><tr><td><code>config?</code></td><td><code>CarouselStateConfigs[S]</code></td><td>its config</td></tr></table> |
| <code>df$.shadcn.carouselStates: CarouselState[]</code> | The declared states, 'default' first: <code>default</code>. |

## Notes

- **No library needed** - built entirely on CSS scroll-snap + IntersectionObserver.
- **Dots are morph-rendered** (see the "DOM Querying & Morphing" guide): an
  empty `.carousel-dots` is filled by `df$(dots).morph()` with stable
  `<carousel id>-dot-<i>` ids; out-of-band slide add/remove reconciles the
  dot list in one keyed pass (retained dots keep node identity).
  Consumer-authored dots are never re-rendered - only `aria-current` is
  flagged.
- **Slide sizing** - default is `flex: 0 0 100%`. Override `flex` on `.carousel-slide` to show multiple slides (e.g., `calc(33.333% - 0.667rem)` for 3-up).
- **Gap / spacing** - controlled by the `gap` property on `.carousel-viewport` (default `1rem`).
- **Dot auto-generation** - if `.carousel-dots` is present but empty, dots are auto-created by JS from the slide count.
- **Autoplay pauses** - on `mouseenter` and `focusin`, restarts on `mouseleave` and `focusout` (per WAI-ARIA APG).
- **Reduced motion** - `prefers-reduced-motion: reduce` removes smooth scrolling and all transitions.
- **Scroll containment** - `overscroll-behavior: contain` prevents parent page scrolling when at carousel edges.
- **Buttons disabled at edges** - in non-loop mode, prev is disabled at first slide, next at last slide.
