storybookjs/addon-svelte-csf

How to use components with slot variable.

Closed this issue ยท 3 comments

scorsi commented

Hello,

This title may be not clear... An example may be better:

I have this component which declares a slot and gives two slot variable which are in fact components

<script lang="ts">
	import AvatarImage from './AvatarImage.svelte';
	import AvatarFallback from './AvatarFallback.svelte';
</script>
<div>
	<slot {AvatarImage} {AvatarFallback} />
</div>

We use it like so:

<Avatar let:AvatarImage let:AvatarFallback>
	<AvatarImage src="https://randomuser.me/api/portraits/women/33.jpg" />
	<AvatarFallback>๐Ÿ‘ฉโ€๐Ÿ’ป</AvatarFallback>
</Avatar>

I wonder how to use that inside the svelte addon because I tried multiple things and still got this error

this={...} of <svelte:component> should specify a Svelte component.

Here how I use it (which doesn't work)

<Meta title="Avatar" component={Avatar} />

<Template let:args> <!-- also tried here -->
	<Avatar {...args} />
</Template>

<Story name="Example" let:AvatarImage let:AvatarFallback>
	<AvatarImage src="https://randomuser.me/api/portraits/women/33.jpg" />
	<AvatarFallback>๐Ÿ‘ฉโ€๐Ÿ’ป</AvatarFallback>
</Story>

I guess it works similar has if it was:

<script>
  let active = false;
</script>
<slot {active} />
Tyneor commented

Hello,

You have to use your component in the Template or in the Story, like so :

<Story name="Example">
  <Avatar let:AvatarImage let:AvatarFallback>
    <AvatarImage src="https://randomuser.me/api/portraits/women/33.jpg" />
    <AvatarFallback>๐Ÿ‘ฉโ€๐Ÿ’ป</AvatarFallback>
  </Avatar>
</Story>

It worked on my side, hope this helps !

It looks like this was answered

scorsi commented

Hello,

You have to use your component in the Template or in the Story, like so :

<Story name="Example">
  <Avatar let:AvatarImage let:AvatarFallback>
    <AvatarImage src="https://randomuser.me/api/portraits/women/33.jpg" />
    <AvatarFallback>๐Ÿ‘ฉโ€๐Ÿ’ป</AvatarFallback>
  </Avatar>
</Story>

It worked on my side, hope this helps !

Thanks !