235 lines
6.4 KiB
TypeScript
235 lines
6.4 KiB
TypeScript
/** REACT */
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
/** SWIPER PACKAGE */
|
|
import { type Swiper as SwiperType } from "swiper";
|
|
import { Pagination } from "swiper/modules";
|
|
import { Swiper, type SwiperClass, SwiperSlide } from "swiper/react";
|
|
import { type PaginationOptions } from "swiper/types";
|
|
|
|
import "swiper/css";
|
|
import "swiper/css/navigation";
|
|
import "swiper/css/pagination";
|
|
/** THEME */
|
|
import defaultStarIcon from "&/images/guaranteesSlider/eclat.png";
|
|
import CustomPagination from "@/front/Components/Shared/Carousel/CustomPagination";
|
|
import {
|
|
type CarouselBreakpointsProps,
|
|
type CarouselPropsWithNavigation,
|
|
} from "@/front/types/carousel";
|
|
import useDeviceType from "@/hooks/use-device-type";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
const defaultSlidesPerView: Record<number, number> = {
|
|
768: 2,
|
|
1024: 3,
|
|
1248: 4,
|
|
};
|
|
|
|
export default function CarouselContent<T>(
|
|
props: Readonly<CarouselPropsWithNavigation<T>>,
|
|
) {
|
|
const {
|
|
items,
|
|
carousel,
|
|
carouselClass,
|
|
renderItem,
|
|
activeIndex: activeIndexState = 0,
|
|
draggable = true,
|
|
showStar = false,
|
|
starIcon = defaultStarIcon,
|
|
controlsColor,
|
|
controlsDotColor,
|
|
} = props;
|
|
const { isMobile, isTablet } = useDeviceType();
|
|
const loop = carousel?.loop ?? false;
|
|
const [activeIndex, setActiveIndex] = useState(activeIndexState);
|
|
const isBeginning = !loop && activeIndex === 0;
|
|
const isEnd = !loop && activeIndex === items.length - 1;
|
|
|
|
const starPosition = isMobile
|
|
? "right-5 -top-10"
|
|
: isTablet
|
|
? "left-[75%] -top-10"
|
|
: "left-[60%] -top-3";
|
|
|
|
const handlePaginationClick = (index: number) => {
|
|
const swiper = swiperRef.current;
|
|
|
|
if (!swiper) return;
|
|
|
|
setActiveIndex(index);
|
|
|
|
if (draggable) {
|
|
const maxTranslateIndex = Math.max(
|
|
items.length - Number(slidesPerView),
|
|
0,
|
|
);
|
|
swiper.slideTo(Math.min(index, maxTranslateIndex));
|
|
} else {
|
|
const currentIndex = swiper.realIndex;
|
|
|
|
if (index > currentIndex) {
|
|
swiper.slideTo(Math.min(index, items.length - Number(slidesPerView)));
|
|
} else {
|
|
swiper.slideTo(index);
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleSlideChange = (swiper: SwiperClass) => {
|
|
setActiveIndex(swiper.realIndex);
|
|
carousel?.onSlideChange?.(swiper);
|
|
};
|
|
|
|
const swiperRef = useRef<SwiperType | null>(null);
|
|
|
|
const spaceBetween = carousel?.spaceBetween ?? 50;
|
|
const slidesPerView = carousel?.slidesPerView ?? 1;
|
|
const slidesPerGroup = carousel?.slidesPerGroup ?? 1;
|
|
const defaultPagination = useMemo((): PaginationOptions => {
|
|
return {
|
|
clickable: true,
|
|
bulletClass: `inline-block w-3 h-3 rounded-full mx-1`,
|
|
bulletActiveClass: "bg-primary-foreground w-5 h-5",
|
|
};
|
|
}, []);
|
|
|
|
const breakpoints = useMemo(() => {
|
|
return Object.keys({
|
|
...defaultSlidesPerView,
|
|
...carousel?.breakpoints,
|
|
}).reduce<CarouselBreakpointsProps>((acc, key) => {
|
|
const numKey = Number(key);
|
|
|
|
acc[numKey] = {
|
|
...carousel?.breakpoints?.[numKey],
|
|
slidesPerView:
|
|
carousel?.breakpoints?.[numKey]?.slidesPerView ??
|
|
defaultSlidesPerView[numKey],
|
|
};
|
|
|
|
return acc;
|
|
}, {});
|
|
}, [carousel?.breakpoints]);
|
|
|
|
const carouselPagination = carousel?.pagination;
|
|
|
|
const pagination = useMemo(() => {
|
|
if (carouselPagination === false) return false;
|
|
|
|
return {
|
|
...defaultPagination,
|
|
...(typeof carouselPagination === "object" ? carouselPagination : {}),
|
|
};
|
|
}, [carouselPagination, defaultPagination]);
|
|
|
|
const handleSwiper = (swiper: SwiperClass) => {
|
|
swiperRef.current = swiper;
|
|
carousel?.onSwiper?.(swiper);
|
|
};
|
|
|
|
const handleNext = () => {
|
|
const swiper = swiperRef.current;
|
|
|
|
if (!swiper) return;
|
|
|
|
const nextIndex = Math.min(activeIndex + 1, items.length - 1);
|
|
|
|
setActiveIndex(nextIndex);
|
|
|
|
// On fait avancer Swiper uniquement s'il le faut
|
|
const maxTranslateIndex = items.length - Number(slidesPerView);
|
|
|
|
if (nextIndex <= maxTranslateIndex) {
|
|
swiper.slideTo(nextIndex);
|
|
}
|
|
};
|
|
|
|
const handlePrev = () => {
|
|
const swiper = swiperRef.current;
|
|
|
|
if (!swiper) return;
|
|
|
|
const prevIndex = Math.max(activeIndex - 1, 0);
|
|
|
|
setActiveIndex(prevIndex);
|
|
|
|
swiper.slideTo(prevIndex);
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (!swiperRef.current) return;
|
|
|
|
swiperRef.current.allowTouchMove = draggable;
|
|
}, [draggable]);
|
|
|
|
return (
|
|
<div className="w-full min-w-0 max-w-full">
|
|
<Swiper
|
|
modules={[Pagination]}
|
|
centeredSlides={carousel?.centeredSlides}
|
|
spaceBetween={spaceBetween}
|
|
slidesPerView={slidesPerView}
|
|
slidesPerGroup={slidesPerGroup}
|
|
allowTouchMove={draggable}
|
|
simulateTouch={true}
|
|
touchRatio={1}
|
|
touchAngle={45}
|
|
grabCursor={true}
|
|
threshold={5}
|
|
touchEventsTarget="wrapper"
|
|
centerInsufficientSlides={carousel?.centerInsufficientSlides}
|
|
onSlideChange={(swiper) => {
|
|
handleSlideChange(swiper);
|
|
}}
|
|
onAfterInit={(swiper) => {
|
|
swiper.slideTo(activeIndexState, 0);
|
|
setActiveIndex(activeIndexState);
|
|
}}
|
|
watchOverflow={carousel?.watchOverflow}
|
|
onSwiper={handleSwiper}
|
|
autoplay={carousel?.autoplay}
|
|
pagination={pagination}
|
|
navigation={carousel?.navigation}
|
|
breakpoints={breakpoints}
|
|
loop={loop}
|
|
wrapperClass={cn(
|
|
"pb-20 !items-stretch relative",
|
|
carousel?.wrapperClass,
|
|
)}
|
|
className={cn("w-full relative", carouselClass)}
|
|
>
|
|
{showStar && (
|
|
<img
|
|
src={starIcon}
|
|
alt=""
|
|
className={`absolute z-10 w-12 lg:w-16 ${starPosition}`}
|
|
/>
|
|
)}
|
|
{items.map((item, index) => {
|
|
return (
|
|
<SwiperSlide
|
|
// eslint-disable-next-line @eslint-react/no-array-index-key
|
|
key={index}
|
|
className="!flex !h-auto items-end justify-center"
|
|
>
|
|
{renderItem(item, index, index === activeIndex)}
|
|
</SwiperSlide>
|
|
);
|
|
})}
|
|
</Swiper>
|
|
|
|
<CustomPagination
|
|
count={items.length}
|
|
activeIndex={activeIndex}
|
|
onClick={handlePaginationClick}
|
|
handleNext={handleNext}
|
|
handlePrev={handlePrev}
|
|
isBeginning={isBeginning}
|
|
isEnd={isEnd}
|
|
{...(controlsColor ? { color: controlsColor } : {})}
|
|
{...(controlsDotColor ? { dotColor: controlsDotColor } : {})}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|