-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(menu): support lazy rendering and passing in context data (#9271)
* Introduces the `matMenuContent` directive that allows for menu content to be rendered lazily. * Adds the `matMenuTriggerData` input to the `MatMenuTrigger` that allows for contextual data to be passed in to the lazily-rendered menu panel. This allows for the menu instance to be re-used between triggers. Fixes #9251.
- Loading branch information
Showing
9 changed files
with
289 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { | ||
Directive, | ||
TemplateRef, | ||
ComponentFactoryResolver, | ||
ApplicationRef, | ||
Injector, | ||
ViewContainerRef, | ||
Inject, | ||
OnDestroy, | ||
} from '@angular/core'; | ||
import {TemplatePortal, DomPortalOutlet} from '@angular/cdk/portal'; | ||
import {DOCUMENT} from '@angular/common'; | ||
|
||
/** | ||
* Menu content that will be rendered lazily once the menu is opened. | ||
*/ | ||
@Directive({ | ||
selector: 'ng-template[matMenuContent]' | ||
}) | ||
export class MatMenuContent implements OnDestroy { | ||
private _portal: TemplatePortal<any>; | ||
private _outlet: DomPortalOutlet; | ||
|
||
constructor( | ||
private _template: TemplateRef<any>, | ||
private _componentFactoryResolver: ComponentFactoryResolver, | ||
private _appRef: ApplicationRef, | ||
private _injector: Injector, | ||
private _viewContainerRef: ViewContainerRef, | ||
@Inject(DOCUMENT) private _document: any) {} | ||
|
||
/** | ||
* Attaches the content with a particular context. | ||
* @docs-private | ||
*/ | ||
attach(context: any = {}) { | ||
if (!this._portal) { | ||
this._portal = new TemplatePortal(this._template, this._viewContainerRef); | ||
} else if (this._portal.isAttached) { | ||
this._portal.detach(); | ||
} | ||
|
||
if (!this._outlet) { | ||
this._outlet = new DomPortalOutlet(this._document.createElement('div'), | ||
this._componentFactoryResolver, this._appRef, this._injector); | ||
} | ||
|
||
const element: HTMLElement = this._template.elementRef.nativeElement; | ||
|
||
// Because we support opening the same menu from different triggers (which in turn have their | ||
// own `OverlayRef` panel), we have to re-insert the host element every time, otherwise we | ||
// risk it staying attached to a pane that's no longer in the DOM. | ||
element.parentNode!.insertBefore(this._outlet.outletElement, element); | ||
this._portal.attach(this._outlet, context); | ||
} | ||
|
||
ngOnDestroy() { | ||
if (this._outlet) { | ||
this._outlet.dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.