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

Core: Allow a teardown function to be returned from renderToDOM #18457

Merged
merged 45 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4c80c21
Allow a teardown function to be returned from `renderToDOM`
tmeasday Jun 10, 2022
06e4c29
Add integration tests for teardown function
tmeasday Jun 15, 2022
95d3f32
Update addons/docs/src/blocks/Story.tsx
tmeasday Jun 16, 2022
3a57672
Update snapshots
tmeasday Jun 17, 2022
383b5b5
Fixed PnP compatibility for bundled ui and router packages
Andarist Jun 6, 2022
d7cb989
chore(CI): reenable cra pnp tests
yannbf Jun 6, 2022
d66cb53
fix: change the way presets are required
yannbf Jun 7, 2022
d2928de
telemetry: strip out preset from addon name
yannbf Jun 9, 2022
31aef64
Warn when storyName usage is detected in CSF3 stories
joshwooding Jun 11, 2022
f1ae954
Move memoizerific to dependencies in theming
joshwooding May 31, 2022
8335942
Move memoizerific and qs to dependencies in router
joshwooding May 31, 2022
d410bd9
Move memoizerific and qs to dependencies in ui
joshwooding May 31, 2022
e1d7f08
Core: Fix process is not defined when using components
joshwooding Jun 13, 2022
d35bc15
Remove duplicate statement
mattevenson Jun 13, 2022
f92dd04
bundle the syntax highlighter again inside lib/components
ndelangen Jun 7, 2022
2e9fb44
Update test-runner.md
alexguja Jun 15, 2022
f19b470
fix: display skip to sidebar button
darleendenno Jun 14, 2022
d5b741e
Don't intercept calls inside callbacks, and bubble exceptions inside …
ghengeveld Jun 10, 2022
3bcec76
Show child interactions in log and display errors in the appropriate …
ghengeveld Jun 10, 2022
1ea2f9c
Fix mock data
ghengeveld Jun 12, 2022
136c121
Fix exception type
ghengeveld Jun 12, 2022
ee7baf2
Disable interaction on child interactions
ghengeveld Jun 12, 2022
6f42962
Many fixes
ghengeveld Jun 13, 2022
edc79c9
Remove unused eslint directives
ghengeveld Jun 14, 2022
3364473
Fix prop forwarding
ghengeveld Jun 14, 2022
ea77b6d
Fix back button
ghengeveld Jun 14, 2022
dbcf78b
Fix pausedAt indicator
ghengeveld Jun 14, 2022
6d540e0
Fix broken play function
ghengeveld Jun 14, 2022
1605cbb
Value might be null
ghengeveld Jun 14, 2022
1fa16ca
Tweak indicator style
ghengeveld Jun 14, 2022
249496f
Fix rendering inconsistency
ghengeveld Jun 14, 2022
fcf5728
Fix zIndex
ghengeveld Jun 14, 2022
41913b6
Fix tests
ghengeveld Jun 14, 2022
2509e7e
Fix story data
ghengeveld Jun 15, 2022
3b668db
Show pausedAt indicator in story
ghengeveld Jun 15, 2022
b134d72
Better story data
ghengeveld Jun 15, 2022
18cf8c6
Add story book interaction row with parentId
ghengeveld Jun 15, 2022
2e23d67
Better story data
ghengeveld Jun 15, 2022
3285a9e
Collapse child interactions
ghengeveld Jun 15, 2022
e39fb22
Expand by default
ghengeveld Jun 16, 2022
aa7f2b6
Tweak anonymous function color
ghengeveld Jun 16, 2022
6fdd19c
Update addons/interactions/src/components/Interaction/Interaction.tsx
ghengeveld Jun 16, 2022
7b313ae
Merge remote-tracking branch 'origin/next' into tom/sb-415-return-cle…
tmeasday Jun 17, 2022
f0169ca
Use node14 in netlify
tmeasday Jun 20, 2022
661011f
Merge branch 'next' into tom/sb-415-return-cleanup-function-from-rend…
shilman Jun 20, 2022
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
2 changes: 2 additions & 0 deletions app/react/src/client/preview/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,6 @@ export async function renderToDOM(
}

await renderElement(element, domElement);

return () => unmountElement(domElement);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we patch this back we should make sure we don't include this change (in fact should we take it out until we've considered this properly?)

}
2 changes: 1 addition & 1 deletion examples/react-ts/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const config: StorybookConfig = {
},
features: {
postcss: false,
// modernInlineRender: true,
modernInlineRender: true,
storyStoreV7: !global.navigator?.userAgent?.match?.('jsdom'),
buildStoriesJson: true,
babelModeV7: true,
Expand Down
19 changes: 13 additions & 6 deletions examples/react-ts/src/button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ComponentType, ButtonHTMLAttributes } from 'react';
import React, { ComponentType, ButtonHTMLAttributes, useEffect } from 'react';

