<script setup lang="ts">
import { CarouselContent, CarouselIndicator, CarouselIndicatorGroup, CarouselItem, CarouselNext, CarouselPrevious, CarouselRoot, CarouselViewport } from 'akar';
import './styles.css';
const slides = [
'Mountains',
'Lakes',
'Forests',
'Deserts',
'Coasts',
];
</script>
<template>
<div class="akar-demo akar-demo-Carousel">
<CarouselRoot
v-slot="{ snapCount }"
class="CarouselRoot"
aria-label="Landscapes"
loop
>
<CarouselViewport class="CarouselViewport">
<CarouselContent>
<CarouselItem
v-for="slide in slides"
:key="slide"
class="CarouselItem"
>
<div class="CarouselSlide">
{{ slide }}
</div>
</CarouselItem>
</CarouselContent>
</CarouselViewport>
<div class="CarouselControls">
<CarouselPrevious class="CarouselButton">
<PIcon name="i-lucide-chevron-left" />
</CarouselPrevious>
<CarouselIndicatorGroup class="CarouselIndicators">
<CarouselIndicator
v-for="(_, index) in snapCount"
:key="index"
:index="index"
class="CarouselIndicator"
/>
</CarouselIndicatorGroup>
<CarouselNext class="CarouselButton">
<PIcon name="i-lucide-chevron-right" />
</CarouselNext>
</div>
</CarouselRoot>
</div>
</template>@import '../_colors/grass.css';
@scope (.akar-demo-Carousel) {
/* reset */
button {
all: unset;
}
.CarouselRoot {
width: 320px;
}
.CarouselViewport {
border-radius: 8px;
outline: none;
}
.CarouselViewport:focus-visible {
box-shadow: 0 0 0 2px var(--grass-7);
}
.CarouselItem {
flex-basis: 100%;
}
.CarouselSlide {
display: flex;
align-items: center;
justify-content: center;
height: 160px;
border-radius: 8px;
border: 1px solid var(--ui-color-border);
background-color: var(--ui-color-bg);
color: var(--ui-color-text-highlighted);
font-size: 18px;
font-weight: 500;
}
.CarouselControls {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 12px;
}
.CarouselButton {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border-radius: 9999px;
border: 1px solid var(--ui-color-border);
background-color: var(--ui-color-bg);
color: var(--ui-color-text-highlighted);
cursor: pointer;
}
.CarouselButton:hover {
background-color: var(--ui-color-bg-elevated);
}
.CarouselButton:focus-visible,
.CarouselIndicator:focus-visible {
box-shadow: 0 0 0 2px var(--grass-7);
}
.CarouselButton[data-disabled] {
opacity: 0.5;
cursor: default;
}
.CarouselIndicators {
display: flex;
gap: 6px;
}
.CarouselIndicator {
width: 8px;
height: 8px;
border-radius: 9999px;
background-color: var(--ui-color-border-accented);
cursor: pointer;
transition:
width 200ms,
background-color 200ms;
}
.CarouselIndicator[data-active] {
width: 20px;
background-color: var(--ui-color-primary);
}
}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.
pnpm add akar
yarn add akar
npm install akar
bun add akar
Anatomy
Import all parts and piece them together.
<script setup>
import {
CarouselAutoplayToggle,
CarouselContent,
CarouselIndicator,
CarouselIndicatorGroup,
CarouselItem,
CarouselNext,
CarouselPrevious,
CarouselRoot,
CarouselViewport,
} from 'akar';
</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.
| Prop | Default | Type |
|---|---|---|
align | "start" | "start" | "center" | "end"Where a snap places its slides in the viewport. |
as | "div" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
autoHeight | booleanWhen | |
autoplay | false | boolean | CarouselAutoplayOptionsAdvance to the next snap on an interval. Pauses on hover, stops when keyboard focus enters the carousel. |
autoScroll | false | boolean | CarouselAutoScrollOptionsScroll the track continuously. Pauses on hover, stops when keyboard focus enters the carousel. |
containScroll | "trimSnaps" | false | "trimSnaps" | "keepSnaps"How the snaps at the ends of the track are handled. |
defaultPlaying | true | booleanThe rotation state when initially rendered. Rotation stops on mount when the user prefers reduced motion. |
defaultValue | 0 | numberThe index of the snap to show when initially rendered. Use when you do not need to control the index. |
dir | "ltr" | "rtl"The reading direction of the carousel. If omitted, inherits globally from | |
dragFree | booleanWhen | |
draggable | true | booleanWhen |
fade | booleanWhen | |
inViewThreshold | 0 | numberThe fraction (0 to 1) of an item that has to be visible for it to count as in view. |
itemCount | numberThe number of slides. Only needed on the server, to render | |
loop | booleanWhen | |
modelValue | numberThe controlled index of the current snap. Can be bound as | |
orientation | "horizontal" | "vertical" | "horizontal"The axis the slides are laid out and scroll along. |
playing | booleanThe controlled rotation state of | |
skipSnaps | booleanWhen | |
slidesToScroll | 1 | number | "auto"How many slides one navigation step moves. |
wheel | booleanWhen |
| Event | Payload |
|---|---|
settle | value: numberEvent handler called once the slides rest on the current snap after a change: after the scroll, drag or fade that moved them. |
update:modelValue | value: numberEvent handler called when the current snap changes. |
update:playing | value: booleanEvent handler called when rotation starts or stops. |
| Slot (default) | Payload |
|---|---|
modelValue | numberThe current snap index |
snapCount | numberThe number of snaps |
canScrollPrev | booleanWhether there is a snap before the current one |
canScrollNext | booleanWhether there is a snap after the current one |
scrollPrev | (): booleanGo to the previous snap |
scrollNext | (): booleanGo to the next snap |
scrollTo | (index: number): booleanGo to the snap at |
playing | booleanWhether |
paused | booleanWhether rotation is held for now: hover, a hidden page, the carousel out of view, a finger down |
play | (): booleanStart rotation |
stop | (): booleanStop rotation |
| 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 |
|---|---|
--akar-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 |
--akar-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.
| Prop | Default | Type |
|---|---|---|
as | "div" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Data attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
[data-dragging] | Present while the track is dragged with a mouse or pen |
| CSS variable | Description |
|---|---|
--akar-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.
| Prop | Default | Type |
|---|---|---|
as | "div" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| 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.
| Prop | Default | Type |
|---|---|---|
as | "div" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Slot (default) | Payload |
|---|---|
active | booleanWhether the current snap brings this item into place |
inView | booleanWhether the item is visible in the viewport |
| 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.
| Prop | Default | Type |
|---|---|---|
as | "button" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| 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.
| Prop | Default | Type |
|---|---|---|
as | "button" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| 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.
| Prop | Default | Type |
|---|---|---|
as | "div" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| 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".
| Prop | Default | Type |
|---|---|---|
as | "button" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
index* | numberThe index of the snap this indicator goes to. |
| Slot (default) | Payload |
|---|---|
active | booleanWhether this indicator's snap is the current one |
| 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.
| Prop | Default | Type |
|---|---|---|
as | "button" | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Slot (default) | Payload |
|---|---|
playing | booleanWhether |
| 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. |
| ArrowRight/ArrowLeft | In a CarouselIndicatorGroup, moves to the next or previous indicator and shows its snap. |
| Space/Enter | 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.