Skip to content

Commit 405f5d0

Browse files
authored
fix(menu): trigger should aria-controls panel (#17365)
* fix(menu): trigger should aria-controls panel * update api golden * update mdc-based menu
1 parent aa826e9 commit 405f5d0

File tree

8 files changed

+28
-0
lines changed

8 files changed

+28
-0
lines changed

src/material-experimental/mdc-menu/menu.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<ng-template>
22
<div
33
class="mat-mdc-menu-panel mdc-menu-surface mdc-menu-surface--open"
4+
[id]="panelId"
45
[ngClass]="_classList"
56
(keydown)="_handleKeydown($event)"
67
(click)="closed.emit('click')"

src/material-experimental/mdc-menu/menu.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ describe('MatMenu', () => {
7474
overlayContainer.ngOnDestroy();
7575
}));
7676

77+
it('should aria-controls the menu panel', () => {
78+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
79+
fixture.detectChanges();
80+
fixture.componentInstance.trigger.openMenu();
81+
fixture.detectChanges();
82+
expect(fixture.componentInstance.triggerEl.nativeElement.getAttribute('aria-controls'))
83+
.toBe(fixture.componentInstance.menu.panelId);
84+
});
85+
7786
it('should open the menu as an idempotent operation', () => {
7887
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
7988
fixture.detectChanges();

src/material/menu/menu-panel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface MatMenuPanel<T = any> {
3737
lazyContent?: MatMenuContent;
3838
backdropClass?: string;
3939
hasBackdrop?: boolean;
40+
readonly panelId?: string;
4041

4142
/**
4243
* @deprecated To be removed.

src/material/menu/menu-trigger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const passiveEventListenerOptions = normalizePassiveListenerOptions({passive: tr
7676
'class': 'mat-menu-trigger',
7777
'aria-haspopup': 'true',
7878
'[attr.aria-expanded]': 'menuOpen || null',
79+
'[attr.aria-controls]': 'menuOpen ? menu.panelId : null',
7980
'(mousedown)': '_handleMousedown($event)',
8081
'(keydown)': '_handleKeydown($event)',
8182
'(click)': '_handleClick($event)',

src/material/menu/menu.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<ng-template>
22
<div
33
class="mat-menu-panel"
4+
[id]="panelId"
45
[ngClass]="_classList"
56
(keydown)="_handleKeydown($event)"
67
(click)="closed.emit('click')"

src/material/menu/menu.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ describe('MatMenu', () => {
7676
overlayContainer.ngOnDestroy();
7777
}));
7878

79+
it('should aria-controls the menu panel', () => {
80+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
81+
fixture.detectChanges();
82+
fixture.componentInstance.trigger.openMenu();
83+
fixture.detectChanges();
84+
expect(fixture.componentInstance.triggerEl.nativeElement.getAttribute('aria-controls'))
85+
.toBe(fixture.componentInstance.menu.panelId);
86+
});
87+
7988
it('should open the menu as an idempotent operation', () => {
8089
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
8190
fixture.detectChanges();

src/material/menu/menu.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export function MAT_MENU_DEFAULT_OPTIONS_FACTORY(): MatMenuDefaultOptions {
9090
*/
9191
const MAT_MENU_BASE_ELEVATION = 4;
9292

93+
let menuPanelUid = 0;
94+
9395
/** Base class with all of the `MatMenu` functionality. */
9496
@Directive({
9597
// TODO(devversion): this selector can be removed when we update to Angular 9.0.
@@ -237,6 +239,8 @@ export class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatMenuItem>
237239
*/
238240
@Output() close = this.closed;
239241

242+
readonly panelId = `mat-menu-panel-${menuPanelUid++}`;
243+
240244
constructor(
241245
private _elementRef: ElementRef<HTMLElement>,
242246
private _ngZone: NgZone,

tools/public_api_guard/material/menu.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export declare class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatM
2020
lazyContent: MatMenuContent;
2121
overlapTrigger: boolean;
2222
panelClass: string;
23+
readonly panelId: string;
2324
parentMenu: MatMenuPanel | undefined;
2425
templateRef: TemplateRef<any>;
2526
xPosition: MenuPositionX;
@@ -105,6 +106,7 @@ export interface MatMenuPanel<T = any> {
105106
hasBackdrop?: boolean;
106107
lazyContent?: MatMenuContent;
107108
overlapTrigger: boolean;
109+
readonly panelId?: string;
108110
parentMenu?: MatMenuPanel | undefined;
109111
removeItem?: (item: T) => void;
110112
resetActiveItem: () => void;

0 commit comments

Comments
 (0)