Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Carousel for UI SDK

A row of slides with Previous and Next buttons, swipe and arrow keys, for galleries, quotes and onboarding. A React carousel on UI SDK.

On this page9 sections

A carousel shows one slide, or a few, from a longer row, with Previous and Next buttons to move along it and a swipe on touch screens. Reach for it for a product gallery, customer quotes or a short onboarding sequence, where the slides are alike and nobody needs to see them all at once.

When each panel has a name people jump between, use Tabs. Content that people must read in full belongs in a list or a grid, where nothing hides behind a button.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Carousel anatomy: content, item, previous, next, indicator group

Usage

import { Card, CardContent } from "@/components/ui/card";
import {
  Carousel, CarouselContent, CarouselItem, CarouselNext,
  CarouselPrevious,
} from "@/components/ui/carousel";
const slides = [1, 2, 3, 4, 5];

const CarouselDemo = () => (
  <Carousel className="w-full max-w-[12rem] sm:max-w-xs">
    <CarouselContent>
      {slides.map((slide) => (
        <CarouselItem key={slide}>
          <div className="p-1">
            <Card>
              <CardContent className="flex aspect-square items-center justify-center p-6">
                <span className="font-semibold text-4xl">{slide}</span>
              </CardContent>
            </Card>
          </div>
        </CarouselItem>
      ))}
    </CarouselContent>
    <CarouselPrevious />
    <CarouselNext />
  </Carousel>
);

Accessibility

The carousel is a section with the region role and aria-roledescription set to carousel, and each CarouselItem is a group described as a slide and labeled with its position, such as 2 of 5. Slides less than half in view are inert and hidden from screen readers, and the slides are a polite live region, so a new one is announced, except while autoplay runs. Previous and Next have hidden text labels and stay focusable at either end, marked aria-disabled. The region's name is the generic Carousel, so pass an aria-label that says what the slides hold, such as Customer quotes.

Tab
Moves focus to the next focusable element: the slides themselves when they hold nothing focusable, or the links and controls of the slides in view, then Previous and Next. Both stay focusable at the first or last slide.
Shift+Tab
Moves focus to the previous focusable element.
Enter
When focus is on Previous or Next, moves one slide back or forward.
Space
When focus is on Previous or Next, moves one slide back or forward.
Arrow Right
When focus is in a horizontal carousel, outside a field or a widget that uses the arrows itself, moves to the next slide, or to the previous one when the carousel is right to left. Focus inside a slide moves to the slide that comes in.
Arrow Left
When focus is in a horizontal carousel, outside a field or a widget that uses the arrows itself, moves to the previous slide, or to the next one when the carousel is right to left. Focus inside a slide moves to the slide that comes in.
Arrow Down
When focus is in a vertical carousel, outside a field or a widget that uses the arrows itself, moves to the next slide. Focus inside a slide moves to the slide that comes in.
Arrow Up
When focus is in a vertical carousel, outside a field or a widget that uses the arrows itself, moves to the previous slide. Focus inside a slide moves to the slide that comes in.
Home
When focus is in the slides, moves to the first slide.
End
When focus is in the slides, moves to the last slide.

The arrow keys leave the carousel alone while Alt, Ctrl, Cmd or Shift is held, and inside a slide, a slider, a list or tabs keep them, so those widgets work as they do anywhere else.

Demos

Size

Each CarouselItem takes half the track with basis-1/2, and a third from the lg breakpoint, while align set to start in opts lines each slide up with the leading edge instead of centering it. Use a width class when the slides are thumbnails or logos and one full-width slide would waste the row. The carousel's own width is capped, which keeps the cards a readable size.

Preview width

Spacing

The gap between slides is each item's start padding, offset by a negative margin on the track. Here ps-1 on every CarouselItem and -ms-1 on CarouselContent cut the default 16 pixels to 4, with two cards in view and three from the lg breakpoint. Change both classes together, or the first card shifts off its edge.

Preview width

Orientation

orientation set to vertical turns the track into a column 270 pixels tall, with each CarouselItem at half of it, so two cards show at a time. Previous and Next move above and below, their chevrons turned to point up and down. The height on CarouselContent is what the slides are sized against, so a vertical carousel needs one. Use it for a short rail beside a detail view.

Preview width

API

setApi hands the demo the carousel's API, with Embla's names over UI SDK's controller, and the caption under the carousel reads selectedScrollSnap() on every select event to show Slide 2 of 5. The caption is the page's own markup, so word it however the page speaks. Reach for the API when something outside the carousel must follow it, such as a caption, a thumbnail strip or a step counter.

Preview width

Indicators

Five dots under the slides, one button per entry of scrollSnapList(), each named Go to slide 1 to 5 and calling scrollTo with its index. A select listener marks the current dot with aria-current and a brighter fill. Reach for dots when the slides are few and people want to jump between them, such as the photos of one product.

Preview width

Plugin

defaultPlaying starts UI SDK's own autoplay, and interval set to 2000 moves a slide every two seconds, back to the first after the last. The button under the slides calls setPlaying on the API's controller, and its label follows the autoplay:play and autoplay:stop events. The pointer over the carousel pauses it, and paging or keyboard focus inside stops it. Use autoplay only for slides nobody has to read in time.

