Skip to content

fix(material/menu): set correct focus origin on item when opened via keyboard #21252

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
Jan 6, 2021
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
32 changes: 31 additions & 1 deletion src/material-experimental/mdc-menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ import {
} from '@angular/core';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {OverlayContainer, Overlay} from '@angular/cdk/overlay';
import {ESCAPE, LEFT_ARROW, RIGHT_ARROW, DOWN_ARROW, TAB, HOME, END} from '@angular/cdk/keycodes';
import {
ESCAPE,
LEFT_ARROW,
RIGHT_ARROW,
DOWN_ARROW,
TAB,
HOME,
END,
ENTER,
} from '@angular/cdk/keycodes';
import {MatMenu, MatMenuModule, MatMenuItem} from './index';
import {MatRipple} from '@angular/material-experimental/mdc-core';
import {
Expand Down Expand Up @@ -723,6 +732,27 @@ describe('MDC-based MatMenu', () => {
expect(items[2].classList).toContain('cdk-keyboard-focused');
}));

it('should set the keyboard focus origin when opened using the keyboard', fakeAsync(() => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
const trigger = fixture.componentInstance.triggerEl.nativeElement;

// Note that we dispatch both a `click` and a `keydown` to imitate the browser behavior.
dispatchKeyboardEvent(trigger, 'keydown', ENTER);
trigger.click();
fixture.detectChanges();

const items =
Array.from<HTMLElement>(document.querySelectorAll('.mat-mdc-menu-panel [mat-menu-item]'));

items.forEach(item => patchElementFocus(item));
tick(500);
tick();
fixture.detectChanges();

expect(items[0].classList).toContain('cdk-keyboard-focused');
}));

it('should toggle the aria-expanded attribute on the trigger', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
Expand Down
10 changes: 8 additions & 2 deletions src/material/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {FocusMonitor, FocusOrigin, isFakeMousedownFromScreenReader} from '@angular/cdk/a11y';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {LEFT_ARROW, RIGHT_ARROW} from '@angular/cdk/keycodes';
import {ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes';
import {
FlexibleConnectedPositionStrategy,
HorizontalConnectionPos,
Expand Down Expand Up @@ -103,7 +103,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {

// Tracking input type is necessary so it's possible to only auto-focus
// the first item of the list when the menu is opened via the keyboard
_openedBy: 'mouse' | 'touch' | null = null;
_openedBy: Exclude<FocusOrigin, 'program'> = null;

/**
* @deprecated
Expand Down Expand Up @@ -520,9 +520,15 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
_handleKeydown(event: KeyboardEvent): void {
const keyCode = event.keyCode;

// Pressing enter on the trigger will trigger the click handler later.
if (keyCode === ENTER || keyCode === SPACE) {
this._openedBy = 'keyboard';
}

if (this.triggersSubmenu() && (
(keyCode === RIGHT_ARROW && this.dir === 'ltr') ||
(keyCode === LEFT_ARROW && this.dir === 'rtl'))) {
this._openedBy = 'keyboard';
this.openMenu();
}
}
Expand Down
32 changes: 31 additions & 1 deletion src/material/menu/menu.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import {FocusMonitor} from '@angular/cdk/a11y';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {DOWN_ARROW, END, ESCAPE, HOME, LEFT_ARROW, RIGHT_ARROW, TAB} from '@angular/cdk/keycodes';
import {
DOWN_ARROW,
END,
ENTER,
ESCAPE,
HOME,
LEFT_ARROW,
RIGHT_ARROW,
TAB,
} from '@angular/cdk/keycodes';
import {Overlay, OverlayContainer} from '@angular/cdk/overlay';
import {ScrollDispatcher} from '@angular/cdk/scrolling';
import {
Expand Down Expand Up @@ -745,6 +754,27 @@ describe('MatMenu', () => {
expect(items[2].classList).toContain('cdk-keyboard-focused');
}));

it('should set the keyboard focus origin when opened using the keyboard', fakeAsync(() => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
const trigger = fixture.componentInstance.triggerEl.nativeElement;

// Note that we dispatch both a `click` and a `keydown` to imitate the browser behavior.
dispatchKeyboardEvent(trigger, 'keydown', ENTER);
trigger.click();
fixture.detectChanges();

const items =
Array.from<HTMLElement>(document.querySelectorAll('.mat-menu-panel [mat-menu-item]'));

items.forEach(item => patchElementFocus(item));
tick(500);
tick();
fixture.detectChanges();

expect(items[0].classList).toContain('cdk-keyboard-focused');
}));

it('should toggle the aria-expanded attribute on the trigger', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/menu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export interface MatMenuPanel<T = any> {
export declare class MatMenuTrigger implements AfterContentInit, OnDestroy {
get _deprecatedMatMenuTriggerFor(): MatMenuPanel;
set _deprecatedMatMenuTriggerFor(v: MatMenuPanel);
_openedBy: 'mouse' | 'touch' | null;
_openedBy: Exclude<FocusOrigin, 'program'>;
get dir(): Direction;
get menu(): MatMenuPanel;
set menu(menu: MatMenuPanel);
Expand Down