Tutorial

Composing Your Svelte Components Using Slots

Published on May 31, 2019
    Default avatar

    By Alligator.io

    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.

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    Alligator.io

    author

    Still looking for an answer?

    Ask a questionSearch for more help

    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!

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel