shadcn.io is an independent project built on shadcn/ui
Components
Free
Carousel for Svelte UI SDK
A row of slides with Previous and Next buttons, swipe and arrow keys, for galleries, quotes and onboarding. A Svelte 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 root is a region described as a carousel, and each CarouselItem 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 page is announced, except while autoplay runs. Previous and Next are labeled buttons that 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, Next and the current indicator. Previous and Next stay focusable at the first or last page.
Shift+Tab
Moves focus to the previous focusable element.
Enter
When focus is on Previous, Next or an indicator, presses it.
Space
When focus is on Previous, Next or an indicator, presses it.
Arrow Right
When focus is in a horizontal carousel, outside a field or a widget that uses the arrows itself, moves to the next page, or to the previous one when the carousel is right to left. On the indicators, moves to the next one, or the previous one right to left, and shows its page.
Arrow Left
When focus is in a horizontal carousel, outside a field or a widget that uses the arrows itself, moves to the previous page, or to the next one when the carousel is right to left. On the indicators, moves to the previous one, or the next one right to left, and shows its page.
Arrow Down
When focus is in a vertical carousel, outside a field or a widget that uses the arrows itself, moves to the next page. On the indicators, moves to the next one and shows its page.
Arrow Up
When focus is in a vertical carousel, outside a field or a widget that uses the arrows itself, moves to the previous page. On the indicators, moves to the previous one and shows its page.
Home
When focus is in the slides, moves to the first page. On the indicators, moves to the first one.
End
When focus is in the slides, moves to the last page. On the indicators, moves to the last one.
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
slidesPerPage set to 2 puts two cards in the frame at every width, and Previous and Next move the frame along by a page. Use it when the slides are thumbnails or logos and one full-width slide would waste the row. The carousel's own width is capped by a class, which keeps the cards a readable size as the screen grows.
Spacing
spacing set to 4px on Carousel narrows the gap between slides from the default of 16 pixels, with two cards in view through slidesPerPage. The value is any CSS length, so 0px lets full-bleed photos meet edge to edge and a larger one separates cards that have no border of their own.
Orientation
orientation set to vertical, with slidesPerPage at 2 and a height of 270 pixels on CarouselContent, gives a column with two cards in view. Previous and Next move above and below, their chevrons turned to point up and down. The height is what the slides are sized against, so a vertical carousel needs one. Use it for a short rail beside a detail view.
API
The current page lives in the demo's state: page passes it in, onPageChange writes it back, and the caption under the carousel shows Slide 2 of 5 from it. The caption is the page's own markup, so word it however the page speaks. Control the page when something outside the carousel must follow it or move it, such as a thumbnail strip or a step counter.
Indicators
Five dots under the slides: a CarouselIndicatorGroup with one CarouselIndicator per slide, each given its index. The group is a tablist of dots named Go to slide 1 to 5, and the current one carries data-selected, which the shipped classes fill brighter. Reach for dots when the slides are few and people want to jump between them, such as the photos of one product.
Plugin
autoplay with a delay of 2000 advances a page every two seconds, and loop carries it from the last slide back to the first. CarouselAutoplayTrigger under the slides stops and restarts it, and the pointer over the slides pauses it through pause and play on the carousel's context, resuming on leave only if it was playing. Pressing Previous or Next stops it.
Multiple
slidesPerPage set to 3 shows three cards at every width, and Previous and Next are hidden below the sm breakpoint, where people swipe instead. Pick this for tiles people compare side by side, such as plans or products. The three is fixed, so on a narrow phone each card is a third of the frame.
RTL
Arabic numerals, in a Direction provider set to right to left, which sets dir on an element around the carousel. UI SDK reads the direction the carousel inherits, so the slides start on the right and Previous sits on the right, its chevron turned. With focus in the slides, ArrowLeft brings in the next page.
FAQ
How do I show several slides at once in the Carousel?
Set slidesPerPage on Carousel, such as 3, and the slides shrink to share the frame, with spacing as the gap between them. Previous and Next then move a whole page of that many slides.
The Multiple demo shows three per page, and on a phone people swipe from one page to the next.
How do I make the Carousel autoplay in Svelte?
Add autoplay to Carousel, or set it to an object with a delay in milliseconds, such as 3000, in place of the default 5000. 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 add CarouselAutoplayTrigger, a button that plays and pauses, as the demo does under its slides.
How do I get the current slide of the Carousel?
Hold the page in state and bind it with bind:page, or pass it as page and update it in onPageChange, which receives the new page first, counted from zero. The API demo prints Slide 2 of 5 under the carousel the second way. It is a page, not a slide: with slidesPerPage above one, each page holds several slides.
How do I loop the Carousel back to the first slide?
Add loop to Carousel. 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.
Set slidesPerPage to fit more than one slide in that height, as the demo does with 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?
Add a CarouselIndicatorGroup after the slides with one CarouselIndicator per page, each given its index, as the Indicators demo does. The group is a tablist named Choose a slide and each dot a tab labeled Go to slide 1, Go to slide 2 and so on, the current one marked with aria-selected, and the arrow keys, Home and End move between them. With slidesPerPage above one, take the number of pages from pageSnapPoints on the carousel's context.
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, so allowMouseDrag changes nothing here. Set drag to false on Carousel 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 lets a fling coast without snapping to a page.
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 are labeled buttons, 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. Two-way with bind:page.
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.
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?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?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?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?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?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?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?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?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?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?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.