Skip to content

fix(menu): forward aria attribute to menu panel #17957

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 1 commit into from
Dec 19, 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
5 changes: 4 additions & 1 deletion src/material-experimental/mdc-menu/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
(@transformMenu.start)="_onAnimationStart($event)"
(@transformMenu.done)="_onAnimationDone($event)"
tabindex="-1"
role="menu">
role="menu"
[attr.aria-label]="ariaLabel || null"
[attr.aria-labelledby]="ariaLabelledby || null"
[attr.aria-describedby]="ariaDescribedby || null">
<div class="mat-mdc-menu-content mdc-list">
<ng-content></ng-content>
</div>
Expand Down
40 changes: 39 additions & 1 deletion src/material-experimental/mdc-menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,38 @@ describe('MDC-based MatMenu', () => {
expect(role).toBe('menu', 'Expected panel to have the "menu" role.');
});

it('should forward ARIA attributes to the menu panel', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
const instance = fixture.componentInstance;
fixture.detectChanges();
instance.trigger.openMenu();
fixture.detectChanges();

const menuPanel = overlayContainerElement.querySelector('.mat-mdc-menu-panel')!;
expect(menuPanel.hasAttribute('aria-label')).toBe(false);
expect(menuPanel.hasAttribute('aria-labelledby')).toBe(false);
expect(menuPanel.hasAttribute('aria-describedby')).toBe(false);

// Note that setting all of these at the same time is invalid,
// but it's up to the consumer to handle it correctly.
instance.ariaLabel = 'Custom aria-label';
instance.ariaLabelledby = 'custom-labelled-by';
instance.ariaDescribedby = 'custom-described-by';
fixture.detectChanges();

expect(menuPanel.getAttribute('aria-label')).toBe('Custom aria-label');
expect(menuPanel.getAttribute('aria-labelledby')).toBe('custom-labelled-by');
expect(menuPanel.getAttribute('aria-describedby')).toBe('custom-described-by');

// Change these to empty strings to make sure that we don't preserve empty attributes.
instance.ariaLabel = instance.ariaLabelledby = instance.ariaDescribedby = '';
fixture.detectChanges();

expect(menuPanel.hasAttribute('aria-label')).toBe(false);
expect(menuPanel.hasAttribute('aria-labelledby')).toBe(false);
expect(menuPanel.hasAttribute('aria-describedby')).toBe(false);
});

