Full Stack SvelteKit, a full and comprehensive video course that will teach you how to build and deploy full stack web applications using SvelteKit. Full Stack SvelteKit
Buy Now

Everything you need to know about Svelte Slots

By Justin Ahinon.

Table of Contents

One of the things you'll find yourself doing a lot when building web applications (in Svelte or other component frameworks) is to create reusable components. Depending on the tool you're using, you will use different approaches to do this. Partials, children, slots, etc.

In Svelte, this is done using the `` special element.

In this article, we'll dive deep into Svelte slots, exploring their types, practical uses, advanced techniques, and best practices.

By the end, you'll have a comprehensive understanding of slots and how to leverage them in your Svelte projects.

Understanding Svelte Slots

Slots in Svelte  are  a special element that serve as placeholders within a component that allow you to inject content from a parent component.

This concept is similar to React’s children or Vue’s slots, providing a way to compose components dynamically.

Slots make it possible to create highly reusable components that can adapt to various contexts.

Why Use Slots?

Slots offer several benefits that make them an essential feature for building modern web applications with Svelte:

  • Flexibility : Slots allow you to design highly flexible components. By defining placeholders for content, you can create a single component structure that can be reused in multiple contexts with different content. This means you can build a versatile component library without duplicating code.

  • Separation of Concerns : With slots, you can keep the structure and behavior of a component separate from its content. This separation enhances the readability and maintainability of your code, as the layout logic is decoupled from the content it displays. It allows developers to focus on the component's functionality while enabling designers to work on the content independently.

  • Enhanced Reusability : Components designed with slots can be easily reused across different parts of your application. This reusability reduces code duplication and ensures consistency in the UI. By passing different content into slots, you can create multiple variations of a component without modifying its core structure.

  • Dynamic Content Injection : Slots enable dynamic content injection, allowing you to  insert different pieces of content into predefined areas of a component at runtime. This is particularly useful for building components that need to render varying content based on user interactions or data fetched from APIs.

  • Better Composition : Using slots, you can compose complex UIs by nesting components and passing content down the component tree. This composition model encourages the creation of small, focused components that do one thing well and can be combined to form more complex structures.

  • Customizable Layouts : When building UI components such as modals, cards, or dashboards, slots provide a way to customize the layout without changing the component's implementation. For instance, you can define slots for different parts of a modal (header, body, footer) and customize the content and structure for each instance of the modal.

In summary,  slots in Svelte enable you to build more flexible, reusable, and maintainable components, enhancing both the development process and the end-user experience. By leveraging slots, you can create a robust and scalable component architecture that adapts to various use cases and design requirements.

Types of Slots in Svelte

Default Slots

Default slots are the most basic type of slot in Svelte. They act as placeholders for content passed from a parent component. Here’s a simple example:

<!-- ParentComponent.svelte -->
<ChildComponent>
  <p>This is some slot content!</p>
</ChildComponent>

<!-- ChildComponent.svelte -->
<div>
  <slot></slot>
</div>

In this example, the content inside   in the parent is injected into the   in the child component.

Named Slots

Named slots allow you to define multiple slots with specific names, providing more control over where content is placed within a component.

<!-- ParentComponent.svelte -->
<ChildComponent>
  <p slot="header">Header Content</p>
  <p slot="footer">Footer Content</p>
</ChildComponent>

<!-- ChildComponent.svelte -->
<div>
  <header>
    <slot name="header"></slot>
  </header>

  <footer>
    <slot name="footer"></slot>
  </footer>
</div>

With named slots, you can pass different pieces of content to specific parts of a component.

Slots with fallback content

Slots can also have fallback content, which is content that is displayed when no content is passed into the slot. This can be useful for providing default content or placeholders when a component is used in a context where content is not available.

<!-- ParentComponent.svelte -->
<ChildComponent>
  <p slot="footer">Footer Content</p>
</ChildComponent>

