Skip to content

Commit

Permalink
feat(a11y): automatically switch slides on focus (Tab) in inactive …
Browse files Browse the repository at this point in the history
…slides

fixes #3149
  • Loading branch information
nolimits4web committed Jan 28, 2022
1 parent c91f222 commit 1288271
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/modules/a11y/a11y.js
Expand Up @@ -161,6 +161,18 @@ export default function A11y({ swiper, extendParams, on }) {
addElControls($el, wrapperId);
};

const handleFocus = (e) => {
const slideEl = e.target.closest(`.${swiper.params.slideClass}`);
if (!slideEl || !swiper.slides.includes(slideEl)) return;
const isActive = swiper.slides.indexOf(slideEl) === swiper.activeIndex;
const isVisible =
swiper.params.watchSlidesProgress &&
swiper.visibleSlides &&
swiper.visibleSlides.includes(slideEl);
if (isActive || isVisible) return;
swiper.slideTo(swiper.slides.indexOf(slideEl), 0);
};

function init() {
const params = swiper.params.a11y;

Expand Down Expand Up @@ -228,6 +240,9 @@ export default function A11y({ swiper, extendParams, on }) {
onEnterOrSpaceKey,
);
}

// Tab focus
swiper.$el.on('focus', handleFocus, true);
}
function destroy() {
if (liveRegion && liveRegion.length > 0) liveRegion.remove();
Expand Down Expand Up @@ -255,6 +270,9 @@ export default function A11y({ swiper, extendParams, on }) {
onEnterOrSpaceKey,
);
}

// Tab focus
swiper.$el.off('focus', handleFocus, true);
}

on('beforeInit', () => {
Expand Down

0 comments on commit 1288271

Please sign in to comment.