Report this

What is the reason for this report?

Composing Your Svelte Components Using Slots

Published on May 31, 2019
Composing Your Svelte Components Using Slots

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:

Card.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>

Placeholder / fallback content

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:

Card.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>
      <p>😢 No details!</p>
    </slot>
  </div>
</section>

Now here’s an example of how you’d use this Card component:

App.svelte
<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.

Named Slots

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:

Card.svelte
<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:

App.svelte
<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.

Wrapping Up

🦄 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.

Learn more about our products

About the author

Alligator
Alligator
Author
See author profile

Alligator.io is a developer-focused resource that offers tutorials and insights on a wide range of modern front-end technologies, including Angular 2+, Vue.js, React, TypeScript, Ionic, and JavaScript.

Category:
Tags:
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.

Still looking for an answer?

Was this helpful?


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!

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.