Skip to content
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

Angular: Fix components disappearing on docs page on property change #21944

Merged
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
25 changes: 14 additions & 11 deletions code/frameworks/angular/src/client/angular-beta/AbstractRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApplicationRef, enableProdMode, importProvidersFrom, NgModule } from '@angular/core';
import { ApplicationRef, enableProdMode, NgModule } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

import { BehaviorSubject, Subject } from 'rxjs';
Expand All @@ -14,16 +14,16 @@ type StoryRenderInfo = {
moduleMetadataSnapshot: string;
};

const applicationRefs = new Set<ApplicationRef>();
const applicationRefs = new Map<HTMLElement, ApplicationRef>();

export abstract class AbstractRenderer {
/**
* Wait and destroy the platform
*/
public static resetApplications() {
public static resetApplications(domNode?: HTMLElement) {
componentNgModules.clear();
applicationRefs.forEach((appRef) => {
if (!appRef.destroyed) {
applicationRefs.forEach((appRef, appDOMNode) => {
if (!appRef.destroyed && (!domNode || appDOMNode === domNode)) {
appRef.destroy();
}
});
Expand All @@ -50,7 +50,7 @@ export abstract class AbstractRenderer {
}
};

protected previousStoryRenderInfo: StoryRenderInfo;
protected previousStoryRenderInfo = new Map<HTMLElement, StoryRenderInfo>();

// Observable to change the properties dynamically without reloading angular module&component
protected storyProps$: Subject<ICollection | undefined>;
Expand All @@ -67,7 +67,7 @@ export abstract class AbstractRenderer {
}
}

protected abstract beforeFullRender(): Promise<void>;
protected abstract beforeFullRender(domNode?: HTMLElement): Promise<void>;

protected abstract afterFullRender(): Promise<void>;

Expand Down Expand Up @@ -100,6 +100,7 @@ export abstract class AbstractRenderer {

if (
!this.fullRendererRequired({
targetDOMNode,
storyFnAngular,
moduleMetadata: {
...storyFnAngular.moduleMetadata,
Expand All @@ -112,7 +113,7 @@ export abstract class AbstractRenderer {
return;
}

await this.beforeFullRender();
await this.beforeFullRender(targetDOMNode);

// Complete last BehaviorSubject and set a new one for the current module
if (this.storyProps$) {
Expand Down Expand Up @@ -140,7 +141,7 @@ export abstract class AbstractRenderer {
],
});

applicationRefs.add(applicationRef);
applicationRefs.set(targetDOMNode, applicationRef);

await this.afterFullRender();
}
Expand Down Expand Up @@ -171,22 +172,24 @@ export abstract class AbstractRenderer {
}

private fullRendererRequired({
targetDOMNode,
storyFnAngular,
moduleMetadata,
forced,
}: {
targetDOMNode: HTMLElement;
storyFnAngular: StoryFnAngularReturnType;
moduleMetadata: NgModule;
forced: boolean;
}) {
const { previousStoryRenderInfo } = this;
const previousStoryRenderInfo = this.previousStoryRenderInfo.get(targetDOMNode);

const currentStoryRender = {
storyFnAngular,
moduleMetadataSnapshot: stringify(moduleMetadata),
};

this.previousStoryRenderInfo = currentStoryRender;
this.previousStoryRenderInfo.set(targetDOMNode, currentStoryRender);

if (
// check `forceRender` of story RenderContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class DocsRenderer extends AbstractRenderer {
await super.render({ ...options, forced: false });
}

async beforeFullRender(): Promise<void> {
DocsRenderer.resetApplications();
async beforeFullRender(domNode?: HTMLElement): Promise<void> {
DocsRenderer.resetApplications(domNode);
}

async afterFullRender(): Promise<void> {
Expand Down