Skip to main content

Carousel

The Carousel component is a versatile slideshow component built on top of Swiper.js. It supports manual navigation, autoplay, crossfade effects, and various customization options for creating engaging image galleries and content sliders.

Overview

The Carousel component is designed to provide flexible slideshow functionality with:

  • Manual and automatic navigation
  • Crossfade and slide transition effects
  • Navigation arrows and pagination dots
  • Progress bar for autoplay
  • Support for images with text overlays
  • Customizable styling and behavior
  • Responsive design with touch support

Basic Usage

import { Carousel } from '@/components/molecules'

function MyComponent() {
const slides = [
<img src="image1.jpg" alt="Slide 1" className="w-full h-64 object-cover rounded-xl" />,
<img src="image2.jpg" alt="Slide 2" className="w-full h-64 object-cover rounded-xl" />,
<img src="image3.jpg" alt="Slide 3" className="w-full h-64 object-cover rounded-xl" />
]

return (
<Carousel slides={slides} />
)
}

Props

PropTypeDefaultDescription
slidesReactNode[][]Array of React elements to display as slides
slidesWithTextArray<{image, title, desc}>-Array of objects with image, title, and description
autoplayboolean | numberfalseEnable autoplay (true for default 2000ms, or specify milliseconds)
effectstring-Transition effect ('fade' for crossfade)
navigationbooleantrueShow navigation arrows
paginationbooleantrueShow pagination dots
classNamestring''Additional CSS classes for the carousel
swiperPropsobject{}Additional props to pass to Swiper component
showProgressBarbooleanfalseShow progress bar for autoplay
progressBarClassNamestring'bg-blue-500'CSS classes for progress bar
progressBarPositionstring'top'Position of progress bar ('top' or 'bottom')
spaceBetweennumber16Space between slides in pixels
slidesPerViewnumber1Number of slides visible at once
childrenReactNode-Alternative to slides prop for child elements
onSlideChangefunction() => {}Callback when slide changes
onSwiperfunction-Callback when Swiper instance is created

Manual Navigation

<Card>
<h2>Carousel Basic (Manual)</h2>
<Carousel
slides={dataSlides.map((item) => (
<img src={item.image} alt={item.title} className='w-full h-64 object-cover rounded-xl' />
))}
/>
</Card>

With Children

<Carousel>
<img src="image1.jpg" alt="Slide 1" className="w-full h-64 object-cover rounded-xl" />
<img src="image2.jpg" alt="Slide 2" className="w-full h-64 object-cover rounded-xl" />
<img src="image3.jpg" alt="Slide 3" className="w-full h-64 object-cover rounded-xl" />
</Carousel>

Using slidesWithText Prop

<Card>
<h2>Carousel with Title & Description</h2>
<Carousel slidesWithText={dataSlides} />
</Card>

Data Structure for slidesWithText

const dataSlides = [
{
image: 'https://images.pexels.com/photos/8004110/pexels-photo-8004110.jpeg',
title: 'Beautiful Nature',
desc: 'Enjoy the beauty of nature in every moment.',
},
{
image: 'https://images.pexels.com/photos/3798101/pexels-photo-3798101.jpeg',
title: 'Urban Life',
desc: 'Experience the vibrant city atmosphere.',
},
{
image: 'https://images.pexels.com/photos/3396668/pexels-photo-3396668.jpeg',
title: 'Mountain Adventure',
desc: 'Feel the thrill of adventure in the mountains.',
},
]

Basic Autoplay

<Card>
<h2>Carousel Interval (Autoplay)</h2>
<Carousel
slides={dataSlides.map((item) => (
<img src={item.image} alt={item.title} className='w-full h-64 object-cover rounded-xl' />
))}
autoplay={2000}
/>
</Card>

Autoplay with Progress Bar

<Card>
<h2>Carousel with Progress</h2>
<Carousel
slides={dataSlides.map((item) => (
<img src={item.image} alt={item.title} className='w-full h-64 object-cover rounded-xl' />
))}
autoplay={3000}
showProgressBar
progressBarClassName='bg-blue-500 dark:bg-blue-400'
progressBarPosition='bottom'
/>
</Card>

Transition Effects

Crossfade Effect

<Card>
<h2>Carousel Cross Fade</h2>
<Carousel
slides={dataSlides.map((item) => (
<img src={item.image} alt={item.title} className='w-full h-64 object-cover rounded-xl' />
))}
effect='fade'
/>
</Card>

Without Navigation

<Carousel
slides={slides}
navigation={false}
pagination={true}
/>

Without Pagination

<Carousel
slides={slides}
navigation={true}
pagination={false}
/>

Without Both

<Carousel
slides={slides}
navigation={false}
pagination={false}
/>

Multiple Slides Per View

Two Slides Per View

