# Carousel

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Carousel aria-label="Horizontal single cards" class="mx-auto max-w-86.5"> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"1"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"2"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"3"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"4"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"5"}</div></CarouselSlide> </Carousel>
</div>

```astro
---
---

<Carousel aria-label="Horizontal single cards" class="mx-auto max-w-86.5">
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">1</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">2</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">3</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">4</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">5</div></CarouselSlide>
</Carousel>
```

## Installation

<DocsTabs syncKey="pkg">
  <DocsTabItem label="bun">
  ```bash
  bunx bejamas add carousel
  ```
  </DocsTabItem>
  <DocsTabItem label="npm">
  ```bash
  npx bejamas add carousel
  ```
  </DocsTabItem>
  <DocsTabItem label="pnpm">
  ```bash
  pnpm dlx bejamas add carousel
  ```
  </DocsTabItem>
  <DocsTabItem label="yarn">
  ```bash
  yarn dlx bejamas add carousel
  ```
  </DocsTabItem>
</DocsTabs>

## Usage

```astro nocollapse
---
---

<Carousel aria-label="Featured cards" class="max-w-sm">
  <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 1</div></CarouselSlide>
  <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 2</div></CarouselSlide>
  <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 3</div></CarouselSlide>
</Carousel>
```

## Props

| Prop | Type | Default |
|---|---|---|
| <code>orientation</code> | `"horizontal" \| "vertical"` | `axis === "y" ? "vertical" : "horizontal"` |
| <code>variant</code> | `"single" \| "multiple"` | `"single"` |
| <code>defaultIndex</code> | `number` | `0` |
| <code>"data-loop"</code> | `boolean \| string` | `false` |
| <code>drag</code> | `boolean` | `true` |
| <code>controls</code> | `boolean` | `true` |
| <code>showCounter</code> | `boolean` | `orientation === "horizontal" && variant === "single"` |
| <code>counterLabel</code> | `string` | `variant === "multiple" ? "Page" : "Card"` |
| <code>previousLabel</code> | `string` | `"Previous slide"` |
| <code>nextLabel</code> | `string` | `"Next slide"` |
| <code>contentClass</code> | `string` |  |
| <code>axis</code> | `"x" \| "y"` | `"x"` |
| <code>snapType</code> | `"mandatory" \| "proximity"` | `"mandatory"` |
| <code>fade</code> | `boolean` | `false` |
| <code>class</code> | `string` |  |

## Examples

### Horizontal single

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Carousel aria-label="Horizontal single cards" class="mx-auto max-w-86.5"> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"1"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"2"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"3"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"4"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"5"}</div></CarouselSlide> </Carousel>
</div>

```astro
---
---

<Carousel aria-label="Horizontal single cards" class="mx-auto max-w-86.5">
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">1</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">2</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">3</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">4</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">5</div></CarouselSlide>
</Carousel>
```

### Horizontal multiple

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Carousel aria-label="Horizontal multiple cards" variant="multiple" class="mx-auto max-w-108"> <CarouselSlide> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"1"}</div> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"2"}</div> </CarouselSlide> <CarouselSlide> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"3"}</div> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"4"}</div> </CarouselSlide> <CarouselSlide> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"5"}</div> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"6"}</div> </CarouselSlide> </Carousel>
</div>

```astro
---
---

<Carousel aria-label="Horizontal multiple cards" variant="multiple" class="mx-auto max-w-108">
  <CarouselSlide>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">1</div>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">2</div>
  </CarouselSlide>
  <CarouselSlide>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">3</div>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">4</div>
  </CarouselSlide>
  <CarouselSlide>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">5</div>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">6</div>
  </CarouselSlide>
</Carousel>
```

