Skip to content

Commit

Permalink
accessibility(Carousel) implement keyboard accessibility on indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
gselderslaghs committed Dec 11, 2024
1 parent ecb57d3 commit 4775948
Showing 1 changed file with 13 additions and 0 deletions.
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 (e.keyCode === 13) {
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

0 comments on commit 4775948

Please sign in to comment.