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
16 changes: 11 additions & 5 deletions src/modules/a11y/a11y.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ export default function A11y({ swiper, extendParams, on }) {

function updatePagination() {
const params = swiper.params.a11y;
if (hasPagination()) {
swiper.pagination.bullets.each((bulletEl) => {
const $bulletEl = $(bulletEl);
const doHasPagination = hasPagination();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is a sense to replace this line with just

if (!hasPagination()) return;

I mean what is the point to to loop over swiper.pagination.bullets and do checks for pagination inside if hasPagination() return false?

nolimits4web marked this conversation as resolved.
Show resolved Hide resolved
swiper.pagination.bullets.each((bulletEl) => {
const $bulletEl = $(bulletEl);
if (doHasPagination) {
makeElFocusable($bulletEl);
if (!swiper.params.pagination.renderBullet) {
addElRole($bulletEl, 'button');
Expand All @@ -142,8 +143,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