Skip to content

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.

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

<script lang="ts">
import {
  Carousel, CarouselContent, CarouselControl, CarouselItem,
  CarouselNext, CarouselPrevious,
} from "@/components/ui/carousel";
</script>

slideCount is required, and each CarouselItem takes its position in the list as index.

const slides = ["Ocean", "Forest", "Desert"];

const Gallery = () => (
  <Carousel className="w-full max-w-xs" slideCount={slides.length}>
    <CarouselContent>
      {slides.map((slide, index) => (
        <CarouselItem index={index} key={slide}>
          <div className="flex aspect-square items-center justify-center rounded-lg border">
            {slide}
          </div>
        </CarouselItem>
      ))}
    </CarouselContent>
    <CarouselControl>
      <CarouselPrevious />
      <CarouselNext />
    </CarouselControl>
  </Carousel>
);

Accessibility

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.

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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

<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.

Type
{ root?: string; item?: (index: number) → string; itemGroup?: string; nextTrigger?: string; prevTrigger?: string; indicatorGroup?: string; indicator?: (index: number) → string }
inViewThreshold?number | number[]

The threshold for determining if an item is in view.

Type
number | number[]
Default
0.6
loop?boolean

Whether the carousel should loop around.

Default
false
onAutoplayStatusChange?(details: AutoplayStatusDetails) → void

Function called when the autoplay status changes.

Type
(details: AutoplayStatusDetails) → void
Parameters

details – { type: "autoplay.start" | "autoplay" | "autoplay.stop"; page: number; isPlaying: boolean }

Returns
void
onDragStatusChange?(details: DragStatusDetails) → void

Function called when the drag status changes.

Type
(details: DragStatusDetails) → void
Parameters

details – { type: "dragging.start" | "dragging" | "dragging.end"; page: number; isDragging: boolean }

Returns
void
orientation?"horizontal" | "vertical"

The orientation of the element.

Type
"horizontal" | "vertical"
Default
"horizontal"
padding?string

Defines the extra space added around the scrollable area, enabling nearby items to remain partially in view.

slidesPerMove?number | "auto"

The number of slides to scroll at a time. When set to auto, the number of slides to scroll is determined by the slidesPerPage property.

Type
number | "auto"
Default
"auto"
slidesPerPage?number

The number of slides to show at a time.

Default
1
snapType?"proximity" | "mandatory"

The snap type of the item.

Type
"proximity" | "mandatory"
Default
"mandatory"
spacing?string

The amount of space between items. Ark default: 0px.

Default
"16px"
translations?object

The localized messages to use.

Type
{ nextTrigger?: string; prevTrigger?: string; indicator?: (index: number) → string; item?: (index: number, count: number) → string; autoplayStart?: string; autoplayStop?: string; progressText?: (details: ProgressTextDetails) → string }
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-orientation"horizontal" | "vertical"

The orientation of the carousel.

Type
"horizontal" | "vertical"
CSS variableType
--slide-item-sizestring

The calculated size of each slide item.

--slide-spacingstring

The spacing between slides.

--slides-per-pagestring

The number of slides visible per page.

CarouselContent

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

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-dragging?string

Present when in the dragging state.

data-orientation"horizontal" | "vertical"

The orientation of the item.

Type
"horizontal" | "vertical"

CarouselItem

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

PropType
indexnumber

The index of the item.

snapAlign?"start" | "end" | "center"

The snap alignment of the item.

Type
"start" | "end" | "center"
Default
"start"
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-indexstring

The index of the item.

data-inview?string

Present when in viewport.

data-orientation"horizontal" | "vertical"

The orientation of the item.

Type
"horizontal" | "vertical"

CarouselControl

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

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-orientation"horizontal" | "vertical"

The orientation of the control.

Type
"horizontal" | "vertical"

CarouselNext

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

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-orientation"horizontal" | "vertical"

The orientation of the nexttrigger.

Type
"horizontal" | "vertical"

CarouselPrevious

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

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-orientation"horizontal" | "vertical"

The orientation of the prevtrigger.

Type
"horizontal" | "vertical"

CarouselIndicatorGroup

<div> data-slot="carousel-indicator-group"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-orientation"horizontal" | "vertical"

The orientation of the indicatorgroup.

Type
"horizontal" | "vertical"

CarouselIndicator

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

PropType
indexnumber

The index of the indicator.

readOnly?boolean

Whether the indicator is read only.

Default
false
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-current?string

