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

accessibility(Carousel) indicators not accessible by keyboard interaction #547

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions spec/tests/carousel/carouselSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ describe('Carousel', () => {
afterEach(() => XunloadFixtures());

describe('carousel plugin', () => {
afterEach(() => {
M.Carousel.getInstance(document.querySelector('.carousel')).destroy();
});

it('No wrap next and prev should not overflow', (done) => {
const noWrap = M.Carousel.init(document.querySelector('#slider-no-wrap'), {
duration: 10,
Expand All @@ -39,5 +43,19 @@ describe('Carousel', () => {
}, 30);
}, 50);
});

it('should change index and focus its respective item on indicator click', (done) => {
const carousel = M.Carousel.init(document.querySelector('.carousel'), {
duration: 10,
indicators: true
});

document.querySelectorAll('.indicator-item')[1].click();
setTimeout(() => {
expect(carousel.center).toEqual(1, 'carousel item was not visible after indicator interaction');
done();
}, 30);
});

});
});
13 changes: 13 additions & 0 deletions src/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class Carousel extends Component<CarouselOptions> {
if (this.showIndicators) {
const indicator = document.createElement('li');
indicator.classList.add('indicator-item');
indicator.tabIndex = 0;
if (i === 0) {
indicator.classList.add('active');
}
Expand Down Expand Up @@ -211,6 +212,7 @@ export class Carousel extends Component<CarouselOptions> {
if (this.showIndicators && this._indicators) {
this._indicators.querySelectorAll('.indicator-item').forEach((el) => {
el.addEventListener('click', this._handleIndicatorClick);
el.addEventListener('keypress', this._handleIndicatorKeyPress);
});
}
// Resize
Expand Down Expand Up @@ -352,6 +354,17 @@ export class Carousel extends Component<CarouselOptions> {

_handleIndicatorClick = (e: Event) => {
e.stopPropagation();
this._handleIndicatorInteraction(e);
}

_handleIndicatorKeyPress = (e: KeyboardEvent) => {
e.stopPropagation();
if (Utils.keys.ENTER.includes(e.key)) {
this._handleIndicatorInteraction(e);
}
}

_handleIndicatorInteraction = (e: Event) => {
const indicator = (<HTMLElement>e.target).closest('.indicator-item');
if (indicator) {
const index = [...indicator.parentNode.children].indexOf(indicator);
Expand Down
Loading