react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

`useOverlayPosition` with a custom `boundaryElement`: page scroll skews flipping, and `maxHeight` ignores the boundary's bottom

Open cescox opened this issue 2 months ago โ€ข 1 comments

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:

  • getAvailableSpace compares it with trigger coordinates in viewport terms. The space below comes out scrollY too large and the space above scrollY too small, so past some scroll offset the flip never wins.
  • getMaxHeight takes the smaller of the boundary's bottom and the visual viewport's bottom. Once scrollY is 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

cescox avatar Sep 25 '26 11:09 cescox

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:

  1. Run document.body.style.position = 'relative' in the console.
  2. 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:

  1. 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.
  2. 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.

  • childOffset comes from getPosition(trigger, body), which is in document coordinates (viewportTop + scrollY).
  • containerDimensions.scroll.top is scrollY, from the BODY branch.
  • containerOffsetWithBoundary.top is -(bodyRect.top - 0), which is also scrollY.
  • isContainerDescendentOfBoundary is nodeContains(body, body), which is true. So containerOffsetWithBoundary gets 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.

artola avatar Oct 20 '26 16:10 artola