Skip to content

Commit 9fd6e68

Browse files
committed
feat: a11y.slideLabelMessage
1 parent 3aad554 commit 9fd6e68

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

cypress/integration/modules/a11y.js

+17
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,30 @@ context('Core', () => {
1414
cy.getSliderWrapper().should('have.attr', 'aria-live', 'polite');
1515
});
1616

17+
it.only('paginationBulletMessage', () => {
18+
cy.initSwiper({
19+
pagination: true,
20+
a11y: { paginationBulletMessage: 'Slide to {{index}}' },
21+
});
22+
cy.getPaginationBullet(1).should('have.attr', 'Slide to', '2');
23+
});
24+
1725
it('should add aria-role-description="slide" to swiper-slide', () => {
1826
cy.initSwiper({
1927
a11y: { itemRoleDescriptionMessage: 'test' },
2028
});
2129
cy.getSlides().should('have.attr', 'aria-role-description', 'test');
2230
});
2331

32+
it('should add aria-label="1 of 10" to swiper-slide', () => {
33+
cy.initSwiper({
34+
a11y: { slideLabelMessage: '{{index}} of {{slidesLength}}' },
35+
});
36+
cy.getSlide(0).should('have.attr', 'aria-label', '1 of 10');
37+
cy.getSlide(4).should('have.attr', 'aria-label', '5 of 10');
38+
cy.getSlide(9).should('have.attr', 'aria-label', '10 of 10');
39+
});
40+
2441
it('should add aria-role-description="slide" to swiper-container', () => {
2542
cy.initSwiper({
2643
a11y: { containerRoleDescriptionMessage: 'test' },

cypress/support/commands.js

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Cypress.Commands.add('getSliderContainer', { prevSubject: 'optional' }, () => {
3030
Cypress.Commands.add('getSlide', { prevSubject: 'optional' }, (subject, slideIndex) => {
3131
return cy.get(`.swiper-slide:nth-child(${slideIndex + 1})`);
3232
});
33+
3334
Cypress.Commands.add('getSlideContains', { prevSubject: 'optional' }, (subject, content) => {
3435
cy.get('.swiper-container').contains(content);
3536
});
@@ -40,6 +41,10 @@ Cypress.Commands.add('swiperPage', { prevSubject: 'optional' }, () => {
4041
return cy.visit('cypress/test.html');
4142
});
4243

44+
Cypress.Commands.add('getPaginationBullet', { prevSubject: 'optional' }, (subject, bulletIndex) => {
45+
return cy.get(`.swiper-pagination-bullet:nth-child(${bulletIndex + 1})`);
46+
});
47+
4348
Cypress.Commands.add(
4449
'initSwiper',
4550
{ prevSubject: 'optional' },

src/components/a11y/a11y.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ const A11y = {
167167
swiper.a11y.addElRole($(swiper.slides), 'group');
168168
swiper.slides.each((slideEl) => {
169169
const $slideEl = $(slideEl);
170-
swiper.a11y.addElLabel($slideEl, `${$slideEl.index() + 1} / ${swiper.slides.length}`);
170+
const ariaLabelMessage = params.slideLabelMessage
171+
.replace(/\{\{index\}\}/, $slideEl.index() + 1)
172+
.replace(/\{\{slidesLength\}\}/, swiper.slides.length);
173+
swiper.a11y.addElLabel($slideEl, ariaLabelMessage);
171174
});
172175

173176
// Navigation
@@ -259,6 +262,7 @@ export default {
259262
firstSlideMessage: 'This is the first slide',
260263
lastSlideMessage: 'This is the last slide',
261264
paginationBulletMessage: 'Go to slide {{index}}',
265+
slideLabelMessage: '{{index}} / {{slidesLength}}',
262266
containerMessage: null,
263267
containerRoleDescriptionMessage: null,
264268
itemRoleDescriptionMessage: null,

src/types/components/a11y.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ export interface A11yOptions {
7272
* @default null
7373
*/
7474
itemRoleDescriptionMessage?: string | null;
75+
76+
/**
77+
* Message for screen readers describing the label of slide element
78+
*
79+
* @default '{{index}} / {{slidesLength}}'
80+
*/
81+
slideLabelMessage?: string;
7582
}

0 commit comments

Comments
 (0)