Skip to content

Commit

Permalink
Merge pull request #17276 from storybookjs/15498-svelte-default-rende…
Browse files Browse the repository at this point in the history
…r-function

Svelte: Add CSF3 default render function
  • Loading branch information
shilman authored Jan 19, 2022
2 parents b076507 + 7aa1426 commit 6657698
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/svelte/src/client/preview/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { renderToDOM } from './render';
export { render, renderToDOM } from './render';
export { decorateStory } from './decorators';

export const parameters = { framework: 'svelte' };
19 changes: 11 additions & 8 deletions app/svelte/src/client/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { start } from '@storybook/core/client';
import { decorateStory } from './decorators';

import './globals';
import { renderToDOM } from './render';
import { render, renderToDOM } from './render';

const {
configure: coreConfigure,
clientApi,
forceReRender,
} = start(renderToDOM, {
const { configure: coreConfigure, clientApi, forceReRender } = start(renderToDOM, {
decorateStory,
render,
});

export const { setAddon, addDecorator, addParameters, clearDecorators, getStorybook, raw } =
clientApi;
export const {
setAddon,
addDecorator,
addParameters,
clearDecorators,
getStorybook,
raw,
} = clientApi;

const framework = 'svelte';
export const storiesOf = (kind: string, m: any) =>
Expand Down
12 changes: 12 additions & 0 deletions app/svelte/src/client/preview/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import global from 'global';
import { ArgsStoryFn } from '@storybook/csf';
import { RenderContext } from '@storybook/store';
import { SvelteFramework } from './types';
import PreviewRender from './PreviewRender.svelte';
Expand Down Expand Up @@ -37,3 +38,14 @@ export function renderToDOM(

showMain();
}

export const render: ArgsStoryFn<SvelteFramework> = (args, context) => {
const { id, component: Component } = context;
if (!Component) {
throw new Error(
`Unable to render story ${id} as the component annotation is missing from the default export`
);
}

return { Component, props: args };
};
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,23 @@ exports[`Storyshots Button Square 1`] = `
</p>
</section>
`;

exports[`Storyshots Button With Default Render 1`] = `
<section
class="storybook-snapshot-container"
>
<button
class="button svelte-n2tx93"
>
<strong>
Square
corners
</strong>

<br />

Magic!

</button>
</section>
`;
7 changes: 7 additions & 0 deletions examples/svelte-kitchen-sink/src/stories/button.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ Square.args = {
rounded: false,
text: 'Square',
};

export const WithDefaultRender = {
args: {
rounded: false,
text: 'Magic!',
},
};

0 comments on commit 6657698

Please sign in to comment.