/* eslint-disable react-hooks/exhaustive-deps */ import clsx from "clsx"; import PopupHeader from "./PopupHeader"; import { useEffect, useState } from "react"; import { useRef } from "react"; import usePopupStore from "../store/popupStore"; interface PopupWrapperProps { children: React.ReactNode; className?: string; title?: string; leftButton?: React.ReactNode; draggable?: boolean; } function PopupWrapper({ children, className, title, leftButton, draggable, }: PopupWrapperProps) { const { position, setPosition } = usePopupStore(); const [mouseDown, setMouseDown] = useState(false); const [mouseDownPosition, setMouseDownPosition] = useState(position); const wrapperRef = useRef(null); const headerRef = useRef(null); useEffect(() => { addEventListener("mouseup", () => setMouseDown(false)); addEventListener("touchend", () => setMouseDown(false)); return () => { removeEventListener("mouseup", () => setMouseDown(false)); removeEventListener("touchend", () => setMouseDown(false)); }; }, []); function handleMove(e: MouseEvent | TouchEvent) { if (draggable && mouseDown && wrapperRef.current) { e.preventDefault(); const x = "clientX" in e ? e.clientX : e.touches[0].clientX; const y = "clientY" in e ? e.clientY : e.touches[0].clientY; setPosition({ x: Math.min( Math.max(0, position.x + x - mouseDownPosition.x), innerWidth - wrapperRef.current.clientWidth ), y: Math.min( Math.max(0, position.y + y - mouseDownPosition.y), innerHeight - wrapperRef.current.clientHeight ), }); setMouseDownPosition({ x, y }); } } useEffect(() => { addEventListener("mousemove", handleMove); addEventListener("touchmove", handleMove); return () => { removeEventListener("mousemove", handleMove); removeEventListener("touchmove", handleMove); }; }, [handleMove]); useEffect(() => { if (headerRef.current) { headerRef.current.addEventListener("mousedown", (e) => { setMouseDown(true); setMouseDownPosition({ x: e.clientX, y: e.clientY }); }); headerRef.current.addEventListener("touchstart", (e) => { setMouseDown(true); setMouseDownPosition({ x: e.touches[0].clientX, y: e.touches[0].clientY, }); }); } return () => { if (headerRef.current) { headerRef.current.removeEventListener("mousedown", (e) => { setMouseDown(true); setMouseDownPosition({ x: e.clientX, y: e.clientY }); }); headerRef.current.removeEventListener("touchstart", (e) => { setMouseDown(true); setMouseDownPosition({ x: e.touches[0].clientX, y: e.touches[0].clientY, }); }); } }; }, []); return (
{/* Полоска-ручка для свайпа на мобильных */}
{children}
); } export default PopupWrapper;