You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I encountered an issue where sometimes the pagination bullets are not activatable by keyboard even though pagination.clickable is set to true. This happens when the swiper element has size 0 during initialization, e.g. because one of its parent elements is hidden. In the reproduction I've simulated this by wrapping it inside a hidden div that gets unhidden after a delay, but in practical use I've also encountered this when the swiper isn't intentionally hidden and just has size 0 due to some initialization logic.
I backtraced the issue:
This is the code that adds the keyboard event listener to the pagination. It doesn't get run because hasClickablePagination() returns false.
However, since I don't know the codebase, I don't know if that breaks any assumptions elsewhere.
Either (or both) of these changes should fix the issue by not skipping the initialization in the a11y module where the pagination keydown listener gets added.
Expected Behavior
Focusing a clickable pagination bullet with the keyboard and then activating it with Enter or Space should switch to the corresponding slide.
Check that this is really a bug
Reproduction link
https://stackblitz.com/edit/swiper-pagination-a11y-bug
Bug description
I encountered an issue where sometimes the pagination bullets are not activatable by keyboard even though
pagination.clickable
is set to true. This happens when the swiper element has size 0 during initialization, e.g. because one of its parent elements is hidden. In the reproduction I've simulated this by wrapping it inside a hidden div that gets unhidden after a delay, but in practical use I've also encountered this when the swiper isn't intentionally hidden and just has size 0 due to some initialization logic.I backtraced the issue:
hasClickablePagination()
returnsfalse
.swiper/src/modules/a11y/a11y.mjs
Lines 333 to 339 in 488636f
false
becauseswiper.pagination.bullets.length
is0
.swiper/src/modules/a11y/a11y.mjs
Lines 175 to 181 in 488636f
bullets
array is empty becauseswiper.snapGrid
is empty.swiper/src/modules/pagination/pagination.mjs
Line 289 in 488636f
snapGrid
is initialized to the empty array:swiper/src/core/core.mjs
Line 141 in 488636f
Usually, if it's empty, it gets set to the 1-element array
[0]
here:swiper/src/core/update/updateSlides.mjs
Line 234 in 488636f
However, that code doesn't get run because the function returns early when
swiper.size
isundefined
:swiper/src/core/update/updateSlides.mjs
Lines 43 to 45 in 488636f
swiper.size
only gets set when it has a size > 0:swiper/src/core/update/updateSize.mjs
Lines 7 to 20 in 488636f
When the swiper element is hidden during initialization, or has width 0 for some other reason,
size
doesn't get set.I can see two potential fixes for this issue:
bullets.length > 0
is unnecessary:The only place where
bullets
is accessed is here, andforEach
is safe to call on an empty array:swiper/src/modules/a11y/a11y.mjs
Line 186 in 488636f
snapGrid
gets set to[0]
when it is empty inupdateSlides.mjs
, it might be sensible to initialize it with that instead of an empty array:swiper/src/core/core.mjs
Line 141 in 488636f
However, since I don't know the codebase, I don't know if that breaks any assumptions elsewhere.
Either (or both) of these changes should fix the issue by not skipping the initialization in the a11y module where the pagination keydown listener gets added.
Expected Behavior
Focusing a clickable pagination bullet with the keyboard and then activating it with
Enter
orSpace
should switch to the corresponding slide.Actual Behavior
Nothing happens.
Swiper version
11.1.15
Platform/Target and Browser Versions
Windows, Chrome 131/Firefox 131
Validations
Would you like to open a PR for this bug?
The text was updated successfully, but these errors were encountered: