-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(expansion-panel): allow for content to be rendered lazily #8243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @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} from '@angular/core'; | ||
|
||
/** | ||
* Expansion panel content that will be rendered lazily | ||
* after the panel is opened for the first time. | ||
*/ | ||
@Directive({ | ||
selector: 'ng-template[matExpansionPanelContent]' | ||
}) | ||
export class MatExpansionPanelContent { | ||
constructor(public _template: TemplateRef<any>) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
<ng-content select="mat-expansion-panel-header"></ng-content> | ||
<div [class.mat-expanded]="expanded" class="mat-expansion-panel-content" | ||
[@bodyExpansion]="_getExpandedState()" [id]="id"> | ||
<div class="mat-expansion-panel-content" | ||
[class.mat-expanded]="expanded" | ||
[@bodyExpansion]="_getExpandedState()" | ||
[id]="id"> | ||
<div class="mat-expansion-panel-body"> | ||
<ng-content></ng-content> | ||
<ng-template [cdkPortalOutlet]="_portal"></ng-template> | ||
</div> | ||
<ng-content select="mat-action-row"></ng-content> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,21 @@ import { | |
Optional, | ||
SimpleChanges, | ||
ViewEncapsulation, | ||
ViewContainerRef, | ||
AfterContentInit, | ||
ContentChild, | ||
} from '@angular/core'; | ||
import {CdkAccordionItem} from '@angular/cdk/accordion'; | ||
import {UniqueSelectionDispatcher} from '@angular/cdk/collections'; | ||
import {CanDisable, mixinDisabled} from '@angular/material/core'; | ||
import {TemplatePortal} from '@angular/cdk/portal'; | ||
import {Subject} from 'rxjs/Subject'; | ||
import {take} from 'rxjs/operators/take'; | ||
import {filter} from 'rxjs/operators/filter'; | ||
import {startWith} from 'rxjs/operators/startWith'; | ||
import {MatAccordion} from './accordion'; | ||
import {coerceBooleanProperty} from '@angular/cdk/coercion'; | ||
import {MatExpansionPanelContent} from './expansion-panel-content'; | ||
|
||
/** Workaround for https://github.com/angular/angular/issues/17849 */ | ||
export const _CdkAccordionItem = CdkAccordionItem; | ||
|
@@ -91,7 +99,7 @@ export type MatExpansionPanelState = 'expanded' | 'collapsed'; | |
], | ||
}) | ||
export class MatExpansionPanel extends _MatExpansionPanelMixinBase | ||
implements CanDisable, OnChanges, OnDestroy { | ||
implements CanDisable, AfterContentInit, OnChanges, OnDestroy { | ||
|
||
/** Whether the toggle indicator should be hidden. */ | ||
@Input() | ||
|
@@ -109,9 +117,16 @@ export class MatExpansionPanel extends _MatExpansionPanelMixinBase | |
/** Optionally defined accordion the expansion panel belongs to. */ | ||
accordion: MatAccordion; | ||
|
||
/** Content that will be rendered lazily. */ | ||
@ContentChild(MatExpansionPanelContent) _lazyContent: MatExpansionPanelContent; | ||
|
||
/** Portal holding the user's content. */ | ||
_portal: TemplatePortal<any>; | ||
|
||
constructor(@Optional() @Host() accordion: MatAccordion, | ||
_changeDetectorRef: ChangeDetectorRef, | ||
_uniqueSelectionDispatcher: UniqueSelectionDispatcher) { | ||
_uniqueSelectionDispatcher: UniqueSelectionDispatcher, | ||
private _viewContainerRef: ViewContainerRef) { | ||
super(accordion, _changeDetectorRef, _uniqueSelectionDispatcher); | ||
this.accordion = accordion; | ||
} | ||
|
@@ -137,6 +152,19 @@ export class MatExpansionPanel extends _MatExpansionPanelMixinBase | |
return this.expanded ? 'expanded' : 'collapsed'; | ||
} | ||
|
||
ngAfterContentInit() { | ||
if (this._lazyContent) { | ||
// Render the content as soon as the panel becomes open. | ||
this.opened.pipe( | ||
startWith(null!), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this start with never? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the |
||
filter(() => this.expanded && !this._portal), | ||
take(1) | ||
).subscribe(() => { | ||
this._portal = new TemplatePortal<any>(this._lazyContent._template, this._viewContainerRef); | ||
}); | ||
} | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges) { | ||
this._inputChanges.next(changes); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick question: Don't you use
_
forprivate
variables only?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They also use it for "internal use only" properties