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

Conversation

connorlanigan
Copy link
Member

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:

  • AWSUI-60630

How has this been tested?

Review checklist

The following items are to be evaluated by the author(s) and the reviewer(s).

Correctness

  • Changes include appropriate documentation updates.
  • Changes are backward-compatible if not indicated, see CONTRIBUTING.md.
  • Changes do not include unsupported browser features, see CONTRIBUTING.md.
  • Changes were manually tested for accessibility, see accessibility guidelines.

Security

Testing

  • Changes are covered with new/existing unit tests?
  • Changes are covered with new/existing integration tests?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@connorlanigan connorlanigan requested a review from a team as a code owner April 15, 2025 12:04
@connorlanigan connorlanigan requested review from orangevolon and removed request for a team April 15, 2025 12:04
Copy link
Member

@orangevolon orangevolon left a 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.

updatePositionHandler();
});
await new Promise(r => requestIdleCallback(r));
Copy link
Member

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);

Copy link

codecov bot commented May 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.50%. Comparing base (4e31a2f) to head (7561daa).
Report is 47 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@connorlanigan connorlanigan requested a review from orangevolon May 13, 2025 11:13
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.

@connorlanigan connorlanigan added this pull request to the merge queue May 13, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 13, 2025
@connorlanigan connorlanigan added this pull request to the merge queue May 13, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 13, 2025
@connorlanigan connorlanigan added this pull request to the merge queue May 13, 2025
Merged via the queue into main with commit 62b757d May 13, 2025
38 checks passed
@connorlanigan connorlanigan deleted the fix/popover-layout branch May 13, 2025 14:25
just-boris added a commit that referenced this pull request May 13, 2025
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.

2 participants