`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
Same symptom here with the default boundaryElement, reached through a different code path: body { position: relative }. That's a common app-shell style (nextjs.org ships it, for instance), and the fix proposed above doesn't cover it.
Repro on the React Aria docs
On https://react-aria.adobe.com/Select, viewport 1089px high:
- Run
document.body.style.position = 'relative'in the console. - Scroll so a Select trigger sits near the bottom of the viewport, and open it.
body |
scrollY |
trigger (viewport) | placement | popover (viewport) | max-height |
|---|---|---|---|---|---|
static (default) |
1479 | 1017 โ 1049 | top | above trigger โ | 997px |
relative |
1479 | 1017 โ 1049 | bottom | 1057 โ 1077 โ | 20.5px |
static (default) |
77 | 1017 โ 1049 | top | 436 โ 1009 โ | 997px |
relative |
77 | 1017 โ 1049 | top | 5728 โ 6302 โ | 920px |
Two problems:
- The flip is skewed by the page scroll. Once the page is scrolled far enough, the popover never flips and gets squeezed below the trigger.
- When it does flip, it lands in the wrong place. The popover is positioned near the bottom of the page instead of above the trigger.
Tested with react-aria 3.53.1. calculatePosition is unchanged on main.
Why the fix proposed above doesn't apply
With a positioned body, getContainingBlock(overlay) returns body, so the container is body and the boundary is also body (the default). The boundary goes through the BODY/HTML branch of getContainerDimensions, so its top is already in viewport coordinates and is 0. Measuring the custom boundary without the scroll therefore never runs for this case.
Where the extra scroll comes from
1. getAvailableSpace counts the page scroll twice.
childOffsetcomes fromgetPosition(trigger, body), which is in document coordinates (viewportTop + scrollY).containerDimensions.scroll.topisscrollY, from theBODYbranch.containerOffsetWithBoundary.topis-(bodyRect.top - 0), which is alsoscrollY.isContainerDescendentOfBoundaryisnodeContains(body, body), which istrue. SocontainerOffsetWithBoundarygets added on top of the scroll.
The result:
- Space below = real space below +
scrollY - Space above = real space above โ
scrollY
That's the same skew as in this issue, just from a different term.
2. computePosition positions a flipped popover against the wrong height. For top placement it sets bottom = containerHeight - childOffset.top + offset. Because isContainerPositioned is true, containerHeight is containerDimensions.height. But the BODY branch of getContainerDimensions returns the viewport height (1089), not body's own height (6382). The browser then resolves bottom against body's full height, which pushes the popover about 5,300px down the page.
The underlying cause looks the same as in #6333 and here: page, viewport and container coordinates get mixed up once the containing block isn't the viewport. A positioned body is probably the most common way apps hit it.