# Carousel

A compound carousel powered by Embla with responsive sizing, orientation, controls, and accessible semantics.

Carousel presents a sequence of related content in a swipeable viewport. Use it when horizontal or vertical browsing is more useful than showing every item at once; do not hide essential content behind a carousel when a list or grid would be easier to scan.

<p className="zenos-recipe-source">
  Powered by <a href="https://www.embla-carousel.com/">Embla Carousel</a> for drag gestures,
  snapping, direction, and loop behavior. Zenos owns the compound anatomy, controls, dots, states,
  and accessible semantics.
</p>

```bash
pnpm dlx @zentara-co/zenos-cli add carousel
npx @zentara-co/zenos-cli add carousel
yarn dlx @zentara-co/zenos-cli add carousel
bunx @zentara-co/zenos-cli add carousel
```

The command installs `embla-carousel-react` and adds `carousel.tsx` to the consumer project. Import the recipe through the path configured by your application:

```tsx
import { Carousel } from "./components/carousel";
```

## Basic usage

Compose Root, Viewport, Content, and Item first, then add Previous, Next, or Dots where the product needs explicit navigation.

```tsx
import { Badge, Card } from "@zentara-co/zenos-ui";
import { Carousel } from "@docs/src/recipes/carousel/carousel";

const slides = [
  { title: "Asset inventory", detail: "128 monitored assets" },
  { title: "Exposure review", detail: "7 findings need attention" },
  { title: "Scan coverage", detail: "94% checked today" },
];

function CarouselRecipeExample() {
  return (
    <Carousel.Root className="w-full max-w-xl" align="center" aria-label="Workspace insights">
      <div className="grid grid-cols-[2.5rem_minmax(0,1fr)_2.5rem] items-center gap-4">
        <Carousel.Previous style={{ position: "static", transform: "none", translate: "none" }} />
        <Carousel.Viewport>
          <Carousel.Content style={{ marginInlineStart: 0 }}>
            {slides.map((slide, index) => (
              <Carousel.Item key={slide.title} style={{ paddingInline: 8 }}>
                <Card className="min-h-40">
                  <Card.Header>
                    <Badge color={index === 1 ? "warning" : "neutral"}>Insight {index + 1}</Badge>
                    <Card.Title>{slide.title}</Card.Title>
                    <Card.Description>{slide.detail}</Card.Description>
                  </Card.Header>
                </Card>
              </Carousel.Item>
            ))}
          </Carousel.Content>
        </Carousel.Viewport>
        <Carousel.Next style={{ position: "static", transform: "none", translate: "none" }} />
      </div>
      <Carousel.Dots style={{ marginTop: 24 }} />
    </Carousel.Root>
  );
}
```

```text
Carousel.Root
├── Carousel.Viewport
│   └── Carousel.Content
│       ├── Carousel.Item
│       └── Carousel.Item
├── Carousel.Previous
├── Carousel.Next
└── Carousel.Dots
```

`Carousel.Root` owns Embla state and provides it to every compound part. `Viewport` is the clipped drag surface, `Content` is the moving flex track, and each `Item` is one accessible slide.

## Responsive item sizes

Items default to `basis-full`, so one slide occupies the viewport. Override the item basis to show multiple slides. Responsive basis values let the same carousel show one item on narrow screens and more items when space permits.

```tsx
import { Badge, Card } from "@zentara-co/zenos-ui";
import { Carousel } from "@docs/src/recipes/carousel/carousel";

const carouselExampleItems = [
  "Asset inventory",
  "Exposure review",
  "Scan coverage",
  "Policy checks",
  "Team activity",
  "Audit history",
];

function CarouselExampleCard({ index, title }: { index: number; title: string }) {
  return (
    <Card className="grid min-h-32 place-items-center">
      <Card.Header className="items-center text-center">
        <Badge color="neutral">{index + 1}</Badge>
        <Card.Title>{title}</Card.Title>
      </Card.Header>
    </Card>
  );
}

function CarouselResponsiveItemsExample() {
  return (
    <Carousel.Root className="w-full max-w-3xl" align="center" aria-label="Security workspace">
      <div className="grid grid-cols-[2.5rem_minmax(0,1fr)_2.5rem] items-center gap-4">
        <Carousel.Previous style={{ position: "static", transform: "none", translate: "none" }} />
        <Carousel.Viewport>
          <Carousel.Content style={{ marginInlineStart: 0 }}>
            {carouselExampleItems.map((title, index) => (
              <Carousel.Item
                key={title}
                className="basis-full sm:basis-1/2 lg:basis-1/3"
                style={{ paddingInline: 8 }}
              >
                <CarouselExampleCard index={index} title={title} />
              </Carousel.Item>
            ))}
          </Carousel.Content>
        </Carousel.Viewport>
        <Carousel.Next style={{ position: "static", transform: "none", translate: "none" }} />
      </div>
      <Carousel.Dots style={{ marginTop: 24 }} />
    </Carousel.Root>
  );
}
```

Keep enough of the next item visible—or provide enabled controls and dots—so users can discover that more content exists.

## Custom spacing

