|
| 1 | +import * as React from "react" |
| 2 | +import { useTranslation } from "next-i18next" |
| 3 | +import { EffectCards, Keyboard, Navigation, Pagination } from "swiper/modules" |
| 4 | +import { |
| 5 | + Swiper as SwiperReact, |
| 6 | + type SwiperProps as SwiperReactProps, |
| 7 | + SwiperRef, |
| 8 | + SwiperSlide, |
| 9 | +} from "swiper/react" |
| 10 | + |
| 11 | +import { ChevronNext, ChevronPrev } from "@/components/Chevron" |
| 12 | + |
| 13 | +import { cn } from "@/lib/utils/cn" |
| 14 | + |
| 15 | +import "swiper/css" |
| 16 | +import "swiper/css/navigation" |
| 17 | +import "swiper/css/pagination" |
| 18 | +import "swiper/css/effect-cards" |
| 19 | + |
| 20 | +const SwiperContainer = React.forwardRef< |
| 21 | + HTMLDivElement, |
| 22 | + React.HTMLAttributes<HTMLDivElement> |
| 23 | +>(({ className, ...props }, ref) => ( |
| 24 | + <div ref={ref} className={cn("h-fit", className)} {...props} /> |
| 25 | +)) |
| 26 | +SwiperContainer.displayName = "SwiperContainer" |
| 27 | + |
| 28 | +type SwiperProps = SwiperReactProps & { |
| 29 | + children: React.ReactNode[] |
| 30 | +} |
| 31 | +const Swiper = React.forwardRef<SwiperRef, SwiperProps>( |
| 32 | + ({ children, ...props }, ref) => { |
| 33 | + const { t } = useTranslation("common") |
| 34 | + return ( |
| 35 | + <SwiperReact |
| 36 | + ref={ref} |
| 37 | + navigation={{ |
| 38 | + nextEl: ".swiper-button-next", |
| 39 | + prevEl: ".swiper-button-prev", |
| 40 | + }} |
| 41 | + a11y={{ |
| 42 | + prevSlideMessage: t("previous"), |
| 43 | + nextSlideMessage: t("next"), |
| 44 | + }} |
| 45 | + pagination={{ clickable: true, el: ".swiper-pagination" }} |
| 46 | + keyboard |
| 47 | + modules={[Navigation, Pagination, Keyboard, EffectCards]} |
| 48 | + slidesPerView={1} |
| 49 | + slidesPerGroup={1} |
| 50 | + lazyPreloadPrevNext={0} |
| 51 | + slideClass="swiper-slide" |
| 52 | + {...props} |
| 53 | + > |
| 54 | + {children.map((child, index) => ( |
| 55 | + <SwiperSlide key={index}>{child}</SwiperSlide> |
| 56 | + ))} |
| 57 | + |
| 58 | + <ChevronPrev className="swiper-button-prev" /> |
| 59 | + <div className="swiper-pagination" /> |
| 60 | + <ChevronNext className="swiper-button-next" /> |
| 61 | + </SwiperReact> |
| 62 | + ) |
| 63 | + } |
| 64 | +) |
| 65 | +Swiper.displayName = "Swiper" |
| 66 | + |
| 67 | +export { Swiper, SwiperContainer } |
0 commit comments