-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarousel.js
175 lines (151 loc) Β· 4.93 KB
/
carousel.js
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
"use client";
import * as React from "react";
import useEmblaCarousel from "embla-carousel-react";
import { cn } from "@/modules/utils";
import {
ChevronLeft as ChevronLeftIcon,
ChevronRight as ChevronRightIcon,
} from "@/components/common/icons";
const CarouselContext = React.createContext({});
const useCarousel = () => {
const carouselContext = React.useContext(CarouselContext);
if (!carouselContext) {
throw new Error("useCarousel should be used within <Carousel>");
}
const { api } = carouselContext;
return {
...api,
};
};
const Carousel = ({ options = {}, className, ...props }) => {
const [emblaRef, emblaApi] = useEmblaCarousel({
loop: true,
watchDrag: (_, event) => {
if (event.type === "mousedown") {
return false;
}
return true;
},
...options,
});
const [currentIndex, setCurrentIndex] = React.useState(0);
React.useEffect(() => {
const handleSelectChange = () => {
const index = emblaApi.selectedScrollSnap();
setCurrentIndex(index || 0);
};
emblaApi?.on("select", handleSelectChange);
return () => {
emblaApi?.off("select", handleSelectChange);
};
}, [emblaApi]);
const selectedIndex = React.useCallback(() => currentIndex, [currentIndex]);
return (
<CarouselContext.Provider value={{ api: { ...emblaApi, selectedIndex } }}>
<div
ref={emblaRef}
className={cn("relative w-full overflow-hidden", className)}
{...props}
/>
</CarouselContext.Provider>
);
};
Carousel.displayName = "Carousel";
const CarouselViewport = React.forwardRef(({ className, ...props }, ref) => (
<div ref={ref} className={cn("flex", className)} {...props} />
));
CarouselViewport.displayName = "CarouselViewport";
const CarouselItem = React.forwardRef(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
"min-w-0 flex-grow-0 flex-shrink-0 basis-full mx-2 rounded-md border overflow-hidden",
className
)}
{...props}
/>
));
CarouselItem.displayName = "CarouselItem";
const CarouselContent = React.forwardRef(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex flex-col space-y-4 p-6 bg-background", className)}
{...props}
/>
));
CarouselContent.displayName = "CarouselContent";
const CarouselControls = React.forwardRef(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex justify-between items-center p-2", className)}
{...props}
/>
));
CarouselControls.displayName = "CarouselControls";
const CarouselTrigger = React.forwardRef(
({ index, className, ...props }, ref) => {
const { scrollTo, selectedIndex } = useCarousel();
const currentIndex = selectedIndex();
const selected = currentIndex === index;
return (
<button
ref={ref}
data-selected={selected ? "" : undefined}
className={cn(
"h-2 w-2 inline-flex items-center justify-center rounded-full text-sm font-medium bg-primary/90 ring-offset-background transition-colors hover:bg-primary data-[selected]:bg-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
className
)}
onClick={() => scrollTo(index)}
{...props}
/>
);
}
);
CarouselTrigger.displayName = "CarouselTrigger";
const CarouselPrev = React.forwardRef(({ className, ...props }, ref) => {
const { scrollPrev } = useCarousel();
return (
<button
ref={ref}
className={cn(
"h-9 w-9 inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
className
)}
onClick={scrollPrev}
{...props}
>
<ChevronLeftIcon />
<span className="sr-only">Previous testimonial</span>
</button>
);
});
CarouselPrev.displayName = "CarouselPrev";
const CarouselNext = React.forwardRef(({ className, ...props }, ref) => {
const { scrollNext } = useCarousel();
return (
<button
ref={ref}
className={cn(
"h-9 w-9 inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
className
)}
onClick={scrollNext}
{...props}
>
<ChevronRightIcon />
<span className="sr-only">Next testimonial</span>
</button>
);
});
CarouselNext.displayName = "CarouselNext";
export {
useCarousel,
Carousel,
CarouselViewport,
CarouselItem,
CarouselContent,
CarouselControls,
CarouselTrigger,
CarouselPrev,
CarouselNext,
};