The recipe defaults to a 16px gap using a negative inline margin on Content and matching start padding on Item. For a visually centered group, remove the track offset and split the desired gap evenly across both sides of every Item.

```tsx
import { Badge, Card } from "@zentara-co/zenos-ui";
import { Carousel } from "@docs/src/recipes/carousel/carousel";

const carouselExampleItems = [
  "Asset inventory",
  "Exposure review",
  "Scan coverage",
  "Policy checks",
  "Team activity",
  "Audit history",
];

function CarouselExampleCard({ index, title }: { index: number; title: string }) {
  return (
    <Card className="grid min-h-32 place-items-center">
      <Card.Header className="items-center text-center">
        <Badge color="neutral">{index + 1}</Badge>
        <Card.Title>{title}</Card.Title>
      </Card.Header>
    </Card>
  );
}

function CarouselCustomSpacingExample() {
  return (
    <Carousel.Root className="w-full max-w-3xl" align="center" aria-label="Reports">
      <div className="grid grid-cols-[2.5rem_minmax(0,1fr)_2.5rem] items-center gap-4">
        <Carousel.Previous style={{ position: "static", transform: "none", translate: "none" }} />
        <Carousel.Viewport>
          <Carousel.Content style={{ marginInlineStart: 0 }}>
            {carouselExampleItems.slice(0, 4).map((title, index) => (
              <Carousel.Item key={title} className="basis-1/2" style={{ paddingInline: 12 }}>
                <CarouselExampleCard index={index} title={title} />
              </Carousel.Item>
            ))}
          </Carousel.Content>
        </Carousel.Viewport>
        <Carousel.Next style={{ position: "static", transform: "none", translate: "none" }} />
      </div>
      <Carousel.Dots style={{ marginTop: 24 }} />
    </Carousel.Root>
  );
}
```

The example uses `marginInlineStart: 0` on Content and 12px inline padding on each side of Item, producing a centered 24px gap. Use logical `paddingInline` or `paddingBlock` so the same layout works with RTL and vertical orientation.

## Vertical orientation

Set `orientation="vertical"`, give Viewport an explicit height, and size items relative to that height. The recipe switches the track axis and touch-action behavior automatically.

```tsx
import { Badge, Card } from "@zentara-co/zenos-ui";
import { Carousel } from "@docs/src/recipes/carousel/carousel";

const carouselExampleItems = [
  "Asset inventory",
  "Exposure review",
  "Scan coverage",
  "Policy checks",
  "Team activity",
  "Audit history",
];

function CarouselExampleCard({ index, title }: { index: number; title: string }) {
  return (
    <Card className="grid min-h-32 place-items-center">
      <Card.Header className="items-center text-center">
        <Badge color="neutral">{index + 1}</Badge>
        <Card.Title>{title}</Card.Title>
      </Card.Header>
    </Card>
  );
}

function CarouselVerticalExample() {
  return (
    <Carousel.Root
      className="w-full max-w-sm"
      orientation="vertical"
      align="center"
      aria-label="Recent activity"
    >
      <div
        style={{
          display: "grid",
          gridTemplateRows: "40px 288px 40px",
          justifyItems: "center",
          rowGap: 16,
          width: "100%",
        }}
      >
        <Carousel.Previous
          style={{ position: "static", transform: "rotate(90deg)", translate: "none" }}
        />
        <Carousel.Viewport style={{ width: "100%", height: "100%", margin: 0 }}>
          <Carousel.Content style={{ marginTop: 0 }}>
            {carouselExampleItems.slice(0, 4).map((title, index) => (
              <Carousel.Item key={title} className="basis-1/2" style={{ paddingBlock: 8 }}>
                <CarouselExampleCard index={index} title={title} />
              </Carousel.Item>
            ))}
          </Carousel.Content>
        </Carousel.Viewport>
        <Carousel.Next
          style={{ position: "static", transform: "rotate(90deg)", translate: "none" }}
        />
      </div>
    </Carousel.Root>
  );
}
```

Previous and Next are intentionally composable. The example places them outside the viewport and rotates the default chevrons so their visual direction matches the vertical axis.

## Looping and grouped movement

`loop` wraps navigation from the last snap to the first. `slidesToScroll` advances more than one slide per navigation action. Use grouped movement when the visible items form a meaningful page.

```tsx
import { Badge, Card } from "@zentara-co/zenos-ui";
import { Carousel } from "@docs/src/recipes/carousel/carousel";

const carouselExampleItems = [
  "Asset inventory",
  "Exposure review",
  "Scan coverage",
  "Policy checks",
  "Team activity",
  "Audit history",
];

function CarouselExampleCard({ index, title }: { index: number; title: string }) {
  return (
    <Card className="grid min-h-32 place-items-center">
      <Card.Header className="items-center text-center">
        <Badge color="neutral">{index + 1}</Badge>
        <Card.Title>{title}</Card.Title>
      </Card.Header>
    </Card>
  );
}

function CarouselLoopExample() {
  return (
    <Carousel.Root
      className="w-full max-w-2xl"
      align="center"
      loop
      slidesToScroll={2}
      aria-label="Looping findings"
    >
      <div className="grid grid-cols-[2.5rem_minmax(0,1fr)_2.5rem] items-center gap-4">
        <Carousel.Previous style={{ position: "static", transform: "none", translate: "none" }} />
        <Carousel.Viewport>
          <Carousel.Content style={{ marginInlineStart: 0 }}>
            {carouselExampleItems.map((title, index) => (
              <Carousel.Item key={title} className="basis-1/2" style={{ paddingInline: 8 }}>
                <CarouselExampleCard index={index} title={title} />
              </Carousel.Item>
            ))}
          </Carousel.Content>
        </Carousel.Viewport>
        <Carousel.Next style={{ position: "static", transform: "none", translate: "none" }} />
      </div>
      <Carousel.Dots style={{ marginTop: 24 }} />
    </Carousel.Root>
  );
}
```

