Carousel
A slideshow component that cycles through elements.
A slideshow component that cycles through elements.
To set up the carousel correctly, you’ll need to understand its anatomy and how we name its parts.
Each part includes a
data-part
attribute to help identify them in the DOM.
Learn how to use the Caorusel
component in your project. Let’s take a look at
the most basic example:
import { Carousel } from '@ark-ui/react'
const Basic = () => {
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
]
return (
<Carousel.Root>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
{images.map((_, index) => (
<Carousel.Indicator key={index} index={index}>
{index + 1}
</Carousel.Indicator>
))}
</Carousel.IndicatorGroup>
<Carousel.Viewport>
<Carousel.ItemGroup>
{images.map((image, index) => (
<Carousel.Item key={index} index={index}>
<img src={image} />
</Carousel.Item>
))}
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
)
}
import { Carousel } from '@ark-ui/solid'
import { Index } from 'solid-js'
const Basic = () => {
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
'https://tinyurl.com/6jurv23t',
'https://tinyurl.com/yp4rfum7',
]
return (
<Carousel.Root>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
<Index each={images}>
{(_, index) => <Carousel.Indicator index={index}>{index + 1}</Carousel.Indicator>}
</Index>
</Carousel.IndicatorGroup>
<Carousel.Viewport>
<Carousel.ItemGroup>
<Index each={images}>
{(image, index) => (
<Carousel.Item index={index}>
<img src={image()} />
</Carousel.Item>
)}
</Index>
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Carousel } from '@ark-ui/vue'
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
'https://tinyurl.com/6jurv23t',
'https://tinyurl.com/yp4rfum7',
]
const index = ref(0)
</script>
<template>
<Carousel.Root>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
<Carousel.Indicator v-for="(_, idx) in images" :key="idx" :index="idx">
{{ idx + 1 }}
</Carousel.Indicator>
</Carousel.IndicatorGroup>
<Carousel.Viewport>
<Carousel.ItemGroup>
<Carousel.Item v-for="(image, idx) in images" :key="idx" :index="idx">
<img
:src="image"
alt=""
:style="{ height: '300px', width: '100%', objectFit: 'cover' }"
/>
</Carousel.Item>
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
</template>
To create a controlled Carousel component, you can manage the state of the
carousel using the index
prop and update it when the onIndexChange
event
handler is called:
import { Carousel } from '@ark-ui/react'
import { useState } from 'react'
const Controlled = () => {
const [currentIndex, setCurrentIndex] = useState(0)
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
]
return (
<Carousel.Root index={currentIndex} onIndexChange={(details) => setCurrentIndex(details.index)}>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
{images.map((_, index) => (
<Carousel.Indicator key={index} index={index}>
{index + 1}
</Carousel.Indicator>
))}
</Carousel.IndicatorGroup>
<Carousel.Viewport>
<Carousel.ItemGroup>
{images.map((image, index) => (
<Carousel.Item key={index} index={index}>
<img src={image} />
</Carousel.Item>
))}
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
)
}
import { Carousel } from '@ark-ui/solid'
import { Index, createSignal } from 'solid-js'
const Controlled = () => {
const [currentIndex, setCurrentIndex] = createSignal(0)
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
'https://tinyurl.com/6jurv23t',
'https://tinyurl.com/yp4rfum7',
]
return (
<>
<Carousel.Root
index={currentIndex()}
onIndexChange={(details) => setCurrentIndex(details.index)}
>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.Viewport>
<Carousel.ItemGroup>
<Index each={images}>
{(image, index) => (
<Carousel.Item index={index}>
<img src={image()} />
</Carousel.Item>
)}
</Index>
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
</>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Carousel } from '@ark-ui/vue'
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
'https://tinyurl.com/6jurv23t',
'https://tinyurl.com/yp4rfum7',
]
const index = ref(0)
</script>
<template>
<Carousel.Root v-model="index">
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
<Carousel.Indicator v-for="(_, idx) in images" :key="idx" :index="idx">
{{ idx + 1 }}
</Carousel.Indicator>
</Carousel.IndicatorGroup>
<Carousel.Viewport>
<Carousel.ItemGroup>
<Carousel.Item v-for="(image, idx) in images" :key="idx" :index="idx">
<img
:src="image"
alt=""
:style="{ height: '300px', width: '100%', objectFit: 'cover' }"
/>
</Carousel.Item>
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
</template>
You can customize the Carousel component by setting various props. Here’s an example of a customized Carousel:
import { Carousel } from '@ark-ui/react'
const Customized = () => {
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
]
return (
<Carousel.Root
align="center"
loop={true}
slidesPerView={2}
spacing="16px"
orientation="horizontal"
>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
{images.map((_, index) => (
<Carousel.Indicator key={index} index={index}>
{index + 1}
</Carousel.Indicator>
))}
</Carousel.IndicatorGroup>
<Carousel.Viewport>
<Carousel.ItemGroup>
{images.map((image, index) => (
<Carousel.Item key={index} index={index}>
<img src={image} />
</Carousel.Item>
))}
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
)
}
import { Carousel } from '@ark-ui/solid'
import { Index } from 'solid-js'
const Customized = () => {
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
]
return (
<Carousel.Root
align="center"
loop={true}
slidesPerView={2}
spacing="16px"
orientation="horizontal"
>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
<Index each={images}>
{(_, index) => <Carousel.Indicator index={index}>{index + 1}</Carousel.Indicator>}
</Index>
</Carousel.IndicatorGroup>
<Carousel.Viewport>
<Carousel.ItemGroup>
<Index each={images}>
{(image, index) => (
<Carousel.Item index={index}>
<img src={image()} />
</Carousel.Item>
)}
</Index>
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
)
}
<script setup lang="ts">
import { ref } from 'vue'
import { Carousel } from '@ark-ui/vue'
const images = [
'https://tinyurl.com/5b6ka8jd',
'https://tinyurl.com/7rmccdn5',
'https://tinyurl.com/59jxz9uu',
'https://tinyurl.com/6jurv23t',
'https://tinyurl.com/yp4rfum7',
]
const index = ref(0)
</script>
<template>
<Carousel.Root
:align="'center'"
loop
:slides-per-view="2"
spacing="16px"
orientation="horizontal"
>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
<Carousel.Indicator v-for="(_, idx) in images" :key="idx" :index="idx">
{{ idx + 1 }}
</Carousel.Indicator>
</Carousel.IndicatorGroup>
<Carousel.Viewport>
<Carousel.ItemGroup>
<Carousel.Item v-for="(image, idx) in images" :key="idx" :index="idx">
<img
:src="image"
alt=""
:style="{ height: '300px', width: '100%', objectFit: 'cover' }"
/>
</Carousel.Item>
</Carousel.ItemGroup>
</Carousel.Viewport>
</Carousel.Root>
</template>
index | number | |
asChild Render as a different element type. | boolean |
align The alignment of the slides in the carousel. | 'center' | 'end' | 'start' | |
asChild Render as a different element type. | boolean | |
defaultIndex The initial selected index of the carousel. | number | |
dir The document's text/writing direction. | 'ltr' | 'rtl' | "ltr" |
getRootNode A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron. | () => Node | ShadowRoot | Document | |
id The unique identifier of the machine. | string | |
ids The ids of the elements in the carousel. Useful for composition. | Partial<{
root: string
viewport: string
slide(index: number): string
slideGroup: string
nextSlideTrigger: string
prevSlideTrigger: string
indicatorGroup: string
indicator(index: number): string
}> | |
index The current slide index. | number | |
loop Whether the carousel should loop around. | boolean | |
onIndexChange Function called when the slide changes. | (details: SlideChangeDetails) => void | |
orientation The orientation of the carousel. | 'horizontal' | 'vertical' | "horizontal" |
slidesPerView The number of slides to show at a time. | number | 'auto' | |
spacing The amount of space between slides. | string |
asChild Render as a different element type. | boolean |
asChild Render as a different element type. | boolean |
index | number | |
asChild Render as a different element type. | boolean | |
readOnly | boolean |
asChild Render as a different element type. | boolean |
asChild Render as a different element type. | boolean |
asChild Render as a different element type. | boolean |
asChild Render as a different element type. | boolean |