Components
Carousel

Carousel

The Carousel component is a dynamic and responsive content display tool for your applications. It provides support for various configurations, including slide alignment, text direction, and slide orientation.

Basic Usage

To get started, import the necessary components from the package:

import {
  Carousel,
  CarouselControl,
  CarouselNextSlideTrigger,
  CarouselPrevSlideTrigger,
  CarouselSlide,
  CarouselSlideGroup,
  CarouselViewport,
  CarouselIndicator,
  CarouselIndicatorGroup,
} from '@ark-ui/react'

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

import {
  Carousel,
  CarouselControl,
  CarouselIndicator,
  CarouselIndicatorGroup,
  CarouselNextSlideTrigger,
  CarouselPrevSlideTrigger,
  CarouselSlide,
  CarouselSlideGroup,
  CarouselViewport,
} from '@ark-ui/react'

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>
      <CarouselControl>
        <CarouselPrevSlideTrigger>Previous</CarouselPrevSlideTrigger>
        <CarouselNextSlideTrigger>Next</CarouselNextSlideTrigger>
      </CarouselControl>
      <CarouselIndicatorGroup>
        {images.map((_, index) => (
          <CarouselIndicator key={index} index={index}>
            {index + 1}
          </CarouselIndicator>
        ))}
      </CarouselIndicatorGroup>
      <CarouselViewport>
        <CarouselSlideGroup>
          {images.map((image, index) => (
            <CarouselSlide key={index} index={index}>
              <img
                src={image}
                alt=""
                style={{ height: '300px', width: '100%', objectFit: 'cover' }}
              />
            </CarouselSlide>
          ))}
        </CarouselSlideGroup>
      </CarouselViewport>
    </Carousel>
  )
}

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>
  )
}

Conclusion

The Carousel component offers a flexible and powerful solution for displaying dynamic content in your applications. With its comprehensive set of features and customizability, it can accommodate a wide range of use cases and designs.

Previous

Avatar

Next

Checkbox