-
Notifications
You must be signed in to change notification settings - Fork 172
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we could find a way to have a unit test for the 1000ms
part of the logic.
src/popover/container.tsx
Outdated
updatePositionHandler(); | ||
}); | ||
await new Promise(r => requestIdleCallback(r)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if having an extra micro-task here is necessary. It took me some time to understand what's happening.
The way I read this is that: we create a micro-task, wait for a free cycle and then resolve the promise. This means the calculation doesn't happen in the cpu's free time, we just wait for a free CPU time to run the next cycle.
A simpler approach IMO would be simply having animation pattern:
function updatePosition() {
if (controller.signal.aborted) return;
if (performance.now() >= targetTime) return;
updatePositionHandler();
requestIdleCallback(updatePosition);
}
requestIdleCallback(updatePosition);
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3427 +/- ##
==========================================
- Coverage 96.51% 96.50% -0.02%
==========================================
Files 806 804 -2
Lines 23028 23143 +115
Branches 7550 8013 +463
==========================================
+ Hits 22226 22333 +107
+ Misses 795 756 -39
- Partials 7 54 +47 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
if (typeof requestIdleCallback !== 'undefined') { | ||
await new Promise(r => requestIdleCallback(r)); | ||
} else { | ||
await new Promise(r => setTimeout(r, 50)); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
Description
When an Annotation Popover is open, and the user interacts with the page so that the layout changes (e.g. toggling the side navigation panel), the Popover needs to update its position. With the existing heuristic, the Popover recalculates whenever the user clicks on the page (or interacts with a button using the keyboard), but the current logic assumes that any layout update then happens in a single frame. In the case of the navigation panel opening, it slides in with an animation, so the Popover only adapts to the first frame.
With this change, the Popover continuously updates its position for one second after the user interacts with the page. This should give enough time for any layout-related animations to finish, and it smoothly updates the Popover's position while its trigger moves. Since we're using
requestIdleCallback
, the browser will only execute these updates if the CPU is otherwise idle.Related links, issue #, if available:
How has this been tested?
Review checklist
The following items are to be evaluated by the author(s) and the reviewer(s).
Correctness
CONTRIBUTING.md
.CONTRIBUTING.md
.Security
checkSafeUrl
function.Testing
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.