import { useState, useEffect, useRef, type ReactNode } from 'react'; import { Box, IconButton, Typography } from '@mui/material'; import { ChevronLeft, ChevronRight } from '@mui/icons-material'; import { useRdsTokens } from '../shared/hooks/useRdsTokens'; import clsx from 'clsx'; import './rds-carousel.scss'; const EMPTY_TITLES: string[] = []; const EMPTY_SUBTITLES: string[] = []; export interface RdsCarouselProps { children: ReactNode[]; autoPlay?: boolean; className?: string; autoPlayInterval?: number; showArrows?: boolean; showDots?: boolean; height?: string | number; type?: 'circle' | 'line'; state?: '1' | '2' | '3' | '4'; style?: 'default' | 'with title' | 'full width image'; titles?: string[]; subtitles?: string[]; title?: string; subtitle?: string; } const RdsCarousel = ({ children, className, autoPlay = false, autoPlayInterval = 3000, showArrows = true, showDots = true, height = '400px', type = 'circle', state, style = 'default', titles, subtitles, title, subtitle, }:RdsCarouselProps) => { const resolvedTitles = titles ?? EMPTY_TITLES; const resolvedSubtitles = subtitles ?? EMPTY_SUBTITLES; const [currentIndex, setCurrentIndex] = useState(0); const prevStateRef = useRef(state); if (state !== prevStateRef.current) { prevStateRef.current = state; if (state && !isNaN(parseInt(state))) { const newStateIndex = parseInt(state) - 1; if (newStateIndex >= 0 && newStateIndex < children.length) { setCurrentIndex(newStateIndex); } } } const tokens = useRdsTokens(); const hasTitleLayout = style === 'with title' || style === 'full width image'; useEffect(() => { if (autoPlay && children.length > 1) { const interval = setInterval(() => { setCurrentIndex((prev) => (prev + 1) % children.length); }, autoPlayInterval); return () => clearInterval(interval); } }, [autoPlay, autoPlayInterval, children.length]); const nextSlide = () => { setCurrentIndex((prev) => (prev + 1) % children.length); }; const prevSlide = () => { setCurrentIndex((prev) => (prev - 1 + children.length) % children.length); }; const goToSlide = (index: number) => { setCurrentIndex(index); }; const carouselClassName = clsx( 'rds-carousel', `rds-carousel--${style.replace(' ', '-')}`, className, ); return ( {children.map((child, index) => { const displayTitle = resolvedTitles[index] ?? title ?? `Card Title`; const displaySubtitle = resolvedSubtitles[index] ?? subtitle ?? `In a laoreet purus. Integer turpis quam, laoreet id`; return ( {style === 'with title' && ( {displayTitle && ( {displayTitle} )} {displaySubtitle && ( {displaySubtitle} )} )} {child} {style === 'full width image' && ( {displayTitle && ( {displayTitle} )} {displaySubtitle && ( {displaySubtitle} )} )} ); })} {showArrows && children.length > 1 && ( <> )} {showDots && children.length > 1 && ( {children.map((_, index) => ( goToSlide(index)} className={clsx( 'rds-carousel__indicator', `rds-carousel__indicator--${type}`, currentIndex === index && 'rds-carousel__indicator__active', )} sx={{ width: type === 'circle' ? tokens.space(1.5) : tokens.space(3), height: type === 'circle' ? tokens.space(1.5) : tokens.space(0.5), borderRadius: type === 'circle' ? tokens.radius.full : tokens.radius.sm, // Use carousel-indicator tokens (present in both themes). Avoid // --rds-neutral-* here — those light-theme-only vars are removed in dark mode. backgroundColor: currentIndex === index ? tokens.color.primary : tokens.cssVar('carousel-indicator-bg'), opacity: currentIndex === index || style !== 'full width image' ? 1 : 0.8, border: style === 'full width image' ? `1px solid ${currentIndex === index ? tokens.color.primary : tokens.cssVar('carousel-indicator-bg')}` : 'none', cursor: 'pointer', transition: 'background-color 0.2s, opacity 0.2s', '&:hover': { backgroundColor: currentIndex === index ? tokens.color.primary : tokens.cssVar('carousel-indicator-bg-hover'), opacity: 1, }, }} /> ))} )} ); }; RdsCarousel.displayName = 'RdsCarousel'; export default RdsCarousel;