Skip to content

fix(menu): trigger should aria-controls panel #17365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/material-experimental/mdc-menu/menu.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<ng-template>
<div
class="mat-mdc-menu-panel mdc-menu-surface mdc-menu-surface--open"
[id]="panelId"
[ngClass]="_classList"
(keydown)="_handleKeydown($event)"
(click)="closed.emit('click')"
Expand Down
9 changes: 9 additions & 0 deletions src/material-experimental/mdc-menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ describe('MatMenu', () => {
overlayContainer.ngOnDestroy();
}));

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

it('should open the menu as an idempotent operation', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
Expand Down
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-controls]': '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-controls the menu panel', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
expect(fixture.componentInstance.triggerEl.nativeElement.getAttribute('aria-controls'))
.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
2 changes: 2 additions & 0 deletions tools/public_api_guard/material/menu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatM
lazyContent: MatMenuContent;
overlapTrigger: boolean;
panelClass: string;
readonly panelId: string;
parentMenu: MatMenuPanel | undefined;
templateRef: TemplateRef<any>;
xPosition: MenuPositionX;
Expand Down Expand Up @@ -105,6 +106,7 @@ export interface MatMenuPanel<T = any> {
hasBackdrop?: boolean;
lazyContent?: MatMenuContent;
overlapTrigger: boolean;
readonly panelId?: string;
parentMenu?: MatMenuPanel | undefined;
removeItem?: (item: T) => void;
resetActiveItem: () => void;
Expand Down