### Vertical single

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Carousel aria-label="Vertical single cards" orientation="vertical" class="mx-auto max-w-75" contentClass="h-50"> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">{"1"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">{"2"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">{"3"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">{"4"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">{"5"}</div></CarouselSlide> </Carousel>
</div>

```astro
---
---

<Carousel aria-label="Vertical single cards" orientation="vertical" class="mx-auto max-w-75" contentClass="h-50">
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">1</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">2</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">3</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">4</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold h-full">5</div></CarouselSlide>
</Carousel>
```

### Vertical multiple

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Carousel aria-label="Vertical multiple cards" orientation="vertical" variant="multiple" class="mx-auto max-w-75" contentClass="h-79"> <CarouselSlide> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">{"1"}</div> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">{"2"}</div> </CarouselSlide> <CarouselSlide> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">{"3"}</div> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">{"4"}</div> </CarouselSlide> <CarouselSlide> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">{"5"}</div> <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">{"6"}</div> </CarouselSlide> </Carousel>
</div>

```astro
---
---

<Carousel aria-label="Vertical multiple cards" orientation="vertical" variant="multiple" class="mx-auto max-w-75" contentClass="h-79">
  <CarouselSlide>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">1</div>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">2</div>
  </CarouselSlide>
  <CarouselSlide>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">3</div>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">4</div>
  </CarouselSlide>
  <CarouselSlide>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">5</div>
    <div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold min-h-0">6</div>
  </CarouselSlide>
</Carousel>
```

### Header controls

Use `CarouselRoot` to compose your own layout. Place `CarouselPrevious` and `CarouselNext` anywhere inside the root, including a header above `CarouselContent`. The runtime keeps their disabled states in sync. Place slides directly inside `CarouselContent`.

`CarouselRoot` accepts `orientation`, `axis`, `variant`, `defaultIndex`, `data-loop`, and `drag`. `CarouselContent` inherits orientation and drag from the root and accepts `snapType`, `fade`, and `class`; give vertical content a height. Controls accept button attributes, `variant`, `size`, and `class`, with an optional slot for a custom icon or text and `aria-label` for a custom accessible name. The composed root renders only its slot; the existing `Carousel` continues to provide its default controls, viewport, and counter.

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<CarouselRoot aria-label="Featured cards" class="mx-auto max-w-sm space-y-4"> <header class="flex items-center justify-between gap-4"> <h3 class="font-semibold">{"Featured cards"}</h3> <div class="flex gap-2"> <CarouselPrevious /> <CarouselNext /> </div> </header> <CarouselContent> <CarouselSlide><div class="rounded-lg bg-muted p-6">{"Card 1"}</div></CarouselSlide> <CarouselSlide><div class="rounded-lg bg-muted p-6">{"Card 2"}</div></CarouselSlide> <CarouselSlide><div class="rounded-lg bg-muted p-6">{"Card 3"}</div></CarouselSlide> </CarouselContent> </CarouselRoot>
</div>

```astro
---
---

<CarouselRoot aria-label="Featured cards" class="mx-auto max-w-sm space-y-4">
  <header class="flex items-center justify-between gap-4">
    <h3 class="font-semibold">Featured cards</h3>
    <div class="flex gap-2">
      <CarouselPrevious />
      <CarouselNext />
    </div>
  </header>
  <CarouselContent>
    <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 1</div></CarouselSlide>
    <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 2</div></CarouselSlide>
    <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 3</div></CarouselSlide>
  </CarouselContent>
</CarouselRoot>
```

### Looping

Set `data-loop="true"` to wrap button, keyboard, and programmatic navigation from the last slide to the first and back. Looping defaults to `false`.

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Carousel aria-label="Looping cards" data-loop="true" class="mx-auto max-w-86.5"> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"1"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"2"}</div></CarouselSlide> <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">{"3"}</div></CarouselSlide> </Carousel>
</div>

```astro
---
---

<Carousel aria-label="Looping cards" data-loop="true" class="mx-auto max-w-86.5">
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">1</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">2</div></CarouselSlide>
  <CarouselSlide><div class="flex items-center justify-center rounded-lg bg-muted text-muted-foreground font-semibold aspect-4/5">3</div></CarouselSlide>
</Carousel>
```

## API Reference

See [@data-slot](https://data-slot.com/components/carousel) documentation for more info.

### Events

The carousel emits custom events on its root element:

| Event | Detail | Description |
|-------|--------|-------------|
| `carousel:change` | `{ index: number }` | Fired when the active slide changes. The index is zero-based. |

```astro nocollapse
<Carousel id="my-carousel" aria-label="Featured cards" class="max-w-sm">
  <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 1</div></CarouselSlide>
  <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 2</div></CarouselSlide>
  <CarouselSlide><div class="rounded-lg bg-muted p-6">Card 3</div></CarouselSlide>
</Carousel>
```

```js nocollapse
const carousel = document.getElementById('my-carousel');

carousel.addEventListener('carousel:change', (event) => {
  console.log('Active slide:', event.detail.index);
});
```

### Data Attributes

The carousel exposes these attributes for styling and state queries:

| Attribute | Element | Description |
|-----------|---------|-------------|
| `data-orientation` | carousel | Current orientation (`horizontal` or `vertical`) |
| `data-variant` | carousel | Layout variant (`single` or `multiple`) |
| `data-loop` | carousel | Set to `"true"` to wrap navigation at either end; defaults to `"false"` |
| `data-index` | carousel | Zero-based index of the active slide |
| `data-dragging` | carousel | Present during an active drag gesture |
| `data-state` | carousel-item | Current slide state (`active` or `inactive`) |