'use client'; import * as React from 'react'; import { CSSProperties, 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 FULL_OPACITY = 1; const position = React.useRef(0); const canBeCloseRef = 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 SCROLL_OVERFLOW_THRESHOLD = 150; const TOP_POSITION = 0; const overlay = overlayRef.current; const wrapper = wrapperRef.current; if (!overlay) return; const { clientY } = event.touches[0]; const scrollOverflow = clientY - position.current; const top = overlay.scrollTop; const isTop = top === TOP_POSITION; const canBeClosed = scrollOverflow > SCROLL_OVERFLOW_THRESHOLD; canBeCloseRef.current = canBeClosed && isTop; if (isTop && wrapper) { wrapper.style.opacity = `${FULL_OPACITY - scrollOverflow / SCROLL_OVERFLOW_THRESHOLD}`; } if (top <= TOP_POSITION && delta.current < TOP_POSITION && canBeClosed) onClose?.(); }; const handleTouchEnd = () => { if (canBeCloseRef.current) return onClose?.(); canBeCloseRef.current = false; if (!wrapperRef.current) return; wrapperRef.current.style.opacity = `${FULL_OPACITY}`; }; const handleRef = (element: HTMLDivElement | null) => { if (!element || !ref) return; typeof ref === 'function' ? ref(element) : (ref.current = 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; style?: CSSProperties; onClose: () => void; scrollRef?: React.MutableRefObject; }> = ({ children, onClose, className, style, 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 };