Svelte Slots

By

Published · Updated

Tagged: Svelte

Use Svelte 5 snippets in place of slots. Pass default content, add named snippets, and migrate a legacy slot with let: props.

In Svelte 5 we use snippets to pass markup into a component. If you’re coming from Svelte 4, think of children as the default slot. The markup between a component’s opening and closing tags becomes the children snippet.

Render children and set a fallback

In Button.svelte, we get the snippet from $props() and render it inside the button. I use an if block here so the button has text even if the parent leaves it empty:

<script>
  let { children } = $props()
</script>

<button type="button">
  {#if children}
    {@render children()}
  {:else}
    Save
  {/if}
</button>

In App.svelte, the first button gets its text from the parent, and the second uses the fallback:

<script>
  import Button from './Button.svelte'
</script>

<Button>Save changes</Button>
<Button />

If the content is optional and you don’t need a fallback, you can use {@render children?.()} on its own. The ?. skips the call when children is missing.

Replace named slots with named snippets

Say a card has room for a heading, body, and optional footer. In Card.svelte, we can take separate snippets for each part:

<script>
  let { header, children, footer } = $props()
</script>

<article>
  <header>{@render header()}</header>
  <div>{@render children()}</div>

  {#if footer}
    <footer>{@render footer()}</footer>
  {/if}
</article>

A snippet declared directly inside <Card> becomes a prop with the same name. The paragraph outside the named snippets becomes children. Put this in App.svelte:

<script>
  import Card from './Card.svelte'
</script>

<Card>
  {#snippet header()}
    <h2>Account</h2>
  {/snippet}

  <p>Update your account details.</p>

  {#snippet footer()}
    <button type="button">Save</button>
  {/snippet}
</Card>

The card expects a header and some body content each time you use it, so it calls those snippets directly. You can leave out the footer entirely.

Pass data to a snippet

A snippet can take arguments. This is how I would replace a scoped slot, where a child component passed a value back to the parent’s markup with let:.

In FruitList.svelte, the list component decides which items to show. The parent decides how to write each row:

<script>
  let { row } = $props()
  const fruit = ['Pear', 'Apple']
</script>

<ul>
  {#each fruit as name}
    <li>{@render row(name)}</li>
  {/each}
</ul>

In App.svelte, name is the argument for the row snippet:

<script>
  import FruitList from './FruitList.svelte'
</script>

<FruitList>
  {#snippet row(name)}
    <strong>{name}</strong>
  {/snippet}
</FruitList>

This renders two list items, with each fruit name in bold.

Keep legacy slots while you migrate

In a Svelte 4 component, you might see <slot>Save</slot> for default content with a fallback, or <slot name="footer" /> for a named slot. The parent fills that named slot with an element such as <p slot="footer">Changes are saved automatically.</p>.

Svelte 5 still supports these slots in existing components. When you change a component to use {@render}, update its callers that pass content with the old slot= syntax.

You can pass snippets to a legacy component that still declares slots, so you can switch the callers first if that helps.

For a Svelte component compiled as a web custom element, keep using the browser’s <slot> element. The Svelte 5 migration guide shows both syntax styles.

Want me to talk about your product? You can sponsor this site.

Related posts about svelte: