DEV Community

Yimnai Nerus
Yimnai Nerus

Posted on

Working With Svelte 5 Snippets

I've been writing Svelte Since January 2024 and have used Svelte 5 since it was first announced.Something which caught my attention was the introduction of Snippets in Svelte 5.
What are Svelte 5 Snippets? Snippets, together with render tags are a more intuitive way(in my opinion) to write reusable chunks of markup within your components. Here's an example from the Svelte 5 previe documentation Link.
Formerly, this is how you'll do something

Repeated Chunks in Markup
but with Snippets, here's how you can achieve the same

Better Markup management with Snippets
From these two snippets, it becomes obvious the DX improvements something like Snippets bring to the ecosystem.
After getting comfortable with this though, you might wonder how you could use this to replace something like slots, if those slots are defined in separate components. I know because I asked myself the same question too when I started. Fortunately, the good folks at Svelte have already answered that question for us. Here's how you'll achieve that

Passing Snippets As Props

A bigger issue I had which is the crux of this article is this; the fact that at first glance you have to define the snippets within the same component was a bit uncomfortable as that will mean my components could grow exponentially in size and I wanted to avoid that at all cost. My goal was to be able to define whatever child component I wanted in it's own file it the situation required it and still be able to pass it as a Snippet to the appropriate parent.
If my parent component is defined as such:

<script lang="ts">
    import type { Snippet } from "svelte";


    type Props = {
        image: Snippet;
        meta: Snippet;
        description: Snippet;
        controls: Snippet;
    }

    let { image, meta, description, controls }: Props = $props()
</script>

<div class="card">
    <div class="left">
        {@render image()}
    </div>
    <div class="right">
        {@render meta()}
        {@render description()}
        {@render controls()}
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

I do not have to define all these snippets(image, meta, description and controls) within the component that calls them if doing so will make my components too large. Instead, I could define each of them in their own component and simply pass them within snippets with the appropriate name in whatever component the parent component is being called in. Here's how it works;

<ProductQuickviewWrapper>
            {#snippet image()}
                <ProductQuickviewImage />
            {/snippet}
            {#snippet meta()}
                <ProductQuickviewMeta />
            {/snippet}
            {#snippet description()}
                <ProductQuickviewDescription />
            {/snippet}
            {#snippet controls()}
                <ProductQuickviewControls />
            {/snippet}
</ProductQuickviewWrapper>
Enter fullscreen mode Exit fullscreen mode

In this way, I do not have to define all of them in this component, thus making my components cleaner and easy to plug other parts in and out without worrying about any potential increase in the size of components.

Thank you for reading all through. Will like to hear how you deal with situations like this one in the event you're using the Svelte 5 preview already.

Top comments (0)