/* eslint-disable react-hooks/exhaustive-deps */ import { useSwipeable } from "react-swipeable"; import { useEffect, useLayoutEffect, useRef, useState } from "react"; import { Image } from "../../types/image"; import { isMobile } from "react-device-detect"; import Button from "../Button"; import LeftArrowSliderIcon from "../icons/LeftArrowSliderIcon"; import RightArrowSliderIcon from "../icons/RightArrowSliderIcon"; const images: Image[] = [ { id: "1", src: "/images/aboutCompany/livingSolutionsSlider/1.png" }, { id: "2", src: "/images/aboutCompany/livingSolutionsSlider/2.png" }, { id: "3", src: "/images/aboutCompany/livingSolutionsSlider/3.png" }, { id: "4", src: "/images/aboutCompany/livingSolutionsSlider/4.png" }, ]; const getGapOffset = (screenWidth: number) => { if (screenWidth > 1600) return 16; if (screenWidth > 1280) return 24; if (screenWidth > 640) return 16; return 16; }; const LivingSolutionSlider = () => { const [selectedImageIndex, setSelectedImageIndex] = useState(-1); const [rightImageOffset, setRightImageOffset] = useState(""); const [imageWidth, setImageWidth] = useState(0); const imageRef = useRef(null); const handlers = useSwipeable({ onSwipedLeft: next, onSwipedRight: prev, trackMouse: true, }); function next() { const lastIndex = isMobile ? images.length - 2 : images.length - 3; if (selectedImageIndex === lastIndex) return; setSelectedImageIndex((prev) => prev + 1); } function prev() { if (selectedImageIndex === -1) return; setSelectedImageIndex((prev) => prev - 1); } useEffect(() => { if (!imageRef.current) return; const width = imageRef.current.width; setImageWidth(width); }, [imageRef.current?.width]); useLayoutEffect(() => { const screenWidth = window.innerWidth; const gapOffset = getGapOffset(screenWidth); const _rightImageOffset = screenWidth > 1280 ? `${"calc(clamp(315px, 7.0317rem + 19.0319vw, 485px)"} + ${ // ? `${"calc(clamp(315px, 6.9317rem + 17.0319vw, 420px)"} + ${ selectedImageIndex * (imageWidth + gapOffset) }px)` : `${(selectedImageIndex + 1) * (imageWidth + gapOffset)}px`; setRightImageOffset(_rightImageOffset); }, [imageWidth, selectedImageIndex, window.innerWidth]); return (
{images.map((image) => ( ))}
{images.map((image, index) => (
))}
); }; export default LivingSolutionSlider;