| 1 | import * as React from "react"; |
| 2 | import useEmblaCarousel, { type UseEmblaCarouselType } from "embla-carousel-react"; |
| 3 | import { ArrowLeft, ArrowRight } from "lucide-react"; |
| 4 | |
| 5 | import { cn } from "@/lib/utils"; |
| 6 | import { Button } from "@/components/ui/button"; |
| 7 | |
| 8 | type CarouselApi = UseEmblaCarouselType[1]; |
| 9 | type UseCarouselParameters = Parameters<typeof useEmblaCarousel>; |
| 10 | type CarouselOptions = UseCarouselParameters[0]; |
| 11 | type CarouselPlugin = UseCarouselParameters[1]; |
| 12 | |
| 13 | type CarouselProps = { |
| 14 | opts?: CarouselOptions; |
| 15 | plugins?: CarouselPlugin; |
| 16 | orientation?: "horizontal" | "vertical"; |
| 17 | setApi?: (api: CarouselApi) => void; |
| 18 | }; |
| 19 | |
| 20 | type CarouselContextProps = { |
| 21 | carouselRef: ReturnType<typeof useEmblaCarousel>[0]; |
| 22 | api: ReturnType<typeof useEmblaCarousel>[1]; |
| 23 | scrollPrev: () => void; |
| 24 | scrollNext: () => void; |
| 25 | canScrollPrev: boolean; |
| 26 | canScrollNext: boolean; |
| 27 | } & CarouselProps; |
| 28 | |
| 29 | const CarouselContext = React.createContext<CarouselContextProps | null>(null); |
| 30 | |
| 31 | function useCarousel() { |
| 32 | const context = React.useContext(CarouselContext); |
| 33 | |
| 34 | if (!context) { |
| 35 | throw new Error("useCarousel must be used within a <Carousel />"); |
| 36 | } |
| 37 | |
| 38 | return context; |
| 39 | } |
| 40 | |
| 41 | const Carousel = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement> & CarouselProps>( |
| 42 | ({ orientation = "horizontal", opts, setApi, plugins, className, children, ...props }, ref) => { |
| 43 | const [carouselRef, api] = useEmblaCarousel( |
| 44 | { |
| 45 | ...opts, |
| 46 | axis: orientation === "horizontal" ? "x" : "y", |
| 47 | }, |
| 48 | plugins, |
| 49 | ); |
| 50 | const [canScrollPrev, setCanScrollPrev] = React.useState(false); |
| 51 | const [canScrollNext, setCanScrollNext] = React.useState(false); |
| 52 | |
| 53 | const onSelect = React.useCallback((api: CarouselApi) => { |
| 54 | if (!api) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | setCanScrollPrev(api.canScrollPrev()); |
| 59 | setCanScrollNext(api.canScrollNext()); |
| 60 | }, []); |
| 61 | |
| 62 | const scrollPrev = React.useCallback(() => { |
| 63 | api?.scrollPrev(); |
| 64 | }, [api]); |
| 65 | |
| 66 | const scrollNext = React.useCallback(() => { |
| 67 | api?.scrollNext(); |
| 68 | }, [api]); |
| 69 | |
| 70 | const handleKeyDown = React.useCallback( |
| 71 | (event: React.KeyboardEvent<HTMLDivElement>) => { |
| 72 | if (event.key === "ArrowLeft") { |
| 73 | event.preventDefault(); |
| 74 | scrollPrev(); |
| 75 | } else if (event.key === "ArrowRight") { |
| 76 | event.preventDefault(); |
| 77 | scrollNext(); |
| 78 | } |
| 79 | }, |
| 80 | [scrollPrev, scrollNext], |
| 81 | ); |
| 82 | |
| 83 | React.useEffect(() => { |
| 84 | if (!api || !setApi) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | setApi(api); |
| 89 | }, [api, setApi]); |
| 90 | |
| 91 | React.useEffect(() => { |
| 92 | if (!api) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | onSelect(api); |
| 97 | api.on("reInit", onSelect); |
| 98 | api.on("select", onSelect); |
| 99 | |
| 100 | return () => { |
| 101 | api?.off("select", onSelect); |
| 102 | }; |
| 103 | }, [api, onSelect]); |
| 104 | |
| 105 | return ( |
| 106 | <CarouselContext.Provider |
| 107 | value={{ |
| 108 | carouselRef, |
| 109 | api: api, |
| 110 | opts, |
| 111 | orientation: orientation || (opts?.axis === "y" ? "vertical" : "horizontal"), |
| 112 | scrollPrev, |
| 113 | scrollNext, |
| 114 | canScrollPrev, |
| 115 | canScrollNext, |
| 116 | }} |
| 117 | > |
| 118 | <div |
| 119 | ref={ref} |
| 120 | onKeyDownCapture={handleKeyDown} |
| 121 | className={cn("relative", className)} |
| 122 | role="region" |
| 123 | aria-roledescription="carousel" |
| 124 | {...props} |
| 125 | > |
| 126 | {children} |
| 127 | </div> |
| 128 | </CarouselContext.Provider> |
| 129 | ); |
| 130 | }, |
| 131 | ); |
| 132 | Carousel.displayName = "Carousel"; |
| 133 | |
| 134 | const CarouselContent = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>( |
| 135 | ({ className, ...props }, ref) => { |
| 136 | const { carouselRef, orientation } = useCarousel(); |
| 137 | |
| 138 | return ( |
| 139 | <div ref={carouselRef} className="overflow-hidden"> |
| 140 | <div |
| 141 | ref={ref} |
| 142 | className={cn("flex", orientation === "horizontal" ? "-ml-4" : "-mt-4 flex-col", className)} |
| 143 | {...props} |
| 144 | /> |
| 145 | </div> |
| 146 | ); |
| 147 | }, |
| 148 | ); |
| 149 | CarouselContent.displayName = "CarouselContent"; |
| 150 | |
| 151 | const CarouselItem = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>( |
| 152 | ({ className, ...props }, ref) => { |
| 153 | const { orientation } = useCarousel(); |
| 154 | |
| 155 | return ( |
| 156 | <div |
| 157 | ref={ref} |
| 158 | role="group" |
| 159 | aria-roledescription="slide" |
| 160 | className={cn("min-w-0 shrink-0 grow-0 basis-full", orientation === "horizontal" ? "pl-4" : "pt-4", className)} |
| 161 | {...props} |
| 162 | /> |
| 163 | ); |
| 164 | }, |
| 165 | ); |
| 166 | CarouselItem.displayName = "CarouselItem"; |
| 167 | |
| 168 | const CarouselPrevious = React.forwardRef<HTMLButtonElement, React.ComponentProps<typeof Button>>( |
| 169 | ({ className, variant = "outline", size = "icon", ...props }, ref) => { |
| 170 | const { orientation, scrollPrev, canScrollPrev } = useCarousel(); |
| 171 | |
| 172 | return ( |
| 173 | <Button |
| 174 | ref={ref} |
| 175 | variant={variant} |
| 176 | size={size} |
| 177 | className={cn( |
| 178 | "absolute h-8 w-8 rounded-full", |
| 179 | orientation === "horizontal" |
| 180 | ? "-left-12 top-1/2 -translate-y-1/2" |
| 181 | : "-top-12 left-1/2 -translate-x-1/2 rotate-90", |
| 182 | className, |
| 183 | )} |
| 184 | disabled={!canScrollPrev} |
| 185 | onClick={scrollPrev} |
| 186 | {...props} |
| 187 | > |
| 188 | <ArrowLeft className="h-4 w-4" /> |
| 189 | <span className="sr-only">Previous slide</span> |
| 190 | </Button> |
| 191 | ); |
| 192 | }, |
| 193 | ); |
| 194 | CarouselPrevious.displayName = "CarouselPrevious"; |
| 195 | |
| 196 | const CarouselNext = React.forwardRef<HTMLButtonElement, React.ComponentProps<typeof Button>>( |
| 197 | ({ className, variant = "outline", size = "icon", ...props }, ref) => { |
| 198 | const { orientation, scrollNext, canScrollNext } = useCarousel(); |
| 199 | |
| 200 | return ( |
| 201 | <Button |
| 202 | ref={ref} |
| 203 | variant={variant} |
| 204 | size={size} |
| 205 | className={cn( |
| 206 | "absolute h-8 w-8 rounded-full", |
| 207 | orientation === "horizontal" |
| 208 | ? "-right-12 top-1/2 -translate-y-1/2" |
| 209 | : "-bottom-12 left-1/2 -translate-x-1/2 rotate-90", |
| 210 | className, |
| 211 | )} |
| 212 | disabled={!canScrollNext} |
| 213 | onClick={scrollNext} |
| 214 | {...props} |
| 215 | > |
| 216 | <ArrowRight className="h-4 w-4" /> |
| 217 | <span className="sr-only">Next slide</span> |
| 218 | </Button> |
| 219 | ); |
| 220 | }, |
| 221 | ); |
| 222 | CarouselNext.displayName = "CarouselNext"; |
| 223 | |
| 224 | export { type CarouselApi, Carousel, CarouselContent, CarouselItem, CarouselPrevious, CarouselNext }; |