Carousel
The Carousel component provides a flexible, smooth, and accessible slideshow built using Embla Carousel.
It supports autoplay, fading transitions, customizable alignment, and full control over navigation UI (dots and arrows).
import { Carousel } from "tawuniya/components";
π View in Storybook
Propsβ
| Prop | Type | Default | Description |
|---|---|---|---|
slides | SlideItem[] | β | Array of slides containing image, title, and description. |
title | string | β | Default title used if a slide doesnβt provide one. |
description | string | β | Default description used if a slide doesnβt provide one. |
alignContent | { x?: "start" | "center" | "end"; y?: "start" | "center" | "end"; } | { x: "start", y: "end" } | Controls horizontal and vertical alignment of slide content. |
nextIcon | React.ReactNode | <ChevronRight /> | Custom icon for the next button. |
prevIcon | React.ReactNode | <ChevronLeft /> | Custom icon for the previous button. |
options | { loop?: boolean; fade?: boolean; autoPlay?: boolean; delay?: number; startIndex?: number; hideDots?: boolean; hideArrows?: boolean; } | { loop: true, fade: true, autoPlay: true, delay: 3000 } | Carousel behavior configuration. |
classNames | ClassNames | β | Object for overriding internal Tailwind class names. |
children | React.ReactNode | β | Optional React nodes to render within each slide (e.g., CTA buttons). |
onClick | (slideId: string, slide: SlideItem) => void | β | Callback function to handle slide clicks. |
Subtypesβ
SlideItemβ
| Property | Type | Description |
|---|---|---|
id | string | Unique identifier for the slide. |
title | string | Slide title. |
description | string | Slide description text. |
image | string | Background image URL. |
ClassNamesβ
| Property | Description |
|---|---|
className | Root container classes. |
containerClassName | Flex container for all slides. |
backdropClassName | Background overlay (e.g., black/25). |
slideWrapperClassName | Wrapper for each slide. |
slideClassName | Inner slide content container. |
titleClassName | Title typography classes. |
descriptionClassName | Description typography classes. |
actionWrapperClassName | Navigation wrapper (arrows and dots). |
actionButtonClassName | Navigation button classes. |
Basic Exampleβ
import Carousel from "tawuniya/components/carousel";
const slides = [
{
id: "1",
title: "Discover the Future",
description: "Explore innovations that redefine tomorrow.",
image: "/images/slide1.jpg",
},
{
id: "2",
title: "Smart Solutions",
description: "Building a connected, intelligent world.",
image: "/images/slide2.jpg",
},
];
export default function BasicCarousel() {
return <Carousel slides={slides} />;
}
Center Aligned Contentβ
<Carousel
slides={slides}
alignContent={{ x: "center", y: "center" }}
/>
Content is centered both vertically and horizontally.
End Aligned (Bottom-Right)β
<Carousel
slides={slides}
alignContent={{ x: "end", y: "end" }}
/>
Perfect for captions positioned at the bottom-right corner of the slide.
With Custom Action Button Inside Slideβ
<Carousel slides={slides} alignContent={{ x: "start", y: "center" }}>
<Button variant="primary">Learn More</Button>
</Carousel>
Each slide renders the same children component (here, a CTA button).
Fade Transition + Autoplay Disabledβ
<Carousel
slides={slides}
options={{
fade: true,
autoPlay: false,
loop: true,
}}
/>
Ideal for controlled or manual navigation use cases.
Hide Dots and Arrowsβ
<Carousel
slides={slides}
options={{
hideDots: true,
hideArrows: true,
}}
/>
Use this when you want a clean, minimal carousel without navigation UI.
Custom Icons & Delayβ
import { ArrowLeft, ArrowRight } from "lucide-react";
<Carousel
slides={slides}
nextIcon={<ArrowRight />}
prevIcon={<ArrowLeft />}
options={{
autoPlay: true,
delay: 5000,
}}
/>
Customizes icons and increases autoplay delay to 5 seconds.
Styling Exampleβ
<Carousel
slides={slides}
classNames={{
containerClassName: "w-[800px]",
slideClassName: "h-[400px]",
titleClassName: "text-2xl font-bold",
descriptionClassName: "text-lg",
actionButtonClassName: "mt-4",
}}
/>
Notesβ
- The component automatically re-initializes when slides or options change.
- Background images should be optimized for better performance.
- Use the
classNamesprop to adapt the carousel to different layouts or themes. - For best performance, keep the number of slides reasonable when using fade transitions.
Featuresβ
- Fade Transitions: Smooth fade between slides for a polished look.
- Autoplay: Automatically advance slides with configurable delay.
- Navigation Controls: Built-in arrows and pagination dots, customizable or hidable.
- Content Alignment: Align titles, descriptions, and children (x: start/center/end, y: start/center/end).
- Custom Children: Inject React elements like buttons or links into slides.
- Styling Flexibility: Override classes via
classNamesprop for full theming. - Responsive: Automatically scales for desktop, tablet, and mobile.
- Accessible: Works with screen readers and keyboard navigation.
- Dynamic Updates: Reinitializes when slides change.