export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
/**
Expand All @@ -12,8 +12,15 @@ export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
icon?: ComponentType;
}

export const Button = ({ label = 'Hello', icon: Icon, ...props }: ButtonProps) => (
<button type="button" {...props}>
{Icon ? <Icon /> : null} {label}
</button>
);
export const Button = ({ label = 'Hello', icon: Icon, ...props }: ButtonProps) => {
useEffect(() => {
const fn = () => console.log(`click ${label}`);
global.window.document.querySelector('body')?.addEventListener('click', fn);
return () => global.window.document.querySelector('body')?.removeEventListener('click', fn);
});
return (
<button type="button" {...props}>
{Icon ? <Icon /> : null} {label}
</button>
);
};
10 changes: 10 additions & 0 deletions examples/react-ts/src/button2.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Button } from './button';

export default {
component: Button,
title: 'button2',
};

export const one = { args: { label: 'one' } };
export const two = { args: { label: 'two' } };
export const three = { args: { label: 'three' } };
9 changes: 9 additions & 0 deletions examples/react-ts/src/button3.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Button } from './button';

export default {
component: Button,
title: 'button3',
};

export const four = { args: { label: 'four' } };
export const five = { args: { label: 'five' } };
3 changes: 2 additions & 1 deletion lib/preview-web/src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
StoryIndex,
PromiseLike,
WebProjectAnnotations,
RenderToDOM,
} from '@storybook/store';

import { StoryRender } from './StoryRender';
Expand All @@ -45,7 +46,7 @@ export class Preview<TFramework extends AnyFramework> {

importFn?: ModuleImportFn;

renderToDOM: WebProjectAnnotations<TFramework>['renderToDOM'];
renderToDOM: RenderToDOM<TFramework>;

storyRenders: StoryRender<TFramework>[] = [];

Expand Down
28 changes: 19 additions & 9 deletions lib/preview-web/src/StoryRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
StoryContextForLoaders,
StoryContext,
} from '@storybook/csf';
import { Story, RenderContext, StoryStore } from '@storybook/store';
import {
Story,
RenderContext,
StoryStore,
RenderToDOM,
TeardownRenderToDOM,
} from '@storybook/store';
import { Channel } from '@storybook/addons';
import { STORY_RENDER_PHASE_CHANGED, STORY_RENDERED } from '@storybook/core-events';

Expand Down Expand Up @@ -62,13 +68,12 @@ export class StoryRender<TFramework extends AnyFramework> implements Render<TFra

public disableKeyListeners = false;

private teardownRender: TeardownRenderToDOM = () => {};

constructor(
public channel: Channel,
public store: StoryStore<TFramework>,
private renderToScreen: (
renderContext: RenderContext<TFramework>,
canvasElement: HTMLElement
) => void | Promise<void>,
private renderToScreen: RenderToDOM<TFramework>,
private callbacks: RenderContextCallbacks<TFramework>,
public id: StoryId,
public viewMode: ViewMode,
Expand Down Expand Up @@ -190,9 +195,10 @@ export class StoryRender<TFramework extends AnyFramework> implements Render<TFra
unboundStoryFn,
};

await this.runPhase(abortSignal, 'rendering', async () =>
this.renderToScreen(renderContext, this.canvasElement)
);
await this.runPhase(abortSignal, 'rendering', async () => {
this.teardownRender =
(await this.renderToScreen(renderContext, this.canvasElement)) || (() => {});
});
this.notYetRendered = false;
if (abortSignal.aborted) return;

Expand Down Expand Up @@ -241,7 +247,11 @@ export class StoryRender<TFramework extends AnyFramework> implements Render<TFra
// Wait several ticks that may be needed to handle the abort, then try again.
// Note that there's a max of 5 nested timeouts before they're no longer "instant".
for (let i = 0; i < 3; i += 1) {
if (!this.isPending()) return;
if (!this.isPending()) {
// eslint-disable-next-line no-await-in-loop
await this.teardownRender();
return;
}
// eslint-disable-next-line no-await-in-loop
await new Promise((resolve) => setTimeout(resolve, 0));
}
Expand Down
10 changes: 9 additions & 1 deletion lib/store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ export type ModuleExports = Record<string, any>;
export type PromiseLike<T> = Promise<T> | SynchronousPromise<T>;
export type ModuleImportFn = (path: Path) => PromiseLike<ModuleExports>;

type MaybePromise<T> = Promise<T> | T;

export type TeardownRenderToDOM = () => MaybePromise<void>;
export type RenderToDOM<TFramework extends AnyFramework> = (
context: RenderContext<TFramework>,
element: Element
) => MaybePromise<void | TeardownRenderToDOM>;

export type WebProjectAnnotations<TFramework extends AnyFramework> =
ProjectAnnotations<TFramework> & {
renderToDOM?: (context: RenderContext<TFramework>, element: Element) => Promise<void> | void;
renderToDOM?: RenderToDOM<TFramework>;
};

export type NormalizedProjectAnnotations<TFramework extends AnyFramework = AnyFramework> =
Expand Down