/* eslint-disable jsx-a11y/click-events-have-key-events */ /* eslint-disable jsx-a11y/no-static-element-interactions */ import React, { useState, useRef } from 'react'; import Slider from 'react-slick'; import 'slick-carousel/slick/slick.css'; import { Image } from '@components/common/Image.js'; import { useCatalogImageDimensions } from '@components/common/useCatalogImageDimensions.js'; import { useProduct } from '@components/frontStore/catalog/ProductContext.js'; import './Media.scss'; import { ProductNoThumbnail } from '@components/common/ProductNoThumbnail.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { deriveProductImageSize } from '@evershop/evershop/lib/util/deriveProductImageSize'; // `react-slick` is CommonJS. Under webpack with `"type": "module"` the default // import is the whole `module.exports` (the component sits on `.default`); // bundlers that apply `__esModule` interop hand back the component itself. // Accept both so the gallery renders under either resolution. const SliderComponent = ((Slider as any).default ?? Slider) as any; type SliderType = any; const PrevArrow = (props: any) => { const { className, onClick } = props; return ( ); }; const NextArrow = (props: any) => { const { className, onClick } = props; return ( ); }; interface ImageWithDimensionsProps { url: string; alt?: string; width: number; height: number; } interface MediaProps { imageSize?: { width: number; height: number; }; thumbnailSize?: { width: number; height: number; }; modalSize?: { width: number; height: number; }; } export const Media: React.FC = ({ imageSize: imageSizeProp, thumbnailSize: thumbnailSizeProp, modalSize: modalSizeProp }) => { const catalogDimensions = useCatalogImageDimensions(); const product = useProduct(); const [isModalOpen, setIsModalOpen] = useState(false); const [activeSlide, setActiveSlide] = useState(0); const [isImageLoading, setIsImageLoading] = useState(false); const mainSliderRef = useRef(null); const modalSliderRef = useRef(null); // Base target widths ≈ 2× the CSS display size (retina); heights derive from the store's // configured original aspect ratio, clamped to the original resolution (no upscaling). const imageSize = imageSizeProp ?? deriveProductImageSize(1280, catalogDimensions); const thumbnailSize = thumbnailSizeProp ?? deriveProductImageSize(200, catalogDimensions); const modalSize = modalSizeProp ?? deriveProductImageSize(1920, catalogDimensions); const allImages: ImageWithDimensionsProps[] = []; const fullscreenWidth = modalSize.width * 1.5; const fullscreenHeight = modalSize.height * 1.5; if (product.image) { allImages.push({ url: product.image.url, alt: product.image.alt || product.name, width: imageSize.width, height: imageSize.height }); } if (product.gallery && Array.isArray(product.gallery)) { product.gallery.forEach((img) => { allImages.push({ url: img.url, alt: img.alt || product.name, width: imageSize.width, height: imageSize.height }); }); } const mainSliderSettings = { dots: allImages.length > 1, dotsClass: 'slick-dots slick-thumb', infinite: true, speed: 500, slidesToShow: 1, slidesToScroll: 1, arrows: allImages.length > 1, fade: false, prevArrow: , nextArrow: , beforeChange: (_: number, next: number) => { setActiveSlide(next); }, customPaging: function (i: number) { return (
{`Thumbnail
); } }; const modalSliderSettings = { dots: allImages.length > 1, dotsClass: 'slick-dots slick-thumb', infinite: true, speed: 500, slidesToShow: 1, slidesToScroll: 1, arrows: true, prevArrow: , nextArrow: , initialSlide: activeSlide, adaptiveHeight: true, lazyLoad: 'ondemand', fade: false, swipe: true, beforeChange: () => setIsImageLoading(true), afterChange: () => setIsImageLoading(false), customPaging: function (i: number) { return (
{`Thumbnail
); } }; const openModal = (index: number) => { setActiveSlide(index); setIsModalOpen(true); setTimeout(() => { if (modalSliderRef.current) { modalSliderRef.current.slickGoTo(index); } }, 100); document.addEventListener('keydown', handleKeyDown); document.body.style.overflow = 'hidden'; }; const closeModal = () => { setIsModalOpen(false); document.removeEventListener('keydown', handleKeyDown); document.body.style.overflow = ''; }; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { closeModal(); } else if (e.key === 'ArrowRight' && modalSliderRef.current) { modalSliderRef.current.slickNext(); } else if (e.key === 'ArrowLeft' && modalSliderRef.current) { modalSliderRef.current.slickPrev(); } }; return (
{allImages.length > 0 && ( {allImages.map((image, index) => (
openModal(index)} style={{ width: '100%', aspectRatio: `${imageSize.width} / ${imageSize.height}` }} > {image.alt
))}
)} {allImages.length === 0 && (
)}
{isModalOpen && (
{isImageLoading && (
)} {allImages.map((image, index) => (
{image.alt
))}
)}
); };