Carousel

ARIAGitHubnpm
A set of slides shown one or a few at a time, scrolled with touch, mouse, wheel, keyboard or controls.
<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>

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

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.

PropDefaultType
align
"start"
"start" | "center" | "end"

Where a snap places its slides in the viewport.

as
"div"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

autoHeight
boolean

When true, the viewport takes the height of the current slides. Horizontal only.

autoplay
false
boolean | CarouselAutoplayOptions

Advance to the next snap on an interval. Pauses on hover, stops when keyboard focus enters the carousel.

autoScroll
false
boolean | CarouselAutoScrollOptions

Scroll 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. 'trimSnaps' merges the snaps that would leave empty space, 'keepSnaps' keeps one snap per group, false adds empty space so every snap can be aligned. Ignored with loop.

defaultPlaying
true
boolean

The rotation state when initially rendered. Rotation stops on mount when the user prefers reduced motion.

defaultValue
0
number

The 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 ConfigProvider or assumes LTR (left-to-right) reading mode.

dragFree
boolean

When true, the track scrolls freely with momentum instead of resting on a snap.

draggable
true
boolean

When false, the track can't be dragged, swiped or scrolled by the user; the controls still navigate.

fade
boolean

When true, slides are stacked and cross-fade instead of sliding. Set the transition on the items yourself.

inViewThreshold
0
number

The fraction (0 to 1) of an item that has to be visible for it to count as in view.

itemCount
number

The number of slides. Only needed on the server, to render snapCount, the indicators, the enabled Previous/Next and the slide labels before hydration: the slides register once mounted, and their count wins from then on.

loop
boolean

When true, the last slide is followed by the first one, and vice versa. When the slides are too few to wrap, Next on the last snap scrolls back to the first instead.

modelValue
number

The controlled index of the current snap. Can be bound as v-model.

orientation
"horizontal"
"vertical" | "horizontal"

The axis the slides are laid out and scroll along.

playing
boolean

The controlled rotation state of autoplay or autoScroll. Can be bound as v-model:playing.

skipSnaps
boolean

When true, a fast gesture can move past several snaps. When false, one gesture moves one snap.

slidesToScroll
1
number | "auto"

How many slides one navigation step moves. 'auto' moves as many as fit in the viewport.

wheel
boolean

When true, a mouse wheel pages through the snaps, and in fade mode the wheel and trackpad do too.

EventPayload
settle
value: number

Event 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: number

Event handler called when the current snap changes.

update:playing
value: boolean

Event handler called when rotation starts or stops.

Slot (default)Payload
modelValue
number

The current snap index

snapCount
number

The number of snaps

canScrollPrev
boolean

Whether there is a snap before the current one

canScrollNext
boolean

Whether there is a snap after the current one

scrollPrev
(): boolean

Go to the previous snap

scrollNext
(): boolean

Go to the next snap

scrollTo
(index: number): boolean

Go to the snap at index

playing
boolean

Whether autoplay or autoScroll is rotating

paused
boolean

Whether rotation is held for now: hover, a hidden page, the carousel out of view, a finger down

play
(): boolean

Start rotation

stop
(): boolean

Stop rotation

CarouselRoot.vue source

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.

PropDefaultType
as
"div"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

CarouselViewport.vue source

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.

PropDefaultType
as
"div"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

CarouselContent.vue source

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.

PropDefaultType
as
"div"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change 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
boolean

Whether the current snap brings this item into place

inView
boolean

Whether the item is visible in the viewport

CarouselItem.vue source

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.

PropDefaultType
as
"button"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

CarouselPrevious.vue source

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.

PropDefaultType
as
"button"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

CarouselNext.vue source

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.

PropDefaultType
as
"div"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

CarouselIndicatorGroup.vue source

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

PropDefaultType
as
"button"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

index*
number

The index of the snap this indicator goes to.

Slot (default)Payload
active
boolean

Whether this indicator's snap is the current one

CarouselIndicator.vue source

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.

PropDefaultType
as
"button"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change 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
boolean

Whether autoplay or autoScroll is rotating

CarouselAutoplayToggle.vue source

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.