"use client"; import "photoswipe/style.css"; import PhotoSwipeLightbox from "photoswipe/lightbox"; import { useCallback, useEffect, useRef, useState } from "react"; import { AspectRatio } from "@/components/ui/aspect-ratio"; import { badgeVariants } from "@/components/ui/badge"; import { buttonVariants } from "@/components/ui/button"; import type { CarouselApi } from "@/components/ui/carousel"; import { Carousel, CarouselContent, CarouselItem, } from "@/components/ui/carousel"; import { cn } from "@/lib/utils"; type ProductImages = { srcset: string; src: string; alt: string; width: number; height: number; sizes: string; }[]; interface ProductGallery3Props { images: ProductImages; galleryID: string; className?: string; } const PRODUCT_IMAGES: ProductImages = [ { srcset: "/block-media/shadcnblocks/block/ecommerce/clothes/pexels-cottonbro-6764033-3.jpg 1086w, /block-media/shadcnblocks/block/ecommerce/clothes/pexels-cottonbro-6764033-2.jpg 1086w", src: "/block-media/shadcnblocks/block/ecommerce/clothes/pexels-cottonbro-6764033-3.jpg", alt: "", width: 1086, height: 1448, sizes: "(min-width: 1086px) 1086px, 100vw", }, { srcset: "/block-media/shadcnblocks/block/ecommerce/clothes/Man-in-Hoodie-&-Jacket-2.png 904w, /block-media/shadcnblocks/block/ecommerce/clothes/Man-in-Hoodie-&-Jacket-1.png 482w", src: "/block-media/shadcnblocks/block/ecommerce/clothes/Man-in-Hoodie-&-Jacket-2.png", alt: "", width: 904, height: 1200, sizes: "(min-width: 904px) 904px, 100vw", }, { srcset: "/block-media/shadcnblocks/block/ecommerce/clothes/Young-Man-in-Blue-Hoodie-2.png 675w, /block-media/shadcnblocks/block/ecommerce/clothes/Young-Man-in-Blue-Hoodie-1.png 360w", src: "/block-media/shadcnblocks/block/ecommerce/clothes/Young-Man-in-Blue-Hoodie-2.png", alt: "", width: 675, height: 1200, sizes: "(min-width: 675px) 675px, 100vw", }, ]; const ProductGallery3 = ({ images = PRODUCT_IMAGES, galleryID = "ProductGallery3", className, }: ProductGallery3Props) => { const [api, setApi] = useState(); const [current, setCurrent] = useState(1); const lightboxRef = useRef(null); useEffect(() => { const lightbox = new PhotoSwipeLightbox({ gallery: "#" + galleryID, children: "a", bgOpacity: 1, wheelToZoom: true, arrowPrev: false, arrowNext: false, close: false, zoom: false, counter: false, mainClass: "[&>div:first-child]:!bg-background", pswpModule: () => import("photoswipe"), }); lightbox.init(); lightboxRef.current = lightbox; const buttonClassName = cn( buttonVariants({ variant: "outline", size: "icon", className: "rounded-full", }), ); const indicatorClassName = cn(badgeVariants({ variant: "secondary" })); lightbox.on("uiRegister", () => { if (lightbox?.pswp?.ui) { lightbox.pswp.ui.registerElement({ name: "custom-close-btn", order: 10, isButton: false, appendTo: "root", className: "absolute top-5 right-5", html: ` `, onInit: (el, pswp) => { el.querySelector("#pswp-close")?.addEventListener("click", () => pswp.close(), ); }, }); } if (lightbox?.pswp?.ui) { lightbox.pswp.ui.registerElement({ name: "custom-indicator-btn", order: 10, isButton: false, appendTo: "root", className: "absolute top-5 left-5", html: `
`, onInit: (el, pswp) => { const indicatorElem = el.querySelector("#pswp-indicator"); let prevIndex = -1; pswp.on("change", () => { prevIndex = pswp.currIndex; if (indicatorElem) { indicatorElem.innerHTML = `${prevIndex + 1} / ${pswp.getNumItems()}`; } }); }, }); } if (lightbox?.pswp?.ui) { lightbox.pswp.ui.registerElement({ name: "custom-next-prev-btns", order: 10, isButton: false, appendTo: "root", className: "absolute top-1/2 inset-x-0 -translate-y-1/2", html: `
`, onInit: (el, pswp) => { el.querySelector("#pswp-prev")?.addEventListener("click", () => pswp.prev(), ); el.querySelector("#pswp-next")?.addEventListener("click", () => pswp.next(), ); }, }); } }); return () => lightbox.destroy(); }, [galleryID]); const onSelect = useCallback((api: CarouselApi) => { if (!api) return; setCurrent(api.selectedScrollSnap() + 1); }, []); useEffect(() => { if (!api) return; onSelect(api); api.on("reInit", onSelect); api.on("select", onSelect); return () => { api?.off("select", onSelect); }; }, [api, onSelect]); useEffect(() => { if (lightboxRef.current && api) { lightboxRef.current.on("change", () => { api?.scrollTo(lightboxRef.current?.pswp?.currIndex || 0); }); } }, [api, current]); if (!images) return; return (
{images.map((img, index) => ( {img.alt} ))}
    {images.map((_, index) => ( ))}
); }; export { ProductGallery3 };