Fix mobile menu collapsing on scroll in iOS Safari#2261
Open
PeterKnightDigital wants to merge 1 commit into
Open
Fix mobile menu collapsing on scroll in iOS Safari#2261PeterKnightDigital wants to merge 1 commit into
PeterKnightDigital wants to merge 1 commit into
Conversation
iOS Safari fires a resize event when scrolling because the browser
address bar hide/show changes window.innerHeight. The initResizeHandler
was unconditionally hiding the nav on every mobile resize, collapsing
a menu the user had intentionally opened.
Guard the classList.add('hidden') call so it only runs when the menu
is not currently expanded (aria-expanded !== 'true').
Co-authored-by: Cursor <cursoragent@cursor.com>
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.
What this fixes
On iPhone and iPad, opening the mobile navigation menu and then scrolling down causes the menu to snap closed before you can tap a link. This makes it impossible to navigate the site using the mobile menu on iOS Safari — a potentially significant issue given how much traffic comes from mobile devices.
Why it happens
iOS Safari fires a
resizeevent whenever the user scrolls, because the browser address bar slides away as you scroll down (changing the visible height of the page). The menu's resize handler interpreted this as a layout change and hid the navigation menu — even if the user had just opened it intentionally.The fix
A one-line guard checks whether the menu is currently open before hiding it. If the user has the menu open, the resize event from scrolling is ignored. The menu still closes correctly in all other cases (e.g. rotating the device, resizing a browser window on desktop).
Steps to reproduce (before fix)
Test plan