Skip to content

fix: prevent page scroll when restoring focus on modal close - #578

Merged
pradel merged 1 commit into
masterfrom
fix/focus-restore-scroll
Sep 24, 2026
Merged

pradel merged 1 commit into
masterfrom
fix/focus-restore-scroll

Conversation

@pradel

@pradel pradel commented Sep 24, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #515.

When a modal is closed, FocusTrap restores focus to the element that was focused before the modal opened using .focus(). Chrome and Safari scroll that element into view, so the page jumps to a "specific scroll distance from the top" (the trigger position) on close — exactly what the issue describes.

This restores the focus with { preventScroll: true }, so focus is still returned to the trigger without moving the page scroll position.

Tests

  • Added a unit test asserting focus is restored with { preventScroll: true }
  • Added a Cypress e2e test asserting the page scroll position is preserved when the modal is closed

Both tests fail without the fix. Verified vp check, unit tests (59) and Cypress specs (14) pass with the fix.

@pradel
pradel merged commit 6ca8424 into master Sep 24, 2026
1 check passed
@pradel
pradel deleted the fix/focus-restore-scroll branch September 24, 2026 21:03
@codecov

codecov Bot commented Sep 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.66%. Comparing base (b69f310) to head (89b6f87).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #578      +/-   ##
==========================================
+ Coverage   80.69%   83.66%   +2.97%     
==========================================
  Files           7        7              
  Lines         202      202              
  Branches       74       74              
==========================================
+ Hits          163      169       +6     
+ Misses         29       24       -5     
+ Partials       10        9       -1     

☔ View full report in Codecov by Harness.
📢 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.

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.

When modal closes, the scroll position changes.

1 participant