shadcn.io is an independent project built on shadcn/ui
Components
Free
Carousel for Svelte
A row of slides with Previous and Next buttons, swipe and arrow keys, for galleries, quotes and onboarding. A Svelte carousel on Ark UI.
On this page9 sections
UI library: Ark UI
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 slide a group labeled with its position, such as 2 of 5, hidden from screen readers while it is out of view. The slides are a polite live region, so a new page is announced, except while autoplay runs. Previous and Next are labeled buttons, disabled at either end unless loop is on. The region has no name of its own, so give Carousel 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, then Previous and Next.
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 the slides, moves to the next page, in a vertical carousel too, though not while autoplay runs. On an indicator of a horizontal carousel, does the same.
Arrow Left
When focus is in the slides, moves to the previous page, in a vertical carousel too, though not while autoplay runs. On an indicator of a horizontal carousel, does the same.
Arrow Down
When focus is on an indicator of a vertical carousel, moves to the next page.
Arrow Up
When focus is on an indicator of a vertical carousel, moves to the previous page.
Home
When focus is in the slides or on an indicator, moves to the first page.
End
When focus is in the slides or on an indicator, moves to the last page.
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. Each dot is a button named Go to slide 1 to 5, and the current one carries data-current, 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. Ark UI takes the direction from the locale rather than from a dir attribute, so the provider is what starts the slides on the right and puts Previous on the right, its chevron turned. With focus in the slides, ArrowRight still goes to the next page, which now waits on the left.
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 page of that many slides, unless you set slidesPerMove to a smaller number.
The Multiple demo shows three per page. For a peek of the neighboring slides at the edges, add padding, which insets the scroll area and leaves them partly in view.
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 4000. Autoplay loops back to the first slide on its own. It stops when the browser tab is hidden and when someone presses Previous or Next, and the Plugin demo also pauses it under the pointer.
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, pass it as page and update it in onPageChange, which receives an object whose page is the new one, counted from zero. The API demo prints Slide 2 of 5 under the carousel this 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. Next on the last page then goes back to the first, Previous on the first goes to the last, and neither button is ever disabled. A carousel with autoplay loops already, since autoplay turns loop on unless you set it to false.
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. Focus in the slides still answers ArrowLeft and ArrowRight, and only the indicators answer ArrowUp and ArrowDown.
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. Each dot is a button labeled Go to slide 1, Go to slide 2 and so on, the current one marked with data-current, 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?
Add allowMouseDrag to Carousel, since Ark UI leaves it off. Touch swipes and trackpad scrolling work without it, because the slides sit in a native scroll container that snaps to each page. While someone drags, CarouselContent carries data-dragging, a hook for a grabbing cursor.
Is the Ark UI Carousel accessible?
Mostly, with one gap you close yourself. Ark UI marks the root as a carousel region, labels each slide with its position, hides slides out of view from screen readers and announces page changes, and Previous and Next are labeled buttons. The region has no name, so give Carousel 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 controlled page of the carousel.
defaultPage?number
The initial page to scroll to when rendered.
Use when you don't need to control the page of the carousel.
Default
0
onPageChange?(details: PageChangeDetails) → void
Function called when the page changes.
Type
(details: PageChangeDetails) → void
Parameters
details – { page: number; pageSnapPoint: number }
Returns
void
slideCountnumber
The total number of slides.
Useful for SSR to render the initial ating the snap points.
allowMouseDrag?boolean
Whether to allow scrolling via dragging with mouse.
Default
false
autoplay?boolean | { delay: number; }
Whether to scroll automatically. The default delay is 4000ms.
Type
boolean | { delay: number; }
Default
false
autoSize?boolean
Whether to enable variable width slides.
Default
false
dir?"ltr" | "rtl"
The document's text/writing direction.
Type
"ltr" | "rtl"
Default
"ltr"
id?string
The unique identifier of the machine.
ids?object
The ids of the elements in the carousel. Useful for composition.