Another Svelte3 cheat sheet
Tags: svelte, javascript
Published: 2022-01-27 13:05:01

Be aware it's **not an exhaustive list**.

## Basic Concepts

Svelte is a phenomenal JavaScript compiler that generates blazing fast and highly interactive apps.


### Reactivity

It measures how the DOM syncs with the current state’s updates. Most Javascript frameworks, such as React, adds an intermediary layer for that. In React, it’s the virtual DOM. In Svelte, it’s the build time.


### Looks familiar?

Svelte is pretty close to pure HTML, CSS, and Js. However, it adds a few extensions to save time and generate an ultra-optimized vanilla JavaScript bundle.


### .svelte files

Components are `.svelte` files that use a superset of HTML.


### Typical structure

```
project
│
├── .gitignore
├── node_modules/
├── src/
│   ├── App.svelte
│   └── main.js
│
├── scripts/
│   └── special-script.js (optional)
│
├── public/
│   ├── global-styles.css
│   ├── index.html
│   └── favicon.png
│
├── LICENSE
├── README.md
├── rollup.config.js
└── package.json
```


### No virtual DOM

Virtual DOM is pure overhead for Svelte lovers, but I don't think you should focus on that point. Instead, the coolest thing is that you can get the same model and features provided by React and other frameworks without any diff algorithm and its limitation.

<a href="https://svelte.dev/blog/virtual-dom-is-pure-overhead">
    Source: virtual DOM is pure overhead
</a>

### Svelte compilation

<a href="https://lihautan.com/the-svelte-compiler-handbook/">
    Read the compiler handbook 
</a>(pretty hard to find better explanation!)

## Svelte styles

### Scoped styles

Don't be confused by the typical structure. While you can have global styles, each component has its own **automatically scoped** styles:


```html
// section.svelte
<style>
  section {
    background-color: limegreen;
    color: black;
  }
</style>

<section>
  <slot/>
</section>
```

Svelte auto-generates classes, for example, <code>svelte-1kxxubc</code>, which is a hash of your component styles.


### Custom CSS classes

```html
// paragraph.svelte
<style>
  .para {
    background-color: limegreen;
    color: black;
  }

  .graph {
      letter-spacing: -.1em;
  }
</style>

<p class:para class:graph>
  <slot/>
</p>
```

### Dynamic CSS

```html
<script>
    export let color = "fuschia";
  </script>
  
  <style>
    p {
        color: {color}
    }
  </style>
  
  <p>I'm a FBI agent</p>
```

## Variables

### Basics
 
```html
<script>
    export let a = 'a';
</script>

<p>It's {a}</p>
```

### Destructuring

`$:` (dollar label) is Svelte magic to make things reactive automatically:


```html 
<script>
    export let article;
  
    $: ({ title, excerpt, url } = article);
  </script>
  
  <article>
      <h2><a href="{url}">{title}</a></h2>
      <p>{excerpt}</p>
  </article>
```

### Import

```html 
<script>
    import z from 'external';
</script>

<p>It's {z}</p>
```

### Loop, loop, loop...

```html 
<script>
    export let items = [
        'item1',
        'item2',
        'item3',
    ];
    </script>
    <ol>
    {#each items as item}
        <li>{item}</li>
    {/each}
    </ol>
```

### Destructuring and loop

```html 
<script>
    export let articles;
</script>
  
{#each articles as {title, url, excerpt }}
  <article>
    <h2><a href="{url}">{title}</a></h2>
    <p>{excerpt}</p>
  </article>
{/each}
```

## Conditions

```html 
{#if CONDITION1}
    <p>case 1</p>
{:else if CONDITION2}
    <p>case 2</p>
{:else}
    <p>everything else</p>
{/if}
```

## Events

You can use `on:EVENT`, for example, `on:click` or `on:mouseup`.


```html 
<script>
    let count = 1;
    const increment = () => {
        count += 1
    }
</script>

<p>counter: {count}
<button on:click={increment}>Increment</button>
```

### Lifecycle onmount

After the first render:

```html 
<script>
    import { onMount } from 'svelte';

    let data = [];

    onMount(async () => {
       const res = await fetch(`https://myapi.com/endpoint`);
       data = await res.json();
    });
</script>
```

### Dispatcher

Dispatcher is meant for custom events (not like classic DOM events). Svelte provides `createEventDispatcher` for that:


```html 
// Button.svelte
<button on:click={fireStarter}>
  Custom click event
</button>

<script>
  import { createEventDispatcher } from "svelte";
  const dispatch = createEventDispatcher();

  export let fireStarter = () => dispatch("fireStarter");
</script>
```

```html 
<script>
  import Button from './Button.svelte';
</script>
<Button on:fireStarter={() => console.log('fire starter')} />
```

## Passing props

```html
// ChildComponent.svelte
<script>
  export let text = "no one" // default value
</script>
<h1>Cotton-eyed {text}</h1>
```

```html
// App.svelte
<script>
  import ChildComponent from './ChildComponent.svelte'
</script>

<ChildComponent text="joe" /> // <h1>Cotton-eyed joe</h1>
```

## Bindings

Svelte allows you to attach event handlers to elements with a very convenient and readable syntax.

### `bind:value`

```html 
<script>
let guy = "cotton-eyed joe"
</script>
<input bind:value={guy}>
<p>Where did you come from? Where did you go? {guy}</p>
```

### `bind:checked`

```html
<script>
    let show = false;
  </script>
  
  <label>
    <input type="checkbox" bind:checked="{show}" />
    Show
  </label>  
  {#if show}
  Hello
  {:else}
  Bye
  {/if} 
```

### `bind:group`

<a href="https://svelte.dev/tutorial/group-inputs">A practical use is for radio inputs</a>


### `bind:this`

```html 
<script>
    import { onMount } from 'svelte';

    let canvasElement;

    onMount(() => {
	    const ctx = canvasElement.getContext('2d');
	    drawStuff(ctx);
    });
</script>
```

## Stores

Stores help passing data between components when you don’t have only parent-child relationships in your component hierarchy.

You can read <a href="https://blog.julien-maury.dev/en/svelte-stores/">this Introduction</a>.


## Transitions

Svelte handles <a href="https://svelte.dev/tutorial/transition">transitions</a> natively. You can even pass parameters.

## Slots

`slot` elements allow for nesting one or several components inside another. You can also pass data from children to the parent.

```html
<script>
import Article from "./Article.svelte";
</script>

<Article>
    <span slot="title">Title1</span>
    <span slot="excerpt">excerpt1</span>
</Article>
```

```html
// Article.svelte
<article>
  <h2>
    <slot name="title">
      <span class="error">No title</span>
    </slot>
  </h2>
  <slot name="excerpt">
      <span class="error">No excerpt</span>
    </slot>
</article>
```

## Debug

To inspect value, it’s best to use `{@debug VAR}`:

```html
{@debug post}
<h1>{post.title}</h1>
```

It inspects the variable and pause the execution.

## Svelte frameworks

There are helpful resources to ease your dev:

### Sveltekit

```
npm init svelte@next my-app
cd my-app
npm install
npm run dev -- --open
``` 
<a href="https://kit.svelte.dev/">Source: kit.svelte.dev</a>

### Plenti

<a href="https://plenti.co/">What is Plenti?</a>