Question:
How to make the dropdown for this button ADA accessible in Angular?

Solution

With the help of Angular's HostListener you can listen to keyboard events and manipulate the focus.


To make the mat-checkbox more focused, add a tabIndex. Then, implement HostListener in your component to receive keydown event notifications.


Upon pressing arrow keys, adjust the checkboxes' focus correspondingly.


The following is the HTML code:

<mat-menu #settings="matMenu">

    <div *ngFor="let id of arraycolumns; let i = index">

        <div *ngIf="![‘Cashless', 'Borrower', 'options', 'isfavorite', 'Progress', 'Deal Name'].includes(id)" mat-menu-item>

            <mat-checkbox class="chkbx" #singleCheckbox [checked]='fieldcolumnsChecked.indexOf(id) >= 0' (change)="checkcolumnsChanged(id)" (click)="$event.stopPropagation();" [tabIndex]="i">

                {{id}} 

            </mat-checkbox>

        </div> 

    </div> 

</mat-menu>


Use Typescript code like the following:

import { HostListener, QueryList, ViewChildren } from '@angular/core';

import { MatCheckbox } from '@angular/material/checkbox';


// ... (other imports)


export class YourComponent {


    @ViewChildren("singleCheckbox") checkboxes!: QueryList<MatCheckbox>;


    currentFocusIndex = 0;


    // ...


    @HostListener('document:keydown', ['$event'])

    handleKeyboardEvent(event: KeyboardEvent) {

        if (this.checkboxes && this.checkboxes.length) {

            if (event.key === 'ArrowDown') {

                event.preventDefault();

                this.setFocus(true);

            } else if (event.key === 'ArrowUp') {

                event.preventDefault();

                this.setFocus(false);

            }

        }

    }


    setFocus(goDown: boolean) {

        if (goDown && this.currentFocusIndex < this.checkboxes.length - 1) {

            this.currentFocusIndex++;

        } else if (!goDown && this.currentFocusIndex > 0) {

            this.currentFocusIndex--;

        }

        const checkboxArray = this.checkboxes.toArray();

        checkboxArray[this.currentFocusIndex].focus();

    }


    // ... (rest of your code)

}


Suggested blogs:

>What is the Idiomatic way to write a Typescript function with casting

>Why Redux-toolkit payload don't reconigze type in TypeScript?

>TypeScript: Define a type that refers type parameter from another line?

>Solve return type on TypeScript with boolean check doesn't seem to work

>How to fix TS2551: Property toSpliced does not exist on type Array

>Making an argument required if generic interface as no properties

>How to make argument required if generic interface as no properties?

>Cause a type error when a function returns any in TypeScript?

>Library isn’t work in Nestjs useGlobalFilters with library in TypeScript

>How to get the last cell with data for a given column in TypeScript?

>Ignore requests by interceptors based on request content in TypeScript?

>Create data with Typescript Sequelize model without passing in an id?

>How to delete duplicate names from Array in Typescript?


Nisha Patel

Nisha Patel

Submit
0 Answers