<Carousel
slides={slides}
slidesPerView={2}
spaceBetween={20}
/>

Three Slides Per View

<Carousel
slides={slides}
slidesPerView={3}
spaceBetween={24}
/>

Progress Bar Customization

Top Position with Custom Color

<Carousel
slides={slides}
autoplay={4000}
showProgressBar
progressBarClassName='bg-green-500'
progressBarPosition='top'
/>

Bottom Position with Custom Color

<Carousel
slides={slides}
autoplay={5000}
showProgressBar
progressBarClassName='bg-purple-500'
progressBarPosition='bottom'
/>

Event Handlers

Slide Change Callback

<Carousel
slides={slides}
onSlideChange={(swiper) => {
console.log('Current slide index:', swiper.activeIndex)
}}
/>

Swiper Instance Callback

<Carousel
slides={slides}
onSwiper={(swiper) => {
console.log('Swiper instance:', swiper)
}}
/>

Advanced Configuration

Custom Swiper Props

<Carousel
slides={slides}
swiperProps={{
loop: true,
speed: 800,
grabCursor: true,
onAutoplayTimeLeft: (swiper, time, progress) => {
console.log('Autoplay progress:', progress)
}
}}
/>

Responsive Configuration

<Carousel
slides={slides}
swiperProps={{
breakpoints: {
640: {
slidesPerView: 1,
spaceBetween: 16
},
768: {
slidesPerView: 2,
spaceBetween: 20
},
1024: {
slidesPerView: 3,
spaceBetween: 24
}
}
}}
/>

Examples

<Card>
<h2>Carousel Basic (Manual)</h2>
<Carousel
slides={dataSlides.map((item) => (
<img src={item.image} alt={item.title} className='w-full h-64 object-cover rounded-xl' />
))}
/>
</Card>
<Card>
<h2>Carousel with Title & Description</h2>
<Carousel slidesWithText={dataSlides} />
</Card>
<Card>
<h2>Carousel Interval (Autoplay)</h2>
<Carousel
slides={dataSlides.map((item) => (
<img src={item.image} alt={item.title} className='w-full h-64 object-cover rounded-xl' />
))}
autoplay={2000}
/>
</Card>
<Card>
<h2>Carousel Cross Fade</h2>
<Carousel
slides={dataSlides.map((item) => (
<img src={item.image} alt={item.title} className='w-full h-64 object-cover rounded-xl' />
))}
effect='fade'
/>
</Card>
<Card>
<h2>Carousel with Progress</h2>
<Carousel
slides={dataSlides.map((item) => (
<img src={item.image} alt={item.title} className='w-full h-64 object-cover rounded-xl' />
))}
autoplay={3000}
showProgressBar
progressBarClassName='bg-blue-500 dark:bg-blue-400'
progressBarPosition='bottom'
/>
</Card>

Data Structure

slidesWithText Array

const dataSlides = [
{
image: 'https://example.com/image1.jpg',
title: 'Slide Title',
desc: 'Slide description text'
},
{
image: 'https://example.com/image2.jpg',
title: 'Another Title',
desc: 'Another description'
}
]

slides Array

const slides = [
<img src="image1.jpg" alt="Slide 1" className="w-full h-64 object-cover rounded-xl" />,
<img src="image2.jpg" alt="Slide 2" className="w-full h-64 object-cover rounded-xl" />,
<div className="bg-blue-500 h-64 flex items-center justify-center text-white">
<h2>Custom Slide Content</h2>
</div>
]

Styling

The Carousel component uses Swiper.js and includes:

  • Responsive Design: Works on all screen sizes
  • Touch Support: Swipe gestures on mobile devices
  • Smooth Transitions: CSS transitions for slide changes
  • Custom Styling: Configurable progress bars and navigation
  • Dark Mode Support: Automatic dark mode variants
  • Accessibility: Keyboard navigation and screen reader support

Accessibility

The Carousel component includes accessibility features:

  • Keyboard navigation support
  • Screen reader friendly
  • ARIA labels for navigation
  • Focus management
  • Touch gesture support

Best Practices

  1. Use appropriate autoplay timing (2-5 seconds recommended)
  2. Provide meaningful alt text for images
  3. Consider mobile users with touch-friendly navigation
  4. Use progress bars for longer autoplay durations
  5. Optimize images for web performance
  6. Test keyboard navigation for accessibility compliance

Common Use Cases

  • Image galleries: Photo collections and portfolios
  • Content sliders: Featured content and promotions
  • Testimonials: Customer reviews and quotes
  • Product showcases: Product images and features
  • Hero sections: Landing page banners
  • News feeds: Article previews and updates
  • Card - Often used as a container for carousel examples
  • Button - For custom navigation controls
  • NavIcon - For custom navigation icons