<!-- ChildComponent.svelte -->
<div>
  <header>
    <slot name="header">
      <h1>Default Header Content</h1>
    </slot>
  </header>

  <footer>
    <slot name="footer">
      <h2>Default Footer Content</h2>
    </slot>
  </footer>
</div>

In the example above, while the child component has two slots defined ( header  and  footer ), only one of them is used in the parent component.

The other slot is left empty, and its content is replaced by the default content defined in the child component.

Using Slots with Props

Slot props in Svelte allow components to pass data back to their slotted content. This is particularly useful for creating dynamic, data-driven components. Here's an example to demonstrate how slot props work:

Passing Data with Slot Props

Imagine you have a  UserList  component that renders a list of users, and you want to pass user data to the slotted content. Here’s how you can achieve this:

<!-- UserList.svelte -->

<script>
  export let users;
</script>

<ul>
  {#each users as user}
    <li>
      <slot {user}>
        <p>User: {user.name}</p>
      </slot>
    </li>
  {/each}
</ul>

In this example, the UserList component iterates over the users array and passes each user object to the slot using the  {user}  shorthand.

The parent component can then use the slot props to render different content for each user:

<!-- Parent component -->

<script>
  import UserList from './UserList.svelte';

  let users = [\
    { name: 'Alice', age: 30 },\
    { name: 'Bob', age: 25 },\
    { name: 'Charlie', age: 35 }\
  ];
</script>

<UserList {users} let:user>
  <div class="user-card">
    <h3>{user.name}</h3>
    <p>Age: {user.age}</p>
  </div>
</UserList>

In the parent component, the  let:user  directive exposes the user prop to the slotted content. This allows the parent component to access and display the user data within the slot.

Named Slots with Props

Named slots can also pass data back to the parent. Here's an example of how to use named slots with props:

<!-- ProductList.svelte -->

<script>
  export let products = [];
</script>

<ul>
  {#each products as product}
    <li>
      <slot name="product" {product}></slot>
    </li>
  {/each}
  <li>
    <slot name="footer"></slot>
  </li>
</ul>
<!-- Parent component -->

<script>
  import ProductList from './ProductList.svelte';

  let products = [\
    { name: 'Laptop', price: 1000 },\
    { name: 'Smartphone', price: 500 },\
    { name: 'Tablet', price: 300 }\
  ];
</script>

<ProductList {products}>
  <div slot="product" let:product class="product-card">
    <h3>{product.name}</h3>
    <p>Price: ${product.price}</p>
  </div>
  <p slot="footer">End of product list</p>
</ProductList>

Here, the  let:product  directive on the element with the  slot="product"  attribute allows the parent component to receive and display product data within the slot. The footer slot is also used to display additional static content at the end of the list.

Using slot props, you can create highly dynamic and reusable components that adapt to various data inputs, enhancing the flexibility and maintainability of your Svelte applications.

Common Pitfalls and How to Avoid Them

Using slots in Svelte can greatly enhance your component design, but there are a few common pitfalls that developers might encounter. Here’s how to avoid them:

Misuse of Slots

Pitfall : Overcomplicating Components

One of the most common mistakes is overcomplicating components by using too many slots or unnecessarily complex slot structures. This can make components difficult to understand and maintain.

Solution:

  • Keep It Simple : Use slots only when necessary. If a component can be built without slots, avoid using them.

  • Clear Naming Conventions : Use clear and descriptive names for named slots to ensure they are easily understandable.

Wrapping Up

Svelte slots are a powerful tool for creating flexible and reusable components. By understanding and utilizing slots, you can build more dynamic and maintainable applications. From default slots to advanced slot props, this guide provides you with the knowledge to implement slots effectively in your projects.

Experiment with slots in your components to see how they can simplify your code and enhance your application's flexibility.

Happy coding!

Stay Updated with the Full Stack SvelteKit course

Want to be the first to know when the Full Stack SvelteKit course launches? Sign up now to receive updates, exclusive content, and early access!