|
|
|
|
@ -1,11 +1,9 @@
@@ -1,11 +1,9 @@
|
|
|
|
|
import * as React from "react" |
|
|
|
|
import useEmblaCarousel, { |
|
|
|
|
type UseEmblaCarouselType, |
|
|
|
|
} from "embla-carousel-react" |
|
|
|
|
import { ArrowLeft, ArrowRight } from "lucide-react" |
|
|
|
|
import * as React from 'react' |
|
|
|
|
import useEmblaCarousel, { type UseEmblaCarouselType } from 'embla-carousel-react' |
|
|
|
|
import { ArrowLeft, ArrowRight } from 'lucide-react' |
|
|
|
|
|
|
|
|
|
import { cn } from "@/lib/utils" |
|
|
|
|
import { Button } from "@/components/ui/button" |
|
|
|
|
import { cn } from '@/lib/utils' |
|
|
|
|
import { Button } from '@/components/ui/button' |
|
|
|
|
|
|
|
|
|
type CarouselApi = UseEmblaCarouselType[1] |
|
|
|
|
type UseCarouselParameters = Parameters<typeof useEmblaCarousel> |
|
|
|
|
@ -15,7 +13,7 @@ type CarouselPlugin = UseCarouselParameters[1]
@@ -15,7 +13,7 @@ type CarouselPlugin = UseCarouselParameters[1]
|
|
|
|
|
type CarouselProps = { |
|
|
|
|
opts?: CarouselOptions |
|
|
|
|
plugins?: CarouselPlugin |
|
|
|
|
orientation?: "horizontal" | "vertical" |
|
|
|
|
orientation?: 'horizontal' | 'vertical' |
|
|
|
|
setApi?: (api: CarouselApi) => void |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -34,7 +32,7 @@ function useCarousel() {
@@ -34,7 +32,7 @@ function useCarousel() {
|
|
|
|
|
const context = React.useContext(CarouselContext) |
|
|
|
|
|
|
|
|
|
if (!context) { |
|
|
|
|
throw new Error("useCarousel must be used within a <Carousel />") |
|
|
|
|
throw new Error('useCarousel must be used within a <Carousel />') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return context |
|
|
|
|
@ -43,23 +41,11 @@ function useCarousel() {
@@ -43,23 +41,11 @@ function useCarousel() {
|
|
|
|
|
const Carousel = React.forwardRef< |
|
|
|
|
HTMLDivElement, |
|
|
|
|
React.HTMLAttributes<HTMLDivElement> & CarouselProps |
|
|
|
|
>( |
|
|
|
|
( |
|
|
|
|
{ |
|
|
|
|
orientation = "horizontal", |
|
|
|
|
opts, |
|
|
|
|
setApi, |
|
|
|
|
plugins, |
|
|
|
|
className, |
|
|
|
|
children, |
|
|
|
|
...props |
|
|
|
|
}, |
|
|
|
|
ref |
|
|
|
|
) => { |
|
|
|
|
>(({ orientation = 'horizontal', opts, setApi, plugins, className, children, ...props }, ref) => { |
|
|
|
|
const [carouselRef, api] = useEmblaCarousel( |
|
|
|
|
{ |
|
|
|
|
...opts, |
|
|
|
|
axis: orientation === "horizontal" ? "x" : "y", |
|
|
|
|
axis: orientation === 'horizontal' ? 'x' : 'y' |
|
|
|
|
}, |
|
|
|
|
plugins |
|
|
|
|
) |
|
|
|
|
@ -85,10 +71,10 @@ const Carousel = React.forwardRef<
@@ -85,10 +71,10 @@ const Carousel = React.forwardRef<
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = React.useCallback( |
|
|
|
|
(event: React.KeyboardEvent<HTMLDivElement>) => { |
|
|
|
|
if (event.key === "ArrowLeft") { |
|
|
|
|
if (event.key === 'ArrowLeft') { |
|
|
|
|
event.preventDefault() |
|
|
|
|
scrollPrev() |
|
|
|
|
} else if (event.key === "ArrowRight") { |
|
|
|
|
} else if (event.key === 'ArrowRight') { |
|
|
|
|
event.preventDefault() |
|
|
|
|
scrollNext() |
|
|
|
|
} |
|
|
|
|
@ -110,11 +96,11 @@ const Carousel = React.forwardRef<
@@ -110,11 +96,11 @@ const Carousel = React.forwardRef<
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onSelect(api) |
|
|
|
|
api.on("reInit", onSelect) |
|
|
|
|
api.on("select", onSelect) |
|
|
|
|
api.on('reInit', onSelect) |
|
|
|
|
api.on('select', onSelect) |
|
|
|
|
|
|
|
|
|
return () => { |
|
|
|
|
api?.off("select", onSelect) |
|
|
|
|
api?.off('select', onSelect) |
|
|
|
|
} |
|
|
|
|
}, [api, onSelect]) |
|
|
|
|
|
|
|
|
|
@ -124,18 +110,17 @@ const Carousel = React.forwardRef<
@@ -124,18 +110,17 @@ const Carousel = React.forwardRef<
|
|
|
|
|
carouselRef, |
|
|
|
|
api: api, |
|
|
|
|
opts, |
|
|
|
|
orientation: |
|
|
|
|
orientation || (opts?.axis === "y" ? "vertical" : "horizontal"), |
|
|
|
|
orientation: orientation || (opts?.axis === 'y' ? 'vertical' : 'horizontal'), |
|
|
|
|
scrollPrev, |
|
|
|
|
scrollNext, |
|
|
|
|
canScrollPrev, |
|
|
|
|
canScrollNext, |
|
|
|
|
canScrollNext |
|
|
|
|
}} |
|
|
|
|
> |
|
|
|
|
<div |
|
|
|
|
ref={ref} |
|
|
|
|
onKeyDownCapture={handleKeyDown} |
|
|
|
|
className={cn("relative", className)} |
|
|
|
|
className={cn('relative', className)} |
|
|
|
|
role="region" |
|
|
|
|
aria-roledescription="carousel" |
|
|
|
|
{...props} |
|
|
|
|
@ -144,14 +129,11 @@ const Carousel = React.forwardRef<
@@ -144,14 +129,11 @@ const Carousel = React.forwardRef<
|
|
|
|
|
</div> |
|
|
|
|
</CarouselContext.Provider> |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
Carousel.displayName = "Carousel" |
|
|
|
|
}) |
|
|
|
|
Carousel.displayName = 'Carousel' |
|
|
|
|
|
|
|
|
|
const CarouselContent = React.forwardRef< |
|
|
|
|
HTMLDivElement, |
|
|
|
|
React.HTMLAttributes<HTMLDivElement> |
|
|
|
|
>(({ className, ...props }, ref) => { |
|
|
|
|
const CarouselContent = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>( |
|
|
|
|
({ className, ...props }, ref) => { |
|
|
|
|
const { carouselRef, orientation } = useCarousel() |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
@ -159,21 +141,20 @@ const CarouselContent = React.forwardRef<
@@ -159,21 +141,20 @@ const CarouselContent = React.forwardRef<
|
|
|
|
|
<div |
|
|
|
|
ref={ref} |
|
|
|
|
className={cn( |
|
|
|
|
"flex", |
|
|
|
|
orientation === "horizontal" ? "-ml-4" : "-mt-4 flex-col", |
|
|
|
|
'flex', |
|
|
|
|
orientation === 'horizontal' ? '-ml-4' : '-mt-4 flex-col', |
|
|
|
|
className |
|
|
|
|
)} |
|
|
|
|
{...props} |
|
|
|
|
/> |
|
|
|
|
</div> |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
CarouselContent.displayName = "CarouselContent" |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
CarouselContent.displayName = 'CarouselContent' |
|
|
|
|
|
|
|
|
|
const CarouselItem = React.forwardRef< |
|
|
|
|
HTMLDivElement, |
|
|
|
|
React.HTMLAttributes<HTMLDivElement> |
|
|
|
|
>(({ className, ...props }, ref) => { |
|
|
|
|
const CarouselItem = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>( |
|
|
|
|
({ className, ...props }, ref) => { |
|
|
|
|
const { orientation } = useCarousel() |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
@ -182,20 +163,19 @@ const CarouselItem = React.forwardRef<
@@ -182,20 +163,19 @@ const CarouselItem = React.forwardRef<
|
|
|
|
|
role="group" |
|
|
|
|
aria-roledescription="slide" |
|
|
|
|
className={cn( |
|
|
|
|
"min-w-0 shrink-0 grow-0 basis-full", |
|
|
|
|
orientation === "horizontal" ? "pl-4" : "pt-4", |
|
|
|
|
'min-w-0 shrink-0 grow-0 basis-full', |
|
|
|
|
orientation === 'horizontal' ? 'pl-4' : 'pt-4', |
|
|
|
|
className |
|
|
|
|
)} |
|
|
|
|
{...props} |
|
|
|
|
/> |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
CarouselItem.displayName = "CarouselItem" |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
CarouselItem.displayName = 'CarouselItem' |
|
|
|
|
|
|
|
|
|
const CarouselPrevious = React.forwardRef< |
|
|
|
|
HTMLButtonElement, |
|
|
|
|
React.ComponentProps<typeof Button> |
|
|
|
|
>(({ className, variant = "outline", size = "icon", ...props }, ref) => { |
|
|
|
|
const CarouselPrevious = React.forwardRef<HTMLButtonElement, React.ComponentProps<typeof Button>>( |
|
|
|
|
({ className, variant = 'outline', size = 'icon', ...props }, ref) => { |
|
|
|
|
const { orientation, scrollPrev, canScrollPrev } = useCarousel() |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
@ -204,10 +184,10 @@ const CarouselPrevious = React.forwardRef<
@@ -204,10 +184,10 @@ const CarouselPrevious = React.forwardRef<
|
|
|
|
|
variant={variant} |
|
|
|
|
size={size} |
|
|
|
|
className={cn( |
|
|
|
|
"absolute h-8 w-8 rounded-full", |
|
|
|
|
orientation === "horizontal" |
|
|
|
|
? "-left-12 top-1/2 -translate-y-1/2" |
|
|
|
|
: "-top-12 left-1/2 -translate-x-1/2 rotate-90", |
|
|
|
|
'absolute h-8 w-8 rounded-full', |
|
|
|
|
orientation === 'horizontal' |
|
|
|
|
? '-left-12 top-1/2 -translate-y-1/2' |
|
|
|
|
: '-top-12 left-1/2 -translate-x-1/2 rotate-90', |
|
|
|
|
className |
|
|
|
|
)} |
|
|
|
|
disabled={!canScrollPrev} |
|
|
|
|
@ -218,13 +198,12 @@ const CarouselPrevious = React.forwardRef<
@@ -218,13 +198,12 @@ const CarouselPrevious = React.forwardRef<
|
|
|
|
|
<span className="sr-only">Previous slide</span> |
|
|
|
|
</Button> |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
CarouselPrevious.displayName = "CarouselPrevious" |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
CarouselPrevious.displayName = 'CarouselPrevious' |
|
|
|
|
|
|
|
|
|
const CarouselNext = React.forwardRef< |
|
|
|
|
HTMLButtonElement, |
|
|
|
|
React.ComponentProps<typeof Button> |
|
|
|
|
>(({ className, variant = "outline", size = "icon", ...props }, ref) => { |
|
|
|
|
const CarouselNext = React.forwardRef<HTMLButtonElement, React.ComponentProps<typeof Button>>( |
|
|
|
|
({ className, variant = 'outline', size = 'icon', ...props }, ref) => { |
|
|
|
|
const { orientation, scrollNext, canScrollNext } = useCarousel() |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
@ -233,10 +212,10 @@ const CarouselNext = React.forwardRef<
@@ -233,10 +212,10 @@ const CarouselNext = React.forwardRef<
|
|
|
|
|
variant={variant} |
|
|
|
|
size={size} |
|
|
|
|
className={cn( |
|
|
|
|
"absolute h-8 w-8 rounded-full", |
|
|
|
|
orientation === "horizontal" |
|
|
|
|
? "-right-12 top-1/2 -translate-y-1/2" |
|
|
|
|
: "-bottom-12 left-1/2 -translate-x-1/2 rotate-90", |
|
|
|
|
'absolute h-8 w-8 rounded-full', |
|
|
|
|
orientation === 'horizontal' |
|
|
|
|
? '-right-12 top-1/2 -translate-y-1/2' |
|
|
|
|
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90', |
|
|
|
|
className |
|
|
|
|
)} |
|
|
|
|
disabled={!canScrollNext} |
|
|
|
|
@ -247,14 +226,8 @@ const CarouselNext = React.forwardRef<
@@ -247,14 +226,8 @@ const CarouselNext = React.forwardRef<
|
|
|
|
|
<span className="sr-only">Next slide</span> |
|
|
|
|
</Button> |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
CarouselNext.displayName = "CarouselNext" |
|
|
|
|
|
|
|
|
|
export { |
|
|
|
|
type CarouselApi, |
|
|
|
|
Carousel, |
|
|
|
|
CarouselContent, |
|
|
|
|
CarouselItem, |
|
|
|
|
CarouselPrevious, |
|
|
|
|
CarouselNext, |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
CarouselNext.displayName = 'CarouselNext' |
|
|
|
|
|
|
|
|
|
export { type CarouselApi, Carousel, CarouselContent, CarouselItem, CarouselPrevious, CarouselNext } |
|
|
|
|
|