Svelte Slots #22774
Replies: 3 comments 7 replies
-
Good day, (FYI, I am not a regular svelte user) Maybe, you can achieve a similar result by passing content to slots using the Here's how you can modify your example to achieve a similar effect in Svelte: import Button from './Button.svelte';
import TextWithIcon from './TextWithIcon.svelte';
export const Default = () => ({
Component: Button,
props: {}, // Optionally, provide props for your component
slot: `
<TextWithIcon />
`,
}); In the example above, we define a Inside the <script>
export let title;
</script>
<div>
<h1>{title}</h1>
<slot></slot> <!-- Default slot -->
<slot name="footer"></slot> <!-- Named slot -->
</div> In this case, you would need to ensure that the Let me know if this helped! |
Beta Was this translation helpful? Give feedback.
-
I've just run into this myself and have been pulling my hair out after scouring the docs for an answer 😂 So far the only solution I've found anywhere is to use addon-svelte-csf, which I'm not a huge fan of because it still seems to lack some features of the ES6 CSF format (such as specifying tags) and has so-so TS support inside the Svelte markup. It's also experimental, I think. Slots are a major feature of Svelte and the fact that there are so many docs about using Svelte + SvelteKit with Storybook but no discussion of using slots is mind-boggling to me. I'm tempted to open an issue about it. |
Beta Was this translation helpful? Give feedback.
-
Hi 👋. component.svelte:
component.stories.svelte
|
Beta Was this translation helpful? Give feedback.
-
Hi there, thank you for all your work on this project.
I am trying to integrate storybook into a brand new sveltekit project - created today. The project is working as expected - sveltekit version is 1.5.0 and @storybook/svelte is 7.0.17. I am wondering if someone can help me understand how I can use svelte slots in my stories. I cannot find any documentation on how to do this, neither with a default slot nor a named slot.
Say I have a simple svelte button component named Button.svelte like this:
I create a story named Button.stories.ts like this:
When the storybook renders in the browser i can see a section called Slots, which lists one slot named default. What I can't figure out is how to populate content in this slot. In the react docs the render function for the story can return JSX, which would allow me to place text or a sub component, but this isn't the case for the svelte renderer.
React example:
My example is very simple, where the slot would just receive text, how can I add text to the slot. And, how about a more complex example, like the react example above, where a slot (default or named) can take a svelte component?
Any help greatly appreciated.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions