Skip to content

fix(overlays): preserve scroll position when popover repositions - #11020

Open
o-fidler wants to merge 2 commits into
adobe:mainfrom
o-fidler:fix/overlay-preserve-scroll-10964
Open

o-fidler wants to merge 2 commits into
adobe:mainfrom
o-fidler:fix/overlay-preserve-scroll-10964

Conversation

@o-fidler

Copy link
Copy Markdown

Closes #10964

Summary

When a popover repositions (e.g. on window resize), updatePosition() in useOverlayPosition temporarily sets the overlay's max-height to the full viewport height before measuring. Scroll containers that use max-height: inherit (like the RAC ListBox) briefly grow, and the browser clamps their scrollTop to scrollHeight - viewportHeight. The existing scroll-anchor restore only ran when real DOM focus was inside the scroll container, so in a ComboBox (where focus stays in the input) the position was never restored. The list jumped up, and the last options couldn't be reached.

Changes:

  • Virtual focus support (per @snowystinger's suggestion on the issue): a new getFocusedElementWithin() helper returns the focused element inside the scroll container, either via real DOM focus or via the active element's aria-activedescendant. The existing top/bottom anchor logic now uses this element, so a keyboard-highlighted option stays in place when the popover repositions. The lookup uses getRootNode() and nodeContains so it works in shadow DOM.
  • Fallback when nothing is focused: scrollTop is saved before the max-height reset and restored afterward when there is no anchor element (e.g. the user scrolled with the mouse).

Out of scope: the issue also describes a continuous reposition loop on Windows, where a temporary page scrollbar fires visualViewport resize events. This PR stops the visible scroll jumping, but it doesn't stop the extra repositions themselves. That could be a follow-up if wanted.

📝 Test Instructions

Regression test: added preserves the scroll position of the scroll container when repositioning to packages/react-aria/test/overlays/useOverlayPosition.browser.test.tsx. It scrolls an overlay's list to the bottom, fires a resize, and asserts that scrollTop is unchanged.

  • Without the fix: fails in Chromium, WebKit, and Firefox (expected 2280 to be 2522, i.e. scrollTop was clamped).
  • With the fix: passes in all three.

yarn test:browser packages/react-aria/test/overlays/useOverlayPosition.browser.test.tsx # 9/9 passed
yarn test packages/react-aria/test/overlays packages/react-aria-components/test/ComboBox.test.js # 244/244 passed

Manual: In Storybook (React Aria Components → ComboBox), opened the list, scrolled to the last item, resized the window — list stayed in place. With the keyboard, ArrowDown past the old cap keeps the highlighted option visible. Tested on Windows 11 / Chrome.

Not covered: the automated test covers the mouse-scroll path (no focused element). The virtual-focus (aria-activedescendant) path was verified manually only. Not tested: RTL, touch, or screen readers [edit if you did test any of these].

Release impact

  • Only changes docs, tests, CI, or development tools. Nothing shipped to users changes.
  • Fixes a bug or improves existing code without changing how apps use it.
  • Adds a feature or API while keeping existing usage working.
  • May break existing usage or require changes in apps that use this code.

✅ PR Checklist

  • I followed the contributing guide.
  • I tested this change locally and included the results in Test Instructions, or explained why testing does not apply.
  • I understand and can explain every change in this PR. If I used AI assistance, I followed the AI contribution guidance.

🧢 Your Project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ComboBox/Popover list loses scroll position when the popover repositions

1 participant