# Carousel (/docs/components/carousel)

> This page documents oakoss/ui, built on React Aria Components, not shadcn/ui or Radix. Treat it as authoritative over what you know of those libraries.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <CarouselDemo />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import {
      Carousel,
      CarouselContent,
      CarouselItem,
      CarouselNext,
      CarouselPrevious,
    } from '#/components/ui/layout/carousel';

    export function CarouselDemo() {
      return (
        <div className="not-prose w-full max-w-xs px-12">
          <Carousel aria-label="Numbers">
            <CarouselContent>
              {Array.from({ length: 5 }, (_, index) => (
                <CarouselItem key={index}>
                  <div className="flex aspect-square items-center justify-center rounded-lg border text-4xl font-semibold">
                    {index + 1}
                  </div>
                </CarouselItem>
              ))}
            </CarouselContent>
            <CarouselPrevious />
            <CarouselNext />
          </Carousel>
        </div>
      );
    }

    ```
  </TabsContent>
</Tabs>


## Installation [#installation]

<Tabs defaultValue="CLI">
  <TabsList>
    <TabsTrigger value="CLI">
      CLI
    </TabsTrigger>

    <TabsTrigger value="Manual">
      Manual
    </TabsTrigger>
  </TabsList>

  <TabsContent value="CLI">
    <CodeBlockTabs defaultValue="npm" groupId="package-manager" persist>
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="npm">
          npm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="pnpm">
          pnpm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="yarn">
          yarn
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="bun">
          bun
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="npm">
        ```bash
        npx shadcn@latest add @oakoss/carousel
        ```
      </CodeBlockTab>

      <CodeBlockTab value="pnpm">
        ```bash
        pnpm dlx shadcn@latest add @oakoss/carousel
        ```
      </CodeBlockTab>

      <CodeBlockTab value="yarn">
        ```bash
        yarn dlx shadcn@latest add @oakoss/carousel
        ```
      </CodeBlockTab>

      <CodeBlockTab value="bun">
        ```bash
        bunx --bun shadcn@latest add @oakoss/carousel
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </TabsContent>

  <TabsContent value="Manual">
    <Steps>
      <Step>
        Set up the theme and base styles from [Installation](/docs/installation).
      </Step>

      <Step>
        Install the packages.

        <CodeBlockTabs defaultValue="npm" groupId="package-manager" persist>
          <CodeBlockTabsList>
            <CodeBlockTabsTrigger value="npm">
              npm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="pnpm">
              pnpm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="yarn">
              yarn
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="bun">
              bun
            </CodeBlockTabsTrigger>
          </CodeBlockTabsList>

          <CodeBlockTab value="npm">
            ```bash
            npm install cn embla-carousel-react@^8 react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="pnpm">
            ```bash
            pnpm add cn embla-carousel-react@^8 react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="yarn">
            ```bash
            yarn add cn embla-carousel-react@^8 react-aria-components tailwind-variants
            ```
          </CodeBlockTab>

          <CodeBlockTab value="bun">
            ```bash
            bun add cn embla-carousel-react@^8 react-aria-components tailwind-variants
            ```
          </CodeBlockTab>
        </CodeBlockTabs>
      </Step>

      <Step>
        Copy each file into your project, and change its `#/` imports to your aliases.

        * [`components/ui/feedback/spinner.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/feedback/spinner.tsx)

        * [`components/icons.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/icons.tsx)

        * [`components/icon-placeholder.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/icon-placeholder.tsx)

        * [`lib/cx.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/cx.ts)

        * [`lib/cn-config.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/cn-config.ts)

        * [`components/ui/inputs/button.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/inputs/button.tsx)

        * [`lib/recipes.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/lib/recipes.ts)

        * [`components/ui/layout/carousel.tsx`](https://github.com/oakoss/ui/blob/main/packages/ui/src/components/ui/layout/carousel.tsx)

        * [`hooks/use-carousel-state.ts`](https://github.com/oakoss/ui/blob/main/packages/ui/src/hooks/use-carousel-state.ts)
      </Step>

      <Step>
        Replace each `<IconPlaceholder>` in `icons.tsx` with your icon library's icon, named in its props. The CLI does this for you; without it, icons render as empty squares.
      </Step>
    </Steps>
  </TabsContent>
</Tabs>


## Usage [#usage]

```tsx
import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from '#/components/ui/layout/carousel';

<Carousel aria-label="Featured products">
  <CarouselContent>
    <CarouselItem>…</CarouselItem>
    <CarouselItem>…</CarouselItem>
    <CarouselItem>…</CarouselItem>
  </CarouselContent>
  <CarouselPrevious />
  <CarouselNext />
</Carousel>;
```

The buttons sit 3rem outside the slides, so leave that room beside the
carousel, or place them with `className`. `opts` passes options to
[Embla](https://www.embla-carousel.com/api/options/), such as `loop`,
`align`, `dragFree` and `breakpoints`, and `plugins` takes Embla plugins.

## Examples [#examples]

### Sizes [#sizes]

A slide's `basis-*` sets how many show at once, and responsive classes
change that by width. `opts={{ slidesToScroll: 'auto' }}` steps a view at a
time rather than a slide.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <CarouselSizes />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import {
      Carousel,
      CarouselContent,
      CarouselItem,
      CarouselNext,
      CarouselPrevious,
    } from '#/components/ui/layout/carousel';

    export function CarouselSizes() {
      return (
        <div className="not-prose w-full max-w-md px-12">
          <Carousel aria-label="Albums" opts={{ slidesToScroll: 'auto' }}>
            <CarouselContent>
              {Array.from({ length: 9 }, (_, index) => (
                <CarouselItem className="basis-1/2 sm:basis-1/3" key={index}>
                  <div className="flex aspect-square items-center justify-center rounded-lg border text-2xl font-semibold">
                    {index + 1}
                  </div>
                </CarouselItem>
              ))}
            </CarouselContent>
            <CarouselPrevious />
            <CarouselNext />
          </Carousel>
        </div>
      );
    }

    ```
  </TabsContent>
</Tabs>


### Loop and peek [#loop-and-peek]

`opts={{ loop: true }}` wraps from the last slide to the first, so neither
button disables. A basis under full width, such as `basis-4/5`, lets the
next slide peek in; `align: 'start'` lines the current slide up with the
start edge.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <CarouselLoop />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import {
      Carousel,
      CarouselContent,
      CarouselItem,
      CarouselNext,
      CarouselPrevious,
    } from '#/components/ui/layout/carousel';

    export function CarouselLoop() {
      return (
        <div className="not-prose w-full max-w-sm px-12">
          <Carousel aria-label="Highlights" opts={{ align: 'start', loop: true }}>
            <CarouselContent>
              {Array.from({ length: 5 }, (_, index) => (
                <CarouselItem className="basis-4/5" key={index}>
                  <div className="flex aspect-video items-center justify-center rounded-lg border text-2xl font-semibold">
                    {index + 1}
                  </div>
                </CarouselItem>
              ))}
            </CarouselContent>
            <CarouselPrevious />
            <CarouselNext />
          </Carousel>
        </div>
      );
    }

    ```
  </TabsContent>
</Tabs>


### Vertical [#vertical]

`orientation="vertical"` scrolls up and down; give `CarouselContent` a
height. The buttons sit above and below.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <CarouselVertical />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import {
      Carousel,
      CarouselContent,
      CarouselItem,
      CarouselNext,
      CarouselPrevious,
    } from '#/components/ui/layout/carousel';

    export function CarouselVertical() {
      return (
        <div className="not-prose w-full max-w-xs py-12">
          <Carousel aria-label="Steps" orientation="vertical">
            <CarouselContent className="h-48">
              {Array.from({ length: 5 }, (_, index) => (
                <CarouselItem className="basis-1/2" key={index}>
                  <div className="flex h-full items-center justify-center rounded-lg border text-2xl font-semibold">
                    {index + 1}
                  </div>
                </CarouselItem>
              ))}
            </CarouselContent>
            <CarouselPrevious />
            <CarouselNext />
          </Carousel>
        </div>
      );
    }

    ```
  </TabsContent>
</Tabs>


### Counter [#counter]

`setApi` hands you Embla's API, for controls and readouts of your own, such
as a visible count. The carousel already announces each slide, so a
counter like this one is hidden from screen readers.

<Tabs defaultValue="Preview">
  <TabsList>
    <TabsTrigger value="Preview">
      Preview
    </TabsTrigger>

    <TabsTrigger value="Code">
      Code
    </TabsTrigger>
  </TabsList>

  <TabsContent value="Preview">
    <CarouselCounter />
  </TabsContent>

  <TabsContent value="Code">
    ```tsx
    import {
      Carousel,
      type CarouselApi,
      CarouselContent,
      CarouselItem,
      CarouselNext,
      CarouselPrevious,
    } from '#/components/ui/layout/carousel';
    import { useEffect, useState } from 'react';

    export function CarouselCounter() {
      const [api, setApi] = useState<CarouselApi>();
      const [current, setCurrent] = useState(0);
      const [count, setCount] = useState(0);

      useEffect(() => {
        const update = () => {
          setCount(api?.scrollSnapList().length ?? 0);
          setCurrent((api?.selectedScrollSnap() ?? 0) + 1);
        };
        update();
        api?.on('select', update).on('reInit', update);
        return () => {
          api?.off('select', update).off('reInit', update);
        };
      }, [api]);

      return (
        <div className="not-prose w-full max-w-xs px-12">
          <Carousel aria-label="Photos" setApi={setApi}>
            <CarouselContent>
              {Array.from({ length: 5 }, (_, index) => (
                <CarouselItem key={index}>
                  <div className="flex aspect-square items-center justify-center rounded-lg border text-4xl font-semibold">
                    {index + 1}
                  </div>
                </CarouselItem>
              ))}
            </CarouselContent>
            <CarouselPrevious />
            <CarouselNext />
          </Carousel>
          {/* The carousel announces each slide, so the counter isn't repeated. */}
          <p aria-hidden className="mt-2 text-center text-sm text-muted-foreground">
            {current} / {count}
          </p>
        </div>
      );
    }

    ```
  </TabsContent>
</Tabs>


## Accessibility [#accessibility]

The carousel follows the
[carousel pattern](https://www.w3.org/WAI/ARIA/apg/patterns/carousel/): a
`region` with the role description "carousel", whose name is required
(`aria-label` or `aria-labelledby`), holding slides that are `group`s with
the role description "slide", each named by its position ("2 of 5").

This component adds:

- **Hidden slides:** slides out of view are `inert`, so they leave the tab
  order and the accessibility tree; a slide keeps focus it already holds as
  it scrolls away, until focus leaves it.
- **Announcements:** once the carousel moves and comes to rest, a `status`
  region names the slides at least half on screen ("Slide 2 of 5", "Slides
  4 to 6 of 9"); nothing is announced on load or mid-drag.
- **Focus at the ends:** when Previous or Next disables at the first or last
  slide, focus moves to the other button rather than to the page.
- **Reduced motion:** with `prefers-reduced-motion`, the buttons jump
  between slides rather than animating; a drag still follows the pointer.
- **Clicks after a drag:** releasing a drag over a link or button in a slide
  doesn't activate it, which Embla handles.
- **Nested carousels:** a drag that starts in a carousel inside a slide moves
  only that one, unless `opts` sets its own `watchDrag`.
- **Visibility:** the buttons keep their border and focus outline in forced
  colors.

The carousel takes no arrow keys, since the pattern defines none, so text
fields and other controls in a slide keep theirs.

| Key               | Action                                                    |
| ----------------- | --------------------------------------------------------- |
| `Tab`             | Moves through the buttons and the content of slides shown |
| `Enter` / `Space` | Shows the previous or next slide from its button          |

## API [#api]

### Carousel [#carousel]

A `div` with Embla's carousel. Its strings, from the role descriptions to
the announcement, can be replaced through `messages`.

#### Props [#props]

| Prop               | Type                        | Description                                                           |
| ------------------ | --------------------------- | --------------------------------------------------------------------- |
| `aria-label?`      | `string`                    | Defines a string value that labels the current element.               |
| `aria-labelledby?` | `string`                    | Identifies the element (or elements) that labels the current element. |
| `messages?`        | `Partial<CarouselMessages>` |                                                                       |
| `opts?`            | `Partial<OptionsType>`      |                                                                       |
| `orientation?`     | `union`                     |                                                                       |
| `plugins?`         | `array`                     |                                                                       |
| `setApi?`          | `function`                  |                                                                       |

Also accepts `div` props.

### CarouselContent [#carouselcontent]

The viewport and the row of slides, as `div`s; its `className` styles the
row.

### CarouselItem [#carouselitem]

A slide, as a `div`. Size it with `basis-*`.

### CarouselPrevious and CarouselNext [#carouselprevious-and-carouselnext]

Extend [Button](/docs/components/button), outline and icon-sized by default.
Their names come from `messages`, or their own `aria-label`.

#### Data attributes [#data-attributes]

| Attribute   | Present when                                                                                    |
| ----------- | ----------------------------------------------------------------------------------------------- |
| `data-slot` | Always: `carousel`, `carousel-content`, `carousel-item`, `carousel-previous` or `carousel-next` |
| `inert`     | On a `carousel-item` out of view that doesn't hold focus                                        |

### useCarousel [#usecarousel]

Reads the carousel inside it, for parts of your own, with shadcn's shape:
`api`, `carouselRef`, `opts`, `orientation`, `scrollPrev`, `scrollNext`,
`canScrollPrev` and `canScrollNext`, plus `slidesInView`, the indexes of
slides with any part on screen. `scrollPrev` and `scrollNext` jump under
reduced motion, as the buttons do.
