Svelte logo

Svelte by Example: Slots

The contents of a Svelte component can be passed through slots. Svelte supports default slots, named slots, and slots that pass data back to the slotted content (also known as scoped slots).

In this example, we'll wrap our todo list in a layout component.

A slot can be declared with the self-closing <slot> tag.

Slots can have a name.

Default slot contents can be passed as children.

Named slot contents can be passed by setting a slot attribute on an HTML element.

  <slot />

  <slot name="footer" />
  import Layout from './Layout.svelte';

  <h1>Todo List</h1>
  <!-- … -->

  <p slot="footer">
    Subtracting from your list of priorities
    is as important as adding to it.

You can check whether a component has provided named slot contents through $$slots.

{#if $$slots.footer}
    <slot name="footer" />

With slot props, a component can pass data to the slot content.

Data can be passed to slot like it's a component.

Data can be bound to a variable with a let:<name> attribute.

  let quote = "Subtracting from your list…";

  <slot {quote} />
  import Quote from "./Quote.svelte";

<Quote let:quote={quote}>
  <p>Quote: {quote}</p>