Alligator.io
In Svelte components can be composed together using slots. Composition means allowing your components to contain other components or HTML elements. Slots are made possible in Svelte by using the <slot>
component inside components that can accept components or markup within.
It’s something that we’re already used to do naturally with HTML elements. Let’s demonstrate how using the <slot>
component works by building a simple Card
component
This Card
component is similar to the one I used to demonstrate using props in Svelte:
<script>
export let title;
export let imageUrl;
</script>
<style>
/* Something to make it look pretty */
</style>
<section>
<h1>
<img src={imageUrl} alt="Avatar for {title}" />
{title}
</h1>
<div>
<slot />
</div>
</section>
As you can see, we simply put the <slot>
component wherever we want to allow for users of the component to be able to add custom components and markup.
We can use it in a self-closing fashion:
<slot />
Or with the closing tag:
<slot></slot>
You can put fallback content without the <slot>
component and this content will be used if the component that contains the slot is used without any elements within:
<script>
export let title;
export let imageUrl;
</script>
<style>
/* Something to make it look pretty */
</style>
<section>
<h1>
<img src={imageUrl} alt="Avatar for {title}" />
{title}
</h1>
<div>
<slot>
<p>😢 No details!</p>
</slot>
</div>
</section>
Now here’s an example of how you’d use this Card
component:
<script>
import Card from "./Card.svelte";
const items = [
{
title: "Pirate",
description: "Argg!!",
imageUrl: "https://alligator.io/images/pirate.svg"
},
{
title: "Chef",
description: "À la soupe!",
imageUrl: "https://alligator.io/images/chef.svg"
}
];
</script>
{#each items as item}
<Card title={item.title} imageUrl={item.imageUrl}>
<hr />
<p>{item.description}</p>
</Card>
{/each}
With this, an horizontal line and a paragraph with the item’s description will appear inside our Card
component in the place of the <slot>
component.
You’re not limited to just one slot per component. You can name your slots using the name attribute on the <slot>
component and then you can have as many slots as you want inside a component.
As you can see from the following example, you can also combine named slots with a default/unnamed slot:
<style>
/* Make it pretty! */
</style>
<section>
<slot name="title" />
<slot name="description">
<p>😮 No description!</p>
</slot>
<footer>
<slot />
</footer>
</section>
And we make use of the named slots by adding a name attribute to the parent element for each slot location:
<script>
import Card from "./Card.svelte";
const items = [
{
title: "Pirate",
description: "Argg!!",
imageUrl: "https://alligator.io/images/pirate.svg"
},
{
title: "Chef",
description: "À la soupe!",
imageUrl: "https://alligator.io/images/chef.svg"
}
];
</script>
{#each items as item}
<Card>
<h1 slot="title">
<img src={item.imageUrl} alt="Avatar for {item.title}" />
{item.title}
</h1>
<p slot="description">{item.description}</p>
<p>Something else!</p>
</Card>
{/each}
Naturally, there can only be one default slot.
🦄 Now go on and compose to your heart’s content! Check out the official tutorial for more examples of using slots in Svelte. One additional interesting thing that you can learn from the tutorial is that slots can make use of props, called slot props, to pass data back to the component using them.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!