Blog

Part 28 · Accessible Components with Angular Aria

A div with a click handler works for a mouse and for nobody else. This part uses Angular Aria to build a listbox, tabs and a menu that work with a keyboard and a screen reader, and styles them with their ARIA attributes.

Some people never touch a mouse. They move around a page with the keyboard, or they listen to it through a screen reader, a program that reads the page out loud and tells them what each thing is. A picker built from <div>s and (click) handlers works for a mouse, and for nobody else.

This part shows what a keyboard and a screen reader need from a widget, and how @angular/aria gives it to you. We build a listbox, tabs and a menu, look at the attributes Angular Aria adds to the HTML, press keys to move around them, and style them with those same attributes. It’s built from directives with host bindings, which we met in Part 11, and its selection is a model() signal from Part 6. Every example here is a whole file you can paste into your practice project from Part 1. Each one was built with Angular 22.3 and run in a browser before this was published.

Try this first

Here’s a size picker made the quick way. Each size is a <div>, and a click sets the size:

// src/app/app.ts
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <p>Pick a size:</p>
    @for (s of sizes; track s) {
      <div class="size" [class.picked]="s === size()" (click)="size.set(s)">{{ s }}</div>
    }
    <p>Size: {{ size() }}</p>
  `,
  styles: `
    .size {
      padding: 4px 8px;
      cursor: pointer;
    }
    .picked {
      background: #dbeafe;
    }
  `,
})
export class App {
  protected readonly sizes = ['Small', 'Medium', 'Large'];
  protected readonly size = signal('Medium');
}

With a mouse, it works. Now put the mouse away. Guess: if you press Tab to reach the picker, then ArrowDown, then Enter, which size is picked?

Press them, and the page shows:

Pick a size:
Small
Medium
Large
Size: Medium

Nothing changed. Tab found nothing to stop on, because a <div> can’t take keyboard focus. So the arrow key and Enter went nowhere. Here’s the HTML the browser has:

<div class="size">Small</div><div class="size picked">Medium</div><div class="size">Large</div>

A screen reader reads the same HTML. It finds three lines of text. Nothing says they’re choices, and nothing says which one is picked. The blue background is only paint.

What a keyboard and a screen reader need

A screen reader asks four questions about each thing on the page:

  • Role: what is it? A button, a list of options, a tab.
  • Name: what’s it called? “Size”, “Account settings”.
  • State: is it selected, expanded, disabled?
  • Keyboard: can I reach it with Tab, and which keys work once I’m there?

Plain HTML answers these for its own elements. A <button> has the role button, takes focus, and clicks on Enter and Space. Build your own widget from <div>s, and you answer them yourself.

You answer them with ARIA (Accessible Rich Internet Applications), a set of HTML attributes. role="listbox" says “this is a list of choices”. role="option" marks each choice. aria-selected="true" says which one is picked. aria-label="Size" gives it a name. These attributes change nothing on the screen. They change what the browser tells a screen reader.

The keys are on you too. A W3C guide, the ARIA Authoring Practices Guide (APG), says how each kind of widget should behave. For a listbox: Tab moves focus into the list and Tab again leaves it, so the whole list is one stop. Inside it, ArrowDown and ArrowUp move between options, Home and End jump to the first and last, and typing a letter jumps to an option that starts with it. Tabs and menus have their own rules.

The usual way to make a group one Tab stop is called roving tabindex. The tabindex attribute decides whether an element takes focus. The option you’re on gets tabindex="0", which puts it in the Tab order. Every other option gets tabindex="-1", which means “can take focus from code, but Tab skips it”. When an arrow key moves you, the 0 moves with you.

That’s a lot to get right for every widget. Angular Aria does it for you.

Angular Aria: behaviour and ARIA, no styles

@angular/aria is a set of headless directives. It’s new: its first stable release came with Angular 21. Headless means they bring the behaviour (roles, states, focus and keys) and no look at all. You write the HTML elements and all the CSS. Install it in your practice project:

npm install @angular/aria@22 @angular/cdk@22

@angular/aria needs @angular/cdk, Angular’s Component Dev Kit, at the same version. Recent npm installs it on its own if you leave it out, but then it isn’t listed in your package.json. Naming both keeps them in step.

Each widget has its own import path. Angular Aria 22.3 ships these: @angular/aria/listbox, tabs, menu (menus and menu bars), accordion, combobox, toolbar, tree and grid. This part uses the first three.

Every piece is a directive you put on your own element, like ngListbox on a <ul>. Inside, each one uses what Part 11 covered: a host with role, [attr.aria-selected] and [attr.tabindex] bindings, and (keydown), (click) and (focusin) listeners.

A listbox

Here’s the size picker again, with Angular Aria. ngListbox goes on the list, ngOption on each choice:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Listbox, Option } from '@angular/aria/listbox';

@Component({
  selector: 'app-root',
  imports: [Listbox, Option],
  template: `
    <p>Pick a size:</p>
    <ul
      ngListbox
      aria-label="Size"
      [(value)]="size"
      (focusin)="onFocus($event)"
      (focusout)="focused.set('outside')"
    >
      @for (s of sizes; track s) {
        <li ngOption [value]="s" [label]="s">{{ s }}</li>
      }
    </ul>
    <p>Size: {{ size().join(', ') }}. Focus: {{ focused() }}</p>
  `,
})
export class App {
  protected readonly sizes = ['Small', 'Medium', 'Large'];
  protected readonly size = signal(['Medium']);
  protected readonly focused = signal('nothing');

  protected onFocus(event: FocusEvent) {
    const target = event.target as HTMLElement;
    this.focused.set(target.textContent.trim());
  }
}

Some things to notice:

  • You import Listbox and Option and list them in imports, like any directive.
  • aria-label="Size" is the listbox’s name. Angular Aria can’t guess it, so you give it.
  • [value]="s" says what each option stands for. [label]="s" is the text used when you type a letter to jump. We’ll see below what happens without it.
  • [(value)]="size" is a two-way binding to the selection. It’s a list, even when you can pick only one: ['Medium'].
  • The (focusin) and (focusout) listeners are only here so the page can show where keyboard focus is. focusin fires when something inside the list gets focus, and event.target is that element.

When the page loads, it shows:

Pick a size:
Small
Medium
Large
Size: Medium. Focus: nothing

Press Tab. Focus goes straight to the selected option:

Pick a size:
Small
Medium
Large
Size: Medium. Focus: Medium

Press ArrowDown. Focus moves to Large, and the selection follows it:

Pick a size:
Small
Medium
Large
Size: Large. Focus: Large

Press ArrowDown again. You’re on the last option, so it wraps round to the first:

Pick a size:
Small
Medium
Large
Size: Small. Focus: Small

End jumps to the last option, Home to the first. Press End, then type the letter m:

Pick a size:
Small
Medium
Large
Size: Medium. Focus: Medium

Typing a letter jumped to Medium. Now press Tab once more. Focus leaves the whole list in one step, and the selection stays:

Pick a size:
Small
Medium
Large
Size: Medium. Focus: outside

We wrote no key handling at all. A mouse still works too, as we’ll see after a look at the HTML.

What it added to the HTML

Open the browser’s developer tools (F12) and look at the Elements tab. Angular Aria put these attributes on the <ul>:

<ul role="listbox" nglistbox="" aria-label="Size" id="ng-listbox-a12345-0"
    tabindex="-1" aria-readonly="false" aria-disabled="false"
    aria-orientation="vertical" aria-multiselectable="false">

and these on the selected option, Medium:

<li role="option" ngoption="" data-active="true" id="ng-option-a12345-1"
    tabindex="0" aria-selected="true" aria-disabled="false">Medium</li>

The other two options have data-active="false", tabindex="-1" and aria-selected="false". (The id values are made up by Angular Aria, so yours will have different numbers. We split the lines here to fit.) Here’s what each one tells the browser:

  • role="listbox" and role="option": what they are.
  • aria-selected: which option is picked. A screen reader says “selected” on Medium.
  • tabindex="0" on one option, -1 on the rest: roving tabindex. The <ul> itself has -1, so Tab lands on the option, not the list.
  • aria-orientation="vertical": which arrow keys to expect, up and down.
  • aria-multiselectable="false": only one option can be picked.
  • data-active isn’t ARIA. It’s an extra attribute Angular Aria adds to mark the option you’re on. We’ll use it for styling.

All of these are host bindings, so they update when the state changes. Click Large. The click selects it and moves focus to it:

Pick a size:
Small
Medium
Large
Size: Large. Focus: Large

Now Large has tabindex="0" and aria-selected="true", and Medium has -1 and false:

The selection is a model signal

value on ngListbox is a model(), the two-way input from Part 6. The listbox writes to your signal when the selection changes, and if your code sets the signal, the listbox follows. Your component never asks the listbox what’s selected; it reads its own signal.

Roles and keyboard paths

Here are the listbox and the tabs from the next section side by side. The highlighted item in each is the selected one, and the only one with tabindex="0".

role=listbox Small Medium Large Tab ArrowDown ArrowDown Tab: leave the list role=option on each item Medium: aria-selected=true, tabindex=0 role=tablist Profile Billing Alerts ArrowRight Tab role=tabpanel, tabindex=0 Profile panel Profile: aria-selected=true, tabindex=0 Black: Tab moves between widgets. Each widget is one Tab stop. Blue: arrow keys move inside a widget. Home and End jump to the ends. Highlighted: the selected item, and the only one Tab can land on.

Tab moves from widget to widget. Arrow keys move inside one. The roles and states tell a screen reader what it landed on.

Styling with the ARIA attributes

Angular Aria adds no CSS, so the list above still looks like a plain bulleted list. The best way to style it is with the attributes it already sets, as CSS selectors. Then the look can’t disagree with what a screen reader hears.

This example is a topping picker where you can pick several. It also changes how selection works:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Listbox, Option } from '@angular/aria/listbox';

@Component({
  selector: 'app-root',
  imports: [Listbox, Option],
  template: `
    <p>Toppings:</p>
    <ul ngListbox aria-label="Toppings" [(value)]="toppings" multi selectionMode="explicit">
      @for (t of all; track t) {
        <li ngOption [value]="t" [label]="t">{{ t }}</li>
      }
    </ul>
    <p>Chosen: {{ toppings().join(', ') || 'none' }}</p>
  `,
  styles: `
    [ngListbox] {
      list-style: none;
      padding: 4px;
      border: 1px solid #94a3b8;
      width: 12rem;
    }
    [ngOption] {
      padding: 4px 8px;
      cursor: pointer;
    }
    [ngOption][aria-selected='true'] {
      background: #dbeafe;
      font-weight: bold;
    }
    [ngOption][data-active='true'] {
      outline: 2px solid #2563eb;
    }
  `,
})
export class App {
  protected readonly all = ['Cheese', 'Olives', 'Peppers'];
  protected readonly toppings = signal<string[]>([]);
}

Two new settings on the listbox:

  • multi lets you pick more than one. A plain attribute with no value means true here.
  • selectionMode="explicit" means moving doesn’t select. In the size picker, the selection followed focus, which is right when you pick exactly one. With several, you move with the arrow keys and press Space or Enter to select or unselect the option you’re on.

Press Tab, then Space. Press ArrowDown twice, then Enter. The page shows:

Toppings:
Cheese
Olives
Peppers
Chosen: Cheese, Peppers

You walked past Olives without selecting it. The HTML now says so too: the list has aria-multiselectable="true", Cheese and Peppers have aria-selected="true", and Olives has false. Peppers, where you are, has data-active="true":

Now the CSS. [ngOption] matches every element with the ngOption attribute. [ngOption][aria-selected='true'] matches only the selected ones, and they turn blue and bold. [ngOption][data-active='true'] draws an outline round the option you’re on. Without it, a keyboard user moves through the list blind.

Because the options are written in this component’s template, emulated encapsulation from Part 7 scopes these rules to them, as with any other element.

Tabs

Tabs follow the same idea with four directives: ngTabs round the whole thing, ngTabList round the tab buttons, ngTab on each button, and ngTabPanel on each panel. A tab and its panel are linked by the same value:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Tab, TabContent, TabList, TabPanel, Tabs } from '@angular/aria/tabs';

@Component({
  selector: 'app-root',
  imports: [Tabs, TabList, Tab, TabPanel, TabContent],
  template: `
    <div ngTabs>
      <div ngTabList aria-label="Account" [(selectedTab)]="tab">
        <button ngTab value="profile">Profile</button>
        <button ngTab value="billing">Billing</button>
        <button ngTab value="alerts">Alerts</button>
      </div>
      <div ngTabPanel value="profile">
        <ng-template ngTabContent>Your name and photo.</ng-template>
      </div>
      <div ngTabPanel value="billing">
        <ng-template ngTabContent>Your card ends in 4242.</ng-template>
      </div>
      <div ngTabPanel value="alerts">
        <ng-template ngTabContent>Email me about replies.</ng-template>
      </div>
    </div>
    <p>Selected tab: {{ tab() }}</p>
    <button (click)="tab.set('alerts')">Show alerts</button>
  `,
  styles: `
    [ngTabList] {
      display: flex;
      gap: 4px;
      border-bottom: 1px solid #94a3b8;
    }
    [ngTab] {
      border: none;
      background: none;
      padding: 6px 12px;
      cursor: pointer;
    }
    [ngTab][aria-selected='true'] {
      border-bottom: 3px solid #2563eb;
      font-weight: bold;
    }
    [ngTabPanel] {
      padding: 12px 4px;
    }
  `,
})
export class App {
  protected readonly tab = signal<string | undefined>('profile');
}

The panel’s content sits in an <ng-template ngTabContent>. An <ng-template> isn’t drawn until something asks for it (Part 9), so only the selected panel’s content is on the page. selectedTab is a model, like the listbox’s value, and its type is string | undefined: a tab’s value, or nothing. Typing your signal string | undefined too is honest, because the tab list can write undefined into it.

When the page loads, it shows:

Profile
Billing
Alerts
Your name and photo.
Selected tab: profile
Show alerts

The three tabs are buttons side by side, so the page text runs them together as ProfileBillingAlerts. Press Tab to land on Profile, then ArrowRight:

Profile
Billing
Alerts
Your card ends in 4242.
Selected tab: billing
Show alerts

Tabs sit in a row, so they use ArrowRight and ArrowLeft instead of up and down. As in the size picker, the selection follows focus. Press End, then Tab. End selects Alerts, and Tab moves on to its panel, not to the next tab:

Profile
Billing
Alerts
Email me about replies.
Selected tab: alerts
Show alerts

Here’s the HTML for the selected tab and its panel:

<button role="tab" ngtab="" value="alerts" type="button" data-active="true"
    id="ng-tab-a12345-2" tabindex="0" aria-selected="true" aria-disabled="false"
    aria-controls="ng-tabpanel-a12345-2">Alerts</button>
<div role="tabpanel" ngtabpanel="" value="alerts" id="ng-tabpanel-a12345-2"
    tabindex="0" aria-labelledby="ng-tab-a12345-2">Email me about replies.</div>

aria-controls on the tab points at its panel’s id, and aria-labelledby on the panel points back at the tab, so a screen reader can name the panel “Alerts”. The panels that aren’t selected are empty and get an inert attribute, which takes them out of focus and out of what a screen reader reads:

<div role="tabpanel" ngtabpanel="" value="profile" id="ng-tabpanel-a12345-0"
    tabindex="-1" aria-labelledby="ng-tab-a12345-0" inert="true"></div>

The model works from your side too. Go back to Profile with Shift+Tab and Home, then click Show alerts, which only sets the signal:

Profile
Billing
Alerts
Email me about replies.
Selected tab: alerts
Show alerts

The Alerts tab is selected and its panel shows. The tabs followed the signal. A click on a tab works as you’d expect. Click Billing:

Profile
Billing
Alerts
Your card ends in 4242.
Selected tab: billing
Show alerts

A menu

A menu is a list of actions that opens from a button. Three directives: ngMenuTrigger on the button, ngMenu on the list, ngMenuItem on each action. The trigger finds its menu through a template reference variable, and the menu tells you which item was chosen with an output, itemSelected:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@angular/aria/menu';

@Component({
  selector: 'app-root',
  imports: [Menu, MenuContent, MenuItem, MenuTrigger],
  template: `
    <div (focusin)="onFocus($event)">
      <button ngMenuTrigger [menu]="actions">Actions</button>
      <div ngMenu #actions="ngMenu" (itemSelected)="onPick($event)">
        <ng-template ngMenuContent>
          <div ngMenuItem value="Copy">Copy</div>
          <div ngMenuItem value="Rename">Rename</div>
          <div ngMenuItem value="Delete">Delete</div>
        </ng-template>
      </div>
    </div>
    <p>Picked: {{ picked() }}. Focus: {{ focused() }}</p>
  `,
  styles: `
    [ngMenu][data-visible='false'] {
      display: none;
    }
    [ngMenuItem][data-active='true'] {
      background: #dbeafe;
    }
  `,
})
export class App {
  protected readonly picked = signal('nothing');
  protected readonly focused = signal('nothing');

  protected onPick(value: string | undefined) {
    this.picked.set(value ?? 'nothing');
  }

  protected onFocus(event: FocusEvent) {
    const target = event.target as HTMLElement;
    this.focused.set(target.textContent.trim());
  }
}

#actions="ngMenu" names the menu directive, using its exportAs name (Part 11 covered exportAs), and [menu]="actions" hands it to the trigger. value ?? 'nothing' means “value, or 'nothing' if it’s undefined“.

The items sit in an <ng-template ngMenuContent>, like ngTabContent in the tabs, so a closed menu’s items aren’t on the page at all. They’re created when the menu opens. angular.dev’s menu examples do the same.

Now look at the first CSS rule. Angular Aria doesn’t hide the closed menu itself. It sets data-visible="false" on it, and your CSS decides what closed looks like. Here it’s display: none, so the empty box doesn’t show either. (In a real app you’d also place the menu over the page, for example with the CDK overlay; angular.dev’s menu guide shows how.)

When the page loads, the menu is hidden:

Actions
Picked: nothing. Focus: nothing

and the menu in the HTML is empty:

<div role="menu" ngmenu="" id="ng-menu-a12345-0" aria-disabled="false" tabindex="0" data-visible="false"></div>

Press Tab to reach Actions, then Enter. The menu opens, and focus goes to its first item:

Actions
Copy
Rename
Delete
Picked: nothing. Focus: Copy

The trigger now says the menu is open:

<button ngmenutrigger="" type="button" tabindex="-1" aria-disabled="false"
    aria-haspopup="true" aria-expanded="true" aria-controls="ng-menu-a12345-0">Actions</button>
<div role="menu" ngmenu="" id="ng-menu-a12345-0" aria-disabled="false"
    tabindex="-1" data-visible="true">
  <div ngmenuitem="" value="Copy" role="menuitem" tabindex="0" data-active="true"
      aria-haspopup="false" aria-disabled="false">Copy</div>

aria-haspopup="true" tells a screen reader the button opens something, and aria-expanded says whether it’s open now. Press ArrowDown, then Enter:

Actions
Picked: Rename. Focus: Actions

Enter chose Rename, itemSelected sent its value, and the menu closed. Focus went back to Actions, so the keyboard user is where they started. Open it again with ArrowDown, which also opens a menu from its trigger, then press Escape:

Actions
Copy
Rename
Delete
Picked: Rename. Focus: Copy
Actions
Picked: Rename. Focus: Actions

Escape closes the menu without choosing anything, and focus goes back to the trigger again.

The mouse works the same way. Click Actions, then Delete:

Actions
Picked: Delete. Focus: Actions

The click chose Delete and closed the menu.

Worked example: a picker that builds and does nothing

You write the size picker, and to keep things short you import only Listbox:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Listbox } from '@angular/aria/listbox';

@Component({
  selector: 'app-root',
  imports: [Listbox],
  template: `
    <ul ngListbox aria-label="Size" [(value)]="size">
      @for (s of sizes; track s) {
        <li ngOption [value]="s">{{ s }}</li>
      }
    </ul>
    <p>Size: {{ size().join(', ') }}</p>
  `,
})
export class App {
  protected readonly sizes = ['Small', 'Medium', 'Large'];
  protected readonly size = signal(['Medium']);
}

It builds with no error and no warning. But the page shows:

Small
Medium
Large
Size:

Medium was selected, and now nothing is. Press Tab and ArrowDown, and nothing moves either:

Small
Medium
Large
Size:

The HTML tells you why. The options have no role at all:

<li ngoption="" value="0">Small</li><li ngoption="" value="0">Medium</li><li ngoption="" value="0">Large</li>

Without Option in imports, Angular doesn’t know ngOption is a directive. It’s just an attribute. And why did [value]="s" build? Because an <li> has its own value property: in an ordered list, it sets the item’s number. So Angular bound 'Small' to that, the browser turned it into the number 0, and nothing complained.

So the listbox has no options. It removes any selected value that no option has, which is why Medium vanished from the signal. (That’s useful when an option you had selected is taken out of the list.) It compares values with ===, so if your option values are objects, the selected ones must be the same objects, not copies.

When a widget looks right but ignores the keyboard, check the rendered HTML for role= first. No role usually means a missing import. Add Option to the import line and to imports, and it works.

Now a real build error. You fix the import, and decide a single choice should be a single string:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Listbox, Option } from '@angular/aria/listbox';

@Component({
  selector: 'app-root',
  imports: [Listbox, Option],
  template: `
    <ul ngListbox aria-label="Size" [(value)]="size">
      @for (s of sizes; track s) {
        <li ngOption [value]="s">{{ s }}</li>
      }
    </ul>
  `,
})
export class App {
  protected readonly sizes = ['Small', 'Medium', 'Large'];
  protected readonly size = signal('Medium');
}

ng serve stops:

✘ [ERROR] TS2322: Type 'string' is not assignable to type 'any[]'. [plugin angular-compiler]
      9 │     <ul ngListbox aria-label="Size" [(value)]="size">

Read it piece by piece:

  • TS2322 is TypeScript’s “this type doesn’t fit there” error.
  • Type 'string' is not assignable to type 'any[]' says your signal holds a string, and the binding wants an array. any[] is “an array of anything”. The listbox’s value is a list of whatever your options’ values are, and Angular can’t narrow that down here, so it says any.
  • The ~~~~ under [(value)] in the real output points at the binding, not at the signal. The fix goes in the class, though.

The listbox’s value is always a list, even for one choice. Write signal(['Medium']), and read it in the template with size()[0] or size().join(', ').

Worked example: a tab whose panel has a typo

This settings page has two tabs. It builds, and it even looks right:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Tab, TabContent, TabList, TabPanel, Tabs } from '@angular/aria/tabs';

@Component({
  selector: 'app-root',
  imports: [Tabs, TabList, Tab, TabPanel, TabContent],
  template: `
    <div ngTabs>
      <div ngTabList aria-label="Account" [(selectedTab)]="tab">
        <button ngTab value="profile">Profile</button>
        <button ngTab value="billing">Billing</button>
      </div>
      <div ngTabPanel value="profile">
        <ng-template ngTabContent>Your name and photo.</ng-template>
      </div>
      <div ngTabPanel value="biling">
        <ng-template ngTabContent>Your card ends in 4242.</ng-template>
      </div>
    </div>
  `,
})
export class App {
  protected readonly tab = signal<string | undefined>('profile');
}

Open the Console tab in the developer tools. When the page loads, Angular Aria has written four warnings:

Violations found on element: <button role="tab" ...>:
ngTab with value 'billing' does not have a corresponding ngTabPanel.
Violations found on element: <div role="tabpanel" ...>:
ngTabPanel with value 'biling' does not have a corresponding ngTab.

Each pair names an element (shortened here; in the console you can click it to find it on the page), then says what’s wrong with it. The Billing tab has no panel with the value billing, and the panel biling has no tab. That’s one typo, seen from both sides.

Why does it look right? Press Tab and ArrowRight. The page shows:

ProfileBilling
Your card ends in 4242.

The card shows up under Billing. But the HTML is missing the links between them. The Billing tab has no aria-controls, and its panel has no aria-labelledby:

<button role="tab" ngtab="" value="billing" ... tabindex="0" aria-selected="true" aria-disabled="false">Billing</button>
<div role="tabpanel" ngtabpanel="" value="biling" ... tabindex="0">Your card ends in 4242.</div>

A mouse user sees nothing wrong. A screen reader user reaches a panel with no name. Only the console and the HTML show this kind of bug, so read Angular Aria’s warnings. Fix the panel’s value to billing, and they go away.

Testing it for real

Everything we checked in this part is the HTML and the keys: the roles and states are there, focus moves, the selection changes. Tests can check that too (Part 24 covered component tests). None of it tells you what a person hears.

For that, use a screen reader yourself: VoiceOver on a Mac or iPhone, Narrator or the free NVDA on Windows, TalkBack on Android. Turn one on, close your eyes, and try to pick a size. Screen readers and browsers differ, so try more than one if you can. The browser’s developer tools also have an Accessibility panel that shows each element’s role, name and state. Angular Aria makes the right thing much easier. It doesn’t replace trying it.

In older code

Before Angular Aria, the CDK had its own accessible building blocks, such as cdkListbox and cdkOption from @angular/cdk/listbox and cdkMenu from @angular/cdk/menu. You’ll meet them in existing projects; they’re still in the CDK. Angular Material’s components, like mat-select and mat-tab-group, give you a finished look as well as the behaviour. Pick Material when you want its look, and Angular Aria when you want your own.

Best practices

  • Reach for a native element first. A <button>, <select> or <input type="checkbox"> has its role and keys built in. Use Angular Aria when no native element fits, instead of building a <div> widget by hand.
  • Give every widget a name. Put aria-label (or aria-labelledby) on the listbox, tab list or menu. Angular Aria sets roles and states, but it can’t know what your widget is called.
  • Bind the selection to a signal with [( )]. The listbox’s value and the tab list’s selectedTab are models. Your component reads its own signal, instead of asking the widget what’s selected.
  • Style with the attributes Angular Aria sets. [aria-selected='true'], [data-active='true'], [data-visible='false']. The look then always matches what a screen reader hears, instead of a separate .picked class you keep in step by hand.
  • Always show where focus is. Style data-active or :focus-visible so keyboard users can see the option or item they’re on.

Tips and traps

  • Forgetting to import Option (or Tab, or MenuItem). It builds, it renders, and the keys do nothing. The rendered HTML has no role="option". Add the directive to imports.
  • A single value for the listbox’s value. The build fails with TS2322, Type 'string' is not assignable to type 'any[]'. The selection is always a list: signal(['Medium']).
  • Typing a letter does nothing. Typeahead searches each option’s label input, not its text. Without [label], there’s nothing to match. Add [label]="s".
  • A menu that never hides. Angular Aria only sets data-visible. Add [ngMenu][data-visible='false'] { display: none; } or place the menu in an overlay, and wrap the items in <ng-template ngMenuContent> so a closed menu’s items aren’t on the page.
  • A tab panel’s value that doesn’t match its tab. The page can look fine, but the console shows ngTab with value '...' does not have a corresponding ngTabPanel. Make the two values the same.
  • A tab panel without <ng-template ngTabContent>. The console says ngTabPanel must have an ngTabContent structural directive to render., and the panel’s content stays on the page even when its tab isn’t selected. Wrap the content in the template.
  • Two selected options in a single-select listbox. Starting with signal(['Small', 'Medium']) warns A single-select listbox should not have multiple selected options. Add multi, or start with one.

A widget is accessible when its roles, states and keys match what it looks like, and Angular Aria writes those for you while you write the look.

Exercises

Exercise 1. This size picker ignores typed letters. Paste it, press Tab, then type l. The selection stays on Small. Fix it so typing l jumps to Large.

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Listbox, Option } from '@angular/aria/listbox';

@Component({
  selector: 'app-root',
  imports: [Listbox, Option],
  template: `
    <ul ngListbox aria-label="Size" [(value)]="size">
      @for (s of sizes; track s) {
        <li ngOption [value]="s">{{ s }}</li>
      }
    </ul>
    <p>Size: {{ size().join(', ') }}</p>
  `,
})
export class App {
  protected readonly sizes = ['Small', 'Medium', 'Large'];
  protected readonly size = signal(['Small']);
}
Small
Medium
Large
Size: Small

Exercise 2. Turn the tabs from the Tabs section into vertical tabs down the left side. Which input on ngTabList do you change, and which keys then move between tabs? Check that ArrowDown from Profile selects Billing, and that the tab list says aria-orientation="vertical". Hint: look at the listbox’s aria-orientation.

Exercise 3. Add a fourth action, Archive, to the menu, and make it unavailable for now with [disabled]="true". Open the menu and go to Archive with End. Is it skipped, or can you land on it? What happens when you press Enter? What does its HTML say?

Answers

1. Typeahead matches each option’s label. Give every option one:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Listbox, Option } from '@angular/aria/listbox';

@Component({
  selector: 'app-root',
  imports: [Listbox, Option],
  template: `
    <ul ngListbox aria-label="Size" [(value)]="size">
      @for (s of sizes; track s) {
        <li ngOption [value]="s" [label]="s">{{ s }}</li>
      }
    </ul>
    <p>Size: {{ size().join(', ') }}</p>
  `,
})
export class App {
  protected readonly sizes = ['Small', 'Medium', 'Large'];
  protected readonly size = signal(['Small']);
}

Press Tab, then type l. The page shows:

Small
Medium
Large
Size: Large

The label can differ from the text. A “Large (sold out)” option could still have label="Large".

2. Set orientation="vertical" on ngTabList. The keys become ArrowDown and ArrowUp. Make the tab list a column with CSS, and put it beside the panels:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Tab, TabContent, TabList, TabPanel, Tabs } from '@angular/aria/tabs';

@Component({
  selector: 'app-root',
  imports: [Tabs, TabList, Tab, TabPanel, TabContent],
  template: `
    <div ngTabs class="side">
      <div ngTabList aria-label="Account" orientation="vertical" [(selectedTab)]="tab">
        <button ngTab value="profile">Profile</button>
        <button ngTab value="billing">Billing</button>
        <button ngTab value="alerts">Alerts</button>
      </div>
      <div ngTabPanel value="profile">
        <ng-template ngTabContent>Your name and photo.</ng-template>
      </div>
      <div ngTabPanel value="billing">
        <ng-template ngTabContent>Your card ends in 4242.</ng-template>
      </div>
      <div ngTabPanel value="alerts">
        <ng-template ngTabContent>Email me about replies.</ng-template>
      </div>
    </div>
    <p>Selected tab: {{ tab() }}</p>
  `,
  styles: `
    .side {
      display: flex;
      gap: 16px;
    }
    [ngTabList] {
      display: flex;
      flex-direction: column;
      border-right: 1px solid #94a3b8;
    }
    [ngTab][aria-selected='true'] {
      font-weight: bold;
    }
  `,
})
export class App {
  protected readonly tab = signal<string | undefined>('profile');
}

Press Tab, then ArrowDown. The page shows:

Profile
Billing
Alerts
Your card ends in 4242.
Selected tab: billing

The panels that aren’t selected are still in the flex row, but empty, so only the selected one takes up width (the gap still spaces them out).

3. You can land on it. By default, Angular Aria lets disabled items take focus, so a screen reader user still hears that Archive exists and that it’s unavailable, instead of it vanishing. Enter does nothing, and the menu stays open:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { Menu, MenuItem, MenuTrigger } from '@angular/aria/menu';

@Component({
  selector: 'app-root',
  imports: [Menu, MenuItem, MenuTrigger],
  template: `
    <button ngMenuTrigger [menu]="actions">Actions</button>
    <div ngMenu #actions="ngMenu" (itemSelected)="picked.set($event ?? 'nothing')">
      <div ngMenuItem value="Copy">Copy</div>
      <div ngMenuItem value="Rename">Rename</div>
      <div ngMenuItem value="Delete">Delete</div>
      <div ngMenuItem value="Archive" [disabled]="true">Archive</div>
    </div>
    <p>Picked: {{ picked() }}</p>
  `,
  styles: `
    [ngMenu][data-visible='false'] {
      display: none;
    }
    [ngMenuItem][aria-disabled='true'] {
      color: #94a3b8;
    }
  `,
})
export class App {
  protected readonly picked = signal('nothing');
}

Press Tab, Enter, End and Enter. The page shows:

Actions
Copy
Rename
Delete
Archive
Picked: nothing

and Archive’s HTML says it’s unavailable and focused:

The grey colour comes from the [aria-disabled='true'] rule, the same attribute a screen reader reads. To skip disabled items instead, set [softDisabled]="false" on the ngMenu.

What’s next

Our pages so far were all built in the browser, from an empty <app-root>. In Part 29, we render them on the server first with ng new --ssr, and use incremental hydration to make each part of the page interactive only when someone needs it.

How useful was this post?

Click on a heart to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.