Svelte Cheatsheet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
. # Svelte
├── Component Format
├── Template Syntax
│ ├── Tags
│ ├── Attributes and Props
│ ├── Text Expressions
│ ├── Comments
│ ├── Block
│ ├── Element Directives
│ ├── Component Directives
│ ├── <slot />
│ └── Special Elements
├── Run Time
│ ├── Lifecycle | Context
│ ├── Store
│ ├── Motion | Transition | Animate
│ ├── Custom Element API
│ └── Client-side | Server-side
└── Compile Time
  • A Svelte Component for Preview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!-- Steps.svelte -->
<script>
// logic
import Step from "./Step.svelte";

function onProgressDot() {}
function handleNotify() {}
</script>

<!-- Step.svelte -->
<Step current="1" direction="vert" {onProgressDot} on:notify={handleNotify} />
<img alt="" src="" class="upper" />

<style>
/* scope */
.upper {}

/* global styling */
:global(body) {
}
div :global(strong) {
}
</style>

<!-- Step.svelte -->
<script>
import { createEventDispatcher } from "svelte";

export let current; // props
export let direction = "hori"; // props with default value

// reactive declaration, ECMA labeled statement
$: [statement]
$: cubecurrent = current ** 4;
$: {
const square = () => current ** 2;
console.info("square:", square());
console.info("cubecurrent:", cubecurrent);
}

const dispatch = createEventDispatcher();
let onNotify = () => {
dispatch("notify", ...params);
};
let handleClick = () => variable++; // self handler

export let onProgressDot = () => {};
</script>

<!-- markup | multi markup -->
<span> {current} </span>
<span> {cubecurrent} </span>
<span> {direction} </span>
<button on:click={onNotify} />
<button on:click={handleClick} />
<button on:click={onProgressDot} />

Component Format