Carousel
Features
- Can be controlled or uncontrolled.
- Built on native scroll snapping: renders and scrolls before hydration.
- Touch, mouse, pen, wheel, trackpad and keyboard navigation.
- Supports horizontal/vertical orientation and RTL.
- Loop, alignment, groups of slides, free dragging and fade.
- Autoplay and continuous auto scroll with a rotation control.
- Follows the WAI-ARIA carousel pattern and respects reduced motion.
- No dependencies.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import all parts and piece them together.
<script setup>
import {
CarouselAutoplayToggle,
CarouselContent,
CarouselIndicator,
CarouselIndicatorGroup,
CarouselItem,
CarouselNext,
CarouselPrevious,
CarouselRoot,
CarouselViewport,
} from 'reka-ui'
</script>
<template>
<CarouselRoot v-slot="{ snapCount }">
<CarouselAutoplayToggle />
<CarouselViewport>
<CarouselContent>
<CarouselItem />
</CarouselContent>
</CarouselViewport>
<CarouselPrevious />
<CarouselNext />
<CarouselIndicatorGroup>
<CarouselIndicator
v-for="(_, index) in snapCount"
:key="index"
:index="index"
/>
</CarouselIndicatorGroup>
</CarouselRoot>
</template>How it works
CarouselViewport is a real scroll container with CSS scroll snapping, and CarouselContent is the track inside it. Before hydration the browser already scrolls it with touch, a trackpad or the keyboard, and snaps to the slides. Once mounted, the carousel measures the track, adds mouse and pen dragging, the controls and the options below, and keeps v-model in sync with the scroll position.
A mouse or pen drag follows the pointer, stretches a little past the first and last slides, and settles on a snap based on its speed. A press on a field inside a slide (an input, a textarea, a select or an editable element) is left to the field. settle fires once the slides rest on a new snap, which is the moment for heavier work such as loading the next images.
The parts set the layout styles they need inline: the overflow and snapping on the viewport, display: flex on the content and flex-shrink: 0 on the items. Give the items their size yourself, for example flex-basis: 100% for one slide per view or 33.333% for three. To space them, put a start padding on the items and the same negative start margin on the content, for example ps-4 and -ms-4, or pt-4 and -mt-4 when vertical. The carousel measures where the first slide rests and rests every snap at that offset, so the gutter lines up on every slide. Before hydration the browser snaps to the item edges, so the slides sit one gutter off until the track is measured.
v-model is the index of the current snap, not of a slide. With one slide per snap they are the same. With slidesToScroll, or when containScroll merges the snaps at the end of the track, there are fewer snaps than slides: render the indicators from the snapCount slot prop.
Server rendering
The server renders a working scroller: before hydration, visitors can already swipe, scroll with a trackpad or the keyboard, and land on a snap. The current slide has data-active, and with fade the other slides are hidden. Where the browser supports scroll-initial-target, the track opens on defaultValue; elsewhere it scrolls there on hydration. If a visitor scrolls before hydration, the carousel keeps that place and v-model follows it.
The slides register once they mount, so on the server snapCount is 0, Previous/Next are disabled and the slides have no "2 of 5" label. Pass itemCount with the number of slides to render the indicators, the controls and the labels on the server too. Until the track is measured, every group of slidesToScroll slides counts as one snap.
Wrapping the parts
CarouselRoot declares draggable and defaultPlaying with a true default and playing with an undefined one. A component that wraps it and redeclares these props needs the same defaults in its withDefaults, or Vue casts the absent booleans to false, which turns dragging and rotation off and makes playing controlled.
API Reference
Root
Contains all the parts of a carousel. Renders a region with aria-roledescription="carousel": give it a name with aria-label or aria-labelledby.
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
[data-playing] | Present while autoplay or auto scroll is on |
[data-paused] | Present while rotation is held: hovered, a hidden page, scrolled out of view, or a finger down |
| CSS Variable | Description |
|---|---|
--reka-carousel-progress | From 0 at the first snap to 1 at the last, updated on every scroll frame. With loop it wraps back to 0 |
--reka-carousel-interval | The autoplay delay, to time a progress animation |
Viewport
The scroll container. It is focusable and handles the arrow keys, Home and End.
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
[data-dragging] | Present while the track is dragged with a mouse or pen |
| CSS Variable | Description |
|---|---|
--reka-carousel-height | The height of the current slides, with autoHeight |
Content
The track that holds the slides. It is a polite live region, turned off while the carousel rotates on its own.
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
Item
A slide. Renders a group with aria-roledescription="slide", or a tabpanel next to a CarouselIndicatorGroup, labelled with its position ("2 of 5") unless you pass aria-label or aria-labelledby. It gets a generated id for the tabs to point at. Once measured, slides out of view are inert and hidden from assistive technologies.
| Data Attribute | Value |
|---|---|
[data-index] | The slide index |
[data-active] | Present when the current snap brings the slide into place |
[data-in-view] | Present when the slide is visible in the viewport |
[data-orientation] | "vertical" | "horizontal" |
Previous
Goes to the previous snap. On the first one, unless loop is set, it is aria-disabled and does nothing, but keeps the focus.
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when there is no previous snap |
[data-orientation] | "vertical" | "horizontal" |
Next
Goes to the next snap. On the last one, unless loop is set, it is aria-disabled and does nothing, but keeps the focus.
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when there is no next snap |
[data-orientation] | "vertical" | "horizontal" |
IndicatorGroup
Wraps the indicators in a tablist, labelled "Choose slide to display" unless you pass aria-label. Its indicators become tabs: the current one is the only tab stop, and the arrow keys, Home and End move between them and show their snap. The slides become the tab panels, following the tabbed carousel pattern: each tab is labelled "Slide 1", "Slide 2"..., its aria-controls points at the slides of its snap, and those slides are labelled by it through aria-labelledby. To translate, label the tabs. With two groups, for example dots and thumbnails, each keeps its own tab ids and the slides are labelled by the first one. The slides switch once the group mounts, so the server renders them as slide groups.
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
Indicator
Goes to the snap at index. In a CarouselIndicatorGroup it is a tab with aria-selected. On its own it is a button, and the current one has aria-disabled="true".
| Data Attribute | Value |
|---|---|
[data-active] | Present when its snap is the current one |
[data-orientation] | "vertical" | "horizontal" |
AutoplayToggle
Starts and stops autoplay or autoScroll. Its label names the action it takes next ("Stop slide rotation", "Start slide rotation"). Place it first in the carousel, so it is the first thing keyboard users reach.
| Data Attribute | Value |
|---|---|
[data-state] | "playing" | "paused" |
[data-disabled] | Present when neither autoplay nor autoScroll is set |
Examples
Several slides per view
Size the items to fit several in the viewport. align places the current snap, and slidesToScroll moves several slides per step, or as many as fit with 'auto'.
<template>
<CarouselRoot
aria-label="Products"
align="start"
slides-to-scroll="auto"
>
<CarouselViewport>
<CarouselContent class="-ml-4">
<CarouselItem
v-for="product in products"
:key="product.id"
class="basis-1/3 pl-4"
>
...
</CarouselItem>
</CarouselContent>
</CarouselViewport>
</CarouselRoot>
</template>Controlling the current slide
Bind v-model to read or set the current snap.
<script setup lang="ts">
import { ref } from 'vue'
const current = ref(0)
</script>
<template>
<CarouselRoot
v-model="current"
aria-label="Gallery"
>
...
</CarouselRoot>
<p>Slide {{ current + 1 }}</p>
</template>Thumbnails
Two carousels can share the same v-model: the thumbnails follow the main one, and selecting a thumbnail moves it. containScroll: 'keepSnaps' keeps one snap per thumbnail, so both carousels count the same snaps.
<template>
<CarouselRoot
v-model="current"
aria-label="Photos"
>
...
</CarouselRoot>
<CarouselRoot
v-model="current"
aria-label="Thumbnails"
align="center"
contain-scroll="keepSnaps"
>
<CarouselViewport>
<CarouselContent>
<CarouselItem
v-for="(photo, index) in photos"
:key="photo.id"
class="basis-1/4"
>
<button
:aria-current="index === current"
@click="current = index"
>
<img :src="photo.thumbnail" :alt="photo.alt">
</button>
</CarouselItem>
</CarouselContent>
</CarouselViewport>
</CarouselRoot>
</template>Autoplay
autoplay moves to the next snap on an interval, and autoScroll scrolls the track continuously. Both pause while a mouse hovers the carousel, while the page is hidden, while the carousel is scrolled out of view and while a finger is down on it. They stop when keyboard focus enters it, and stop on mount when the user prefers reduced motion. With stopOnInteraction, the default, they also stop on the first drag, press or key. Bind v-model:playing to control them.
<template>
<CarouselRoot
:autoplay="{ delay: 5000 }"
aria-label="Announcements"
loop
>
<CarouselAutoplayToggle v-slot="{ playing }">
{{ playing ? 'Pause' : 'Play' }}
</CarouselAutoplayToggle>
...
</CarouselRoot>
</template>Fade
With fade, the slides are stacked and the current one has opacity: 1, the others opacity: 0. Add the transition to the items.
<template>
<CarouselRoot
fade
aria-label="Testimonials"
>
<CarouselViewport>
<CarouselContent>
<CarouselItem class="transition-opacity duration-500">
...
</CarouselItem>
</CarouselContent>
</CarouselViewport>
</CarouselRoot>
</template>Auto height
With autoHeight, the viewport takes the height of the current slides. Animate it with a height transition on the viewport.
<template>
<CarouselRoot
auto-height
aria-label="Reviews"
>
<CarouselViewport class="transition-[height] duration-300">
...
</CarouselViewport>
</CarouselRoot>
</template>Styling slides by state
data-active marks the slides of the current snap and data-in-view the visible ones, for example to dim the slides around a centered one.
<template>
<CarouselItem
v-for="slide in slides"
:key="slide.id"
class="opacity-40 transition-opacity data-[active]:opacity-100"
>
{{ slide.title }}
</CarouselItem>
</template>Accessibility
Adheres to the Carousel WAI-ARIA design pattern.
The root is a region with aria-roledescription="carousel", each item a group with aria-roledescription="slide", and the content a live region that announces slide changes, except while the carousel rotates on its own. The controls point at the content with aria-controls. Slides out of view are inert, so keyboard and screen reader users only meet the visible ones. When the slide holding the focus leaves the view, the focus moves to the slide that comes in, or to the viewport when it has nothing to focus. Previous and Next stay focusable at the ends, and CarouselIndicatorGroup turns the indicators into a tablist with a single tab stop.
Rotation follows the pattern: it pauses on hover, stops for good when keyboard focus enters the carousel, and only restarts from CarouselAutoplayToggle. When the user prefers reduced motion, rotation stops on mount and programmatic scrolls jump instead of animating.
Keyboard Interactions
| Key | Description |
|---|---|
Tab | Moves focus through the controls, the viewport and the visible slides. |
ArrowRight | When the viewport is focused, goes to the next snap in a horizontal carousel. |
ArrowLeft | When the viewport is focused, goes to the previous snap in a horizontal carousel. |
ArrowDown | When the viewport is focused, goes to the next snap in a vertical carousel. |
ArrowUp | When the viewport is focused, goes to the previous snap in a vertical carousel. |
Home | When the viewport is focused, goes to the first snap. |
End | When the viewport is focused, goes to the last snap. |
ArrowRightArrowLeft | In a CarouselIndicatorGroup, moves to the next or previous indicator and shows its snap. |
SpaceEnter | Activates the focused control. |
Labels
The built-in accessible labels are in English. Override them with attributes: aria-roledescription on CarouselRoot and CarouselItem, and aria-label on CarouselItem, CarouselPrevious, CarouselNext, CarouselIndicator, CarouselIndicatorGroup and CarouselAutoplayToggle, whose label follows the playing slot prop of the root. The item label is its position, built from the v-for index and the number of slides. Next to a CarouselIndicatorGroup the slides take their name from the tabs, so label the indicators instead.
<template>
<CarouselRoot
v-slot="{ playing }"
:aria-label="t('carousel.label')"
:aria-roledescription="t('carousel.roledescription')"
>
<CarouselAutoplayToggle :aria-label="playing ? t('carousel.stop') : t('carousel.start')" />
<CarouselViewport>
<CarouselContent>
<CarouselItem
v-for="(slide, index) in slides"
:key="slide.id"
:aria-roledescription="t('carousel.slide')"
:aria-label="t('carousel.position', { index: index + 1, total: slides.length })"
>
...
</CarouselItem>
</CarouselContent>
</CarouselViewport>
<CarouselPrevious :aria-label="t('carousel.previous')" />
<CarouselNext :aria-label="t('carousel.next')" />
</CarouselRoot>
</template>In a right-to-left layout (dir="rtl" on CarouselRoot, or on a ConfigProvider), ArrowLeft goes to the next snap and ArrowRight to the previous one. See Internationalization.
