OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do we do "named slots" in in Svelte 5?

  • Thread starter Thread starter Tithos
  • Start date Start date
T

Tithos

Guest

Svelte 5 Slots are Confusing​


Now, with Svelte 5 at pre-release, I feel I can express some confusion about slots in Svelte 5. In Svelte 4, slot and names slots where done in the following way:

ContactCard.svelte


Code:
<article class="contact-card">
    <h2>
        <slot name="name">
            <span class="missing">Unknown name</span>
        </slot>
    </h2>

    <div class="address">
        <slot name="address">
            <span class="missing">Unknown address</span>
        </slot>
    </div>

    <div class="email">
        <slot name="email">
            <span class="missing">Unknown email</span>
        </slot>
    </div>
</article>

<style>...</style>

+page.svelte


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

<ContactCard>
    <span slot="name"> P. Sherman </span>

    <span slot="address">
        42 Wallaby Way<br />
        Sydney
    </span>
</ContactCard>

Pretty easy and straight forward in my humble opinion.

Svelte 5 Single Slot example​

Component.svelte


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

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

+page.svelte


Code:
<script>
  import { Component } from '$lib';
</script>

<Component>
  ...content...
</Component>

Also easy...

My Question...​


How do we do "named slots" in this new paradigm

<h1>Svelte 5 Slots are Confusing</h1>
<p>Now, with Svelte 5 at pre-release, I feel I can express some confusion about slots in Svelte 5. In Svelte 4, slot and names slots where done in the following way:</p>
<h3><code>ContactCard.svelte</code></h3>
<pre class="lang-js prettyprint-override"><code><article class="contact-card">
<h2>
<slot name="name">
<span class="missing">Unknown name</span>
</slot>
</h2>

<div class="address">
<slot name="address">
<span class="missing">Unknown address</span>
</slot>
</div>

<div class="email">
<slot name="email">
<span class="missing">Unknown email</span>
</slot>
</div>
</article>

<style>...</style>
</code></pre>
<h3><code>+page.svelte</code></h3>
<pre class="lang-js prettyprint-override"><code><script>
import ContactCard from './ContactCard.svelte';
</script>

<ContactCard>
<span slot="name"> P. Sherman </span>

<span slot="address">
42 Wallaby Way<br />
Sydney
</span>
</ContactCard>
</code></pre>
<p>Pretty easy and straight forward in my humble opinion.</p>
<h2>Svelte 5 <strong>Single Slot</strong> example</h2>
<h3><code>Component.svelte</code></h3>
<pre class="lang-js prettyprint-override"><code><script>
let { children } = $props();
</script>

<div>
{@render children()}
</div>
</code></pre>
<h3><code>+page.svelte</code></h3>
<pre class="lang-js prettyprint-override"><code><script>
import { Component } from '$lib';
</script>

<Component>
...content...
</Component>
</code></pre>
<p>Also easy...</p>
<h2>My Question...</h2>
<p>How do we do "named slots" in this new paradigm</p>
 

Latest posts

Top