Carousel
Rotate through related content within a limited area, such as homepage banners, image galleries, or announcements.
Import
import { HuiCarousel, HuiCarouselItem } from '@nscode/handy-ui'
Internal State and Controlled Updates
When modelValue is omitted or only an update listener is supplied, the active index is maintained internally from 0. Prop presence enables controlled mode, including undefined, which displays index 0. Arrows, indicators, keyboard interaction, public methods, and autoplay all follow the same parent value.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCarousel, HuiCarouselItem, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No requests yet')
const q1Model = ref(0)
</script>
<template>
<div class="carousel-q1-demo">
<HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton
><HuiSpace direction="vertical" fill>
<div>
<p>Prop omitted: internal state</p>
<div class="q1-local">
<HuiCarousel :autoplay="false" :height="96" arrow="always"
><HuiCarouselItem name="a" label="A"><p>Content A</p></HuiCarouselItem
><HuiCarouselItem name="b" label="B"><p>Content B</p></HuiCarouselItem
><HuiCarouselItem name="c" label="C"><p>Content C</p></HuiCarouselItem></HuiCarousel
>
</div>
</div>
<div>
<p>Listener only: still internal state</p>
<div class="q1-listener">
<HuiCarousel
:autoplay="false"
:height="96"
arrow="always"
@update:model-value="q1Last = JSON.stringify($event)"
><HuiCarouselItem name="a" label="A"><p>Content A</p></HuiCarouselItem
><HuiCarouselItem name="b" label="B"><p>Content B</p></HuiCarouselItem
><HuiCarouselItem name="c" label="C"><p>Content C</p></HuiCarouselItem></HuiCarousel
>
</div>
</div>
<div>
<p>Explicit undefined: parent value wins</p>
<div class="q1-empty">
<HuiCarousel :autoplay="false" :height="96" arrow="always" :model-value="undefined"
><HuiCarouselItem name="a" label="A"><p>Content A</p></HuiCarouselItem
><HuiCarouselItem name="b" label="B"><p>Content B</p></HuiCarouselItem
><HuiCarouselItem name="c" label="C"><p>Content C</p></HuiCarouselItem></HuiCarousel
>
</div>
</div>
<div>
<p>Fixed controlled value: parent rejects updates</p>
<div class="q1-fixed">
<HuiCarousel
:autoplay="false"
:height="96"
arrow="always"
:model-value="1"
@update:model-value="q1Last = JSON.stringify($event)"
><HuiCarouselItem name="a" label="A"><p>Content A</p></HuiCarouselItem
><HuiCarouselItem name="b" label="B"><p>Content B</p></HuiCarouselItem
><HuiCarouselItem name="c" label="C"><p>Content C</p></HuiCarouselItem></HuiCarousel
>
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound">
<HuiCarousel :autoplay="false" :height="96" arrow="always" v-model="q1Model"
><HuiCarouselItem name="a" label="A"><p>Content A</p></HuiCarouselItem
><HuiCarouselItem name="b" label="B"><p>Content B</p></HuiCarouselItem
><HuiCarouselItem name="c" label="C"><p>Content C</p></HuiCarouselItem></HuiCarousel
>
</div>
</div>
<p class="q1-notification">
Bound value:{{ q1Model }}; latest request:{{ q1Last }}
</p></HuiSpace
>
</div>
</template>
Basic Usage
Place each slide in an HuiCarouselItem. height sets the carousel height, defaulting to 300px; slide content should fill it with height: 100%. Slides advance every 3 seconds by default and pause on hover.
<script setup lang="ts">
import { HuiCarousel, HuiCarouselItem } from '@nscode/handy-ui'
</script>
<template>
<HuiCarousel height="200px">
<HuiCarouselItem v-for="i in 4" :key="i">
<div class="slide">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
</template>
<style scoped>
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: var(--hui-color-primary);
color: var(--hui-color-white);
}
</style>
Height
height accepts a CSS length string, such as '200px' or '50vh', or a number in pixels. The examples below use numeric 120 and string '180px':
<template>
<HuiSpace direction="vertical" fill>
<HuiCarousel :height="120">
<HuiCarouselItem v-for="i in 3" :key="i">
<div :class="['slide', `slide--${i}`]">:height="120", slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
<HuiCarousel height="180px">
<HuiCarouselItem v-for="i in 3" :key="i">
<div :class="['slide', `slide--${i}`]">height="180px", slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
</HuiSpace>
</template>
<style scoped>
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--hui-color-white);
}
.slide--1 {
background: var(--hui-color-primary);
}
.slide--2 {
background: var(--hui-color-success);
}
.slide--3 {
background: var(--hui-color-warning);
}
</style>
Autoplay
autoplay enables automatic transitions, interval sets the delay in milliseconds, and pauseOnHover controls pausing on hover. Updates to these props take effect immediately, and asynchronously loaded slides also start autoplay. Change the settings below to compare:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCarousel, HuiCarouselItem, HuiCheckbox, HuiSpace } from '@nscode/handy-ui'
const autoplay = ref(true)
const pauseOnHover = ref(false)
const interval = ref(5000)
const intervals = [1000, 3000, 5000]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap align="center">
<HuiCheckbox v-model="autoplay">Autoplay (autoplay)</HuiCheckbox>
<HuiCheckbox v-model="pauseOnHover">Pause on hover (pause-on-hover)</HuiCheckbox>
<span>Interval (interval):</span>
<HuiButton
v-for="ms in intervals"
:key="ms"
size="small"
:type="interval === ms ? 'primary' : 'default'"
@click="interval = ms"
>
{{ ms / 1000 }} seconds
</HuiButton>
</HuiSpace>
<HuiCarousel
height="200px"
:autoplay="autoplay"
:interval="interval"
:pause-on-hover="pauseOnHover"
>
<HuiCarouselItem v-for="i in 3" :key="i">
<div :class="['slide', `slide--${i}`]">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
</HuiSpace>
</template>
<style scoped>
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--hui-color-white);
}
.slide--1 {
background: var(--hui-color-primary);
}
.slide--2 {
background: var(--hui-color-success);
}
.slide--3 {
background: var(--hui-color-warning);
}
</style>
Arrows
arrow controls when navigation arrows appear: 'hover' (the default) shows them on hover or while keyboard focus is inside the carousel; 'always' keeps them visible; 'never' hides them while indicators and arrow-key navigation remain available.
With loop=false, navigation does not wrap and the corresponding arrow is disabled at the first or last slide. The first carousel below also disables loop:
<template>
<HuiSpace direction="vertical" fill>
<span class="caption">arrow="always", :loop="false"</span>
<HuiCarousel height="160px" arrow="always" :loop="false" :autoplay="false">
<HuiCarouselItem v-for="i in 3" :key="i">
<div :class="['slide', `slide--${i}`]">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
<span class="caption">arrow="hover" (default, shown on hover)</span>
<HuiCarousel height="160px" :autoplay="false">
<HuiCarouselItem v-for="i in 3" :key="i">
<div :class="['slide', `slide--${i}`]">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
<span class="caption">arrow="never"</span>
<HuiCarousel height="160px" arrow="never" :autoplay="false">
<HuiCarouselItem v-for="i in 3" :key="i">
<div :class="['slide', `slide--${i}`]">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--hui-color-white);
}
.slide--1 {
background: var(--hui-color-primary);
}
.slide--2 {
background: var(--hui-color-success);
}
.slide--3 {
background: var(--hui-color-warning);
}
</style>
Indicators
Indicators appear outside the carousel by default (indicator-position="outside"). 'none' hides them. Each indicator's accessible name comes from its HuiCarouselItem.label, falling back to a localized slide label when omitted.
<template>
<HuiSpace direction="vertical" fill>
<span class="caption">indicator-position="outside" (default)</span>
<HuiCarousel height="160px">
<HuiCarouselItem v-for="i in 3" :key="i" :label="`Slide ${i}`">
<div :class="['slide', `slide--${i}`]">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
<span class="caption">indicator-position="none"</span>
<HuiCarousel height="160px" indicator-position="none">
<HuiCarouselItem v-for="i in 3" :key="i" :label="`Slide ${i}`">
<div :class="['slide', `slide--${i}`]">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--hui-color-white);
}
.slide--1 {
background: var(--hui-color-primary);
}
.slide--2 {
background: var(--hui-color-success);
}
.slide--3 {
background: var(--hui-color-warning);
}
</style>
Card Mode
With type="card", the active slide is centered and the previous and next slides appear smaller at the edges. Click a side slide to activate it. The default type='' uses regular mode.
<template>
<HuiCarousel type="card" height="200px">
<HuiCarouselItem v-for="i in 5" :key="i">
<div :class="['slide', `slide--${i}`]">Card {{ i }}</div>
</HuiCarouselItem>
</HuiCarousel>
</template>
<style scoped>
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--hui-color-white);
}
.slide--1 {
background: var(--hui-color-primary);
}
.slide--2 {
background: var(--hui-color-success);
}
.slide--3 {
background: var(--hui-color-warning);
}
.slide--4 {
background: var(--hui-color-danger);
}
.slide--5 {
background: var(--hui-color-help);
}
</style>
Vertical Direction
direction defaults to 'horizontal'. With direction="vertical", slides move vertically, arrows appear at the top and bottom, indicators are centered on the right, and ArrowUp/ArrowDown navigate. Vertical card mode shows the neighboring cards at the top and bottom edges.
<template>
<HuiSpace direction="vertical" fill>
<span class="caption">direction="vertical"</span>
<HuiCarousel direction="vertical" height="240px">
<HuiCarouselItem v-for="i in 4" :key="i">
<div :class="['slide', `slide--${i}`]">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
<span class="caption">direction="vertical", type="card"</span>
<HuiCarousel direction="vertical" type="card" height="240px" :autoplay="false">
<HuiCarouselItem v-for="i in 4" :key="i">
<div :class="['slide', `slide--${i}`]">Card {{ i }}</div>
</HuiCarouselItem>
</HuiCarousel>
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--hui-color-white);
}
.slide--1 {
background: var(--hui-color-primary);
}
.slide--2 {
background: var(--hui-color-success);
}
.slide--3 {
background: var(--hui-color-warning);
}
.slide--4 {
background: var(--hui-color-danger);
}
</style>
Controlled Navigation
Bind the zero-based active index with v-model and change it externally to navigate. You can also call prev, next, and setActiveItem through the ref. Navigation emits change with the new and previous indices and the active slide's name and label.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCarousel, HuiCarouselItem, HuiSpace } from '@nscode/handy-ui'
import type { CarouselChangeEvent } from '@nscode/handy-ui'
const active = ref(0)
const carouselRef = ref<InstanceType<typeof HuiCarousel>>()
const lastChange = ref('')
const handleChange = (activeIndex: number, event: CarouselChangeEvent) => {
lastChange.value = `prevIndex ${event.prevIndex} → activeIndex ${activeIndex}, name: ${event.name}, label: ${event.label}`
}
</script>
<template>
<div style="margin-bottom: 12px">
<HuiSpace wrap>
<HuiButton @click="carouselRef?.prev()">Previous (prev)</HuiButton>
<HuiButton @click="carouselRef?.next()">Next (next)</HuiButton>
<HuiButton @click="carouselRef?.setActiveItem(2)">Go to slide 3 (setActiveItem)</HuiButton>
<HuiButton @click="active = 0">First slide (v-model)</HuiButton>
</HuiSpace>
</div>
<HuiCarousel
ref="carouselRef"
v-model="active"
height="200px"
:autoplay="false"
@change="handleChange"
>
<HuiCarouselItem v-for="i in 4" :key="i" :name="`slide-${i}`" :label="`Slide ${i}`">
<div :class="['slide', `slide--${i}`]">Slide {{ i }} content</div>
</HuiCarouselItem>
</HuiCarousel>
<p style="margin-top: 12px">v-model:{{ active }}</p>
<p>change:{{ lastChange || 'No changes yet' }}</p>
</template>
<style scoped>
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--hui-color-white);
}
.slide--1 {
background: var(--hui-color-primary);
}
.slide--2 {
background: var(--hui-color-success);
}
.slide--3 {
background: var(--hui-color-warning);
}
.slide--4 {
background: var(--hui-color-danger);
}
</style>
Adding and Removing Slides
Render HuiCarouselItem with v-for to add or remove slides. Indicators follow DOM order. When removal leaves the active index beyond the remaining slides, it is corrected to the new last slide, emitting update:modelValue and change.
Go to the last slide and choose Remove the last slide to observe the index correction after confirmation. Pending confirmation does not change slides or the active index; cancel, closing, or Esc preserves them. Reset restores the first three slides, retains added slides, and returns to the first slide without deleting others. Adding, resetting, changing slide content or the active slide, toggling read-only, or unmounting invalidates the old request.
<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import {
HuiButton,
HuiCarousel,
HuiCarouselItem,
HuiMessageBox,
HuiSpace
} from '@nscode/handy-ui'
// Confirm removal of the final Carousel slide
const pages = ref([1, 2, 3])
const active = ref(0)
const readonly = ref(false)
const pending = ref(false)
let alive = true
let version = 0
let nextId = 4
watch([pages, active, readonly], () => version++, { deep: true, flush: 'sync' })
onBeforeUnmount(() => {
alive = false
version++
})
const add = () => {
if (!alive || readonly.value) return
pages.value = [...pages.value, nextId]
nextId += 1
}
const removeLast = async () => {
if (!alive || readonly.value || pending.value || pages.value.length <= 1) return
const collection = pages.value
const snapshot = collection.slice()
const target = snapshot[snapshot.length - 1]
const request = version
pending.value = true
try {
await HuiMessageBox.confirm(
`Remove the last slide “Page ${target}” (1 slide). Out of ${snapshot.length} slides, the remaining ${snapshot.length - 1} will remain. If the active slide is removed, its index will move to the new last slide only after confirmation.`,
'Confirm slide removal',
{ type: 'warning' }
)
if (
!alive ||
readonly.value ||
version !== request ||
pages.value !== collection ||
collection.length !== snapshot.length ||
collection.some((item, index) => item !== snapshot[index])
)
return
pages.value = collection.slice(0, -1)
} catch {
// Cancel, closing, and Esc preserve slides and the active index.
} finally {
if (alive) pending.value = false
}
}
const reset = () => {
if (!alive || readonly.value) return
pages.value = [1, 2, 3, ...pages.value.filter((id) => ![1, 2, 3].includes(id))]
active.value = 0
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<label><input v-model="readonly" type="checkbox" /> Read-only</label>
<HuiButton icon="plus" :disabled="readonly" @click="add">Add a slide</HuiButton>
<HuiButton
icon="minus"
:disabled="readonly || pending || pages.length <= 1"
@click="removeLast"
>
Remove the last slide
</HuiButton>
<HuiButton icon="refresh" :disabled="readonly" @click="reset">Reset carousel</HuiButton>
</HuiSpace>
<HuiCarousel v-model="active" height="160px" arrow="always" :autoplay="false">
<HuiCarouselItem v-for="id in pages" :key="id">
<div class="slide">Page {{ id }}</div>
</HuiCarouselItem>
</HuiCarousel>
<span>Total {{ pages.length }} slides; active index {{ active }}</span>
<span v-if="pending">Waiting for confirmation; slides and the active index are unchanged</span>
</HuiSpace>
</template>
<style scoped>
.slide {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--hui-color-white);
background: var(--hui-color-primary);
}
</style>
Keyboard Interaction
- Arrows and indicators are native buttons. Tab to them and press Space or Enter to navigate.
- With focus inside the carousel, ArrowLeft/ArrowRight navigate horizontally; ArrowUp/ArrowDown navigate vertically.
Comparing Indicator Positions
outside, inside, and none place indicators outside, inside, or hide them respectively.
<script setup lang="ts">
import { HuiCarousel, HuiCarouselItem, HuiSpace } from '@nscode/handy-ui'
const positions = ['outside', 'inside', 'none'] as const
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="position in positions" :key="position">
<p>{{ position }}</p>
<HuiCarousel :indicator-position="position" :autoplay="false" :height="100">
<HuiCarouselItem v-for="page in 3" :key="page"
><div
style="height: 100%; display: grid; place-items: center; background: var(--hui-color-primary-light-9)"
>
Slide {{ page }} content
</div></HuiCarouselItem
>
</HuiCarousel>
</div>
</HuiSpace>
</template>
HuiCarousel Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | number | undefined | Zero-based active index, supporting v-model |
| height | string | number | '' | Carousel height; numeric values use pixels. Defaults to 300px when omitted |
| autoplay | boolean | true | Whether to advance automatically; does not advance with only one slide |
| interval | number | 3000 | Autoplay interval in milliseconds |
| indicatorPosition | 'outside' | 'inside' | 'none' | 'outside' | Indicator placement |
| arrow | 'always' | 'hover' | 'never' | 'hover' | When arrows appear. 'hover' shows them on hover or while keyboard focus is inside |
| loop | boolean | true | Whether navigation wraps; disabling it disables the arrow at each boundary |
| direction | 'horizontal' | 'vertical' | 'horizontal' | Direction |
| pauseOnHover | boolean | true | Whether hovering pauses autoplay |
| type | '' | 'card' | '' | Mode. 'card' shows clickable neighboring cards at the side edges, or top/bottom in vertical mode |
HuiCarouselItem Props
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | number | - | Slide name, included in change |
| label | string | - | Indicator's accessible label, localized when omitted; also included in change |
HuiCarousel Events
| Event | Callback arguments | Description |
|---|---|---|
| update:modelValue | (index: number) | Emitted when the active index changes; used by v-model |
| change | (activeIndex: number, context: CarouselChangeEvent) | Emitted on active-slide changes. The first argument is the new index; the context includes the previous index and new slide's name and label |
HuiCarousel Slots
| Slot | Description |
|---|---|
| default | Place HuiCarouselItem components, one per slide |
HuiCarouselItem Slots
| Slot | Description |
|---|---|
| default | Slide content |
HuiCarousel Methods
Call through the ref.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| setActiveItem | index: number | - | Navigate to an index. Out-of-range values wrap in loop mode and clamp to the boundaries otherwise |
| prev | - | - | Navigate to the previous slide |
| next | - | - | Navigate to the next slide |
The active index is also exposed as activeIndex (Ref<number>).
Types
import type {
CarouselProps,
CarouselItemProps,
CarouselChangeEvent,
CarouselDirection,
CarouselIndicatorPosition,
CarouselArrow,
CarouselType
} from '@nscode/handy-ui'
type CarouselDirection = 'horizontal' | 'vertical'
type CarouselIndicatorPosition = 'outside' | 'inside' | 'none'
type CarouselArrow = 'always' | 'hover' | 'never'
type CarouselType = '' | 'card'
interface CarouselProps {
modelValue?: number
height?: string | number
autoplay?: boolean
interval?: number
indicatorPosition?: CarouselIndicatorPosition
arrow?: CarouselArrow
loop?: boolean
direction?: CarouselDirection
pauseOnHover?: boolean
type?: CarouselType
}
interface CarouselItemProps {
name?: string | number
label?: string
}
interface CarouselChangeEvent {
prevIndex: number
name?: string | number
label?: string
}
CarouselContext, CarouselItemInstance, and CAROUSEL_INJECTION_KEY are also exported for internal communication between HuiCarousel and HuiCarouselItem.