Six Interesting Svelte Features

Published on April 18, 2024

Introduction

Having only recently started using SvelteKit I was impressed with how easy it was to get started and surprised by how much I enjoyed using it. Here are six of the features that I found particularly useful whilst I was learning the framework.

1. Shorthand Properties

When passing a property to an element or component you can use the shorthand syntax if the variable name is the same as the property name. This makes the code cleaner and easier to read, and saves you having to specify the property name.

1
<script>
2
    export let href = '/';
3
</script>
4

5
<a {href}>Link</a>

2. Named Slots

Svelte provides the slot element which allows you to pass child content to a component. If you want to have multiple slots on a component you can give each slot a name and then reference that name when passing content to the slot, making it easy to control how the child content is rendered.

1
<!-- component.svelte -->
2
<div>
3
    <slot name="header" />
4
    <slot name="content" />
5
</div>
6

7
<!-- +page.svelte -->
8
<Component>
9
    <!-- This will be rendered in the header slot -->
10
    <h1 slot="header">Header</h1>
11
    <!-- This will be rendered in the content slot -->
12
    <p slot="content">Content</p>
13
</Component>

3. Element Binding Directive

Getting a reference to an element can be frustrating in some frameworks, but Svelte makes it easy with the bind:this directive. Once you have the element reference you can access its exported properties and functions directly.

1
<script>
2
    let element;
3
</script>
4

5
<Component bind:this={element} />

4. Custom Component Directives

Svelte allows you to emit custom events that can be subscribed to using the on: directive, making it easy to trigger an action in or pass data out of a parent component.

1
<!-- component.svelte -->
2
<script>
3
    import { createEventDispatcher } from 'svelte';
4
    
5
    const dispatch = createEventDispatcher();
6
    
7
    function handleClick() {
8
        dispatch('click', { message: 'Hello from the child component!' });
9
    }
10
</script>
11

12
<button on:click={handleClick}>Trigger</button>
13

14
<!-- +page.svelte -->
15
<Component on:click={e => console.log(e.detail.message)} />

For more information see the docs.

5. Exported Functions

Using the export keyword you can export functions from a component that can be invoked from outside the component, making it easy to trigger an action on or pass data into a child component.

1
<!-- component.svelte -->
2
<script>
3
    export function trigger() {
4
        console.log('Hello from the child component!');
5
    }
6
</script>
7

8
<!-- +page.svelte -->
9
<script>
10
    import Component from './component.svelte';
11

12
    let component;
13

14
    function handleClick() {
15
        component.trigger();
16
    }
17
</script>
18

19
<button on:click={handleClick}>Trigger</button>
20
<Component bind:this={component} />

6. Transitions

When using #await you can animate the new elements as they are added to the DOM using Svelte's in-built transitions. The transitions are applied to the element using the in: directive and can be easily customised.

1
<script>
2
    import { fade } from 'svelte/transition';
3
    
4
    let promise = new Promise(resolve => setTimeout(resolve, 2000));
5
</script>
6

7
{#await promise}
8
    <p>Waiting...</p>
9
{:then _}
10
    <p in:fade>Loaded!</p>
11
{/await}

For more information see the docs.

Conclusion

SvelteKit is a powerful and fully featured framework with comprehensive documentation and a very active community. This list only scratches the surface of all the great features built in.

I've personally fallen in love with it and would highly recommend you give it a try on your next project.

Copyright © 2021 - 2024. All rights reserved.