-
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
* 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
286 additions
and
28 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,68 @@ | ||
/** | ||
* @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; | ||
|
||
// Since the menu might be attached to a different DOM node, we have to re-insert it every time. | ||
element.parentNode!.insertBefore(this._outlet.hostDomElement, 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.