ldc-ovoteam/resources/ts/front/Components/Base/Carousel/CarouselContent.tsx

163 lines
4.9 KiB
TypeScript
Raw Permalink Normal View History

2026-03-30 13:52:00 +02:00
/** REACT */
2026-04-08 13:59:30 +02:00
import { useMemo, useRef, useState } from "react";
2026-03-30 13:52:00 +02:00
/** SWIPER PACKAGE */
import { Swiper as SwiperType } from "swiper";
2026-04-08 13:59:30 +02:00
import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/pagination";
import { Pagination } from "swiper/modules";
import { Swiper, SwiperClass, SwiperSlide } from "swiper/react";
import { PaginationOptions } from "swiper/types";
2026-03-30 13:52:00 +02:00
/** THEME */
import EggArrowIcon from "$/uwil/Icons/EggArrowIcon";
2026-03-30 13:52:00 +02:00
import ItemCard from "$/uwil/ItemCard";
import { bgColors, imageShapes } from "@/front/lib/constants";
import { cn } from "@/lib/utils";
2026-04-08 13:59:30 +02:00
import {
CarouselBreakpointsProps,
CarouselPropsWithNavigation,
SwiperButtonProps,
} from "@/types/front";
2026-03-30 13:52:00 +02:00
2026-04-08 13:59:30 +02:00
function NextButton({ swiperRef, disabled, icon }: SwiperButtonProps) {
return (
<button
type="button"
onClick={() => swiperRef.current?.slideNext()}
disabled={disabled}
className="hidden md:block disabled:opacity-0 z-10 hover:scale-105 transition-transform"
2026-04-08 13:59:30 +02:00
>
{icon ?? <EggArrowIcon direction="right" />}
2026-04-08 13:59:30 +02:00
</button>
);
}
function PrevButton({ swiperRef, disabled, icon }: SwiperButtonProps) {
return (
<button
type="button"
onClick={() => swiperRef.current?.slidePrev()}
disabled={disabled}
className="hidden md:block disabled:opacity-0 z-10 hover:scale-105 transition-transform"
2026-04-08 13:59:30 +02:00
>
{icon ?? <EggArrowIcon direction="left" />}
2026-04-08 13:59:30 +02:00
</button>
);
}
const defaultSlidesPerView: Record<number, number> = {
768: 2,
1024: 3,
1248: 4,
};
export default function CarouselContent(props: CarouselPropsWithNavigation) {
const {
items,
carousel,
variant = bgColors.PRIMARY,
prevBtn,
nextBtn,
shape = imageShapes.ROUNDED,
2026-04-08 13:59:30 +02:00
} = props;
2026-03-30 13:52:00 +02:00
const [isBeginning, setIsBeginning] = useState(true);
const [isEnd, setIsEnd] = useState(false);
const swiperRef = useRef<SwiperType | null>(null);
const spaceBetween = carousel?.spaceBetween ?? 50;
const slidesPerView = carousel?.slidesPerView ?? 1;
2026-04-08 13:59:30 +02:00
const defaultPagination = useMemo((): PaginationOptions => {
const bulletClass =
variant === bgColors.PRIMARY
? "inline-block w-3 h-3 rounded-full bg-background/90 ring-1 ring-primary-foreground/20 mx-1"
: "inline-block w-3 h-3 rounded-full bg-primary mx-1";
2026-03-30 13:52:00 +02:00
2026-04-08 13:59:30 +02:00
return {
clickable: true,
bulletClass,
bulletActiveClass: "!bg-primary-foreground w-6 h-6",
2026-04-08 13:59:30 +02:00
};
}, [variant]);
2026-03-30 13:52:00 +02:00
const breakpoints = useMemo(() => {
return Object.keys({
...defaultSlidesPerView,
...carousel?.breakpoints,
}).reduce((acc, key) => {
const numKey = Number(key);
acc[numKey] = {
...carousel?.breakpoints?.[numKey],
slidesPerView:
carousel?.breakpoints?.[numKey]?.slidesPerView ??
defaultSlidesPerView[numKey],
};
return acc;
}, {} as CarouselBreakpointsProps);
}, [carousel?.breakpoints]);
const pagination = useMemo(() => {
if (carousel?.pagination === false) return false;
return {
...defaultPagination,
2026-04-08 13:59:30 +02:00
...(typeof carousel?.pagination === "object" ? carousel.pagination : {}),
2026-03-30 13:52:00 +02:00
};
2026-04-08 13:59:30 +02:00
}, [carousel?.pagination, defaultPagination]);
2026-03-30 13:52:00 +02:00
const handleSwiper = (swiper: SwiperClass) => {
swiperRef.current = swiper;
carousel?.onSwiper?.(swiper);
};
return (
<div className="relative flex justify-center items-center w-full ">
<PrevButton icon={prevBtn} swiperRef={swiperRef} disabled={isBeginning} />
<Swiper
modules={[Pagination]}
centeredSlides={carousel?.centeredSlides}
spaceBetween={spaceBetween}
slidesPerView={slidesPerView}
onSlideChange={(swiper) => {
carousel?.onSlideChange?.(swiper);
setIsBeginning(swiper.isBeginning);
setIsEnd(swiper.isEnd || swiper.isLocked);
}}
onAfterInit={(swiper) => {
setIsBeginning(swiper.isBeginning);
setIsEnd(swiper.isEnd || swiper.isLocked);
}}
watchOverflow={carousel?.watchOverflow}
onSwiper={handleSwiper}
autoplay={carousel?.autoplay}
pagination={pagination}
navigation={carousel?.navigation}
breakpoints={breakpoints}
wrapperClass={cn("pb-20", carousel?.wrapperClass)}
2026-03-30 13:52:00 +02:00
className="w-full"
>
2026-04-08 13:59:30 +02:00
{items.map((item) => {
const slideKey = `${item.image.src}-${item.title?.text ?? item.button?.href ?? "slide"}`;
2026-03-30 13:52:00 +02:00
return (
<SwiperSlide key={slideKey} className="justify-center !flex">
2026-03-30 13:52:00 +02:00
<ItemCard
2026-04-08 13:59:30 +02:00
image={{ lazyload: true, ...item.image }}
2026-08-04 15:57:20 +02:00
shape={shape}
2026-03-30 13:52:00 +02:00
title={item.title}
subtitle={item.subtitle}
description={item.description}
2026-03-30 13:52:00 +02:00
button={item.button}
variant={variant}
/>
</SwiperSlide>
2026-04-08 13:59:30 +02:00
);
2026-03-30 13:52:00 +02:00
})}
</Swiper>
<NextButton icon={nextBtn} swiperRef={swiperRef} disabled={isEnd} />
</div>
);
2026-04-08 13:59:30 +02:00
}