Skip to content

fix(cdk-experimental/ui-patterns): preserveContent should not render until first visible #31660

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/cdk-experimental/deferred-content/deferred-content.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,25 @@ describe('DeferredContent', () => {
component.preserveContent.set(true);
});

it('creates the content when hidden.', async () => {
it('does not create the content until first visible.', async () => {
collapsible.injector.get(Collapsible).contentVisible.set(false);
await fixture.whenStable();
expect(collapsible.nativeElement.innerText).toBe('Lazy Content');
expect(collapsible.nativeElement.innerText).toBe('');
});

it('creates the content when visible.', async () => {
collapsible.injector.get(Collapsible).contentVisible.set(true);
await fixture.whenStable();
expect(collapsible.nativeElement.innerText).toBe('Lazy Content');
});

it('does not remove the content when hidden.', async () => {
collapsible.injector.get(Collapsible).contentVisible.set(true);
await fixture.whenStable();
collapsible.injector.get(Collapsible).contentVisible.set(false);
await fixture.whenStable();
expect(collapsible.nativeElement.innerText).toBe('Lazy Content');
});
});
});

Expand Down
13 changes: 5 additions & 8 deletions src/cdk-experimental/deferred-content/deferred-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

import {
afterRenderEffect,
Directive,
effect,
inject,
input,
TemplateRef,
Expand All @@ -21,7 +21,7 @@ import {
*/
@Directive()
export class DeferredContentAware {
contentVisible = signal(false);
readonly contentVisible = signal(false);
readonly preserveContent = input(false);
}

Expand All @@ -48,16 +48,13 @@ export class DeferredContent {
private _isRendered = false;

constructor() {
effect(() => {
if (
this._deferredContentAware.preserveContent() ||
this._deferredContentAware.contentVisible()
) {
afterRenderEffect(() => {
if (this._deferredContentAware.contentVisible()) {
if (this._isRendered) return;
this._viewContainerRef.clear();
this._viewContainerRef.createEmbeddedView(this._templateRef);
this._isRendered = true;
} else {
} else if (!this._deferredContentAware.preserveContent()) {
this._viewContainerRef.clear();
this._isRendered = false;
}
Expand Down
Loading