-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathCarousel.tsx
More file actions
182 lines (168 loc) · 6.39 KB
/
Copy pathCarousel.tsx
File metadata and controls
182 lines (168 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import React, { useState, useEffect, useCallback, useRef } from 'react';
export interface CarouselItem {
id: string | number;
content: React.ReactNode;
alt?: string;
}
export interface CarouselProps {
items: CarouselItem[];
autoPlay?: boolean;
interval?: number;
showArrows?: boolean;
showDots?: boolean;
loop?: boolean;
className?: string;
ariaLabel?: string;
}
export const Carousel: React.FC<CarouselProps> = ({
items,
autoPlay = false,
interval = 5000,
showArrows = true,
showDots = true,
loop = true,
className = '',
ariaLabel = 'Image carousel',
}) => {
const [currentIndex, setCurrentIndex] = useState(0);
const [isPaused, setIsPaused] = useState(false);
const timerRef = useRef<NodeJS.Timeout | null>(null);
const nextSlide = useCallback(() => {
setCurrentIndex((prevIndex) => {
if (prevIndex === items.length - 1) {
return loop ? 0 : prevIndex;
}
return prevIndex + 1;
});
}, [items.length, loop]);
const prevSlide = useCallback(() => {
setCurrentIndex((prevIndex) => {
if (prevIndex === 0) {
return loop ? items.length - 1 : 0;
}
return prevIndex - 1;
});
}, [items.length, loop]);
const goToSlide = (index: number) => {
setCurrentIndex(index);
};
useEffect(() => {
if (autoPlay && !isPaused) {
timerRef.current = setInterval(() => {
nextSlide();
}, interval);
}
return () => {
if (timerRef.current) {
clearInterval(timerRef.current);
}
};
}, [autoPlay, interval, isPaused, nextSlide]);
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'ArrowLeft') {
e.preventDefault();
prevSlide();
} else if (e.key === 'ArrowRight') {
e.preventDefault();
nextSlide();
} else if (e.key === 'Home') {
e.preventDefault();
goToSlide(0);
} else if (e.key === 'End') {
e.preventDefault();
goToSlide(items.length - 1);
}
};
if (!items || items.length === 0) return null;
return (
<div
className={`relative w-full overflow-hidden rounded-lg group focus:outline-none focus:ring-2 focus:ring-primary-500 ${className}`}
onMouseEnter={() => setIsPaused(true)}
onMouseLeave={() => setIsPaused(false)}
onFocus={() => setIsPaused(true)}
onBlur={() => setIsPaused(false)}
onKeyDown={handleKeyDown}
tabIndex={0}
role="region"
aria-roledescription="carousel"
aria-label={ariaLabel}
>
<div
className="flex transition-transform duration-500 ease-out h-full"
style={{ transform: `translateX(-${currentIndex * 100}%)` }}
>
{items.map((item, index) => (
<div
key={item.id}
id={`carousel-slide-${index}`}
className="w-full flex-shrink-0 h-full relative"
role="group"
aria-roledescription="slide"
aria-label={`Slide ${index + 1} of ${items.length}`}
aria-hidden={currentIndex !== index}
style={{ visibility: currentIndex === index ? 'visible' : 'hidden' }}
>
{item.content}
</div>
))}
</div>
{showArrows && items.length > 1 && (
<>
<button
className={`absolute top-1/2 left-4 -translate-y-1/2 bg-white/80 hover:bg-white text-gray-800 p-2 rounded-full shadow-md transition-all focus:outline-none focus:ring-2 focus:ring-primary-500 z-10 ${!loop && currentIndex === 0 ? 'opacity-50 cursor-not-allowed' : 'opacity-0 md:group-hover:opacity-100'}`}
onClick={prevSlide}
disabled={!loop && currentIndex === 0}
aria-label="Previous slide"
>
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 19l-7-7 7-7" />
</svg>
</button>
<button
className={`absolute top-1/2 right-4 -translate-y-1/2 bg-white/80 hover:bg-white text-gray-800 p-2 rounded-full shadow-md transition-all focus:outline-none focus:ring-2 focus:ring-primary-500 z-10 ${!loop && currentIndex === items.length - 1 ? 'opacity-50 cursor-not-allowed' : 'opacity-0 md:group-hover:opacity-100'}`}
onClick={nextSlide}
disabled={!loop && currentIndex === items.length - 1}
aria-label="Next slide"
>
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
</svg>
</button>
</>
)}
{autoPlay && (
<button
onClick={() => setIsPaused(!isPaused)}
className="absolute top-4 right-4 bg-white/80 hover:bg-white text-gray-800 p-2 rounded-full shadow-md transition-all focus:outline-none focus:ring-2 focus:ring-primary-500 z-10 opacity-0 md:group-hover:opacity-100 focus:opacity-100"
aria-label={isPaused ? "Start autoplay" : "Pause autoplay"}
>
{isPaused ? (
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
</svg>
) : (
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 9v6m4-6v6m7-3a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
)}
</button>
)}
{showDots && items.length > 1 && (
<div className="absolute bottom-4 left-1/2 -translate-x-1/2 flex space-x-2 z-10">
{items.map((_, index) => (
<button
key={index}
onClick={() => goToSlide(index)}
className={`w-3 h-3 rounded-full transition-all focus:outline-none focus:ring-2 focus:ring-primary-500 ${currentIndex === index ? 'bg-white scale-110' : 'bg-white/50 hover:bg-white/80'}`}
aria-label={`Go to slide ${index + 1}`}
aria-current={currentIndex === index ? 'true' : 'false'}
/>
))}
</div>
)}
<div className="sr-only" aria-live="polite" aria-atomic="true">
Item {currentIndex + 1} of {items.length}
</div>
</div>
);
};