210 lines
5.9 KiB
TypeScript
210 lines
5.9 KiB
TypeScript
import { useState, useRef, type ReactNode } from "react";
|
|
|
|
import defaultStarIcon from "&/images/guaranteesSlider/eclat.png";
|
|
import CarouselControls from "@/front/Components/Shared/CarouselControls";
|
|
|
|
interface ItemCarouselProps<T> {
|
|
items: T[];
|
|
renderItem: (item: T, index: number, isActive: boolean) => ReactNode;
|
|
gap?: string;
|
|
initialIndex?: number;
|
|
}
|
|
|
|
export interface StyleCarouselProps {
|
|
starIcon?: string;
|
|
showStar?: boolean;
|
|
starClassName?: string;
|
|
gap?: string;
|
|
controlsColor?: string;
|
|
controlsDotColor?: string;
|
|
type?: "default" | "contained" | "scrollable" | "window";
|
|
containedMaxWidth?: string;
|
|
windowSize?: number;
|
|
slotClassName?: string;
|
|
}
|
|
|
|
type CarouselProps<T> = StyleCarouselProps & ItemCarouselProps<T>;
|
|
|
|
export default function Carousel<T>({
|
|
items,
|
|
renderItem,
|
|
starIcon = defaultStarIcon,
|
|
showStar = true,
|
|
starClassName = "-top-6 -right-6 w-12 lg:w-16",
|
|
gap = "gap-8",
|
|
initialIndex = 1,
|
|
controlsColor,
|
|
controlsDotColor,
|
|
type = "default",
|
|
containedMaxWidth = "max-w-[1000px]",
|
|
windowSize = 3,
|
|
slotClassName = "w-[296px] lg:w-[364px]",
|
|
}: Readonly<CarouselProps<T>>) {
|
|
const scrollRef = useRef<HTMLDivElement>(null);
|
|
const isWindow = type === "window";
|
|
|
|
const [userIndex, setUserIndex] = useState<number | null>(null);
|
|
|
|
const activeIndex = (() => {
|
|
if (items.length === 0) return 0;
|
|
|
|
if (userIndex !== null && userIndex >= 0 && userIndex < items.length) {
|
|
return userIndex;
|
|
}
|
|
|
|
if (initialIndex >= 0 && initialIndex < items.length) {
|
|
return initialIndex;
|
|
}
|
|
|
|
return items.length > 1 ? 1 : 0;
|
|
})();
|
|
|
|
const scrollToIndex = (index: number) => {
|
|
setUserIndex(index);
|
|
|
|
if (isWindow && windowSize >= items.length) return;
|
|
|
|
const container = scrollRef.current;
|
|
const card = container?.children[index] as HTMLElement | undefined;
|
|
if (!container || !card) return;
|
|
|
|
const containerRect = container.getBoundingClientRect();
|
|
const cardRect = card.getBoundingClientRect();
|
|
const scrollOffset =
|
|
cardRect.left -
|
|
containerRect.left -
|
|
(containerRect.width - cardRect.width) / 2;
|
|
|
|
container.scrollBy({
|
|
left: scrollOffset,
|
|
behavior: "smooth",
|
|
});
|
|
};
|
|
|
|
const goPrev = () => {
|
|
scrollToIndex(activeIndex === 0 ? items.length - 1 : activeIndex - 1);
|
|
};
|
|
const goNext = () => {
|
|
scrollToIndex(activeIndex === items.length - 1 ? 0 : activeIndex + 1);
|
|
};
|
|
|
|
const touchStartXRef = useRef<number | null>(null);
|
|
const touchStartYRef = useRef<number | null>(null);
|
|
|
|
const handleTouchStart = (e: React.TouchEvent) => {
|
|
touchStartXRef.current = e.touches[0].clientX;
|
|
touchStartYRef.current = e.touches[0].clientY;
|
|
};
|
|
|
|
const handleTouchEnd = (e: React.TouchEvent) => {
|
|
if (touchStartXRef.current === null || touchStartYRef.current === null)
|
|
return;
|
|
const deltaX = e.changedTouches[0].clientX - touchStartXRef.current;
|
|
const deltaY = e.changedTouches[0].clientY - touchStartYRef.current;
|
|
touchStartXRef.current = null;
|
|
touchStartYRef.current = null;
|
|
|
|
if (Math.abs(deltaX) > 40 && Math.abs(deltaX) > Math.abs(deltaY)) {
|
|
if (deltaX < 0) {
|
|
goNext();
|
|
} else {
|
|
goPrev();
|
|
}
|
|
}
|
|
};
|
|
|
|
const isScrollable = type === "scrollable";
|
|
const isContained = type === "contained";
|
|
|
|
const halfWindow = Math.floor(windowSize / 2);
|
|
const windowIndices = new Set<number>();
|
|
if (isWindow) {
|
|
let start = activeIndex - halfWindow;
|
|
let end = activeIndex + halfWindow;
|
|
|
|
if (start < 0) {
|
|
end += -start;
|
|
start = 0;
|
|
}
|
|
if (end > items.length - 1) {
|
|
start -= end - (items.length - 1);
|
|
end = items.length - 1;
|
|
}
|
|
start = Math.max(start, 0);
|
|
|
|
for (let idx = start; idx <= end; idx += 1) {
|
|
windowIndices.add(idx);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className="flex flex-col items-center gap-14"
|
|
onTouchStart={handleTouchStart}
|
|
onTouchEnd={handleTouchEnd}
|
|
>
|
|
<div
|
|
ref={scrollRef}
|
|
className={`flex items-end ${gap} ${
|
|
isScrollable
|
|
? "w-full overflow-x-auto scroll-smooth [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
|
|
: isContained
|
|
? `w-full ${containedMaxWidth} overflow-hidden justify-center`
|
|
: isWindow
|
|
? "w-fit justify-center"
|
|
: "w-full justify-center"
|
|
}`}
|
|
>
|
|
{items.map((item, index) => {
|
|
const isActive = index === activeIndex;
|
|
const isInWindow = isWindow && windowIndices.has(index);
|
|
|
|
let visibilityClass: string;
|
|
if (isScrollable) {
|
|
visibilityClass = "snap-center";
|
|
} else if (isWindow) {
|
|
visibilityClass = isActive
|
|
? "block"
|
|
: isInWindow
|
|
? "hidden lg:block"
|
|
: "hidden";
|
|
} else {
|
|
visibilityClass = isActive ? "block" : "hidden lg:block";
|
|
}
|
|
|
|
return (
|
|
<div
|
|
// eslint-disable-next-line @eslint-react/no-array-index-key
|
|
key={index}
|
|
className={`relative ${visibilityClass} ${
|
|
isWindow
|
|
? `flex shrink-0 items-end justify-center ${slotClassName}`
|
|
: ""
|
|
}`}
|
|
>
|
|
{showStar && isActive && (
|
|
<img
|
|
src={starIcon}
|
|
alt=""
|
|
className={`pointer-events-none absolute z-10 ${starClassName}`}
|
|
/>
|
|
)}
|
|
|
|
{renderItem(item, index, isActive)}
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
<CarouselControls
|
|
total={items.length}
|
|
activeIndex={activeIndex}
|
|
onPrev={goPrev}
|
|
onNext={goNext}
|
|
onSelect={scrollToIndex}
|
|
{...(controlsColor ? { color: controlsColor } : {})}
|
|
{...(controlsDotColor ? { dotColor: controlsDotColor } : {})}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|