Skip to content

Commit

Permalink
fix(menu): trigger should aria-owns panel
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba committed Oct 11, 2019
1 parent 43af4c2 commit ba9a41a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/material/menu/menu-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface MatMenuPanel<T = any> {
lazyContent?: MatMenuContent;
backdropClass?: string;
hasBackdrop?: boolean;
readonly panelId?: string;

/**
* @deprecated To be removed.
Expand Down
1 change: 1 addition & 0 deletions src/material/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const passiveEventListenerOptions = normalizePassiveListenerOptions({passive: tr
'class': 'mat-menu-trigger',
'aria-haspopup': 'true',
'[attr.aria-expanded]': 'menuOpen || null',
'[attr.aria-owns]': 'menuOpen ? menu.panelId : null',
'(mousedown)': '_handleMousedown($event)',
'(keydown)': '_handleKeydown($event)',
'(click)': '_handleClick($event)',
Expand Down
1 change: 1 addition & 0 deletions src/material/menu/menu.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<ng-template>
<div
class="mat-menu-panel"
[id]="panelId"
[ngClass]="_classList"
(keydown)="_handleKeydown($event)"
(click)="closed.emit('click')"
Expand Down
9 changes: 9 additions & 0 deletions src/material/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ describe('MatMenu', () => {
overlayContainer.ngOnDestroy();
}));

it('should aria-owns the menu panel', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
expect(fixture.componentInstance.triggerEl.nativeElement.getAttribute('aria-owns'))
.toBe(fixture.componentInstance.menu.panelId);
});

it('should open the menu as an idempotent operation', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
Expand Down
4 changes: 4 additions & 0 deletions src/material/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export function MAT_MENU_DEFAULT_OPTIONS_FACTORY(): MatMenuDefaultOptions {
*/
const MAT_MENU_BASE_ELEVATION = 4;

let menuPanelUid = 0;

/** Base class with all of the `MatMenu` functionality. */
@Directive({
// TODO(devversion): this selector can be removed when we update to Angular 9.0.
Expand Down Expand Up @@ -237,6 +239,8 @@ export class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatMenuItem>
*/
@Output() close = this.closed;

readonly panelId = `mat-menu-panel-${menuPanelUid++}`;

constructor(
private _elementRef: ElementRef<HTMLElement>,
private _ngZone: NgZone,
Expand Down

0 comments on commit ba9a41a

Please sign in to comment.