Skip to content

Commit 123e856

Browse files
authored
Merge pull request #547 from gselderslaghs/carousel-accessibility
accessibility(Carousel) indicators not accessible by keyboard interaction
2 parents 395350e + 3b0aee8 commit 123e856

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

spec/tests/carousel/carouselSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ describe('Carousel', () => {
2020
afterEach(() => XunloadFixtures());
2121

2222
describe('carousel plugin', () => {
23+
afterEach(() => {
24+
M.Carousel.getInstance(document.querySelector('.carousel')).destroy();
25+
});
26+
2327
it('No wrap next and prev should not overflow', (done) => {
2428
const noWrap = M.Carousel.init(document.querySelector('#slider-no-wrap'), {
2529
duration: 10,
@@ -39,5 +43,19 @@ describe('Carousel', () => {
3943
}, 30);
4044
}, 50);
4145
});
46+
47+
it('should change index and focus its respective item on indicator click', (done) => {
48+
const carousel = M.Carousel.init(document.querySelector('.carousel'), {
49+
duration: 10,
50+
indicators: true
51+
});
52+
53+
document.querySelectorAll('.indicator-item')[1].click();
54+
setTimeout(() => {
55+
expect(carousel.center).toEqual(1, 'carousel item was not visible after indicator interaction');
56+
done();
57+
}, 30);
58+
});
59+
4260
});
4361
});

src/carousel.ts

+13
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export class Carousel extends Component<CarouselOptions> {
133133
if (this.showIndicators) {
134134
const indicator = document.createElement('li');
135135
indicator.classList.add('indicator-item');
136+
indicator.tabIndex = 0;
136137
if (i === 0) {
137138
indicator.classList.add('active');
138139
}
@@ -211,6 +212,7 @@ export class Carousel extends Component<CarouselOptions> {
211212
if (this.showIndicators && this._indicators) {
212213
this._indicators.querySelectorAll('.indicator-item').forEach((el) => {
213214
el.addEventListener('click', this._handleIndicatorClick);
215+
el.addEventListener('keypress', this._handleIndicatorKeyPress);
214216
});
215217
}
216218
// Resize
@@ -352,6 +354,17 @@ export class Carousel extends Component<CarouselOptions> {
352354

353355
_handleIndicatorClick = (e: Event) => {
354356
e.stopPropagation();
357+
this._handleIndicatorInteraction(e);
358+
}
359+
360+
_handleIndicatorKeyPress = (e: KeyboardEvent) => {
361+
e.stopPropagation();
362+
if (Utils.keys.ENTER.includes(e.key)) {
363+
this._handleIndicatorInteraction(e);
364+
}
365+
}
366+
367+
_handleIndicatorInteraction = (e: Event) => {
355368
const indicator = (<HTMLElement>e.target).closest('.indicator-item');
356369
if (indicator) {
357370
const index = [...indicator.parentNode.children].indexOf(indicator);

0 commit comments

Comments
 (0)