# Carousel

A Carousel that is a 3D slide projector: Next turns the tray a slot, the next slide drops into the gate and your content lands on the wall. Built with [ui-sdk](https://ui-sdk.dev).

A React component for projects built on shadcn/ui. The [interactive page](https://alexandrejoly.me/lab/literal-ui/carousel) shows each example live.

```tsx
import {
  Carousel,
  CarouselFocus,
  CarouselNext,
  CarouselPrevious,
  CarouselContent,
  CarouselItem,
  CarouselStage,
  CarouselPlayToggle,
} from "@/components/ui/carousel/carousel";
import { CarouselAudio } from "@/components/ui/carousel/carousel-sound";

const slides = [
  { alt: "Sand dunes at dusk", src: "/lab/literal-ui/carousel/dunes.webp" },
  { alt: "A pine forest in fog", src: "/lab/literal-ui/carousel/forest.webp" },
  {
    alt: "A glacier above a lake",
    src: "/lab/literal-ui/carousel/glacier.webp",
  },
  { alt: "A red canyon at noon", src: "/lab/literal-ui/carousel/canyon.webp" },
  { alt: "An alpine meadow", src: "/lab/literal-ui/carousel/meadow.webp" },
  { alt: "A fishing harbour", src: "/lab/literal-ui/carousel/harbor.webp" },
];

export const CarouselDemo = () => (
  <Carousel className="w-full max-w-xl">
    <CarouselStage>
      <CarouselContent aria-label="Holiday slides">
        {slides.map((slide) => (
          <CarouselItem key={slide.src}>
            <img
              alt={slide.alt}
              className="size-full object-cover"
              height={848}
              src={slide.src}
              width={1264}
            />
          </CarouselItem>
        ))}
      </CarouselContent>
      <CarouselFocus />
      <CarouselPlayToggle />
      <CarouselPrevious />
      <CarouselNext />
    </CarouselStage>
    <CarouselAudio />
  </Carousel>
);
```

## Installation

With the shadcn CLI:

```bash
pnpm dlx shadcn@latest add @alexandrejoly/carousel
```

## Usage

```tsx
import {
  Carousel,
  CarouselContent,
  CarouselFocus,
  CarouselItem,
  CarouselNext,
  CarouselPlayToggle,
  CarouselPrevious,
  CarouselStage,
} from "@/components/ui/literal-ui/carousel/carousel";
import { CarouselAudio } from "@/components/ui/literal-ui/carousel/carousel-sound";
```

```tsx
<Carousel>
  <CarouselStage>
    <CarouselContent aria-label="Slides">
      <CarouselItem>
        <img alt="Dunes at dusk" src="/dunes.jpg" />
      </CarouselItem>
      <CarouselItem>
        <img alt="A forest in fog" src="/forest.jpg" />
      </CarouselItem>
    </CarouselContent>
    <CarouselFocus />
    <CarouselPlayToggle />
    <CarouselPrevious />
    <CarouselNext />
  </CarouselStage>
  <CarouselAudio />
</Carousel>
```

## Examples

### Timer

`defaultPlaying` starts the timer, and `interval` sets how long each slide shows. The switch slides on, and Next or Previous turns it off, as on the projector. Pointing at the picture pauses it. It stays off for people who prefer reduced motion, until they slide the switch.

```tsx
import {
  Carousel,
  CarouselFocus,
  CarouselNext,
  CarouselPrevious,
  CarouselContent,
  CarouselItem,
  CarouselStage,
  CarouselPlayToggle,
} from "@/components/ui/carousel/carousel";
import { CarouselAudio } from "@/components/ui/carousel/carousel-sound";

const slides = [
  { alt: "Sand dunes at dusk", src: "/lab/literal-ui/carousel/dunes.webp" },
  { alt: "A pine forest in fog", src: "/lab/literal-ui/carousel/forest.webp" },
  {
    alt: "A glacier above a lake",
    src: "/lab/literal-ui/carousel/glacier.webp",
  },
];

export const CarouselTimer = () => (
  <Carousel className="w-full max-w-sm" defaultPlaying interval={3000}>
    <CarouselStage>
      <CarouselContent aria-label="Holiday slides">
        {slides.map((slide) => (
          <CarouselItem key={slide.src}>
            <img
              alt={slide.alt}
              className="size-full object-cover"
              height={848}
              src={slide.src}
              width={1264}
            />
          </CarouselItem>
        ))}
      </CarouselContent>
      <CarouselFocus />
      <CarouselPlayToggle />
      <CarouselPrevious />
      <CarouselNext />
    </CarouselStage>
    <CarouselAudio />
  </Carousel>
);
```

### Controlled

Set `page` to show a slide from your own code. Here each button asks for its slide, and the tray turns to it. The keys still work by hand, and `onPageChange` reports every change with its reason.

```tsx
import { Button } from "@/components/ui/button";
import {
  Carousel,
  CarouselFocus,
  CarouselNext,
  CarouselPrevious,
  CarouselContent,
  CarouselItem,
  CarouselStage,
  CarouselPlayToggle,
} from "@/components/ui/carousel/carousel";
import { CarouselAudio } from "@/components/ui/carousel/carousel-sound";
import { useState } from "react";

const slides = [
  {
    alt: "A red canyon at noon",
    name: "Canyon",
    src: "/lab/literal-ui/carousel/canyon.webp",
  },
  {
    alt: "An alpine meadow",
    name: "Meadow",
    src: "/lab/literal-ui/carousel/meadow.webp",
  },
  {
    alt: "A fishing harbour",
    name: "Harbour",
    src: "/lab/literal-ui/carousel/harbor.webp",
  },
];

export const CarouselControlled = () => {
  const [page, setPage] = useState(1);

  return (
    <div className="flex w-full max-w-sm flex-col gap-6">
      <Carousel onPageChange={setPage} page={page}>
        <CarouselStage>
          <CarouselContent aria-label="Holiday slides">
            {slides.map((slide) => (
              <CarouselItem key={slide.src}>
                <img
                  alt={slide.alt}
                  className="size-full object-cover"
                  height={848}
                  src={slide.src}
                  width={1264}
                />
              </CarouselItem>
            ))}
          </CarouselContent>
          <CarouselFocus />
          <CarouselPlayToggle />
          <CarouselPrevious />
          <CarouselNext />
        </CarouselStage>
        <CarouselAudio />
      </Carousel>
      <div className="flex gap-2">
        {slides.map((slide, index) => (
          <Button
            aria-pressed={page === index + 1}
            key={slide.src}
            onClick={() => setPage(index + 1)}
            variant={page === index + 1 ? "secondary" : "outline"}
          >
            {slide.name}
          </Button>
        ))}
      </div>
    </div>
  );
};
```

## API reference

### Carousel

Holds the page, the timer and the focus. It loops: Next on the last slide turns the tray on round to the first.

Takes every prop of `div`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `defaultPage` | `number` | `1` | The slide it starts on, from 1. |
| `page` | `number` | — | The current slide, from 1, controlled. |
| `onPageChange` | `(page: number, details: ChangeDetails) => void` | — | Runs when the slide wants to change. `details.reason` says why, and `details.cancel()` keeps the slide. |
| `onPageChangeComplete` | `(page: number) => void` | — | Runs once the carousel has settled on the new slide. |
| `defaultPlaying` | `boolean` | `false` | Starts with the timer on. It stays off for people who prefer reduced motion. |
| `playing` | `boolean` | — | Whether the timer runs, controlled. |
| `onPlayingChange` | `(playing: boolean, details: ChangeDetails) => void` | — | Runs when the timer starts or stops, by the switch, by a key or by keyboard focus moving into the picture. |
| `interval` | `number` | `5000` | How long each slide shows while the timer runs, in ms. |
| `defaultFocus` | `number` | `50` | Where the focus knob starts, from 0 to 100. The picture is sharp at 50 and blurs toward either end. |
| `focus` | `number` | — | The focus, controlled. |
| `onFocusChange` | `(focus: number) => void` | — | Runs when the focus knob turns. |

### CarouselStage

The projector in 3D, with the controls laid over its parts and the picture on the wall in front of it. It shows a still picture of the projector until the scene loads, and keeps it without WebGL 2: the slides still change.

Takes every prop of `div`.

### CSS variables

Custom properties the stage reads from your CSS, set on it or on any element around it. The last one it sets itself, for your page to read.

| Variable | Type | Default | Description |
| --- | --- | --- | --- |
| `--literal-lamp` | `color` | `oklch(0.985 0.04 92 / 0.62)` | The lamp's bare light, shown on the wall between two slides. |
| `--literal-wall` | `color` | — | The colour behind the picture. The picture's darkest parts rise to it, as projected light does on a wall. |
| `--literal-lighting` | `dim` | — | Set to `dim` on a dark page: the projector is lit by its own picture and casts a shadow toward the viewer. Read once, when the stage mounts. |
| `--literal-film` | `number` | — | Set by the stage: 1 while a slide shows, 0 while the lamp shines bare. Read it to light the page between two slides. |

### CarouselContent

The picture on the wall: the slides, one to a view. Name it with `aria-label`. The arrow keys change slide once it has focus. It sets the layout itself, so it leaves out `align`, `drag`, `dragFree`, `loop`, `orientation`, `skipSnaps` and `slidesPerPage`.

Takes every prop of [UI SDK Carousel](https://ui-sdk.dev/docs/react/components/carousel).

### CarouselItem

One slide. Each one also stands in the tray, from the slot over the gate on, up to 80 of them.

Takes every prop of [UI SDK CarouselItem](https://ui-sdk.dev/docs/react/components/carousel).

### CarouselNext

The key on the right. The slide lifts, the tray turns a slot on and the next slide drops into the gate.

Takes every prop of [UI SDK CarouselNext](https://ui-sdk.dev/docs/react/components/carousel).

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` | `string` | `"Next slide"` | Names the key. |

### CarouselPrevious

The key on the left. The tray turns a slot back.

Takes every prop of [UI SDK CarouselPrevious](https://ui-sdk.dev/docs/react/components/carousel).

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` | `string` | `"Previous slide"` | Names the key. |

### CarouselPlayToggle

The timer's slide switch: on, the slides change by themselves.

Takes every prop of [UI SDK CarouselPlayToggle](https://ui-sdk.dev/docs/react/components/carousel).

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `startLabel` | `string` | `"Start the timer"` | Names the switch while the timer is off. |
| `stopLabel` | `string` | `"Stop the timer"` | Names the switch while the timer runs. |

### CarouselFocus

The focus knob. Drag up or down, or use the arrow keys: the lens moves in or out and the picture blurs. Its value and range are the carousel's `focus`, so it leaves out `value`, `defaultValue`, `min`, `max` and `orientation`.

Takes every prop of [UI SDK Slider](https://ui-sdk.dev/docs/react/components/slider).

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` | `string` | `"Focus"` | Names the knob. |

### CarouselAudio

Plays the projector's recorded sounds, its fan included. It renders nothing, and loads nothing until the page is pressed. Import it from `carousel-sound`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `muted` | `boolean` | `false` | Silences this projector alone. |

### CarouselAudioProvider

Mutes every `CarouselAudio` inside it, for a sound preference that covers the whole app or one part of it. It renders nothing of its own.

| Prop | Type | Description |
| --- | --- | --- |
| `muted` | `boolean` | Silences the projectors below. Turned off during a press, their sounds unlock at once. |

### useCarouselEvents

Follows what the projector does as it happens, as `CarouselAudio` does: the keys, the shutter, the slide lifting and dropping, the tray, the timer and the focus knob.

| Returns | Type | Description |
| --- | --- | --- |
| `subscribe` | `(listener: (event: CarouselEvent) => void) => () => void` | Calls `listener` with each event. Returns the unsubscribe. |

[Back to Literal UI](https://alexandrejoly.me/lab/literal-ui.md)