Preview width

Multiple

Each CarouselItem is full width on a phone, half the track from the sm breakpoint and a third from lg, through basis classes. Previous and Next are hidden below sm, where one card fills the frame and a swipe moves it. Pick this for tiles people compare side by side, such as plans or products.

Preview width

RTL

Arabic numerals, with dir set to rtl on Carousel and direction set to rtl in opts. UI SDK needs only one: direction just fills in dir, and the carousel reads the direction it inherits, so on a right-to-left page it needs neither. The slides run right to left, with Previous on the right and Next on the left, and ArrowLeft brings in the next slide.

Preview width

FAQ

How do I show several slides at once in the Carousel?

Give each CarouselItem a width class, such as basis-1/2 for two in view or sm:basis-1/2 lg:basis-1/3 to add slides as the screen grows. Pass opts with align set to start so each press lines a slide up with the leading edge rather than centering it.

The Multiple demo uses those breakpoints. A width just under a full step, such as basis-[85%], leaves the next slide peeking in at the edge, which tells people there is more.

How do I make the Carousel autoplay in React?

Set defaultPlaying on Carousel, with an interval in milliseconds, such as 2000, in place of the default 5000, as the Plugin demo does. Autoplay goes back to the first slide after the last, pauses under the pointer, while the tab is hidden and while the carousel is scrolled out of view, and stops for good once someone pages, drags or moves keyboard focus inside. It doesn't start on its own when the reader prefers reduced motion.

Moving content needs a way to stop it, so put a button beside the carousel that calls setPlaying on the API's controller, as the demo does under its slides.

How do I get the current slide of the Carousel?

Pass setApi to Carousel to receive its API, which keeps Embla's names over UI SDK's controller: read selectedScrollSnap(), counted from zero, and listen for its select event to follow every change. The API demo prints Slide 2 of 5 under the carousel this way. The same API moves the carousel from your code: scrollTo with an index jumps to that slide.

How do I loop the Carousel back to the first slide?

Pass opts with loop set to true. UI SDK then moves slides from one end of the row to the other while it rests, so after the last slide the first one comes in from the same side, and neither button is ever disabled. Looping needs a page of slides on either side of the current one, and with fewer, Next on the last page scrolls back to the first instead.

How do I make a vertical Carousel?

Set orientation to vertical on Carousel and give CarouselContent a fixed height, such as 270 pixels, since the slides are sized against it. Previous and Next move above and below, with their chevrons turned to point up and down, as in the Orientation demo.

Give each CarouselItem a basis class to fit more than one slide in that height, as the demo does with basis-1/2. With focus in the carousel, ArrowUp and ArrowDown move it, in place of ArrowLeft and ArrowRight.

How do I add dots or slide indicators to a Carousel?

This carousel has no dots part, so build them from its API, which keeps Embla's names. Get it through setApi, render one button per entry of scrollSnapList() that calls scrollTo with its index, and mark the current one from selectedScrollSnap() in a select listener, as the Indicators demo does. Label each button, such as Go to slide 3, since a dot has no text.

How do I let people drag the Carousel with a mouse?

There is nothing to add: the slides scroll natively for touch, trackpads and wheels, and UI SDK follows a mouse or pen drag as it ships. Pass opts with watchDrag set to false to turn all of them off, for example when the slides hold a map that needs its own drag. Fields inside a slide, such as a range input, keep their own pointer either way, and dragFree set to true in opts lets a fling coast without snapping to a slide.

Is the UI SDK Carousel accessible?

Mostly, with one gap you close yourself. The carousel is a region described as a carousel, each slide a group labeled with its position, such as 2 of 5, Previous and Next have hidden text labels, and the arrow keys move it while focus is inside. Its name is the generic Carousel, so pass an aria-label that says what it holds, such as Customer quotes.

Many people never press Next, so keep anything they must not miss, such as a price or a deadline, outside the carousel. The Accessibility section lists every key.

API Reference

<div> data-slot="carousel"

PropType
page?number

The current page, from 1. Pass it to control the carousel.

defaultPage?number

The page it starts on, from 1, when uncontrolled.

Default
1
onPageChange?(page: number, details: ChangeDetails<CarouselPageChangeReason>) → void

Runs when the page wants to change. details.reason says why, and details.cancel() stays on the page.

Type
(page: number, details: ChangeDetails<CarouselPageChangeReason>) → void
Parameters

page – number

details – ChangeDetails<CarouselPageChangeReason>

Returns
void
playing?boolean

Whether it rotates through the pages on its own. Pass it to control rotation.

defaultPlaying?boolean

Whether it starts rotating, when uncontrolled. It doesn't when the user prefers reduced motion; the play toggle still starts it.

Default
false
onPlayingChange?(playing: boolean, details: ChangeDetails<CarouselPlayingChangeReason>) → void

Runs when rotation wants to start or stop. Keyboard focus moving in and the user paging stop it, as the carousel pattern asks.

