Carousel

Animated

A draggable strip of slides that snaps where the flick was aimed.

Source
$npx @dinachi/cli@latest add carousel

Projection

Where the flick was aimed

Velocity handoff

No seam on release

Elastic edges

The end of the set, felt

Reduced motion

Becomes a scroll-snap strip

className
CarouselDots
CarouselPrevious
disabled

Usage

import {
  Carousel,
  CarouselDots,
  CarouselNext,
  CarouselPrevious,
  CarouselSlide,
  CarouselViewport,
} from "@/components/ui/carousel"

const slides = [
  { id: "1", title: "Projection" },
  { id: "2", title: "Velocity handoff" },
  { id: "3", title: "Elastic edges" },
]

export function Highlights() {
  return (
    <Carousel label="Highlights">
      <CarouselViewport>
        {slides.map((slide) => (
          <CarouselSlide key={slide.id} label={slide.title}>
            <div className="h-40 rounded-lg border p-4">{slide.title}</div>
          </CarouselSlide>
        ))}
      </CarouselViewport>
      <div className="flex items-center justify-between">
        <CarouselDots labels={slides.map((s) => s.title)} />
        <div className="flex gap-1.5">
          <CarouselPrevious />
          <CarouselNext />
        </div>
      </div>
    </Carousel>
  )
}

Examples

Projection

Where the flick was aimed

Velocity handoff

No seam on release

Elastic edges

The end of the set, felt

Reduced motion

Becomes a scroll-snap strip

Flick it, then drag it slowly and let go.
import {
  Carousel,
  CarouselViewport,
  CarouselSlide,
  CarouselDots,
  CarouselPrevious,
  CarouselNext,
} from '@/components/ui/carousel';

export function Example() {
  return (
    <Carousel label="Gesture techniques">
      <CarouselViewport>
        {slides.map((slide) => (
          <CarouselSlide key={slide.id} label={slide.title}>
            <Card slide={slide} />
          </CarouselSlide>
        ))}
      </CarouselViewport>

      <div className="flex items-center justify-between">
        <CarouselDots labels={slides.map((s) => s.title)} />
        <div className="flex gap-1.5">
          <CarouselPrevious />
          <CarouselNext />
        </div>
      </div>
    </Carousel>
  );
}

Notes

Motion tier

Needs motion. add --all skips it; install with add --motion or by name.

Snapping

On release, the velocity is projected 200ms forward from the strip's position and the strip snaps to the slide that lands in. A flick advances; a slow drag that stops short does not.

Slide sizes

Slide positions are measured from layout, so gaps set by class, uneven widths and container padding all work. A resize re-aligns the strip to the current slide. The dots count the measured slides.

Reduced motion

The drag is replaced by a native scroll-snap strip. Dots and arrows jump instead of animating. Honours the OS setting and an app-level <MotionConfig reducedMotion>.

When to use it

  • A short set viewed one at a time: highlights, screenshots, testimonials. Suits touch-first surfaces.
  • For content to compare side by side, use a grid.
  • Do not hide content that matters; off-screen slides read as optional.

API

label
agent
string = "Carousel"
Names the carousel for screen readers and the agent.
indexnumber
Controlled current slide, from 0.
defaultIndexnumber = 0
Starting slide when uncontrolled.
onIndexChange
agent
(index, details) => void
Runs for people and agents. details.reason says why, and details.cancel() keeps the current slide.
disabled
agent
boolean = false
Turns off the drag, the arrows and the dots, and blocks agents.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as carousel.

Reads

label

The Carousel's label.

value

The current slide's label, or its number when unnamed.

items

Every slide's label, in order.

disabled

True when the carousel is disabled.

Actions

goTo("Velocity handoff")

Moves by label, or by number from 1. A miss lists the slides.

An agent's move runs through onIndexChange like a drag or dot press. isAgentChange(details) is true for it; details.cancel() refuses it and the agent is told the app refused. A disabled carousel refuses on its own. The dots and arrows are not listed separately.

await agent.act({
  steps: [{ ref: "Gesture techniques", action: "goTo", args: ["Velocity handoff"] }],
})

Accessibility

  • The root is role="group" with aria-roledescription="carousel" and your label.
  • Each slide is a group with aria-roledescription="slide", named with its position: "2 of 4: Velocity handoff".
  • The dots are buttons in a group named "Choose a slide"; the current one has aria-current.
  • The set does not wrap. The arrow at either end is disabled.
  • The strip has no arrow-key handling. Keyboard users move with the dots and arrows.