'use client'; import * as React from 'react'; import { CSSProperties, useState } from 'react'; import * as DialogPrimitive from '@radix-ui/react-dialog'; import { DialogContentProps } from '@radix-ui/react-dialog'; import useScrollDelta from '@/hooks/useScrollDelta'; import { cn } from '@/lib/utils'; // Safari does not re-evaluate `:has()` when a sibling is removed, so stacked // backdrops (only the topmost overlay should be dark) are tracked in JS instead: // each open overlay registers itself and we flag the newest one as the top. const overlayStack: HTMLDivElement[] = []; const markTopOverlay = () => { // The top overlay is the last one in document order (portals append on open and // re-renders never move the node), NOT the last registered one: React may re-fire // a ref callback on the same node on re-render, which must not reorder anything. let top: HTMLDivElement | null = null; for (const overlay of overlayStack) { if (!top || top.compareDocumentPosition(overlay) & Node.DOCUMENT_POSITION_FOLLOWING) { top = overlay; } } for (const overlay of overlayStack) { if (overlay === top) overlay.setAttribute('data-overlay-top', ''); else overlay.removeAttribute('data-overlay-top'); } }; 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; portal?: HTMLElement; } >(({ className, children, onClose, scrollRef, portal, ...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(); // A ref callback (not an effect) because Radix's Portal defers attaching the // overlay to a re-render, so a one-shot effect would see a null ref. const handleWrapperRef = React.useCallback((element: HTMLDivElement | null) => { const previous = wrapperRef.current; wrapperRef.current = element; if (element) { if (!overlayStack.includes(element)) overlayStack.push(element); } else if (previous) { const index = overlayStack.indexOf(previous); if (index !== -1) overlayStack.splice(index, 1); } markTopOverlay(); }, []); 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; onInteractOutside?: DialogContentProps['onInteractOutside']; onPointerDownOutside?: DialogContentProps['onPointerDownOutside']; portal?: HTMLElement; }> = ({ children, onClose, className, style, scrollRef, onInteractOutside, onPointerDownOutside, portal }) => { const [open, setOpen] = useState(true); React.useLayoutEffect(() => { const viewport = document.querySelector('[name="viewport"]'); viewport?.setAttribute( 'content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover' ); }, []); const handleClose = () => { onClose?.(); setOpen(false); }; return ( {children} ); }; export { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, Modal };