Skip to content

Commit

Permalink
fix(menu): not handling keyboard events when opened by mouse
Browse files Browse the repository at this point in the history
Fixes the menu keyboard interactions not working when it is opened by a click.

Fixes angular#4991.
  • Loading branch information
crisbeto committed Jun 18, 2017
1 parent 0aaeb69 commit a5cc967
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,15 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
private _initMenu(): void {
this._setIsMenuOpen(true);

// Should only set focus if opened via the keyboard, so keyboard users can
// can easily navigate menu items. According to spec, mouse users should not
// see the focus style.
if (!this._openedByMouse) {
// If the menu was opened by mouse, we focus the root node, which allows for the keyboard
// interactions to work. Otherwise, if the menu was opened by keyboard, we focus the first item.
if (this._openedByMouse) {
let rootNode = this._overlayRef.overlayElement.firstElementChild as HTMLElement;

if (rootNode) {
rootNode.focus();
}
} else {
this.menu.focusFirstItem();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/menu/menu.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ng-template>
<div class="mat-menu-panel" [ngClass]="_classList" (keydown)="_handleKeydown($event)"
(click)="_emitCloseEvent()" [@transformMenu]="'showing'">
(click)="_emitCloseEvent()" [@transformMenu]="'showing'" tabindex="-1">
<div class="mat-menu-content" [@fadeInItems]="'showing'">
<ng-content></ng-content>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/lib/menu/menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $mat-menu-vertical-padding: 8px !default;
@include mat-menu-base();
@include mat-menu-positions();
max-height: calc(100vh - #{$mat-menu-item-height});
outline: 0;

@include cdk-high-contrast {
outline: solid 1px;
Expand Down
19 changes: 18 additions & 1 deletion src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {OverlayContainer} from '../core/overlay/overlay-container';
import {Dir, LayoutDirection} from '../core/rtl/dir';
import {extendObject} from '../core/util/object-extend';
import {ESCAPE} from '../core/keyboard/keycodes';
import {dispatchKeyboardEvent} from '../core/testing/dispatch-events';
import {dispatchKeyboardEvent, dispatchFakeEvent} from '../core/testing/dispatch-events';


describe('MdMenu', () => {
Expand Down Expand Up @@ -134,6 +134,23 @@ describe('MdMenu', () => {
expect(panel.classList).toContain('custom-two');
});

it('should focus the menu panel root node when it was opened by mouse', () => {
const fixture = TestBed.createComponent(SimpleMenu);

fixture.detectChanges();

const triggerEl = fixture.componentInstance.triggerEl.nativeElement;

dispatchFakeEvent(triggerEl, 'mousedown');
triggerEl.click();
fixture.detectChanges();

const panel = overlayContainerElement.querySelector('.mat-menu-panel');

expect(panel).toBeTruthy('Expected the panel to be rendered.');
expect(document.activeElement).toBe(panel, 'Expected the panel to be focused.');
});

describe('positions', () => {
let fixture: ComponentFixture<PositionedMenu>;
let panel: HTMLElement;
Expand Down

0 comments on commit a5cc967

Please sign in to comment.