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

Svelte: Fix docs rendering #19705

Merged
merged 13 commits into from
Nov 3, 2022
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
6 changes: 1 addition & 5 deletions code/lib/store/template/stories/rendering.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ export const ChangeArgs = {
await expect(button).toHaveFocus();

// Vue3: https://github.com/storybookjs/storybook/issues/13913
// Svelte: https://github.com/storybookjs/storybook/issues/19205
// Web-components: https://github.com/storybookjs/storybook/issues/19415
// Preact: https://github.com/storybookjs/storybook/issues/19504
if (
['vue3', 'svelte', 'web-components', 'html', 'preact'].includes(globalThis.storybookRenderer)
)
return;
if (['vue3', 'web-components', 'html', 'preact'].includes(globalThis.storybookRenderer)) return;

// When we change the args to the button, it should not rerender
await channel.emit('updateStoryArgs', { storyId: id, updatedArgs: { label: 'New Text' } });
Expand Down
62 changes: 43 additions & 19 deletions code/renderers/svelte/src/render.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
import global from 'global';

import type { Store_RenderContext, ArgsStoryFn } from '@storybook/types';
import type { SvelteComponentTyped } from 'svelte';
// eslint-disable-next-line import/no-extraneous-dependencies
import PreviewRender from '@storybook/svelte/templates/PreviewRender.svelte';

import type { SvelteFramework } from './types';

const { document } = global;

let previousComponent: SvelteComponentTyped | null = null;
const componentsByDomElement = new Map<Element, SvelteComponentTyped>();

function cleanUpPreviousStory() {
if (!previousComponent) {
function teardown(domElement: Element) {
if (!componentsByDomElement.has(domElement)) {
return;
}
previousComponent.$destroy();
previousComponent = null;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- we know it exists because we just checked
componentsByDomElement.get(domElement)!.$destroy();

// eslint-disable-next-line no-param-reassign -- this is on purpose
domElement.innerHTML = '';
componentsByDomElement.delete(domElement);
}

export function renderToDOM(
{ storyFn, kind, name, showMain, showError, storyContext }: Store_RenderContext<SvelteFramework>,
{
storyFn,
kind,
name,
showMain,
showError,
storyContext,
forceRemount,
}: Store_RenderContext<SvelteFramework>,
domElement: Element
) {
cleanUpPreviousStory();

const target = domElement || document.getElementById('storybook-root');
const existingComponent = componentsByDomElement.get(domElement);

target.innerHTML = '';
if (forceRemount) {
teardown(domElement);
}

previousComponent = new PreviewRender({
target,
props: {
if (!existingComponent || forceRemount) {
const createdComponent = new PreviewRender({
target: domElement,
props: {
storyFn,
storyContext,
name,
kind,
showError,
},
}) as SvelteComponentTyped;
componentsByDomElement.set(domElement, createdComponent);
} else {
existingComponent.$set({
storyFn,
storyContext,
name,
kind,
showError,
},
});
});
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
}

showMain();

// teardown the component when the story changes
return () => {
teardown(domElement);
};
}

export const render: ArgsStoryFn<SvelteFramework> = (args, context) => {
Expand Down
6 changes: 3 additions & 3 deletions code/renderers/svelte/template/cli/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* What background color to use
*/
export let backgroundColor;
export let backgroundColor = undefined;
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
/**
* How large should the button be?
*/
Expand All @@ -19,9 +19,9 @@
*/
export let label = '';

let mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
$: mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';

let style = backgroundColor ? `background-color: ${backgroundColor}` : '';
$: style = backgroundColor ? `background-color: ${backgroundColor}` : '';

const dispatch = createEventDispatcher();

Expand Down
4 changes: 2 additions & 2 deletions code/renderers/svelte/template/components/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
*/
export let label = '';

let mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
$: mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';

let style = backgroundColor ? `background-color: ${backgroundColor}` : '';
$: style = backgroundColor ? `background-color: ${backgroundColor}` : '';

const dispatch = createEventDispatcher();

Expand Down
19 changes: 13 additions & 6 deletions code/renderers/svelte/templates/PreviewRender.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let showError;
export let storyContext;

const {
let {
/** @type {SvelteComponent} */
Component,
/** @type {any} */
Expand All @@ -19,11 +19,16 @@
WrapperData = {},
} = storyFn();

const eventsFromArgTypes = Object.fromEntries(Object.entries(storyContext.argTypes)
// reactive, re-render on storyFn change
$: ({ Component, props = {}, on, Wrapper, WrapperData = {} } = storyFn());

const eventsFromArgTypes = Object.fromEntries(
Object.entries(storyContext.argTypes)
.filter(([k, v]) => v.action && props[k] != null)
.map(([k, v]) => [v.action, props[k]]));
.map(([k, v]) => [v.action, props[k]])
);

const events = {...eventsFromArgTypes, ...on};
const events = { ...eventsFromArgTypes, ...on };

if (!Component) {
showError({
Expand All @@ -36,9 +41,11 @@
});
}
</script>

<SlotDecorator
decorator={Wrapper}
decoratorProps={WrapperData}
component={Component}
props={props}
on={events}/>
{props}
on={events}
/>
7 changes: 4 additions & 3 deletions code/renderers/svelte/templates/SlotDecorator.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
});
}
</script>

{#if decorator}
<svelte:component this={decorator} {...decoratorProps} bind:this={decoratorInstance}>
<svelte:component this={component} {...props} bind:this={instance}/>
<svelte:component this={component} {...props} bind:this={instance} />
</svelte:component>
{:else}
<svelte:component this={component} {...props} bind:this={instance}/>
{/if}
<svelte:component this={component} {...props} bind:this={instance} />
{/if}
5 changes: 0 additions & 5 deletions code/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,6 @@
"root": "lib/core-server",
"type": "library"
},
"@storybook/core-vite": {
"implicitDependencies": [],
"root": "lib/core-vite",
"type": "library"
},
"@storybook/core-webpack": {
"implicitDependencies": [],
"root": "lib/core-webpack",
Expand Down
4 changes: 2 additions & 2 deletions docs/snippets/svelte/button-implementation.js.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* What background color to use
*/
export let backgroundColor;
export let backgroundColor = undefined;
/**
* How large should the button be?
*/
Expand All @@ -34,4 +34,4 @@
</script>

<button type="button" {style} on:click="{onClick}">{label}</button>
```
```