Skip to content

fix: Popover is positioned incorrectly when the layout changes #3427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/popover/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export default function PopoverContainer({
/*
This is a heuristic. Some layout changes are caused by user clicks (e.g. toggling the tools panel, submitting a form),
and by tracking the click event we can adapt the popover's position to the new layout.

TODO: extend this to Enter and Spacebar?
*/

const onClick = (event: UIEvent | KeyboardEvent) => {
const controller = new AbortController();

const onClick = async (event: UIEvent | KeyboardEvent) => {
if (
// Do not update position if keepPosition is true.
keepPosition ||
Expand All @@ -114,14 +114,29 @@ export default function PopoverContainer({
return;
}

requestAnimationFrame(() => {
// Continuously update the popover position for one second to account for any layout changes
// and animations. On browsers where `requestIdleCallback` is supported,
// this runs only while the CPU is otherwise idle. In other browsers (mainly Safari), we call it
// with a low frequency.
const targetTime = performance.now() + 1_000;

while (performance.now() < targetTime) {
if (controller.signal.aborted) {
break;
}

updatePositionHandler();
});

if (typeof requestIdleCallback !== 'undefined') {
await new Promise(r => requestIdleCallback(r));
} else {
await new Promise(r => setTimeout(r, 50));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to fallback to requestAnimationFrame rather than setTimeout?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The position update does not necessarily need to be a smooth animation at the full refresh rate that the browser can do, it just shouldn't lag behind too much. So by using setTimeout, we get a reasonably good-looking behaviour without demanding too much performance from a potentially non-idle CPU.

}
}
};

const updatePositionOnResize = () => requestAnimationFrame(() => updatePositionHandler());
const refreshPosition = () => requestAnimationFrame(() => positionHandlerRef.current());
const controller = new AbortController();

window.addEventListener('click', onClick, { signal: controller.signal });
window.addEventListener('resize', updatePositionOnResize, { signal: controller.signal });
Expand Down
Loading