# Carousel

A native scroll-snap slideshow with arrows, dots, thumbnails, loop, and autoplay.

Source: https://www.zazz.sh/api/primitives/carousel/

## Installation

```bash
pnpm dlx zazz-ui add carousel
```

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Examples

### Default

```html
<ui-carousel>
  <div
    style="
      --display: flex;
      --flex-wrap: wrap;
      --items: end;
      --justify: space-between;
      --pb: 11;
      --gap: 6;
    "
  >
    <h2
      style="
        --font-size: var(--font-size-h5);
        --font-weight: heading;
        --font-family: var(--font-family-heading);
        --leading: var(--leading-h5);
      "
    >
      Featured Articles
    </h2>
    <div
      style="
        --display: flex;
        --display--md: none;
        --gap: 2;
      "
    >
      <button
        type="button"
        data-ui="button"
        data-button-size="icon"
        data-button-variant="muted"
        data-carousel-slot="prev"
      >
        <svg
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 256 256"
          aria-hidden="true"
          style="--size: 4"
        >
          <rect width="256" height="256" fill="none" />
          <polyline
            points="160 208 80 128 160 48"
            fill="none"
            stroke="currentColor"
            stroke-linecap="round"
            stroke-linejoin="round"
            stroke-width="14"
          />
        </svg>
      </button>
      <button
        type="button"
        data-ui="button"
        data-button-size="icon"
        data-button-variant="muted"
        data-carousel-slot="next"
      >
        <svg
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 256 256"
          aria-hidden="true"
          style="--size: 4"
        >
          <rect width="256" height="256" fill="none" />
          <polyline
            points="96 48 176 128 96 208"
            fill="none"
            stroke="currentColor"
            stroke-linecap="round"
            stroke-linejoin="round"
            stroke-width="14"
          />
        </svg>
      </button>
    </div>
  </div>

  <div data-carousel-slot="viewport" style="--overflow: visible">
    <div data-carousel-slot="container" style="--ml: -6">
      <a
        data-carousel-slot="slide"
        href="#"
        style="
          --basis: 100%;
          --basis--sm: 50%;
          --basis--md: 33.333%;
          --basis--lg: 25%;
          --cursor: pointer;
        "
      >
        <div style="--pl: 6">
          <div
            style="
              --display: grid;
              --gap: 4;
              --text: var(--color-card-foreground);
              --bg: var(--color-card);
              --border: 1;
              --radius: 2.5;
              --overflow: clip;
            "
          >
            <figure
              style="
                --aspect: 3 / 2;
                --radius-t: 2.5;
                --overflow: clip;
              "
            >
              <img
                width="300"
                height="200"
                loading="lazy"
                src="https://images.unsplash.com/photo-1500382017468-9049fed747ef?auto=format&fit=crop&w=300&h=200&q=60"
                alt="Zebras grazing at dawn on the savanna"
                style="
                  --size: 100%;
                  --opacity: 1;
                  --opacity--hover: 0.75;
                  --scale: 1;
                  --scale--hover: 1.02;
                  --transition: var(--default-transition);
                  --object-fit: cover;
                "
              />
            </figure>
            <div
              style="
                --display: grid;
                --px: 4;
                --pb: 4;
                --gap: 4;
              "
            >
              <header
                style="
                  --display: grid;
                  --gap: 2;
                "
              >
                <div
                  style="
                    --display: flex;
                    --items: center;
                    --gap: 2;
                  "
                >
                  <span data-ui="badge" data-badge-variant="muted">Field notes</span>
                </div>
                <hgroup
                  style="
                    --display: grid;
                    --gap: 2;
                  "
                >
                  <h3
                    style="
                      --font-size: var(--font-size-lg);
                      --font-family: var(--font-family-heading);
                      --leading: var(--leading-lg);
                      --tracking: var(--tracking-lg);
                      --line-clamp: 1;
                    "
                  >
                    How quickly zebras jump
                  </h3>
                  <p
                    style="
                      --font-size: var(--font-size-sm);
                      --leading: var(--leading-sm);
                      --tracking: var(--tracking-sm);
                      --line-clamp: 2;
                      --text: var(--color-muted-foreground);
                    "
                  >
                    Striped coats and sudden bursts of speed help herds evade predators when the
                    grasslands go quiet at dusk.
                  </p>
                </hgroup>
              </header>
              <footer
                style="
                  --display: grid;
                  --justify: end;
                  --pt: 4;
                  --pb: 2;
                  --gap: 4;
                  --border-t: 1;
                "
              >
                <div data-ui="button" data-button-variant="link">
                  Read article
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
                    <rect width="256" height="256" fill="none" />
                    <line
                      x1="40"
                      y1="128"
                      x2="216"
                      y2="128"
                      fill="none"
                      stroke="currentColor"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="16"
                    />
                    <polyline
                      points="144 56 216 128 144 200"
                      fill="none"
                      stroke="currentColor"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="16"
                    />
                  </svg>
                </div>
              </footer>
            </div>
          </div>
        </div>
      </a>

      <a
        data-carousel-slot="slide"
        href="#"
        style="
          --basis: 100%;
          --basis--sm: 50%;
          --basis--md: 33.333%;
          --basis--lg: 25%;
          --cursor: pointer;
        "
      >
        <div style="--pl: 6">
          <div
            style="
              --display: grid;
              --gap: 4;
              --text: var(--color-card-foreground);
              --bg: var(--color-card);
              --border: 1;
              --radius: 2.5;
              --overflow: clip;
            "
          >
            <figure
              style="
                --aspect: 3 / 2;
                --radius-t: 2.5;
                --overflow: clip;
              "
            >
              <img
                width="300"
                height="200"
                loading="lazy"
                src="https://images.unsplash.com/photo-1564760055775-d63b17a55c44?auto=format&fit=crop&w=300&h=200&q=60"
                alt="A lion resting in tall golden grass"
                style="
                  --size: 100%;
                  --opacity: 1;
                  --opacity--hover: 0.75;
                  --scale: 1;
                  --scale--hover: 1.02;
                  --transition: var(--default-transition);
                  --object-fit: cover;
                "
              />
            </figure>
            <div
              style="
                --display: grid;
                --px: 4;
                --pb: 4;
                --gap: 4;
              "
            >
              <header
                style="
                  --display: grid;
                  --gap: 2;
                "
              >
                <div
                  style="
                    --display: flex;
                    --items: center;
                    --gap: 2;
                  "
                >
                  <span data-ui="badge" data-badge-variant="muted">Wildlife</span>
                </div>
                <hgroup
                  style="
                    --display: grid;
                    --gap: 2;
                  "
                >
                  <h3
                    style="
                      --font-size: var(--font-size-lg);
                      --font-family: var(--font-family-heading);
                      --leading: var(--leading-lg);
                      --tracking: var(--tracking-lg);
                      --line-clamp: 1;
                    "
                  >
                    Lions at golden hour
                  </h3>
                  <p
                    style="
                      --font-size: var(--font-size-sm);
                      --leading: var(--leading-sm);
                      --tracking: var(--tracking-sm);
                      --line-clamp: 2;
                      --text: var(--color-muted-foreground);
                    "
                  >
                    Warm light and long shadows make dawn patrols the best time to photograph big
                    cats on the open plain.
                  </p>
                </hgroup>
              </header>
              <footer
                style="
                  --display: grid;
                  --justify: end;
                  --pt: 4;
                  --pb: 2;
                  --gap: 4;
                  --border-t: 1;
                "
              >
                <div data-ui="button" data-button-variant="link">
                  View gallery
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
                    <rect width="256" height="256" fill="none" />
                    <line
                      x1="40"
                      y1="128"
                      x2="216"
                      y2="128"
                      fill="none"
                      stroke="currentColor"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="16"
                    />
                    <polyline
                      points="144 56 216 128 144 200"
                      fill="none"
                      stroke="currentColor"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="16"
                    />
                  </svg>
                </div>
              </footer>
            </div>
          </div>
        </div>
      </a>

      <a
        data-carousel-slot="slide"
        href="#"
        style="
          --basis: 100%;
          --basis--sm: 50%;
          --basis--md: 33.333%;
          --basis--lg: 25%;
          --cursor: pointer;
        "
      >
        <div style="--pl: 6">
          <div
            style="
              --display: grid;
              --gap: 4;
              --text: var(--color-card-foreground);
              --bg: var(--color-card);
              --border: 1;
              --radius: 2.5;
              --overflow: clip;
            "
          >
            <figure
              style="
                --aspect: 3 / 2;
                --radius-t: 2.5;
                --overflow: clip;
              "
            >
              <img
                width="300"
                height="200"
                loading="lazy"
                src="https://images.unsplash.com/photo-1511735111819-9a3f7709049c?auto=format&fit=crop&w=300&h=200&q=60"
                alt="Elephants crossing a dusty riverbed"
                style="
                  --size: 100%;
                  --opacity: 1;
                  --opacity--hover: 0.75;
                  --scale: 1;
                  --scale--hover: 1.02;
                  --transition: var(--default-transition);
                  --object-fit: cover;
                "
              />
            </figure>
            <div
              style="
                --display: grid;
                --px: 4;
                --pb: 4;
                --gap: 4;
              "
            >
              <header
                style="
                  --display: grid;
                  --gap: 2;
                "
              >
                <div
                  style="
                    --display: flex;
                    --items: center;
                    --gap: 2;
                  "
                >
                  <span data-ui="badge" data-badge-variant="muted">Research</span>
                </div>
                <hgroup
                  style="
                    --display: grid;
                    --gap: 2;
                  "
                >
                  <h3
                    style="
                      --font-size: var(--font-size-lg);
                      --font-family: var(--font-family-heading);
                      --leading: var(--leading-lg);
                      --tracking: var(--tracking-lg);
                      --line-clamp: 1;
                    "
                  >
                    Migration patterns decoded
                  </h3>
                  <p
                    style="
                      --font-size: var(--font-size-sm);
                      --leading: var(--leading-sm);
                      --tracking: var(--tracking-sm);
                      --line-clamp: 2;
                      --text: var(--color-muted-foreground);
                    "
                  >
                    Satellite collars reveal ancient routes herds still follow each dry season
                    across the floodplain.
                  </p>
                </hgroup>
              </header>
              <footer
                style="
                  --display: grid;
                  --justify: end;
                  --pt: 4;
                  --pb: 2;
                  --gap: 4;
                  --border-t: 1;
                "
              >
                <div data-ui="button" data-button-variant="link">
                  See the data
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
                    <rect width="256" height="256" fill="none" />
                    <line
                      x1="40"
                      y1="128"
                      x2="216"
                      y2="128"
                      fill="none"
                      stroke="currentColor"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="16"
                    />
                    <polyline
                      points="144 56 216 128 144 200"
                      fill="none"
                      stroke="currentColor"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="16"
                    />
                  </svg>
                </div>
              </footer>
            </div>
          </div>
        </div>
      </a>

      <a
        data-carousel-slot="slide"
        href="#"
        style="
          --basis: 100%;
          --basis--sm: 50%;
          --basis--md: 33.333%;
          --basis--lg: 25%;
          --cursor: pointer;
        "
      >
        <div style="--pl: 6">
          <div
            style="
              --display: grid;
              --gap: 4;
              --text: var(--color-card-foreground);
              --bg: var(--color-card);
              --border: 1;
              --radius: 2.5;
              --overflow: clip;
            "
          >
            <figure
              style="
                --aspect: 3 / 2;
                --radius-t: 2.5;
                --overflow: clip;
              "
            >
              <img
                width="300"
                height="200"
                loading="lazy"
                src="https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?auto=format&fit=crop&w=300&h=200&q=60"
                alt="Misty highland valley at sunrise"
                style="
                  --size: 100%;
                  --opacity: 1;
                  --opacity--hover: 0.75;
                  --scale: 1;
                  --scale--hover: 1.02;
                  --transition: var(--default-transition);
                  --object-fit: cover;
                "
              />
            </figure>
            <div
              style="
                --display: grid;
                --px: 4;
                --pb: 4;
                --gap: 4;
              "
            >
              <header
                style="
                  --display: grid;
                  --gap: 2;
                "
              >
                <div
                  style="
                    --display: flex;
                    --items: center;
                    --gap: 2;
                  "
                >
                  <span data-ui="badge" data-badge-variant="muted">Conservation</span>
                </div>
                <hgroup
                  style="
                    --display: grid;
                    --gap: 2;
                  "
                >
                  <h3
                    style="
                      --font-size: var(--font-size-lg);
                      --font-family: var(--font-family-heading);
                      --leading: var(--leading-lg);
                      --tracking: var(--tracking-lg);
                      --line-clamp: 1;
                    "
                  >
                    Rewilding the highlands
                  </h3>
                  <p
                    style="
                      --font-size: var(--font-size-sm);
                      --leading: var(--leading-sm);
                      --tracking: var(--tracking-sm);
                      --line-clamp: 2;
                      --text: var(--color-muted-foreground);
                    "
                  >
                    Community nurseries are planting native grasses and bringing endemic songbirds
                    back to the ridgeline.
                  </p>
                </hgroup>
              </header>
              <footer
                style="
                  --display: grid;
                  --justify: end;
                  --pt: 4;
                  --pb: 2;
                  --gap: 4;
                  --border-t: 1;
                "
              >
                <div data-ui="button" data-button-variant="link">
                  Get started
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
                    <rect width="256" height="256" fill="none" />
                    <line
                      x1="40"
                      y1="128"
                      x2="216"
                      y2="128"
                      fill="none"
                      stroke="currentColor"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="16"
                    />
                    <polyline
                      points="144 56 216 128 144 200"
                      fill="none"
                      stroke="currentColor"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="16"
                    />
                  </svg>
                </div>
              </footer>
            </div>
          </div>
        </div>
      </a>
    </div>
  </div>
</ui-carousel>
```

