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 (opens in a new tab).

Sand dunes at dusk
A pine forest in fog
A glacier above a lake
A red canyon at noon
An alpine meadow
A fishing harbour

Installation

pnpm dlx shadcn@latest add @alexandrejoly/carousel

Usage

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";
<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.

Sand dunes at dusk
A pine forest in fog
A glacier above a lake

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.

A red canyon at noon
An alpine meadow
A fishing harbour

API reference

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.

PropTypeDefault
defaultPage

The slide it starts on, from 1.

Default: 1

number1
page

The current slide, from 1, controlled.

numberNone
onPageChange

Runs when the slide wants to change. details.reason says why, and details.cancel() keeps the slide.

(page: number, details: ChangeDetails) => voidNone
onPageChangeComplete

Runs once the carousel has settled on the new slide.

(page: number) => voidNone
defaultPlaying

Starts with the timer on. It stays off for people who prefer reduced motion.

Default: false

booleanfalse
playing

Whether the timer runs, controlled.

booleanNone
onPlayingChange

Runs when the timer starts or stops, by the switch, by a key or by keyboard focus moving into the picture.

(playing: boolean, details: ChangeDetails) => voidNone
interval

How long each slide shows while the timer runs, in ms.

Default: 5000

number5000
defaultFocus

Where the focus knob starts, from 0 to 100. The picture is sharp at 50 and blurs toward either end.

Default: 50

number50
focus

The focus, controlled.

numberNone
onFocusChange

Runs when the focus knob turns.

(focus: number) => voidNone

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.

VariableTypeDefault
--literal-lamp

The lamp's bare light, shown on the wall between two slides.

Default: oklch(0.985 0.04 92 / 0.62)

coloroklch(0.985 0.04 92 / 0.62)
--literal-wall

The colour behind the picture. The picture's darkest parts rise to it, as projected light does on a wall.

colorNone
--literal-lighting

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.

dimNone
--literal-film

Set by the stage: 1 while a slide shows, 0 while the lamp shines bare. Read it to light the page between two slides.

numberNone

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 (opens in a new tab).

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 (opens in a new tab).

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 (opens in a new tab).

PropTypeDefault
aria-label

Names the key.

Default: "Next slide"

string"Next slide"

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

Takes every prop of UI SDK CarouselPrevious (opens in a new tab).

PropTypeDefault
aria-label

Names the key.

Default: "Previous slide"

string"Previous slide"

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

Takes every prop of UI SDK CarouselPlayToggle (opens in a new tab).

PropTypeDefault
startLabel

Names the switch while the timer is off.

Default: "Start the timer"

string"Start the timer"
stopLabel

Names the switch while the timer runs.

Default: "Stop the timer"

string"Stop the timer"

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 (opens in a new tab).

PropTypeDefault
aria-label

Names the knob.

Default: "Focus"

string"Focus"

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.

PropTypeDefault
muted

Silences this projector alone.

Default: false

booleanfalse

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.

PropType
muted

Silences the projectors below. Turned off during a press, their sounds unlock at once.

boolean

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.

ReturnsType
subscribe

Calls listener with each event. Returns the unsubscribe.

(listener: (event: CarouselEvent) => void) => () => void