Add tutorial step for key blocks

pull/6420/head
Eirik Vageskar 5 years ago
parent 17c5402e31
commit 74a4c9fe7d

@ -0,0 +1,19 @@
<script>
import { fly } from 'svelte/transition';
let number = 0;
</script>
<div>
The number is:
<span style="display: inline-block" in:fly={{ y: -20 }}>
{number}
</span>
</div>
<br />
<button
on:click={() => {
number += 1;
}}>
Increment
</button>

@ -0,0 +1,21 @@
<script>
import { fly } from 'svelte/transition';
let number = 0;
</script>
<div>
The number is:
{#key number}
<span style="display: inline-block" in:fly={{ y: -20 }}>
{number}
</span>
{/key}
</div>
<br />
<button
on:click={() => {
number += 1;
}}>
Increment
</button>

@ -0,0 +1,16 @@
---
title: Key blocks
---
Key blocks destroy and recreate their contents when the value of an expression changes.
```html
{#key value}
<div transition:fade>{value}</div>
{/key}
```
This is useful if you want an element to play its transition whenever a value changes.
Wrap the `<span>` element in a key block depending on `number`. This will make the
animation play whenever you press the increment button.
Loading…
Cancel
Save