fix: prevent page scroll when restoring focus on modal close - #578
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #515.
When a modal is closed,
FocusTraprestores 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
{ preventScroll: true }Both tests fail without the fix. Verified
vp check, unit tests (59) and Cypress specs (14) pass with the fix.