Fix: Tab navigation breaks slider when links inside slides get focused (#4006) #8138
+289
−1
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.
Problem
When using the Tab key to navigate through links inside Swiper slides, the browser automatically scrolls the container to show the focused element. This changes the
scrollLeft/scrollTopproperty of the wrapper element, which breaks the slider display and causes slides to become misaligned.This issue affects keyboard accessibility, as users navigating with the Tab key would see the slider break visually.
Solution
The fix prevents the browser's automatic scroll from affecting the slider by:
focusinevents: Added afocusinevent listener (which fires beforefocusand bubbles) to catch focus events on focusable elements (links, buttons, inputs, etc.) inside slides.scrollLeft/scrollTopof the wrapper element (or container in CSS mode).requestAnimationFrameto catch any browser scroll that occurs during the focus event.Changes
src/modules/a11y/a11y.mjs:savedScrollLeft,savedScrollTop,isRestoringScroll,scrollRestoreTimeout)restoreScrollPosition()function to restore saved scroll positionshandleScroll()function to catch and restore scroll during focus eventshandleFocusIn()to save and restore scroll position for all focusable elementsinit()and cleanup indestroy()demos/4006-tab-focus-fix.htmlto verify the fix works correctlyRelated Issues
Fixes #4006