Fix carousel
This commit is contained in:
@@ -103,7 +103,7 @@ export function ProductsCarousel({
|
|||||||
).map((p: any) => (
|
).map((p: any) => (
|
||||||
<CarouselItem
|
<CarouselItem
|
||||||
key={p.id}
|
key={p.id}
|
||||||
className={`pl-6 basis-3/4 sm:basis-1/2 ${basisClass[slidesPerView]}`}
|
className={`pl-6 basis-full sm:basis-1/2 ${basisClass[slidesPerView]}`}
|
||||||
>
|
>
|
||||||
{products.length === 0 ? (
|
{products.length === 0 ? (
|
||||||
<div className="aspect-[4/5] w-full animate-pulse rounded-md bg-muted" />
|
<div className="aspect-[4/5] w-full animate-pulse rounded-md bg-muted" />
|
||||||
@@ -113,8 +113,8 @@ export function ProductsCarousel({
|
|||||||
</CarouselItem>
|
</CarouselItem>
|
||||||
))}
|
))}
|
||||||
</CarouselContent>
|
</CarouselContent>
|
||||||
<CarouselPrevious className="hidden md:inline-flex" />
|
<CarouselPrevious className="left-2 md:-left-12" />
|
||||||
<CarouselNext className="hidden md:inline-flex" />
|
<CarouselNext className="right-2 md:-right-12" />
|
||||||
</Carousel>
|
</Carousel>
|
||||||
</Container>
|
</Container>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@@ -1,102 +1,126 @@
|
|||||||
import React, {
|
import * as React from 'react';
|
||||||
createContext,
|
import useEmblaCarousel, {
|
||||||
useContext,
|
type UseEmblaCarouselType,
|
||||||
useState,
|
} from 'embla-carousel-react';
|
||||||
useCallback,
|
import { ArrowLeft, ArrowRight } from 'lucide-react';
|
||||||
useRef,
|
|
||||||
useEffect,
|
|
||||||
} from 'react';
|
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
import { Button } from './button';
|
import { Button } from './button';
|
||||||
|
|
||||||
interface CarouselContextType {
|
type CarouselApi = UseEmblaCarouselType[1];
|
||||||
currentIndex: number;
|
type UseCarouselParameters = Parameters<typeof useEmblaCarousel>;
|
||||||
totalItems: number;
|
type CarouselOptions = UseCarouselParameters[0];
|
||||||
|
type CarouselPlugin = UseCarouselParameters[1];
|
||||||
|
|
||||||
|
type CarouselProps = {
|
||||||
|
opts?: CarouselOptions;
|
||||||
|
plugins?: CarouselPlugin;
|
||||||
|
orientation?: 'horizontal' | 'vertical';
|
||||||
|
setApi?: (api: CarouselApi) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
type CarouselContextProps = {
|
||||||
|
carouselRef: ReturnType<typeof useEmblaCarousel>[0];
|
||||||
|
api: ReturnType<typeof useEmblaCarousel>[1];
|
||||||
scrollPrev: () => void;
|
scrollPrev: () => void;
|
||||||
scrollNext: () => void;
|
scrollNext: () => void;
|
||||||
canScrollPrev: boolean;
|
canScrollPrev: boolean;
|
||||||
canScrollNext: boolean;
|
canScrollNext: boolean;
|
||||||
orientation: 'horizontal' | 'vertical';
|
} & CarouselProps;
|
||||||
}
|
|
||||||
|
|
||||||
const CarouselContext = createContext<CarouselContextType | undefined>(
|
const CarouselContext = React.createContext<CarouselContextProps | null>(null);
|
||||||
undefined
|
|
||||||
);
|
|
||||||
|
|
||||||
function useCarousel() {
|
function useCarousel() {
|
||||||
const context = useContext(CarouselContext);
|
const context = React.useContext(CarouselContext);
|
||||||
if (!context) {
|
if (!context) {
|
||||||
throw new Error('Carousel components must be used within a Carousel');
|
throw new Error('useCarousel must be used within a <Carousel />');
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CarouselProps {
|
|
||||||
children: React.ReactNode;
|
|
||||||
orientation?: 'horizontal' | 'vertical';
|
|
||||||
className?: string;
|
|
||||||
autoPlay?: boolean;
|
|
||||||
autoPlayInterval?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Carousel({
|
function Carousel({
|
||||||
children,
|
|
||||||
orientation = 'horizontal',
|
orientation = 'horizontal',
|
||||||
|
opts,
|
||||||
|
setApi,
|
||||||
|
plugins,
|
||||||
className,
|
className,
|
||||||
autoPlay = false,
|
children,
|
||||||
autoPlayInterval = 3000,
|
...props
|
||||||
}: CarouselProps) {
|
}: React.ComponentProps<'div'> & CarouselProps) {
|
||||||
const [currentIndex, setCurrentIndex] = useState(0);
|
const [carouselRef, api] = useEmblaCarousel(
|
||||||
const itemCount = React.Children.count(children);
|
{
|
||||||
const autoPlayTimerRef = useRef<NodeJS.Timeout | null>(null);
|
...opts,
|
||||||
|
axis: orientation === 'horizontal' ? 'x' : 'y',
|
||||||
|
},
|
||||||
|
plugins
|
||||||
|
);
|
||||||
|
const [canScrollPrev, setCanScrollPrev] = React.useState(false);
|
||||||
|
const [canScrollNext, setCanScrollNext] = React.useState(false);
|
||||||
|
|
||||||
const canScrollPrev = currentIndex > 0;
|
const onSelect = React.useCallback((api: CarouselApi) => {
|
||||||
const canScrollNext = currentIndex < itemCount - 1;
|
if (!api) return;
|
||||||
|
setCanScrollPrev(api.canScrollPrev());
|
||||||
const scrollPrev = useCallback(() => {
|
setCanScrollNext(api.canScrollNext());
|
||||||
setCurrentIndex((prev) => Math.max(0, prev - 1));
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const scrollNext = useCallback(() => {
|
const scrollPrev = React.useCallback(() => {
|
||||||
setCurrentIndex((prev) => Math.min(itemCount - 1, prev + 1));
|
api?.scrollPrev();
|
||||||
}, [itemCount]);
|
}, [api]);
|
||||||
|
|
||||||
useEffect(() => {
|
const scrollNext = React.useCallback(() => {
|
||||||
if (!autoPlay) return;
|
api?.scrollNext();
|
||||||
|
}, [api]);
|
||||||
|
|
||||||
autoPlayTimerRef.current = setInterval(() => {
|
const handleKeyDown = React.useCallback(
|
||||||
setCurrentIndex((prev) => {
|
(event: React.KeyboardEvent<HTMLDivElement>) => {
|
||||||
if (prev >= itemCount - 1) {
|
if (event.key === 'ArrowLeft') {
|
||||||
return 0;
|
event.preventDefault();
|
||||||
}
|
scrollPrev();
|
||||||
return prev + 1;
|
} else if (event.key === 'ArrowRight') {
|
||||||
});
|
event.preventDefault();
|
||||||
}, autoPlayInterval);
|
scrollNext();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[scrollPrev, scrollNext]
|
||||||
|
);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!api || !setApi) return;
|
||||||
|
setApi(api);
|
||||||
|
}, [api, setApi]);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!api) return;
|
||||||
|
onSelect(api);
|
||||||
|
api.on('reInit', onSelect);
|
||||||
|
api.on('select', onSelect);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (autoPlayTimerRef.current) {
|
api?.off('select', onSelect);
|
||||||
clearInterval(autoPlayTimerRef.current);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}, [autoPlay, autoPlayInterval, itemCount]);
|
}, [api, onSelect]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CarouselContext.Provider
|
<CarouselContext.Provider
|
||||||
value={{
|
value={{
|
||||||
currentIndex,
|
carouselRef,
|
||||||
totalItems: itemCount,
|
api,
|
||||||
|
opts,
|
||||||
|
orientation:
|
||||||
|
orientation || (opts?.axis === 'y' ? 'vertical' : 'horizontal'),
|
||||||
scrollPrev,
|
scrollPrev,
|
||||||
scrollNext,
|
scrollNext,
|
||||||
canScrollPrev,
|
canScrollPrev,
|
||||||
canScrollNext,
|
canScrollNext,
|
||||||
orientation,
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
|
onKeyDownCapture={handleKeyDown}
|
||||||
className={cn('relative', className)}
|
className={cn('relative', className)}
|
||||||
role="region"
|
role="region"
|
||||||
aria-roledescription="carousel"
|
aria-roledescription="carousel"
|
||||||
data-slot="carousel"
|
data-slot="carousel"
|
||||||
|
{...props}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
@@ -104,43 +128,28 @@ function Carousel({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CarouselContentProps {
|
function CarouselContent({ className, ...props }: React.ComponentProps<'div'>) {
|
||||||
className?: string;
|
const { carouselRef, orientation } = useCarousel();
|
||||||
children: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
function CarouselContent({ className, children }: CarouselContentProps) {
|
|
||||||
const { currentIndex, orientation } = useCarousel();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cn('overflow-hidden', className)}
|
ref={carouselRef}
|
||||||
|
className="overflow-hidden"
|
||||||
data-slot="carousel-content"
|
data-slot="carousel-content"
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
'flex transition-transform duration-300 ease-out',
|
'flex',
|
||||||
orientation === 'horizontal' ? 'flex-row' : 'flex-col'
|
orientation === 'horizontal' ? '-ml-4' : '-mt-4 flex-col',
|
||||||
|
className
|
||||||
)}
|
)}
|
||||||
style={{
|
{...props}
|
||||||
transform:
|
/>
|
||||||
orientation === 'horizontal'
|
|
||||||
? `translateX(-${currentIndex * 100}%)`
|
|
||||||
: `translateY(-${currentIndex * 100}%)`,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CarouselItemProps {
|
function CarouselItem({ className, ...props }: React.ComponentProps<'div'>) {
|
||||||
className?: string;
|
|
||||||
children: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
function CarouselItem({ className, children }: CarouselItemProps) {
|
|
||||||
const { orientation } = useCarousel();
|
const { orientation } = useCarousel();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -148,94 +157,74 @@ function CarouselItem({ className, children }: CarouselItemProps) {
|
|||||||
role="group"
|
role="group"
|
||||||
aria-roledescription="slide"
|
aria-roledescription="slide"
|
||||||
data-slot="carousel-item"
|
data-slot="carousel-item"
|
||||||
className={cn('min-w-0 shrink-0 grow-0 basis-full', className)}
|
className={cn(
|
||||||
>
|
'min-w-0 shrink-0 grow-0 basis-full',
|
||||||
{children}
|
orientation === 'horizontal' ? 'pl-4' : 'pt-4',
|
||||||
</div>
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CarouselPreviousProps {
|
function CarouselPrevious({
|
||||||
className?: string;
|
className,
|
||||||
}
|
variant = 'outline',
|
||||||
|
...props
|
||||||
function CarouselPrevious({ className }: CarouselPreviousProps) {
|
}: React.ComponentProps<typeof Button>) {
|
||||||
const { scrollPrev, canScrollPrev, orientation } = useCarousel();
|
const { orientation, scrollPrev, canScrollPrev } = useCarousel();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
data-slot="carousel-previous"
|
data-slot="carousel-previous"
|
||||||
variant="outline"
|
variant={variant}
|
||||||
onClick={scrollPrev}
|
|
||||||
disabled={!canScrollPrev}
|
|
||||||
className={cn(
|
className={cn(
|
||||||
'absolute size-10 rounded-full p-0 flex items-center justify-center',
|
'absolute size-10 rounded-full p-0 inline-flex items-center justify-center',
|
||||||
orientation === 'horizontal'
|
orientation === 'horizontal'
|
||||||
? 'top-1/2 left-2 -translate-y-1/2'
|
? 'top-1/2 -left-12 -translate-y-1/2'
|
||||||
: 'top-2 left-1/2 -translate-x-1/2 -rotate-90',
|
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
|
||||||
className
|
className
|
||||||
)}
|
)}
|
||||||
|
disabled={!canScrollPrev}
|
||||||
|
onClick={scrollPrev}
|
||||||
aria-label="Previous slide"
|
aria-label="Previous slide"
|
||||||
|
{...props}
|
||||||
>
|
>
|
||||||
<svg
|
<ArrowLeft className="size-4" />
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
|
||||||
width="24"
|
|
||||||
height="24"
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
fill="none"
|
|
||||||
stroke="currentColor"
|
|
||||||
strokeWidth="2"
|
|
||||||
strokeLinecap="round"
|
|
||||||
strokeLinejoin="round"
|
|
||||||
className="size-4"
|
|
||||||
>
|
|
||||||
<path d="M15 18l-6-6 6-6" />
|
|
||||||
</svg>
|
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CarouselNextProps {
|
function CarouselNext({
|
||||||
className?: string;
|
className,
|
||||||
}
|
variant = 'outline',
|
||||||
|
...props
|
||||||
function CarouselNext({ className }: CarouselNextProps) {
|
}: React.ComponentProps<typeof Button>) {
|
||||||
const { scrollNext, canScrollNext, orientation } = useCarousel();
|
const { orientation, scrollNext, canScrollNext } = useCarousel();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
data-slot="carousel-next"
|
data-slot="carousel-next"
|
||||||
variant="outline"
|
variant={variant}
|
||||||
onClick={scrollNext}
|
|
||||||
disabled={!canScrollNext}
|
|
||||||
className={cn(
|
className={cn(
|
||||||
'absolute size-10 rounded-full p-0 flex items-center justify-center',
|
'absolute size-10 rounded-full p-0 inline-flex items-center justify-center',
|
||||||
orientation === 'horizontal'
|
orientation === 'horizontal'
|
||||||
? 'top-1/2 right-2 -translate-y-1/2'
|
? 'top-1/2 -right-12 -translate-y-1/2'
|
||||||
: 'bottom-2 left-1/2 -translate-x-1/2 rotate-90',
|
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
|
||||||
className
|
className
|
||||||
)}
|
)}
|
||||||
|
disabled={!canScrollNext}
|
||||||
|
onClick={scrollNext}
|
||||||
aria-label="Next slide"
|
aria-label="Next slide"
|
||||||
|
{...props}
|
||||||
>
|
>
|
||||||
<svg
|
<ArrowRight className="size-4" />
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
|
||||||
width="24"
|
|
||||||
height="24"
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
fill="none"
|
|
||||||
stroke="currentColor"
|
|
||||||
strokeWidth="2"
|
|
||||||
strokeLinecap="round"
|
|
||||||
strokeLinejoin="round"
|
|
||||||
className="size-4"
|
|
||||||
>
|
|
||||||
<path d="M9 18l6-6-6-6" />
|
|
||||||
</svg>
|
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
type CarouselApi,
|
||||||
Carousel,
|
Carousel,
|
||||||
CarouselContent,
|
CarouselContent,
|
||||||
CarouselItem,
|
CarouselItem,
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user