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 async loaders in docs panel #14080

Merged
merged 1 commit into from
Mar 2, 2021
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
9 changes: 5 additions & 4 deletions addons/docs/src/frameworks/svelte/HOC.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script>
export let storyFn;

let { Component: component, props } = storyFn();
export let storyContext;
export let unboundStoryFn;

let { Component: component, props } = unboundStoryFn(storyContext);
</script>

<svelte:component this={component} {...props}/>
<svelte:component this={component} {...props}/>
32 changes: 24 additions & 8 deletions addons/docs/src/frameworks/svelte/prepareForInline.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { StoryFn } from '@storybook/addons';
import { StoryContext, StoryFn } from '@storybook/addons';

import React from 'react';

// @ts-ignore
import HOC from './HOC.svelte';

export const prepareForInline = (storyFn: StoryFn) => {
export const prepareForInline = (storyFn: StoryFn, context: StoryContext) => {
const el = React.useRef(null);
React.useEffect(() => {
const root = new HOC({
target: el.current,
props: {
storyFn,
},
let cancelled = false;
const { applyLoaders, unboundStoryFn } = context;

let cpn: any;

applyLoaders().then((storyContext: StoryContext) => {
if (!cancelled) {
cpn = new HOC({
target: el.current,
props: {
storyContext,
unboundStoryFn,
},
});
}
});
return () => root.$destroy();

return () => {
cancelled = true;
if (cpn) {
cpn.$destroy();
}
};
});

return React.createElement('div', { ref: el });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Async Loaders Async Loaders 1`] = `
<section
class="storybook-snapshot-container"
>
<button
class="button svelte-n2tx93 rounded"
>
<strong>
Round
corners
</strong>

<br />



</button>
</section>
`;
16 changes: 16 additions & 0 deletions examples/svelte-kitchen-sink/src/stories/loaders.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Button from '../components/Button.svelte';

export default {
title: 'Async Loaders',
component: Button,
};

export const AsyncLoaders = (args, { loaded: { text } = {} }) => ({
Component: Button,
props: {
...args,
text,
},
});

AsyncLoaders.loaders = [async () => ({ text: 'asynchronous value' })];