Skip to content

Commit

Permalink
introduce asChild
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Nov 13, 2024
1 parent ee29a01 commit 55c4167
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 9 additions & 0 deletions examples/Button.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@
<Button {...args}>🧡 Svelte</Button>
{/snippet}
</Story>

<!--
Input values from the controls tab will **not** be passed down to story.
Neither the `args` from meta.
-->
<Story name="Static story" parameters={{ controls: { disable: true } }} asChild>
<h1>This is a static story</h1>
<Button>Static button</Button>
</Story>
16 changes: 15 additions & 1 deletion src/runtime/Story.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@
* NOTE: Can be omitted if a default template is set with [`setTemplate()`](https://github.com/storybookjs/addon-svelte-csf/blob/main/README.md#default-snippet)
*/
children?: TChildren;
/**
* Make the children the actual story content. This is useful when you want to create a **static story**.
*/
asChild?: boolean;
template?: never;
}
| {
children?: never;
asChild?: never;
/**
* The content to render in the story with a snippet taking `args` and `storyContext` as parameters
*
Expand All @@ -95,6 +100,7 @@
exportName: exportNameProp,
play,
template,
asChild = false,
...restProps
}: Props = $props();
const exportName = exportNameProp ?? storyNameToExportName(name!);
Expand Down Expand Up @@ -139,7 +145,15 @@
{#if template && isSnippet(template)}
{@render template(renderer.args, renderer.storyContext)}
{:else if children && isSnippet(children)}
{@render children()}
{#if asChild}
{@render children()}
{:else if renderer.storyContext.component}
<renderer.storyContext.component {...renderer.args}>
{@render children()}
</renderer.storyContext.component>
{:else}
{@render children()}
{/if}
{:else if storiesTemplate}
{@render storiesTemplate(renderer.args, renderer.storyContext)}
{:else if renderer.storyContext.component}
Expand Down

0 comments on commit 55c4167

Please sign in to comment.