From 5b41dbca1004446bb43342c1544f0204e53c2d53 Mon Sep 17 00:00:00 2001 From: ner Date: Sun, 13 Sep 2026 19:32:35 +0300 Subject: [PATCH] fix(ui): track top modal overlay in JS, Safari does not re-evaluate :has() on removal --- components/ui/dialog.tsx | 45 ++++++++++++++++++++++++++++++++++++++-- styles/index.css | 4 +++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/components/ui/dialog.tsx b/components/ui/dialog.tsx index bc1ea43..d957a9f 100644 --- a/components/ui/dialog.tsx +++ b/components/ui/dialog.tsx @@ -8,6 +8,29 @@ 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; @@ -47,12 +70,30 @@ const DialogContent = React.forwardRef< const canBeCloseRef = React.useRef(false); - const wrapperRef = React.useRef(null); + 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; }; @@ -104,7 +145,7 @@ const DialogContent = React.forwardRef< return ( -
+
{ if (!element) return; diff --git a/styles/index.css b/styles/index.css index 507997d..b9d62cf 100644 --- a/styles/index.css +++ b/styles/index.css @@ -107,7 +107,9 @@ @apply bg-background text-foreground; } - body [data-overlay]:has(~ [data-overlay]) { + /* The topmost overlay is flagged by JS (see markTopOverlay in dialog.tsx); Safari + does not re-evaluate :has() when a sibling overlay is removed. */ + body [data-overlay]:not([data-overlay-top]) { background: none; }