-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCarousel.js
More file actions
68 lines (65 loc) · 2.14 KB
/
Carousel.js
File metadata and controls
68 lines (65 loc) · 2.14 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
import React, { useState } from 'react';
import LeftChevron from '../Icons/LeftChevron';
import RightChevron from '../Icons/RightChevron';
import { useSwipeable } from 'react-swipeable';
const Carousel = ({ children }) => {
const slideLength = children.length;
const handleNextClick = () => {
const index = currentIndex === slideLength - 1 ? 0 : currentIndex + 1;
setCurrentIndex(index);
};
const handleOnPrevClick = () => {
const index = currentIndex === 0 ? slideLength - 1 : currentIndex - 1;
setCurrentIndex(index);
};
const handlers = useSwipeable({
onSwipedLeft: handleNextClick,
onSwipedRight: handleOnPrevClick,
swipeDuration: 500,
preventScrollOnSwipe: true,
trackMouse: true,
});
const [currentIndex, setCurrentIndex] = useState(0);
return (
<div className="m-auto">
<div className="w-full relative select-none">
<LeftChevron onClick={handleOnPrevClick}
className=" absolute left-0 text-3xl inset-y-1/2 cursor-pointer stroke-slate-800"
fill="black"
/>
<div {...handlers}>
{children.map((child, index) =>
React.cloneElement(child, {
className: `
${
index === currentIndex
? 'block w-full h-auto object-cover'
: 'hidden'
}
`,
}),
)}
</div>
<div className="absolute w-full flex justify-center bottom-0">
{children.map((element, index) => {
return (
<div
className={`h-2 w-2 rounded-full mx-2 mb-2 cursor-pointer ${index === currentIndex ? "bg-black" : "bg-white"}`}
key={index}
onClick={() => {
setCurrentIndex(index);
}}
/>
);
})}
</div>
<RightChevron
onClick={handleNextClick}
className="absolute right-0 text-3xl inset-y-1/2 text-white cursor-pointer stroke-slate-800"
alt="right arrow"
/>
</div>
</div>
);
};
export default Carousel;