`useOverlayPosition` with a custom `boundaryElement`: page scroll skews flipping, and `maxHeight` ignores the boundary's bottom
Provide a general summary of the issue here
With a custom boundaryElement, the placement depends on how far the page is scrolled, even when the boundary, the trigger and the overlay are all in the same place on screen. Scroll far enough and the overlay no longer flips, and its max-height runs past the boundary's bottom to the viewport's.
react-aria 3.52.1, tested headless with Playwright in Chromium 153, Firefox 152 and WebKit 26.5. calculatePosition is unchanged on main.
Possibly related to #6333, but a different code path: here the overlay is portalled to body, so its container is the viewport, and the wrong values come from the page scroll.
Expected Behavior?
The same placement and max-height whatever the page scroll. In the repro, that's the SCROLL = 0 result: only 39px are left below the trigger inside the boundary (after padding), so the overlay should flip above.
Current Behavior
At 1000×800:
SCROLL = 400 |
SCROLL = 0 |
|
|---|---|---|
| trigger top / bottom (viewport) | 600 / 621 | 600 / 621 |
| boundary bottom | 672 | 672 |
| placement | bottom | top |
| overlay top → bottom | 621 → 790 | 18 → 600 |
max-height |
167px | 580px |
Chromium numbers. Firefox and WebKit give the same placement, with the trigger 1–3px taller or shorter.
Possible Solution
For a boundary that isn't body/html, getContainerDimensions measures it with getOffset (:146 → :763), which adds document.documentElement.scrollTop. So the boundary's top ends up in document coordinates, although the comment at :690 says it doesn't include scroll. That has two effects:
getAvailableSpacecompares it with trigger coordinates in viewport terms. The space below comes outscrollYtoo large and the space abovescrollYtoo small, so past some scroll offset the flip never wins.getMaxHeighttakes the smaller of the boundary's bottom and the visual viewport's bottom. OncescrollYis larger than the boundary's bottom inset, the viewport wins and the boundary's bottom is ignored.
Context
I'm building a dropdown on useOverlayPosition that should stay inside the viewport minus some padding, so I pass a padded, viewport-sized element as boundaryElement. On a scrolled page it runs off the bottom of that boundary. For now I work around it by offsetting that element by the page scroll.
️ Steps to Reproduce
Runnable version: CodeSandbox. It opens on the bug, with a link that switches to the control.
import { useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { useOverlayPosition } from 'react-aria'
const SCROLL = 400
export function Repro() {
const targetRef = useRef<HTMLButtonElement>(null)
const overlayRef = useRef<HTMLDivElement>(null)
const [boundary, setBoundary] = useState<HTMLDivElement | null>(null)
const [isOpen, setOpen] = useState(false)
const { overlayProps, placement } = useOverlayPosition({
targetRef,
overlayRef,
boundaryElement: boundary ?? undefined,
placement: 'bottom start',
isOpen,
})
return (
<div style={{ height: 3000 }}>
{/* The boundary: the viewport minus 128px at the bottom. */}
<div ref={setBoundary} style={{ position: 'fixed', inset: '8px 8px 128px 8px', outline: '1px dashed red', pointerEvents: 'none' }} />
<button onClick={() => window.scrollTo(0, SCROLL)} style={{ position: 'fixed', top: 16, right: 16 }}>
scroll to {SCROLL}
</button>
{/* 200px above the viewport bottom: 72px of room above the boundary bottom. */}
<button ref={targetRef} onClick={() => setOpen(o => !o)} style={{ position: 'absolute', top: window.innerHeight - 200 + SCROLL, left: 100 }}>
trigger
</button>
{isOpen &&
createPortal(
<div ref={overlayRef} {...overlayProps} data-placement={placement} style={{ ...overlayProps.style, overflow: 'auto', width: 200, border: '1px solid' }}>
<div style={{ height: 2000 }}>tall content</div>
</div>,
document.body,
)}
</div>
)
}Click "scroll to 400", then "trigger". Set SCROLL = 0 for the expected result.
Version
react-aria 3.52.1
What browsers are you seeing the problem on?
Chrome, Firefox, Other
If other, please specify.
WebKit 26.5 (Playwright's build, the engine behind Safari). Not tested in Safari itself.
What operating system are you using?
macOS
Your Company/Team
No response
Tracking Issue
No response
Source: adobe/react-spectrum