chatbot-template/components/ui/carousel.tsx

263 lines
6.2 KiB
TypeScript
Raw Normal View History

2025-09-09 15:44:07 -04:00
'use client';
2025-08-28 14:15:36 +01:00
2025-09-09 15:44:07 -04:00
import * as React from 'react';
2025-08-28 14:15:36 +01:00
import useEmblaCarousel, {
type UseEmblaCarouselType,
2025-09-09 15:44:07 -04:00
} from 'embla-carousel-react';
import { ArrowLeft, ArrowRight } from 'lucide-react';
2025-08-28 14:15:36 +01:00
2025-09-09 15:44:07 -04:00
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';
2025-08-28 14:15:36 +01:00
2025-09-09 15:44:07 -04:00
type CarouselApi = UseEmblaCarouselType[1];
type UseCarouselParameters = Parameters<typeof useEmblaCarousel>;
type CarouselOptions = UseCarouselParameters[0];
type CarouselPlugin = UseCarouselParameters[1];
2025-08-28 14:15:36 +01:00
type CarouselProps = {
2025-09-09 15:44:07 -04:00
opts?: CarouselOptions;
plugins?: CarouselPlugin;
orientation?: 'horizontal' | 'vertical';
setApi?: (api: CarouselApi) => void;
};
2025-08-28 14:15:36 +01:00
type CarouselContextProps = {
2025-09-09 15:44:07 -04:00
carouselRef: ReturnType<typeof useEmblaCarousel>[0];
api: ReturnType<typeof useEmblaCarousel>[1];
scrollPrev: () => void;
scrollNext: () => void;
canScrollPrev: boolean;
canScrollNext: boolean;
} & CarouselProps;
2025-08-28 14:15:36 +01:00
2025-09-09 15:44:07 -04:00
const CarouselContext = React.createContext<CarouselContextProps | null>(null);
2025-08-28 14:15:36 +01:00
function useCarousel() {
2025-09-09 15:44:07 -04:00
const context = React.useContext(CarouselContext);
2025-08-28 14:15:36 +01:00
if (!context) {
2025-09-09 15:44:07 -04:00
throw new Error('useCarousel must be used within a <Carousel />');
2025-08-28 14:15:36 +01:00
}
2025-09-09 15:44:07 -04:00
return context;
2025-08-28 14:15:36 +01:00
}
const Carousel = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement> & CarouselProps
>(
(
{
2025-09-09 15:44:07 -04:00
orientation = 'horizontal',
2025-08-28 14:15:36 +01:00
opts,
setApi,
plugins,
className,
children,
...props
},
2025-09-09 15:44:07 -04:00
ref,
2025-08-28 14:15:36 +01:00
) => {
const [carouselRef, api] = useEmblaCarousel(
{
...opts,
2025-09-09 15:44:07 -04:00
axis: orientation === 'horizontal' ? 'x' : 'y',
2025-08-28 14:15:36 +01:00
},
2025-09-09 15:44:07 -04:00
plugins,
);
const [canScrollPrev, setCanScrollPrev] = React.useState(false);
const [canScrollNext, setCanScrollNext] = React.useState(false);
2025-08-28 14:15:36 +01:00
const onSelect = React.useCallback((api: CarouselApi) => {
if (!api) {
2025-09-09 15:44:07 -04:00
return;
2025-08-28 14:15:36 +01:00
}
2025-09-09 15:44:07 -04:00
setCanScrollPrev(api.canScrollPrev());
setCanScrollNext(api.canScrollNext());
}, []);
2025-08-28 14:15:36 +01:00
const scrollPrev = React.useCallback(() => {
2025-09-09 15:44:07 -04:00
api?.scrollPrev();
}, [api]);
2025-08-28 14:15:36 +01:00
const scrollNext = React.useCallback(() => {
2025-09-09 15:44:07 -04:00
api?.scrollNext();
}, [api]);
2025-08-28 14:15:36 +01:00
const handleKeyDown = React.useCallback(
(event: React.KeyboardEvent<HTMLDivElement>) => {
2025-09-09 15:44:07 -04:00
if (event.key === 'ArrowLeft') {
event.preventDefault();
scrollPrev();
} else if (event.key === 'ArrowRight') {
event.preventDefault();
scrollNext();
2025-08-28 14:15:36 +01:00
}
},
2025-09-09 15:44:07 -04:00
[scrollPrev, scrollNext],
);
2025-08-28 14:15:36 +01:00
React.useEffect(() => {
if (!api || !setApi) {
2025-09-09 15:44:07 -04:00
return;
2025-08-28 14:15:36 +01:00
}
2025-09-09 15:44:07 -04:00
setApi(api);
}, [api, setApi]);
2025-08-28 14:15:36 +01:00
React.useEffect(() => {
if (!api) {
2025-09-09 15:44:07 -04:00
return;
2025-08-28 14:15:36 +01:00
}
2025-09-09 15:44:07 -04:00
onSelect(api);
api.on('reInit', onSelect);
api.on('select', onSelect);
2025-08-28 14:15:36 +01:00
return () => {
2025-09-09 15:44:07 -04:00
api?.off('select', onSelect);
};
}, [api, onSelect]);
2025-08-28 14:15:36 +01:00
return (
<CarouselContext.Provider
value={{
carouselRef,
api: api,
opts,
orientation:
2025-09-09 15:44:07 -04:00
orientation || (opts?.axis === 'y' ? 'vertical' : 'horizontal'),
2025-08-28 14:15:36 +01:00
scrollPrev,
scrollNext,
canScrollPrev,
canScrollNext,
}}
>
<div
ref={ref}
onKeyDownCapture={handleKeyDown}
2025-09-09 15:44:07 -04:00
className={cn('relative', className)}
2025-08-28 14:15:36 +01:00
role="region"
aria-roledescription="carousel"
{...props}
>
{children}
</div>
</CarouselContext.Provider>
2025-09-09 15:44:07 -04:00
);
},
);
Carousel.displayName = 'Carousel';
2025-08-28 14:15:36 +01:00
const CarouselContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => {
2025-09-09 15:44:07 -04:00
const { carouselRef, orientation } = useCarousel();
2025-08-28 14:15:36 +01:00
return (
<div ref={carouselRef} className="overflow-hidden">
<div
ref={ref}
className={cn(
2025-09-09 15:44:07 -04:00
'flex',
orientation === 'horizontal' ? '-ml-4' : '-mt-4 flex-col',
className,
2025-08-28 14:15:36 +01:00
)}
{...props}
/>
</div>
2025-09-09 15:44:07 -04:00
);
});
CarouselContent.displayName = 'CarouselContent';
2025-08-28 14:15:36 +01:00
const CarouselItem = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => {
2025-09-09 15:44:07 -04:00
const { orientation } = useCarousel();
2025-08-28 14:15:36 +01:00
return (
<div
ref={ref}
role="group"
aria-roledescription="slide"
className={cn(
2025-09-09 15:44:07 -04:00
'min-w-0 shrink-0 grow-0 basis-full',
orientation === 'horizontal' ? 'pl-4' : 'pt-4',
className,
2025-08-28 14:15:36 +01:00
)}
{...props}
/>
2025-09-09 15:44:07 -04:00
);
});
CarouselItem.displayName = 'CarouselItem';
2025-08-28 14:15:36 +01:00
const CarouselPrevious = React.forwardRef<
HTMLButtonElement,
React.ComponentProps<typeof Button>
2025-09-09 15:44:07 -04:00
>(({ className, variant = 'outline', size = 'icon', ...props }, ref) => {
const { orientation, scrollPrev, canScrollPrev } = useCarousel();
2025-08-28 14:15:36 +01:00
return (
<Button
ref={ref}
variant={variant}
size={size}
className={cn(
2025-09-09 15:44:07 -04:00
'absolute h-8 w-8 rounded-full',
orientation === 'horizontal'
? '-left-12 -translate-y-1/2 top-1/2'
: '-top-12 -translate-x-1/2 left-1/2 rotate-90',
className,
2025-08-28 14:15:36 +01:00
)}
disabled={!canScrollPrev}
onClick={scrollPrev}
{...props}
>
<ArrowLeft className="h-4 w-4" />
<span className="sr-only">Previous slide</span>
</Button>
2025-09-09 15:44:07 -04:00
);
});
CarouselPrevious.displayName = 'CarouselPrevious';
2025-08-28 14:15:36 +01:00
const CarouselNext = React.forwardRef<
HTMLButtonElement,
React.ComponentProps<typeof Button>
2025-09-09 15:44:07 -04:00
>(({ className, variant = 'outline', size = 'icon', ...props }, ref) => {
const { orientation, scrollNext, canScrollNext } = useCarousel();
2025-08-28 14:15:36 +01:00
return (
<Button
ref={ref}
variant={variant}
size={size}
className={cn(
2025-09-09 15:44:07 -04:00
'absolute h-8 w-8 rounded-full',
orientation === 'horizontal'
? '-right-12 -translate-y-1/2 top-1/2'
: '-bottom-12 -translate-x-1/2 left-1/2 rotate-90',
className,
2025-08-28 14:15:36 +01:00
)}
disabled={!canScrollNext}
onClick={scrollNext}
{...props}
>
<ArrowRight className="h-4 w-4" />
<span className="sr-only">Next slide</span>
</Button>
2025-09-09 15:44:07 -04:00
);
});
CarouselNext.displayName = 'CarouselNext';
2025-08-28 14:15:36 +01:00
export {
type CarouselApi,
Carousel,
CarouselContent,
CarouselItem,
CarouselPrevious,
CarouselNext,
2025-09-09 15:44:07 -04:00
};