Present when current.

data-indexstring

The index of the item.

data-orientation"horizontal" | "vertical"

The orientation of the indicator.

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

Present when read-only.

CarouselAutoplayTrigger

<button> data-slot="carousel-autoplay-trigger"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Forwarded to the part.

AttributeType
data-orientation"horizontal" | "vertical"

The orientation of the autoplaytrigger.

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

Present when pressed.

CarouselProgressText

<span> data-slot="carousel-progress-text"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

CarouselAutoplayIndicator

<span> data-slot="carousel-autoplay-indicator"

PropType
fallback?Snippet

The fallback content to render when autoplay is paused.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>

CarouselRootProvider

<div> data-slot="carousel"

PropType
valueUseCarouselReturn

What useCarousel() returns: the state and API, made outside the component to control it.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-orientation"horizontal" | "vertical"

Mirrors the orientation prop.

Type
"horizontal" | "vertical"
CSS variableType
--slide-item-sizestring
--slide-spacingstring
--slides-per-pagestring

useCarousel

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
id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the carousel. Useful for composition.

Type
{ root?: string; item?: (index: number) → string; itemGroup?: string; nextTrigger?: string; prevTrigger?: string; indicatorGroup?: string; indicator?: (index: number) → string }
inViewThreshold?number | number[]

The threshold for determining if an item is in view.

Type
number | number[]
Default
0.6
loop?boolean

Whether the carousel should loop around.

Default
false
onAutoplayStatusChange?(details: AutoplayStatusDetails) → void

Function called when the autoplay status changes.

Type
(details: AutoplayStatusDetails) → void
Parameters

details – { type: "autoplay.start" | "autoplay" | "autoplay.stop"; page: number; isPlaying: boolean }

Returns
void
onDragStatusChange?(details: DragStatusDetails) → void

Function called when the drag status changes.

Type
(details: DragStatusDetails) → void
Parameters

details – { type: "dragging.start" | "dragging" | "dragging.end"; page: number; isDragging: boolean }

Returns
void
orientation?"horizontal" | "vertical"

The orientation of the element.

Type
"horizontal" | "vertical"
Default
"horizontal"
padding?string

Defines the extra space added around the scrollable area, enabling nearby items to remain partially in view.

slidesPerMove?number | "auto"

The number of slides to scroll at a time. When set to auto, the number of slides to scroll is determined by the slidesPerPage property.

Type
number | "auto"
Default
"auto"
slidesPerPage?number

The number of slides to show at a time.

Default
1
snapType?"proximity" | "mandatory"

The snap type of the item.

Type
"proximity" | "mandatory"
Default
"mandatory"
spacing?string

The amount of space between items.

Default
"0px"
translations?object

The localized messages to use.

Type
{ nextTrigger?: string; prevTrigger?: string; indicator?: (index: number) → string; item?: (index: number, count: number) → string; autoplayStart?: string; autoplayStop?: string; progressText?: (details: ProgressTextDetails) → string }

CarouselContext

PropType
canScrollNextboolean

Whether the carousel is can scroll to the next view.

canScrollPrevboolean

Whether the carousel is can scroll to the previous view.

getProgress() → number

Returns the current scroll progress as a percentage.

Returns
number
getProgressText() → string

Returns the progress text.

Returns
string
isDraggingboolean

Whether the carousel is being dragged. This only works when draggable is true.

isInView(index: number) → boolean

Whether the item is in view.

Parameters

index – number

Returns
boolean
isPlayingboolean

Whether the carousel is auto playing.

pagenumber

The current index of the carousel.

pageSnapPointsnumber[]

The current snap points of the carousel.

pauseVoidFunction

Function to pause autoplay.

playVoidFunction

Function to start/resume autoplay.

refreshVoidFunction

Function to re-compute the snap points and clamp the page

scrollNext(instant?: boolean) → void

Function to scroll to the next page.

Parameters

instant? – boolean

Returns
void
scrollPrev(instant?: boolean) → void

Function to scroll to the previous page.

Parameters

instant? – boolean

Returns
void
scrollTo(page: number, instant?: boolean) → void

Function to scroll to a specific page.

Type
(page: number, instant?: boolean) → void
Parameters

page – number

instant? – boolean

Returns
void
scrollToIndex(index: number, instant?: boolean) → void

Function to scroll to a specific item index.

Type
(index: number, instant?: boolean) → void
Parameters

index – number

instant? – boolean

Returns
void