Carousel

An interactive slideshow component for cycling through elements.

Loading...

Anatomy

<Carousel.Root>
  <Carousel.Control>
    <Carousel.PrevTrigger />
    <Carousel.NextTrigger />
  </Carousel.Control>
  <Carousel.ItemGroup>
    <Carousel.Item />
  </Carousel.ItemGroup>
  <Carousel.IndicatorGroup>
    <Carousel.Indicator />
  </Carousel.IndicatorGroup>
</Carousel.Root>

Examples

Controlled

Use page and onPageChange to control the current page.

Root Provider

Use RootProvider with the useCarousel hook to access the state and methods from outside the component.

Autoplay

Set autoplay and loop on Carousel.Root to play the slides automatically.

Pause on Hover

Pause on hover isn't built in. Call play() and pause() from Carousel.Context to add it.

Thumbnail Indicators

Render an image thumbnail inside each Carousel.Indicator in place of the default dots.

Vertical

Set orientation="vertical" on Carousel.Root to scroll vertically.

Dynamic

Store slides in state and keep the page in sync. Pass page and onPageChange to Carousel.Root, and update slideCount when slides are added or removed.

Scroll to Slide

Call api.scrollToIndex(index) from Carousel.Context to jump to a specific slide.

Slides Per Page

Set slidesPerPage on Carousel.Root to show several slides at once. Render indicators from api.pageSnapPoints so there is one per page, not per slide. Use spacing to set the gap.

Spacing

Set spacing on Carousel.Root to control the gap between slides. Combine it with slidesPerPage to show partial previews of adjacent slides.

Variable Sizes

Set autoSize on Carousel.Root so each Carousel.Item can define its own width. Use snapAlign on an item to control where it snaps into view.

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
count
number

The total number of slides. Useful for SSR to render the initial snap points.

allowMouseDragfalse
boolean

Whether to allow scrolling via dragging with mouse

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
autoPlayfalse
boolean | { delay: number }

Whether to scroll automatically. The default delay is 4000ms.

autoSizefalse
boolean

Whether to enable variable width slides.

defaultPage0
number

The initial page to scroll to when rendered. Use when you don't need to control the page of the carousel.

ids
ElementIds

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

inViewThreshold0.6
number | number[]

The threshold for determining if an item is in view.

itemSpacing
string

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

loopfalse
boolean

Whether the carousel should loop around.

onAutoplayStatusChange
(details: AutoplayStatusDetails) => void

Function called when the autoplay status changes.

onDragStatusChange
(details: DragStatusDetails) => void

Function called when the drag status changes.

onPageChange
(details: PageChangeDetails) => void

Function called when the page changes.

orientation'horizontal'
'horizontal' | 'vertical'

The orientation of the element.

page
number

The controlled page of the carousel.

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

slidesPerMove'auto'
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.

slidesPerPage1
number

The number of slides to show at a time.

snapType'mandatory'
'proximity' | 'mandatory'

The snap type of the item.

spacing'0px'
string

The amount of space between items.

translations
IntlTranslations

The localized messages to use.

AttributeDescription
[data-carousel-root]<uid>
[data-orientation]The orientation of the carousel
CSS VariableDescription
--slides-per-pageThe number of slides visible per page
--slide-spacingThe spacing between slides
--slide-item-sizeThe calculated size of each slide item

AutoplayIndicator

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
fallback
string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...>

The fallback content to render when autoplay is paused.

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AutoplayTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-carousel-autoplay-trigger]<uid>
[data-orientation]The orientation of the autoplaytrigger
[data-pressed]Present when pressed

Control

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-carousel-control]<uid>
[data-orientation]The orientation of the control

IndicatorGroup

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-carousel-indicator-group]<uid>
[data-orientation]The orientation of the indicatorgroup

Indicator

Defines the <button> element.

AttributeDefaultType
index
number

The index of the indicator.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
readOnlyfalse
boolean

Whether the indicator is read only.

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<CarouselIndicatorState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-carousel-indicator]<uid>
[data-orientation]The orientation of the indicator
[data-index]The index of the item
[data-readonly]Present when read-only
[data-current]Present when current

ItemGroup

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-carousel-item-group]<uid>
[data-orientation]The orientation of the item
[data-dragging]Present when in the dragging state

Item

Defines the <div> element.

AttributeDefaultType
index
number

The index of the item.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<CarouselItemState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

snapAlign'start'
'center' | 'start' | 'end'

The snap alignment of the item.

AttributeDescription
[data-carousel-item]<uid>
[data-index]The index of the item
[data-inview]Present when in viewport
[data-orientation]The orientation of the item

NextTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-carousel-next-trigger]<uid>
[data-orientation]The orientation of the nexttrigger

PrevTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

ProgressText

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseCarouselReturn

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Context

API

PropertyType
page
number

The current index of the carousel

pageSnapPoints
number[]

The current snap points of the carousel

isPlaying
boolean

Whether the carousel is auto playing

isDragging
boolean

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

canScrollNext
boolean

Whether the carousel is can scroll to the next view

canScrollPrevious
boolean

Whether the carousel is can scroll to the previous view

scrollToIndex
(index: number, instant?: boolean) => void

Function to scroll to a specific item index

scrollTo
(page: number, instant?: boolean) => void

Function to scroll to a specific page

scrollNext
(instant?: boolean) => void

Function to scroll to the next page

scrollPrevious
(instant?: boolean) => void

Function to scroll to the previous page

getProgress
() => number

Returns the current scroll progress as a percentage

getProgressText
() => string

Returns the progress text

play
VoidFunction

Function to start/resume autoplay

pause
VoidFunction

Function to pause autoplay

isInView
(index: number) => boolean

Whether the item is in view

refresh
VoidFunction

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

getItemState
(props: ItemProps) => ItemState

Returns the state of a carousel item

getIndicatorState
(props: IndicatorProps) => IndicatorState

Returns the state of a carousel indicator

Accessibility

Complies with the Carousel WAI-ARIA design pattern.