import { useEffect, useRef, useState } from 'react'; import './MobileSheet.css'; export default function MobileSheet({ open, onClose, title, snap = 'full', children }) { const [dragY, setDragY] = useState(0); const [isDragging, setIsDragging] = useState(false); const startYRef = useRef(null); const sheetRef = useRef(null); // Lock body scroll when open useEffect(() => { if (open) { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = ''; } return () => { document.body.style.overflow = ''; }; }, [open]); // Reset drag state on close useEffect(() => { if (!open) { setDragY(0); setIsDragging(false); } }, [open]); const handleHandleTouchStart = (e) => { startYRef.current = e.touches[0].clientY; setIsDragging(true); }; const handleHandleTouchMove = (e) => { if (startYRef.current === null) return; const delta = e.touches[0].clientY - startYRef.current; if (delta < 0) return; // no drag up setDragY(delta); }; const handleHandleTouchEnd = () => { setIsDragging(false); if (dragY > 100) { setDragY(0); onClose?.(); } else { setDragY(0); } startYRef.current = null; }; const sheetTransform = open ? `translateY(${isDragging ? dragY : 0}px)` : 'translateY(100%)'; return ( <>