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.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
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.
KeyAction
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.
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.
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.
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.
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.
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.
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.
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.
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
Carousel
<div>data-slot="carousel"
PropType
page?number
The current page, from 1. Pass it to control the carousel.
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.
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.