Avoid looping when users need a clear sense of completion. When looping is enabled, controls remain available because either direction always has another snap.

## Controls and dots

Previous and Next are Zenos IconButtons. They disable automatically at their respective boundary when `loop` is false. Consumer `onClick` handlers run before carousel navigation and can cancel movement with `event.preventDefault()`.

Dots are derived from Embla's snap list, not directly from the number of Item elements. This matters when multiple items share a snap or `slidesToScroll` groups them. The active dot uses `aria-current="true"`, and every dot has a `Go to slide N` label.

You can omit controls, dots, or both. If drag is the only visible interaction, ensure the surrounding product still makes the additional content discoverable.

## RTL

Pass `dir="rtl"` to Root. The recipe forwards the direction to Embla and uses logical spacing for the horizontal track.

```tsx
<Carousel.Root dir="rtl" aria-label="تقارير الأمان">
  <Carousel.Viewport>
    <Carousel.Content>
      {reports.map((report) => (
        <Carousel.Item key={report.id}>{report.title}</Carousel.Item>
      ))}
    </Carousel.Content>
  </Carousel.Viewport>
  <Carousel.Previous className="rtl:rotate-180" />
  <Carousel.Next className="rtl:rotate-180" />
</Carousel.Root>
```

Rotate the built-in chevrons when their visual direction should mirror with the interface. Custom icons can encode their own direction instead.

## Root API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| orientation | 'horizontal' \| 'vertical' | horizontal | Slide axis and track layout. |
| align | 'start' \| 'center' \| 'end' | start | Embla snap alignment inside the viewport. |
| loop | boolean | false | Wraps navigation between the final and first snaps. |
| slidesToScroll | number | 1 | Number of slides advanced by Previous or Next. |
| dir | 'ltr' \| 'rtl' | ltr | Reading and Embla movement direction. |
| aria-label | string | — | Accessible name for the carousel region. |
| className / native div props | string / HTMLAttributes<HTMLDivElement> | — | Styles or labels the root region. |

## Part API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| Viewport.className | string | — | Viewport sizing and layout overrides; overflow remains clipped by default. |
| Content.className | string | — | Track spacing and flex-layout overrides. |
| Item.className | string | — | Slide basis, spacing, and responsive sizing. |
| Previous / Next.disabled | boolean | — | Consumer-disabled state combined with the current Embla boundary state. |
| Previous / Next.onClick | MouseEventHandler<HTMLButtonElement> | — | Runs before movement; prevent default to cancel carousel navigation. |
| Previous / Next.children | ReactNode | — | Replaces the built-in directional icon. |
| Dots.className | string | — | Styles the snap-navigation container. |

All parts forward their native HTML attributes and refs to the rendered element.

## Embla options, events, and plugins

The Zenos recipe exposes a focused set of Embla options as typed Root props rather than exposing the raw Embla API. There is no `setApi`, `opts`, or `plugins` prop in the current public contract.

Because recipes are consumer-owned, extend the local `carousel.tsx` only when the product has a concrete requirement. For example, autoplay requires installing `embla-carousel-autoplay` and supplying the plugin in the recipe's `useEmblaCarousel` call:

```tsx
import Autoplay from "embla-carousel-autoplay";

const autoplay = Autoplay({ delay: 4000, stopOnInteraction: true });
const [carouselRef, api] = useEmblaCarousel(options, [autoplay]);
```

Pause autoplay while the carousel or its controls have focus or pointer hover, and disable automatic movement when the user prefers reduced motion. Avoid adding plugin dependencies to `@zentara-co/zenos-ui`; they belong to this specialist recipe.

If product logic needs selection events, prefer adding a focused callback such as `onSelectedIndexChange` to the local recipe over leaking the full Embla instance through unrelated feature components.

## Accessibility

The recipe provides region, carousel, slide, control, and active-dot semantics. Complete the accessible experience in consumer code:

- Give Root a concise `aria-label` or `aria-labelledby`.
- Keep Previous, Next, and Dots keyboard reachable when they are rendered.
- Put meaningful interactive content inside Item without intercepting its native keyboard behavior.
- Do not move focus when the active slide changes; users should retain control of navigation.
- Announce automatic slide changes only when the product genuinely requires them, and provide a pause control.
- Preserve sufficient contrast for controls placed over media.

Source: https://zenos-ui.pages.dev/recipes/carousel/