Skip to content

Commit 27db2df

Browse files
committed
Revert "fix(menu): detach lazily-rendered content when the menu is closed (#10005)"
This reverts commit 37b1a09.
1 parent c8ca770 commit 27db2df

File tree

5 files changed

+16
-60
lines changed

5 files changed

+16
-60
lines changed

src/lib/menu/menu-content.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export class MatMenuContent implements OnDestroy {
4444
attach(context: any = {}) {
4545
if (!this._portal) {
4646
this._portal = new TemplatePortal(this._template, this._viewContainerRef);
47+
} else if (this._portal.isAttached) {
48+
this._portal.detach();
4749
}
4850

49-
this.detach();
50-
5151
if (!this._outlet) {
5252
this._outlet = new DomPortalOutlet(this._document.createElement('div'),
5353
this._componentFactoryResolver, this._appRef, this._injector);
@@ -62,16 +62,6 @@ export class MatMenuContent implements OnDestroy {
6262
this._portal.attach(this._outlet, context);
6363
}
6464

65-
/**
66-
* Detaches the content.
67-
* @docs-private
68-
*/
69-
detach() {
70-
if (this._portal.isAttached) {
71-
this._portal.detach();
72-
}
73-
}
74-
7565
ngOnDestroy() {
7666
if (this._outlet) {
7767
this._outlet.dispose();

src/lib/menu/menu-directive.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
NgZone,
3333
} from '@angular/core';
3434
import {Observable} from 'rxjs/Observable';
35-
import {Subject} from 'rxjs/Subject';
3635
import {merge} from 'rxjs/observable/merge';
3736
import {Subscription} from 'rxjs/Subscription';
3837
import {matMenuAnimations} from './menu-animations';
@@ -97,9 +96,6 @@ export class MatMenu implements AfterContentInit, MatMenuPanel, OnDestroy {
9796
/** Current state of the panel animation. */
9897
_panelAnimationState: 'void' | 'enter-start' | 'enter' = 'void';
9998

100-
/** Emits whenever an animation on the menu completes. */
101-
_animationDone = new Subject<void>();
102-
10399
/** Parent menu of the current menu panel. */
104100
parentMenu: MatMenuPanel | undefined;
105101

@@ -304,7 +300,10 @@ export class MatMenu implements AfterContentInit, MatMenuPanel, OnDestroy {
304300
}
305301

306302
/** Callback that is invoked when the panel animation completes. */
307-
_onAnimationDone() {
308-
this._animationDone.next();
303+
_onAnimationDone(event: AnimationEvent) {
304+
// After the initial expansion is done, trigger the second phase of the enter animation.
305+
if (event.toState === 'enter-start') {
306+
this._panelAnimationState = 'enter';
307+
}
309308
}
310309
}

src/lib/menu/menu-trigger.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
} from '@angular/cdk/overlay';
2222
import {TemplatePortal} from '@angular/cdk/portal';
2323
import {filter} from 'rxjs/operators/filter';
24-
import {take} from 'rxjs/operators/take';
2524
import {
2625
AfterContentInit,
2726
Directive,
@@ -239,27 +238,14 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
239238

240239
/** Closes the menu and does the necessary cleanup. */
241240
private _destroyMenu() {
242-
if (!this._overlayRef || !this.menuOpen) {
243-
return;
244-
}
245-
246-
const menu = this.menu;
247-
248-
this._resetMenu();
249-
this._closeSubscription.unsubscribe();
250-
this._overlayRef.detach();
251-
252-
if (menu instanceof MatMenu) {
253-
menu._resetAnimation();
241+
if (this._overlayRef && this.menuOpen) {
242+
this._resetMenu();
243+
this._closeSubscription.unsubscribe();
244+
this._overlayRef.detach();
254245

255-
if (menu.lazyContent) {
256-
// Wait for the exit animation to finish before detaching the content.
257-
menu._animationDone
258-
.pipe(take(1))
259-
.subscribe(() => menu.lazyContent!.detach());
246+
if (this.menu instanceof MatMenu) {
247+
this.menu._resetAnimation();
260248
}
261-
} else if (menu.lazyContent) {
262-
menu.lazyContent.detach();
263249
}
264250
}
265251

src/lib/menu/menu.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
(keydown)="_handleKeydown($event)"
66
(click)="closed.emit('click')"
77
[@transformMenu]="_panelAnimationState"
8-
(@transformMenu.done)="_onAnimationDone()"
8+
(@transformMenu.done)="_onAnimationDone($event)"
99
tabindex="-1"
1010
role="menu">
1111
<div class="mat-menu-content" [@fadeInItems]="'showing'">

src/lib/menu/menu.spec.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ describe('MatMenu', () => {
327327
describe('lazy rendering', () => {
328328
it('should be able to render the menu content lazily', fakeAsync(() => {
329329
const fixture = TestBed.createComponent(SimpleLazyMenu);
330-
331330
fixture.detectChanges();
331+
332332
fixture.componentInstance.triggerEl.nativeElement.click();
333333
fixture.detectChanges();
334334
tick(500);
@@ -340,24 +340,6 @@ describe('MatMenu', () => {
340340
expect(fixture.componentInstance.trigger.menuOpen).toBe(true, 'Expected menu to be open');
341341
}));
342342

343-
it('should detach the lazy content when the menu is closed', fakeAsync(() => {
344-
const fixture = TestBed.createComponent(SimpleLazyMenu);
345-
346-
fixture.detectChanges();
347-
fixture.componentInstance.trigger.openMenu();
348-
fixture.detectChanges();
349-
tick(500);
350-
351-
expect(fixture.componentInstance.items.length).toBeGreaterThan(0);
352-
353-
fixture.componentInstance.trigger.closeMenu();
354-
fixture.detectChanges();
355-
tick(500);
356-
fixture.detectChanges();
357-
358-
expect(fixture.componentInstance.items.length).toBe(0);
359-
}));
360-
361343
it('should focus the first menu item when opening a lazy menu via keyboard', fakeAsync(() => {
362344
let zone: MockNgZone;
363345

@@ -390,8 +372,8 @@ describe('MatMenu', () => {
390372

391373
it('should be able to open the same menu with a different context', fakeAsync(() => {
392374
const fixture = TestBed.createComponent(LazyMenuWithContext);
393-
394375
fixture.detectChanges();
376+
395377
fixture.componentInstance.triggerOne.openMenu();
396378
fixture.detectChanges();
397379
tick(500);
@@ -1575,7 +1557,6 @@ class FakeIcon {}
15751557
class SimpleLazyMenu {
15761558
@ViewChild(MatMenuTrigger) trigger: MatMenuTrigger;
15771559
@ViewChild('triggerEl') triggerEl: ElementRef;
1578-
@ViewChildren(MatMenuItem) items: QueryList<MatMenuItem>;
15791560
}
15801561

15811562

0 commit comments

Comments
 (0)