Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

Svelte offers different ways for components to communicate with each other, including props, slots, etc. You will need to integrate slots to create flexible and reusable components in your Svelte applications.

Understanding Slots in Svelte

Slots in the Svelte framework work similarly to slots in Vue. They provide a way to pass content from a parent to a child component. With slots, you can define placeholders within a component's template where you can inject content from a parent component.

This content can be plain text, HTML markup, or other Svelte components. Working with slots enables you to create highly customizable and dynamic components that adapt to different use cases.

Creating a Basic Slot

To create a slot in Svelte, use the slot element within a component's template. The slot element is a placeholder for the content you will pass from the parent component. By default, the slot will render any content passed to it.

Here is an example of how to create a basic slot:

 <main>
  This is the child component
  <slot></slot>
</main>

The code block above represents a child component that uses the slot element to get content from a parent component.

To pass content from a parent component to a child component, you will first import the child component into the parent component. Then, instead of using a self-closing tag to render the child component, use an opening and closing tag. Finally, within the component's tags, write the content you want to pass from the parent-to-child component.

For example:

 <script>
import Component from "./Component.svelte";
</script>

<main>
This is the parent component
<Component>
<span>This is a message from the parent component</span>
</Component>
</main>

In addition to passing content from the parent-to-child components, you can provide fallback/default content in the slots. This content is what the slot will display if the parent component does not pass any content.

Here is how you can pass a fallback content:

 <main>
  This is the child component
  <slot>Fallback Content</slot>
</main>

This code block provides the text "Fallback Content" as a fallback content for the slot to display if the parent component doesn’t provide any content.

Passing Data Across Slot With Slot Props

Svelte allows you to pass data to slots using slot props. You use the slot props in situations where you want to pass some data from the child component to the content you are slotting in.

For example:

 <script>
  let message = 'Hello Parent Component!'
</script>

<main>
  This is the child component
  <slot message={message}></slot>
</main>

The code block above represents a Svelte component. Within the script tag, you declare the variable message and assign the text "Hello Parent Component!" to it. You also pass the message variable to the slot prop message. This makes the message data available to the parent component when it injects content into this slot.

 <script>
import Component from "./Component.svelte";
</script>

<main>
This is the parent component
<Component let:message>
<div>
The child component says {message}
</div>
</Component>
</main>

The let:message syntax allows the parent component to access the message slot prop that the child component provides. The div tag's message variable is the data from the message slot prop.

Note that you're not limited to a single slot prop, you can pass multiple slot props as required:

 <script>
  let user = {
    firstName: 'John',
    lastName: 'Doe'
  };
</script>

<main>
  This is the child component
  <slot firstName={user.firstName} lastName={user.lastName}></slot>
</main>

In the parent component:

 <script>
import Component from "./Component.svelte";
</script>

<main>
This is the parent component
<Component let:firstName let:lastName>
<div>
The user's name is {firstName} {lastName}
</div>
</Component>
</main>

Working With Named Slots

You can use named slots when you want to pass multiple slots in your components. Named slots make it easier to manage various slots, as you can give each slot a unique name. With named slots, you can build complex components with varying layouts.

To create a named slot, pass a name prop to the slot element:

 <div>
  This is the child component
  
  <p>Header: <slot name='header'></slot></p>
  <p>Footer: <slot name='footer'></slot></p>
</div>

In this example, there are two named slots, the slot named header and the slot named footer. Using the slot prop, you can pass content to each slot from the parent component.

For example:

 <script>
import Component from "./Component.svelte";
</script>

<main>
This is the parent component
<Component>
<span slot="header">This will be passed to the header slot</span>
<span slot="footer">This will be passed to the footer slot</span>
</Component>
</main>

This code demonstrates how you use the slot prop to pass content to named slots. In the first span tag, you pass the slot prop with the value header. This ensures that the text within the span tag will render in the header slot. Similarly, the text within the span tag with the slot prop’s value footer will render in the footer slot.

Understanding Slot Forwarding

Slot forwarding is a feature in Svelte that allows you to pass content from a parent component through a wrapper component into a child component. This can be very useful in cases where you want to pass content from unrelated components.

Here is an example of how to use slot forwarding, first create the child component:

 <main>
  This is the child component
  <slot name="message"></slot>
</main>

Next, you create the wrapper component:

 <script>
    import Component from "./Component.svelte";
</script>

<main>
    <Component>
        <div slot='message'>
            <slot name="wrapperMessage"></slot>
        </div>
    </Component>
</main>

In this code block, you are passing another named slot into the message slot of the child component.

Then, in the parent component, write this code:

 <script>
import Wrapper from "./Wrapper.svelte";
</script>

<main>
This is the parent component
<Wrapper>
<div slot="wrapperMessage">
This is from the parent component
</div>
</Wrapper>
</main>

In the above structure, the content "This is from the parent component" gets passed through the wrapper component and directly into the child component thanks to the wrapperMessage slot inside the wrapper component.

Make Your Life Easier With Svelte Slots

Slots are a powerful feature in Svelte that allows you to create highly customizable and reusable components. You have learned how to create basic slots, named slots, use slot props, etc. By understanding the different types of slots and how to use them, you can build dynamic and flexible user interfaces.