Type
(playing: boolean, details: ChangeDetails<CarouselPlayingChangeReason>) → void
Parameters

playing – boolean

details – ChangeDetails<CarouselPlayingChangeReason>

Returns
void
align?CarouselAlign

Where a page lines up in the viewport.

Default
"start"
controller?CarouselController

Pages the carousel from anywhere. Create one with createCarouselController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Whether it stops being a carousel for now, like at a breakpoint where the slides lay out as a grid. The parts drop their roles, inline styles and listeners, and the controls hide.

Default
false
drag?boolean

Whether swiping, dragging and scrolling move the slides. Without it, only the parts, keys and controllers do.

Default
true
dragFree?boolean

Whether swipes and drags coast to wherever they stop instead of a page. The nearest page becomes the current one.

Default
false
interval?number

How long each page shows while rotating, in ms. Hovering, a hidden tab and scrolling the carousel out of view pause it.

Default
5000
loop?boolean

Whether the slides go round: Next on the last page goes to the first, which comes in after the last, and a swipe back from the first page reaches the last. With fewer than a page of slides to either side, Next on the last page scrolls back to the first instead.

Default
false
onPageChangeComplete?(page: number) → void

Runs once the slides come to rest on a page, after a spring, a drag or a scroll, with that page. Start work there that would stutter the motion.

Parameters

page – number

Returns
void
opts?object

Embla's options UI SDK can follow, such as loop or align.

Type
{ active?: boolean; align?: "start" | "center" | "end"; axis?: "x" | "y"; direction?: "ltr" | "rtl"; dragFree?: boolean; loop?: boolean; skipSnaps?: boolean; slidesToScroll?: number | "auto"; startIndex?: number; watchDrag?: boolean }
orientation?CarouselOrientation

The orientation of the element.

Default
"horizontal"
plugins?CarouselPlugin[]

Started once the slides are measured, and destroyed with the carousel.

setApi?(api: CarouselApi) → void

Called with the carousel's API once it has measured its slides, to control the carousel from outside.

Parameters

api – { canScrollNext: () → boolean; canScrollPrev: () → boolean; containerNode: () → HTMLElement | null; controller: CarouselController; off: (event: CarouselEvent, handler: CarouselEventHandler) → CarouselApi; on: (event: CarouselEvent, handler: CarouselEventHandler) → CarouselApi; previousScrollSnap: () → number; rootNode: () → HTMLElement | null; scrollNext: () → void; scrollPrev: () → void; scrollProgress: () → number; scrollSnapList: () → number[]; scrollTo: (index: number) → void; selectedScrollSnap: () → number; slideNodes: () → HTMLElement[] }

Returns
void
skipSnaps?boolean

Whether a fast swipe can pass over pages. Without it, one swipe moves one page.

Default
false
slidesPerPage?CarouselSlidesPerPage

How many slides one page holds: Next moves this many, and each indicator stands for this many. "auto" fits as many as the viewport shows.

Default
1
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-orientation"horizontal" | "vertical"

Which way the slides scroll, and which arrow keys page.

Type
"horizontal" | "vertical"
data-paused?string

Present while rotation is paused for now: hovered, the tab hidden, scrolled out of view or held. Pause a progress animation with it.

data-playing?string

Present while it rotates on its own.

CSS variableType
--carousel-intervalstring

How long each page shows while rotating, like 500ms. Time a progress bar's animation with it.

--carousel-progressstring

How far the slides have scrolled, from 0 at the first page to 1 at the last, or round the loop. It follows every frame of a scroll or spring.

CarouselContent

<div> data-slot="carousel-content"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-orientation"horizontal" | "vertical"

Which way the slides scroll, and which arrow keys page.

Type
"horizontal" | "vertical"
CSS variableType
--carousel-displaystring

Set it yourself: how the content lays out its slides, like grid. Flex by default.

CarouselItem

<div> data-slot="carousel-item"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-active?string

Present on the slides of the current page.

data-in-view?string

Present on slides at least half in view. Slides without it are inert and hidden from screen readers.

data-orientation"horizontal" | "vertical"

Which way the slides scroll, and which arrow keys page.

Type
"horizontal" | "vertical"
CSS variableType
--carousel-item-progressstring

How far the slide is from its page's resting place, from -1 a page behind to 1 a page ahead, and 0 at rest. It follows every frame of a scroll or spring.

CarouselNext

<button> data-slot="carousel-previous"

PropType
controller?CarouselController

The carousel this part drives when it isn't inside one, like buttons in a page header.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present on Previous or Next when there's no page that way, beside aria-disabled. It stays focusable and does nothing.

data-size?string

Mirrors size.

data-variant?string

Mirrors variant.

CarouselPrevious

<button> data-slot="carousel-previous"

PropType
controller?CarouselController

The carousel this part drives when it isn't inside one, like buttons in a page header.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present on Previous or Next when there's no page that way, beside aria-disabled. It stays focusable and does nothing.

data-size?string

Mirrors size.

data-variant?string

Mirrors variant.

useCarousel