Skip to content

Commit 0fdc5a6

Browse files
crisbetovictoriaaa234
authored andcommitted
fix(menu): toggle aria-expanded attribute on menu trigger (#11751)
[Based on the accessibility guidelines](https://www.w3.org/TR/wai-aria-practices-1.1/#menubutton), buttons that trigger a menu should have the `aria-expanded` attribute.
1 parent 9d613c7 commit 0fdc5a6

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/lib/menu/menu-trigger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export const MENU_PANEL_TOP_PADDING = 8;
7070
selector: `[mat-menu-trigger-for], [matMenuTriggerFor]`,
7171
host: {
7272
'aria-haspopup': 'true',
73+
'[attr.aria-expanded]': 'menuOpen || null',
7374
'(mousedown)': '_handleMousedown($event)',
7475
'(keydown)': '_handleKeydown($event)',
7576
'(click)': '_handleClick($event)',

src/lib/menu/menu.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,24 @@ describe('MatMenu', () => {
388388
expect(items[2].classList).toContain('cdk-keyboard-focused');
389389
}));
390390

391+
it('should toggle the aria-expanded attribute on the trigger', () => {
392+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
393+
fixture.detectChanges();
394+
const triggerEl = fixture.componentInstance.triggerEl.nativeElement;
395+
396+
expect(triggerEl.hasAttribute('aria-expanded')).toBe(false);
397+
398+
fixture.componentInstance.trigger.openMenu();
399+
fixture.detectChanges();
400+
401+
expect(triggerEl.getAttribute('aria-expanded')).toBe('true');
402+
403+
fixture.componentInstance.trigger.closeMenu();
404+
fixture.detectChanges();
405+
406+
expect(triggerEl.hasAttribute('aria-expanded')).toBe(false);
407+
});
408+
391409
describe('lazy rendering', () => {
392410
it('should be able to render the menu content lazily', fakeAsync(() => {
393411
const fixture = createComponent(SimpleLazyMenu);

0 commit comments

Comments
 (0)