// Tutorial //

Composing Your Svelte Components Using Slots

Published on May 31, 2019
    Default avatar
    By Alligator.io
    Developer and author at DigitalOcean.
    Composing Your Svelte Components Using Slots

    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.

    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.


    Want to learn more? Join the DigitalOcean Community!

    Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

    Sign up
    About the authors
    Default avatar
    Developer and author at DigitalOcean.

    Still looking for an answer?

    Was this helpful?
    Leave a comment

    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!