'use client'; import * as React from 'react'; import { useState } from 'react'; import * as DialogPrimitive from '@radix-ui/react-dialog'; import useScrollDelta from '@/hooks/useScrollDelta'; import { cn } from '@/lib/utils'; const Dialog = DialogPrimitive.Root; const DialogTrigger = DialogPrimitive.Trigger; const DialogPortal = DialogPrimitive.Portal; const DialogClose = DialogPrimitive.Close; const DialogOverlay = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( {props?.children} )); DialogOverlay.displayName = DialogPrimitive.Overlay.displayName; const DialogContent = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & { onClose?: () => void; scrollRef?: React.MutableRefObject; } >(({ className, children, onClose, scrollRef, ...props }, ref) => { const position = React.useRef(0); const close = React.useRef(false); const wrapperRef = React.useRef(null); const overlayRef = React.useRef(null) as React.MutableRefObject; const contentRef = React.useRef(null); const { delta, onScroll } = useScrollDelta(); const handleTouchStart = (event: React.TouchEvent) => { position.current = event.touches[0].clientY; }; const handleTouchMove = (event: React.TouchEvent) => { const threshold = 150; const { clientY } = event.touches[0]; const overlay = overlayRef.current; const wrapper = wrapperRef.current; const diff = clientY - position.current; const top = overlay?.scrollTop || 0; const isTop = top === 0; const canBeClosed = diff > threshold; close.current = canBeClosed && isTop; if (isTop && wrapper) { wrapper.style.opacity = `${1 - (diff * 100) / threshold / 100}`; } if (top <= 0 && delta.current < 0 && canBeClosed) onClose?.(); }; const handleTouchEnd = () => { if (close.current) return onClose?.(); close.current = false; if (!wrapperRef.current) return; wrapperRef.current.style.opacity = '1'; }; const handleRef = (element: HTMLDivElement | null) => { if (!element) return; (ref as (element: HTMLElement | null) => void)?.(element); contentRef.current = element; }; return (
{ if (!element) return; overlayRef.current = element; if (scrollRef) { scrollRef.current = element; } }} onScroll={onScroll} > {children}
); }); DialogContent.displayName = DialogPrimitive.Content.displayName; const DialogHeader = ({ className, ...props }: React.HTMLAttributes) => (
); DialogHeader.displayName = 'DialogHeader'; const DialogFooter = ({ className, ...props }: React.HTMLAttributes) => (
); DialogFooter.displayName = 'DialogFooter'; const DialogTitle = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); DialogTitle.displayName = DialogPrimitive.Title.displayName; const DialogDescription = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); DialogDescription.displayName = DialogPrimitive.Description.displayName; const Modal: React.FC<{ children: React.ReactNode; className?: string; onClose: () => void; scrollRef?: React.MutableRefObject; }> = ({ children, onClose, className, scrollRef }) => { const [open, setOpen] = useState(true); const handleClose = () => { onClose?.(); setOpen(false); }; return ( {children} ); }; export { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, Modal };