## Behavior

`primitives/carousel/carousel.js`

`<ui-carousel>` — HTML web component for carousels.
Light-DOM custom element that wraps standard carousel markup
and owns the carousel's lifecycle: it initializes on connect (via
`Carousel.initRoot`) and tears down on disconnect, so dynamically inserted
or SPA-swapped carousels need no manual wiring.

The element *is* the carousel root — CSS and `base/carousel.js` target
`:is(ui-carousel, [data-ui~="carousel"])` directly, and `data-carousel-*`
configuration attributes are read off the element (see base/carousel.js for
the full attribute reference). No shadow DOM; children are regular markup.

Carousels inside a closed `<dialog>` defer initialization until the dialog
first opens (a closed dialog is `display: none`, so the viewport cannot be
measured).

Events: `zazz:dialog-open`

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="carousel"` | identity | free value |
| `<ui-carousel>` | tag | free value |
| `data-carousel-align` | preset | `center` `end` |
| `data-carousel-axis` | preset | `y` |
| `data-carousel-draggable` | preset | `false` |
| `data-carousel-thumbs-axis` | preset | `y` |
| `data-carousel-slot` | slot | `container` `dot` `dots` `next` `prev` `slide` `thumbs` `viewport` |
| `data-carousel-state` | state | `active` `dragging` `free` `gliding` `in-view` `jumping` `settling` `snapped` |
| `data-carousel-init` | config | free value |
| `data-carousel-keyboard` | config | free value |
| `data-carousel-loop` | config | free value |
| `data-carousel-plugins` | config | free value |
| `data-carousel-start` | config | free value |
| `data-carousel-start-index` | config | free value |

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-carousel-cursor--grabbing` | `grabbing` | grabbing |
| `--ui-carousel-gap` | `0px` | — |
| `--ui-carousel-settle-duration` | `calc(var(--default-transition-duration) * 2)` | — |
| `--ui-carousel-settle-ease` | `var(--default-transition-timing-function)` | — |
| `--ui-carousel-slide-basis` | `calc(100% - var(--ui-carousel-gap))` | — |
| `--ui-carousel-slide-min-w` | `0` | — |
| `--ui-carousel-snap-align` | `start` | — |
