Skip to content

Commit 4fe5543

Browse files
authored
docs: sync new Svelte(Kit) changes (#481)
1 parent b98cb8e commit 4fe5543

23 files changed

+1612
-337
lines changed

apps/svelte.dev/content/docs/svelte/01-introduction/03-svelte-files.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ In addition to normal JavaScript, you can use _runes_ to declare [component prop
3737

3838
A `<script>` tag with a `module` attribute runs once when the module first evaluates, rather than for each component instance. Variables declared in this block can be referenced elsewhere in the component, but not vice versa.
3939

40-
You can `export` bindings from this block, and they will become exports of the compiled module. You cannot `export default`, since the default export is the component itself.
41-
4240
```svelte
4341
<script module>
4442
let total = 0;
@@ -50,6 +48,8 @@ You can `export` bindings from this block, and they will become exports of the c
5048
</script>
5149
```
5250

51+
You can `export` bindings from this block, and they will become exports of the compiled module. You cannot `export default`, since the default export is the component itself.
52+
5353
## `<style>`
5454

5555
CSS inside a `<style>` block will be scoped to that component.

apps/svelte.dev/content/docs/svelte/02-runes/02-$state.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ let person = $state.raw({
9797
age: 49
9898
});
9999

100-
// this will have no effect (and will throw an error in dev)
100+
// this will have no effect
101101
person.age += 1;
102102

103103
// this will work, because we're creating a new person

apps/svelte.dev/content/docs/svelte/02-runes/04-$effect.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: $effect
44

55
Effects are what make your application _do things_. When Svelte runs an effect function, it tracks which pieces of state (and derived state) are accessed, and re-runs the function when that state later changes.
66

7-
Most of the effects in a Svelte app are created by Svelte itself — they're the bits that update the text in `<h1>hello {name}!</h1>` when `name` changes.
7+
Most of the effects in a Svelte app are created by Svelte itself — they're the bits that update the text in `<h1>hello {name}!</h1>` when `name` changes, for example.
88

99
But you can also create your own effects with the `$effect` rune, which is useful when you need to synchronize an external system (whether that's a library, or a `<canvas>` element, or something across a network) with state inside your Svelte app.
1010

@@ -68,7 +68,7 @@ You can return a function from `$effect`, which will run immediately before the
6868

6969
`$effect` automatically picks up any reactive values (`$state`, `$derived`, `$props`) that are _synchronously_ read inside its function body and registers them as dependencies. When those dependencies change, the `$effect` schedules a rerun.
7070

71-
Values that are read asynchronously — after an `await` or inside a `setTimeout`, for example — will _not_ be tracked. Here, the canvas will be repainted when `color` changes, but not when `size` changes ([demo](/playground/untitled#H4sIAAAAAAAAE31T246bMBD9lZF3pWSlBEirfaEQqdo_2PatVIpjBrDkGGQPJGnEv1e2IZfVal-wfHzmzJyZ4cIqqdCy9M-F0blDlnqArZjmB3f72XWRHVCRw_bc4me4aDWhJstSlllhZEfbQhekkMDKfwg5PFvihMvX5OXH_CJa1Zrb0-Kpqr5jkiwC48rieuDWQbqgZ6wqFLRcvkC-hYvnkWi1dWqa8ESQTxFRjfQWsOXiWzmr0sSLhEJu3p1YsoJkNUcdZUnN9dagrBu6FVRQHAM10sJRKgUG16bXcGxQ44AGdt7SDkTDdY02iqLHnJVU6hedlWuIp94JW6Tf8oBt_8GdTxlF0b4n0C35ZLBzXb3mmYn3ae6cOW74zj0YVzDNYXRHFt9mprNgHfZSl6mzml8CMoLvTV6wTZIUDEJv5us2iwMtiJRyAKG4tXnhl8O0yhbML0Wm-B7VNlSSSd31BG7z8oIZZ6dgIffAVY_5xdU9Qrz1Bnx8fCfwtZ7v8Qc9j3nB8PqgmMWlHIID6-bkVaPZwDySfWtKNGtquxQ23Qlsq2QJT0KIqb8dL0up6xQ2eIBkAg_c1FI_YqW0neLnFCqFpwmreedJYT7XX8FVOBfwWRhXstZrSXiwKQjUhOZeMIleb5JZfHWn2Yq5pWEpmR7Hv-N_wEqT8hEEAAA=)):
71+
Values that are read _asynchronously_ — after an `await` or inside a `setTimeout`, for example — will not be tracked. Here, the canvas will be repainted when `color` changes, but not when `size` changes ([demo](/playground/untitled#H4sIAAAAAAAAE31T246bMBD9lZF3pWSlBEirfaEQqdo_2PatVIpjBrDkGGQPJGnEv1e2IZfVal-wfHzmzJyZ4cIqqdCy9M-F0blDlnqArZjmB3f72XWRHVCRw_bc4me4aDWhJstSlllhZEfbQhekkMDKfwg5PFvihMvX5OXH_CJa1Zrb0-Kpqr5jkiwC48rieuDWQbqgZ6wqFLRcvkC-hYvnkWi1dWqa8ESQTxFRjfQWsOXiWzmr0sSLhEJu3p1YsoJkNUcdZUnN9dagrBu6FVRQHAM10sJRKgUG16bXcGxQ44AGdt7SDkTDdY02iqLHnJVU6hedlWuIp94JW6Tf8oBt_8GdTxlF0b4n0C35ZLBzXb3mmYn3ae6cOW74zj0YVzDNYXRHFt9mprNgHfZSl6mzml8CMoLvTV6wTZIUDEJv5us2iwMtiJRyAKG4tXnhl8O0yhbML0Wm-B7VNlSSSd31BG7z8oIZZ6dgIffAVY_5xdU9Qrz1Bnx8fCfwtZ7v8Qc9j3nB8PqgmMWlHIID6-bkVaPZwDySfWtKNGtquxQ23Qlsq2QJT0KIqb8dL0up6xQ2eIBkAg_c1FI_YqW0neLnFCqFpwmreedJYT7XX8FVOBfwWRhXstZrSXiwKQjUhOZeMIleb5JZfHWn2Yq5pWEpmR7Hv-N_wEqT8hEEAAA=)):
7272

7373
```ts
7474
// @filename: index.ts
@@ -175,7 +175,7 @@ In rare cases, you may need to run code _before_ the DOM updates. For this we ca
175175
</div>
176176
```
177177

178-
Apart from the timing, `$effect.pre` works exactly like `$effect` — refer to its documentation for more info.
178+
Apart from the timing, `$effect.pre` works exactly like `$effect`.
179179

180180
## `$effect.tracking`
181181

apps/svelte.dev/content/docs/svelte/02-runes/05-$props.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,127 @@
22
title: $props
33
---
44

5-
Coming soon!
5+
The inputs to a component are referred to as _props_, which is short for _properties_. You pass props to components just like you pass attributes to elements:
6+
7+
```svelte
8+
<script>
9+
import MyComponent from './MyComponent.svelte';
10+
</script>
11+
12+
/// file: App.svelte
13+
<MyComponent adjective="cool" />
14+
```
15+
16+
On the other side, inside `MyComponent.svelte`, we can receive props with the `$props` rune...
17+
18+
```svelte
19+
<script>
20+
let props = $props();
21+
</script>
22+
23+
/// file: MyComponent.svelte
24+
<p>this component is {props.adjective}</p>
25+
```
26+
27+
...though more commonly, you'll [_destructure_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) your props:
28+
29+
```svelte
30+
/// file: MyComponent.svelte
31+
<script>
32+
let +++{ adjective }+++ = $props();
33+
</script>
34+
35+
<p>this component is {+++adjective+++}</p>
36+
```
37+
38+
## Fallback values
39+
40+
Destructuring allows us to declare fallback values, which are used if the parent component does not set a given prop:
41+
42+
```js
43+
/// file: MyComponent.svelte
44+
let { adjective = 'happy' } = $props();
45+
```
46+
47+
> [!NOTE] Fallback values are not turned into reactive state proxies.
48+
49+
## Renaming props
50+
51+
We can also use the destructuring assignment to rename props, which is necessary if they're invalid identifiers, or a JavaScript keyword like `super`:
52+
53+
```js
54+
let { super: trouper = 'lights are gonna find me' } = $props();
55+
```
56+
57+
## Rest props
58+
59+
Finally, we can use a _rest property_ to get, well, the rest of the props:
60+
61+
```js
62+
let { a, b, c, ...others } = $props();
63+
```
64+
65+
## Updating props
66+
67+
References to a prop inside a component update when the prop itself updates — when `count` changes in `App.svelte`, it will also change inside `Child.svelte`. But the child component is able to temporarily override the prop value, which can be useful for unsaved emphemeral state ([demo](/playground/untitled#H4sIAAAAAAAAE6WQ0WrDMAxFf0WIQR0Wmu3VTQJln7HsIfVcZubIxlbGRvC_DzuBraN92qPula50tODZWB1RPi_IX16jLALWSOOUq6P3-_ihLWftNEZ9TVeOWBNHlNhGFYznfqCBzeRdYHh6M_YVzsFNsNs3pdpGd4eBcqPVDMrNxNDBXeSRtXioDgO1zU8ataeZ2RE4Utao924RFXQ9iHXwvoPHKpW1xY4g_Bg0cSVhKS0p560Za95612ZC02ONrD8ZJYdZp_rGQ37ff_mSP86Np2TWZaNNmdcH56P4P67K66_SXoK9pG-5dF5Z9QEAAA==)):
68+
69+
<!-- prettier-ignore -->
70+
```svelte
71+
/// file: App.svelte
72+
<script>
73+
import Child from './Child.svelte';
74+
75+
let count = $state(0);
76+
</script>
77+
78+
<button onclick={() => (count += 1)}>
79+
clicks (parent): {count}
80+
</button>
81+
82+
<Child {count} />
83+
```
84+
85+
<!-- prettier-ignore -->
86+
```svelte
87+
/// file: Child.svelte
88+
<script>
89+
let { count } = $props();
90+
</script>
91+
92+
<button onclick={() => (count += 1)}>
93+
clicks (child): {count}
94+
</button>
95+
```
96+
97+
## Type safety
98+
99+
You can add type safety to your components by annotating your props, as you would with any other variable declaration. In TypeScript that might look like this...
100+
101+
```svelte
102+
<script lang="ts">
103+
let { adjective }: { adjective: string } = $props();
104+
</script>
105+
```
106+
107+
...while in JSDoc you can do this:
108+
109+
```svelte
110+
<script>
111+
/** @type {{ adjective: string }} */
112+
let { adjective } = $props();
113+
</script>
114+
```
115+
116+
You can, of course, separate the type declaration from the annotation:
117+
118+
```svelte
119+
<script lang="ts">
120+
interface Props {
121+
adjective: string;
122+
}
123+
124+
let { adjective }: Props = $props();
125+
</script>
126+
```
127+
128+
Adding types is recommended, as it ensures that people using your component can easily discover which props they should provide.

apps/svelte.dev/content/docs/svelte/02-runes/06-$bindable.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,53 @@
22
title: $bindable
33
---
44

5-
Coming soon!
5+
Ordinarily, props go one way, from parent to child. This makes it easy to understand how data flows around your app.
6+
7+
In Svelte, component props can be _bound_, which means that data can also flow _up_ from child to parent. This isn't something you should do often, but it can simplify your code if used sparingly and carefully.
8+
9+
It also means that a state proxy can be _mutated_ in the child.
10+
11+
> [!NOTE] Mutation is also possible with normal props, but is strongly discouraged — Svelte will warn you if it detects that a component is mutating state it does not 'own'.
12+
13+
To mark a prop as bindable, we use the `$bindable` rune:
14+
15+
<!-- prettier-ignore -->
16+
```svelte
17+
/// file: FancyInput.svelte
18+
<script>
19+
let { value = $bindable(), ...props } = $props();
20+
</script>
21+
22+
<input bind:value={value} {...props} />
23+
24+
<style>
25+
input {
26+
font-family: 'Comic Sans MS';
27+
color: deeppink;
28+
}
29+
</style>
30+
```
31+
32+
Now, a component that uses `<FancyInput>` can add the [`bind:`](bind) directive ([demo](/playground/untitled#H4sIAAAAAAAAE3WQwWrDMBBEf2URBSfg2nfFMZRCoYeecqx6UJx1IyqvhLUONcb_XqSkTUOSk1az7DBvJtEai0HI90nw6FHIJIhckO7i78n7IhzQctS2OuAtvXHESByEFFVoeuO5VqTYdN71DC-amvGV_MDQ9q6DrCjP0skkWymKJxYZOgxBfyKs4SGwZlxke7TWZcuVoqo8-1P1z3lraCcP2g64nk4GM5S1osrXf0JV-lrkgvGbheR-wDm_g30V8JL-1vpOCZFogpQsEsWcemtxscyhKArfOx9gjps0Lq4hzRVfemaYfu-PoIqqwKPFY_XpaIqj4tYRP7a6M3aUkD27zjSw0RTgbZN6Z8WNs66XsEP03tBXUueUJFlelvYx_wCuI3leNwIAAA==)):
33+
34+
<!-- prettier-ignore -->
35+
```svelte
36+
/// App.svelte
37+
<script>
38+
import FancyInput from './FancyInput.svelte';
39+
40+
let message = $state('hello');
41+
</script>
42+
43+
<FancyInput bind:value={message} />
44+
<p>{message}</p>
45+
```
46+
47+
The parent component doesn't _have_ to use `bind:` — it can just pass a normal prop. Some parents don't want to listen to what their children have to say.
48+
49+
In this case, you can specify a fallback value for when no prop is passed at all:
50+
51+
```js
52+
/// file: FancyInput.svelte
53+
let { value = $bindable('fallback'), ...props } = $props();
54+
```

apps/svelte.dev/content/docs/svelte/02-runes/07-$inspect.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,43 @@
22
title: $inspect
33
---
44

5-
Coming soon!
5+
The `$inspect` rune is roughly equivalent to `console.log`, with the exception that it will re-run whenever its argument changes. `$inspect` tracks reactive state deeply, meaning that updating something inside an object or array using fine-grained reactivity will cause it to re-fire ([demo](/#H4sIAAAAAAAACkWQ0YqDQAxFfyUMhSotdZ-tCvu431AXtGOqQ2NmmMm0LOK_r7Utfby5JzeXTOpiCIPKT5PidkSVq2_n1F7Jn3uIcEMSXHSw0evHpAjaGydVzbUQCmgbWaCETZBWMPlKj29nxBDaHj_edkAiu12JhdkYDg61JGvE_s2nR8gyuBuiJZuDJTyQ7eE-IEOzog1YD80Lb0APLfdYc5F9qnFxjiKWwbImo6_llKRQVs-2u91c_bD2OCJLkT3JZasw7KLA2XCX31qKWE6vIzNk1fKE0XbmYrBTufiI8-_8D2cUWBA_AQAA)):
6+
7+
```svelte
8+
<script>
9+
let count = $state(0);
10+
let message = $state('hello');
11+
12+
$inspect(count, message); // will console.log when `count` or `message` change
13+
</script>
14+
15+
<button onclick={() => count++}>Increment</button>
16+
<input bind:value={message} />
17+
```
18+
19+
## $inspect(...).with
20+
21+
`$inspect` returns a property `with`, which you can invoke with a callback, which will then be invoked instead of `console.log`. The first argument to the callback is either `"init"` or `"update"`; subsequent arguments are the values passed to `$inspect` ([demo](/#H4sIAAAAAAAACkVQ24qDMBD9lSEUqlTqPlsj7ON-w7pQG8c2VCchmVSK-O-bKMs-DefKYRYx6BG9qL4XQd2EohKf1opC8Nsm4F84MkbsTXAqMbVXTltuWmp5RAZlAjFIOHjuGLOP_BKVqB00eYuKs82Qn2fNjyxLtcWeyUE2sCRry3qATQIpJRyD7WPVMf9TW-7xFu53dBcoSzAOrsqQNyOe2XUKr0Xi5kcMvdDB2wSYO-I9vKazplV1-T-d6ltgNgSG1KjVUy7ZtmdbdjqtzRcphxMS1-XubOITJtPrQWMvKnYB15_1F7KKadA_AQAA)):
22+
23+
```svelte
24+
<script>
25+
let count = $state(0);
26+
27+
$inspect(count).with((type, count) => {
28+
if (type === 'update') {
29+
debugger; // or `console.trace`, or whatever you want
30+
}
31+
});
32+
</script>
33+
34+
<button onclick={() => count++}>Increment</button>
35+
```
36+
37+
A convenient way to find the origin of some change is to pass `console.trace` to `with`:
38+
39+
```js
40+
// @errors: 2304
41+
$inspect(stuff).with(console.trace);
42+
```
43+
44+
> [!NOTE] `$inspect` only works during development. In a production build it becomes a noop.

apps/svelte.dev/content/docs/svelte/02-runes/08-$host.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,36 @@
22
title: $host
33
---
44

5-
Coming soon!
5+
When compiling a component as a custom element, the `$host` rune provides access to the host element, allowing you to (for example) dispatch custom events ([demo](/playground/untitled#H4sIAAAAAAAAE41Ry2rDMBD8FSECtqkTt1fHFpSSL-ix7sFRNkTEXglrnTYY_3uRlDgxTaEHIfYxs7szA9-rBizPPwZOZwM89wmecqxbF70as7InaMjltrWFR3mpkQDJ8pwXVnbKkKiwItUa3RGLVtk7gTHQXRDR2lXda4CY1D0SK9nCUk0QPyfrCovsRoNFe17aQOAwGncgO2gBqRzihJXiQrEs2csYOhQ-7HgKHaLIbpRhhBG-I2eD_8ciM4KnnOCbeE5dD2P6h0Dz0-Yi_arNhPLJXBtSGi2TvSXdbpqwdsXvjuYsC1veabvvUTog2ylrapKH2G2XsMFLS4uDthQnq2t1cwKkGOGLvYU5PvaQxLsxOkPmsm97Io1Mo2yUPF6VnOZFkw1RMoopKLKAE_9gmGxyDFMwMcwN-Bx_ABXQWmOtAgAA)):
6+
7+
<!-- prettier-ignore -->
8+
```svelte
9+
/// file: Stepper.svelte
10+
<svelte:options customElement="my-stepper" />
11+
12+
<script>
13+
function dispatch(type) {
14+
+++$host()+++.dispatchEvent(new CustomEvent(type));
15+
}
16+
</script>
17+
18+
<button onclick={() => dispatch('decrement')}>decrement</button>
19+
<button onclick={() => dispatch('increment')}>increment</button>
20+
```
21+
22+
<!-- prettier-ignore -->
23+
```svelte
24+
/// file: App.svelte
25+
<script>
26+
import './Stepper.svelte';
27+
28+
let count = $state(0);
29+
</script>
30+
31+
<my-stepper
32+
ondecrement={() => count -= 1}
33+
onincrement={() => count += 1}
34+
></my-stepper>
35+
36+
<p>count: {count}</p>
37+
```

apps/svelte.dev/content/docs/svelte/03-template-syntax/02-if.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,39 @@
22
title: {#if ...}
33
---
44

5-
Coming soon!
5+
```svelte
6+
<!--- copy: false --->
7+
{#if expression}...{/if}
8+
```
9+
10+
```svelte
11+
<!--- copy: false --->
12+
{#if expression}...{:else if expression}...{/if}
13+
```
14+
15+
```svelte
16+
<!--- copy: false --->
17+
{#if expression}...{:else}...{/if}
18+
```
19+
20+
Content that is conditionally rendered can be wrapped in an if block.
21+
22+
```svelte
23+
{#if answer === 42}
24+
<p>what was the question?</p>
25+
{/if}
26+
```
27+
28+
Additional conditions can be added with `{:else if expression}`, optionally ending in an `{:else}` clause.
29+
30+
```svelte
31+
{#if porridge.temperature > 100}
32+
<p>too hot!</p>
33+
{:else if 80 > porridge.temperature}
34+
<p>too cold!</p>
35+
{:else}
36+
<p>just right!</p>
37+
{/if}
38+
```
39+
40+
(Blocks don't have to wrap elements, they can also wrap text within elements.)

0 commit comments

Comments
 (0)