Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(a11y): add aria-current to current bullet #5258

Merged
Merged
9 changes: 9 additions & 0 deletions cypress/integration/modules/a11y.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,14 @@ context('Core', () => {
cy.get('.swiper-pagination-bullet:nth-child(5)').trigger('keydown', { keyCode: 32 });
cy.getSlide(4).should('have.class', 'swiper-slide-active');
});

it('Current bullet should have aria-current true', () => {
cy.slideTo(2);
cy.getPaginationBullet(2).should('have.attr', 'aria-current', 'true');
cy.getPaginationBullet(4).should('not.have.attr', 'aria-current');
cy.slideTo(4);
cy.getPaginationBullet(2).should('not.have.attr', 'aria-current');
cy.getPaginationBullet(4).should('have.attr', 'aria-current', 'true');
});
});
});
5 changes: 5 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ Cypress.Commands.add('getPaginationBullet', { prevSubject: 'optional' }, (subjec
return cy.get(`.swiper-pagination-bullet:nth-child(${bulletIndex + 1})`);
});

Cypress.Commands.add('slideTo', { prevSubject: 'optional' }, (subject, slideIndex) => {
return cy.window().then((_window) => {
_window.swiperRef.slideTo(slideIndex);
});
});
Cypress.Commands.add(
'initSwiper',
{ prevSubject: 'optional' },
Expand Down
31 changes: 18 additions & 13 deletions src/modules/a11y/a11y.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ export default function A11y({ swiper, extendParams, on }) {
}

function hasPagination() {
return (
swiper.pagination &&
swiper.params.pagination.clickable &&
swiper.pagination.bullets &&
swiper.pagination.bullets.length
);
return swiper.pagination && swiper.pagination.bullets && swiper.pagination.bullets.length;
}

function hasClickablePagination() {
return hasPagination() && swiper.params.pagination.clickable;
}

function updatePagination() {
const params = swiper.params.a11y;
if (hasPagination()) {
swiper.pagination.bullets.each((bulletEl) => {
const $bulletEl = $(bulletEl);
if (!hasPagination()) return;
swiper.pagination.bullets.each((bulletEl) => {
const $bulletEl = $(bulletEl);
if (swiper.params.pagination.clickable) {
makeElFocusable($bulletEl);
if (!swiper.params.pagination.renderBullet) {
addElRole($bulletEl, 'button');
Expand All @@ -142,8 +142,13 @@ export default function A11y({ swiper, extendParams, on }) {
params.paginationBulletMessage.replace(/\{\{index\}\}/, $bulletEl.index() + 1),
);
}
});
}
}
if ($bulletEl.is(`.${swiper.params.pagination.bulletActiveClass}`)) {
$bulletEl.attr('aria-current', 'true');
} else {
$bulletEl.removeAttr('aria-current');
}
});
}

const initNavEl = ($el, wrapperId, message) => {
Expand Down Expand Up @@ -216,7 +221,7 @@ export default function A11y({ swiper, extendParams, on }) {
}

// Pagination
if (hasPagination()) {
if (hasClickablePagination()) {
swiper.pagination.$el.on(
'keydown',
classesToSelector(swiper.params.pagination.bulletClass),
Expand All @@ -243,7 +248,7 @@ export default function A11y({ swiper, extendParams, on }) {
}

// Pagination
if (hasPagination()) {
if (hasClickablePagination()) {
swiper.pagination.$el.off(
'keydown',
classesToSelector(swiper.params.pagination.bulletClass),
Expand Down