Components
Carousel

Carousel

A slideshow component that cycles through elements.

Basic Usage

Here’s an example of how to use the Carousel component with a set of images:

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.PrevSlideTrigger>Previous</Carousel.PrevSlideTrigger>
        <Carousel.NextSlideTrigger>Next</Carousel.NextSlideTrigger>
      </Carousel.Control>
      <Carousel.IndicatorGroup>
        {images.map((_, index) => (
          <Carousel.Indicator key={index} index={index}>
            {index + 1}
          </Carousel.Indicator>
        ))}
      </Carousel.IndicatorGroup>
      <Carousel.Viewport>
        <Carousel.SlideGroup>
          {images.map((image, index) => (
            <Carousel.Slide key={index} index={index}>
              <img src={image} />
            </Carousel.Slide>
          ))}
        </Carousel.SlideGroup>
      </Carousel.Viewport>
    </Carousel.Root>
  )
}

To create a controlled Carousel component, you can manage the state of the carousel using the index prop and update it when the onSlideChange event handler is called:

const ControlledCarousel = () => {
  const [currentIndex, setCurrentIndex] = useState(0)

  const handleSlideChange = (details) => {
    setCurrentIndex(details.index)
  }

  return (
    <Carousel index={currentIndex} onSlideChange={handleSlideChange}>
      {/* ... */}
    </Carousel>
  )
}

You can customize the Carousel component by setting various props such as align, loop, slidesPerView, and spacing. Here’s an example of a customized Carousel:

const CustomCarousel = () => {
  return (
    <Carousel
      align="center"
      loop={true}
      slidesPerView={2}
      spacing="16px"
      orientation="horizontal"
    >
      {/* ... */}
    </Carousel>
  )
}

Previous

Avatar

Next

Checkbox