it('should set the "menuitem" role on the items by default', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
Expand Down Expand Up @@ -2163,7 +2195,10 @@ describe('MatMenu default overrides', () => {
#menu="matMenu"
[class]="panelClass"
(closed)="closeCallback($event)"
[backdropClass]="backdropClass">
[backdropClass]="backdropClass"
[aria-label]="ariaLabel"
[aria-labelledby]="ariaLabelledby"
[aria-describedby]="ariaDescribedby">

<button mat-menu-item> Item </button>
<button mat-menu-item disabled> Disabled </button>
Expand All @@ -2185,6 +2220,9 @@ class SimpleMenu {
backdropClass: string;
panelClass: string;
restoreFocus = true;
ariaLabel: string;
ariaLabelledby: string;
ariaDescribedby: string;
}

@Component({
Expand Down
5 changes: 4 additions & 1 deletion src/material/menu/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
(@transformMenu.start)="_onAnimationStart($event)"
(@transformMenu.done)="_onAnimationDone($event)"
tabindex="-1"
role="menu">
role="menu"
[attr.aria-label]="ariaLabel || null"
[attr.aria-labelledby]="ariaLabelledby || null"
[attr.aria-describedby]="ariaDescribedby || null">
<div class="mat-menu-content">
<ng-content></ng-content>
</div>
Expand Down
40 changes: 39 additions & 1 deletion src/material/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,38 @@ describe('MatMenu', () => {
expect(role).toBe('menu', 'Expected panel to have the "menu" role.');
});

it('should forward ARIA attributes to the menu panel', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
const instance = fixture.componentInstance;
fixture.detectChanges();
instance.trigger.openMenu();
fixture.detectChanges();

const menuPanel = overlayContainerElement.querySelector('.mat-menu-panel')!;
expect(menuPanel.hasAttribute('aria-label')).toBe(false);
expect(menuPanel.hasAttribute('aria-labelledby')).toBe(false);
expect(menuPanel.hasAttribute('aria-describedby')).toBe(false);

// Note that setting all of these at the same time is invalid,
// but it's up to the consumer to handle it correctly.
instance.ariaLabel = 'Custom aria-label';
instance.ariaLabelledby = 'custom-labelled-by';
instance.ariaDescribedby = 'custom-described-by';
fixture.detectChanges();

expect(menuPanel.getAttribute('aria-label')).toBe('Custom aria-label');
expect(menuPanel.getAttribute('aria-labelledby')).toBe('custom-labelled-by');
expect(menuPanel.getAttribute('aria-describedby')).toBe('custom-described-by');

// Change these to empty strings to make sure that we don't preserve empty attributes.
instance.ariaLabel = instance.ariaLabelledby = instance.ariaDescribedby = '';
fixture.detectChanges();

expect(menuPanel.hasAttribute('aria-label')).toBe(false);
expect(menuPanel.hasAttribute('aria-labelledby')).toBe(false);
expect(menuPanel.hasAttribute('aria-describedby')).toBe(false);
});

it('should set the "menuitem" role on the items by default', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
Expand Down Expand Up @@ -2151,7 +2183,10 @@ describe('MatMenu default overrides', () => {
#menu="matMenu"
[class]="panelClass"
(closed)="closeCallback($event)"
[backdropClass]="backdropClass">
[backdropClass]="backdropClass"
[aria-label]="ariaLabel"
[aria-labelledby]="ariaLabelledby"
[aria-describedby]="ariaDescribedby">

<button mat-menu-item> Item </button>
<button mat-menu-item disabled> Disabled </button>
Expand All @@ -2173,6 +2208,9 @@ class SimpleMenu {
backdropClass: string;
panelClass: string;
restoreFocus = true;
ariaLabel: string;
ariaLabelledby: string;
ariaDescribedby: string;
}

@Component({
Expand Down
9 changes: 9 additions & 0 deletions src/material/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ export class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatMenuItem>
/** Class to be added to the backdrop element. */
@Input() backdropClass: string = this._defaultOptions.backdropClass;

/** aria-label for the menu panel. */
@Input('aria-label') ariaLabel: string;

/** aria-labelledby for the menu panel. */
@Input('aria-labelledby') ariaLabelledby: string;

/** aria-describedby for the menu panel. */
@Input('aria-describedby') ariaDescribedby: string;

/** Position of the menu in the X axis. */
@Input()
get xPosition(): MenuPositionX { return this._xPosition; }
Expand Down
5 changes: 4 additions & 1 deletion tools/public_api_guard/material/menu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export declare class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatM
};
_isAnimating: boolean;
_panelAnimationState: 'void' | 'enter';
ariaDescribedby: string;
ariaLabel: string;
ariaLabelledby: string;
backdropClass: string;
classList: string;
close: EventEmitter<void | 'click' | 'keydown' | 'tab'>;
Expand Down Expand Up @@ -45,7 +48,7 @@ export declare class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatM
resetActiveItem(): void;
setElevation(depth: number): void;
setPositionClasses(posX?: MenuPositionX, posY?: MenuPositionY): void;
static ɵdir: i0.ɵɵDirectiveDefWithMeta<_MatMenuBase, never, never, { 'backdropClass': "backdropClass", 'xPosition': "xPosition", 'yPosition': "yPosition", 'overlapTrigger': "overlapTrigger", 'hasBackdrop': "hasBackdrop", 'panelClass': "class", 'classList': "classList" }, { 'closed': "closed", 'close': "close" }, ["lazyContent", "_allItems", "items"]>;
static ɵdir: i0.ɵɵDirectiveDefWithMeta<_MatMenuBase, never, never, { 'backdropClass': "backdropClass", 'ariaLabel': "aria-label", 'ariaLabelledby': "aria-labelledby", 'ariaDescribedby': "aria-describedby", 'xPosition': "xPosition", 'yPosition': "yPosition", 'overlapTrigger': "overlapTrigger", 'hasBackdrop': "hasBackdrop", 'panelClass': "class", 'classList': "classList" }, { 'closed': "closed", 'close': "close" }, ["lazyContent", "_allItems", "items"]>;
static ɵfac: i0.ɵɵFactoryDef<_MatMenuBase>;
}

Expand Down