-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathImageCarousel.tsx
More file actions
82 lines (75 loc) · 2.38 KB
/
ImageCarousel.tsx
File metadata and controls
82 lines (75 loc) · 2.38 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
import { Image, Button } from "react-bootstrap";
import { useRef } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faChevronLeft,
faChevronRight,
} from "@fortawesome/free-solid-svg-icons";
interface ImageCarouselProps {
images: string[];
currentImage: string;
onSelect: (img: string) => void;
}
const ImageCarousel = ({
images,
currentImage,
onSelect,
}: ImageCarouselProps) => {
const scrollRef = useRef<HTMLDivElement>(null);
const scroll = (direction: "left" | "right") => {
if (scrollRef.current) {
const { scrollLeft } = scrollRef.current;
const scrollTo =
direction === "left" ? scrollLeft - 200 : scrollLeft + 200;
scrollRef.current.scrollTo({ left: scrollTo, behavior: "smooth" });
}
};
return (
<div className="d-flex align-items-center w-100 gap-2">
<Button
variant="dark"
className="rounded-circle border-secondary p-0 d-flex align-items-center justify-content-center shadow-sm"
style={{ minWidth: "40px", height: "40px", zIndex: 2 }}
onClick={() => scroll("left")}
>
<FontAwesomeIcon icon={faChevronLeft} />
</Button>
<div
ref={scrollRef}
className="d-flex gap-2 overflow-auto flex-grow-1 no-scrollbar p-1"
style={{ scrollbarWidth: "none" }}
>
{images.map((img, idx) => (
<div
key={idx}
className={`rounded-3 overflow-hidden border border-2 flex-shrink-0 cursor-pointer transition-all ${
currentImage === img
? "border-primary scale-up"
: "border-secondary opacity-50"
}`}
style={{
width: "150px",
height: "150px",
transition: "all 0.2s ease-in-out",
}}
onClick={() => onSelect(img)}
>
<Image
src={img}
className="w-100 h-100 object-fit-cover shadow-inner"
/>
</div>
))}
</div>
<Button
variant="dark"
className="rounded-circle border-secondary p-0 d-flex align-items-center justify-content-center shadow-sm"
style={{ minWidth: "40px", height: "40px", zIndex: 2 }}
onClick={() => scroll("right")}
>
<FontAwesomeIcon icon={faChevronRight} />
</Button>
</div>
);
};
export default ImageCarousel;