Skip to content

Svelte 5: Docs - Migration of multiple named slots to children/snippets patternΒ #12977

@roycrippen4

Description

@roycrippen4

Describe the bug

Migrating named slots to snippets and the @render children() pattern is not documented well, or is very difficult to find.

The new snippet system is great and a big step forward, thank you all for your hard work.
That being said, snippets and their relationship to a component's children seems under-documented.

The examples I found resembled the following:

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

{#if children}
  {@render children()}
{/if}

The only information I could find online about migrating named slots to snippets was this stack overflow article.

Proposed Documentation (adapted from the linked stack overflow post):

Svelte 4

Component Definition

<!-- ContactCard.svelte -->
<article>
  <!-- With fallback content  -->
  <slot name="name">
    <span>No name found!</span>
  </slot>

  <!-- Without fallback content  -->
  <slot name="address"></slot>
</article>

Component Usage

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

<ContactCard>
  <span slot="name"> P. Sherman </span>
  <span slot="address"> 34 Wallaby Way </span>
</ContactCard>

Svelte 5

Component Definition

<!-- ContactCard.svelte -->
<script>
  let { name, address } = $props();
</script>

<article>
  <!-- With fallback content -->
  {#if name}
    {@render name()}
  {:else}
    <span>No name found!</span>
  {/if}

  <!-- Without fallback content -->
  {@render address?.()}
</article>

Component Usage

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

<ContactCard>
  {#snippet name()}
    P. Sherman
  {/snippet}

  {#snippet address()}
    42 Wallaby Way <br />
    Sydney
  {/snippet}
</ContactCard>

Reproduction

docs issue

Logs

No response

System Info

docs issue

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions