Skip to content

Commit 59d4c77

Browse files
committed
fix(menu): make menu open idempotent
1 parent e270e50 commit 59d4c77

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/lib/menu/menu-trigger.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
6666
}
6767

6868
openMenu(): void {
69-
this._createOverlay();
70-
this._overlayRef.attach(this._portal);
71-
this._initMenu();
69+
if (!this._menuOpen) {
70+
this._createOverlay();
71+
this._overlayRef.attach(this._portal);
72+
this._initMenu();
73+
}
7274
}
7375

7476
closeMenu(): void {

src/lib/menu/menu.spec.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {TestBed, async} from '@angular/core/testing';
2-
import {Component} from '@angular/core';
3-
import {MdMenuModule} from './menu';
2+
import {Component, ViewChild} from '@angular/core';
3+
import {MdMenuModule, MdMenuTrigger} from './menu';
44

55

66
describe('MdMenu', () => {
@@ -14,11 +14,24 @@ describe('MdMenu', () => {
1414
TestBed.compileComponents();
1515
}));
1616

17-
it('should add and remove focus class on focus/blur', () => {
17+
it('should open the menu as an idempotent operation', () => {
1818
let fixture = TestBed.createComponent(TestMenu);
19-
expect(fixture).toBeTruthy();
19+
fixture.detectChanges();
20+
expect(() => {
21+
fixture.componentInstance.trigger.openMenu();
22+
fixture.componentInstance.trigger.openMenu();
23+
}).not.toThrowError();
2024
});
2125
});
2226

23-
@Component({template: ``})
24-
class TestMenu {}
27+
@Component({
28+
template: `
29+
<button [md-menu-trigger-for]="menu">Toggle menu</button>
30+
<md-menu #menu="mdMenu">
31+
Content
32+
</md-menu>
33+
`
34+
})
35+
class TestMenu {
36+
@ViewChild(MdMenuTrigger) trigger: MdMenuTrigger;
37+
}

0 commit comments

Comments
 (0)