Skip to content

Commit 6acaddc

Browse files
committed
regenerate
2 parents 9115eca + 60c0dc7 commit 6acaddc

File tree

110 files changed

+2315
-414
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+2315
-414
lines changed

documentation/docs/02-runes/02-$state.md

Lines changed: 135 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ let todos = $state([
3636
...modifying an individual todo's property will trigger updates to anything in your UI that depends on that specific property:
3737

3838
```js
39-
// @filename: ambient.d.ts
40-
declare global {
41-
const todos: Array<{ done: boolean, text: string }>
42-
}
43-
44-
// @filename: index.js
39+
let todos = [{ done: false, text: 'add more todos' }];
4540
// ---cut---
4641
todos[0].done = !todos[0].done;
4742
```
@@ -64,6 +59,17 @@ todos.push({
6459

6560
> [!NOTE] When you update properties of proxies, the original object is _not_ mutated.
6661
62+
Note that if you destructure a reactive value, the references are not reactive — as in normal JavaScript, they are evaluated at the point of destructuring:
63+
64+
```js
65+
let todos = [{ done: false, text: 'add more todos' }];
66+
// ---cut---
67+
let { done, text } = todos[0];
68+
69+
// this will not affect the value of `done`
70+
todos[0].done = !todos[0].done;
71+
```
72+
6773
### Classes
6874

6975
You can also use `$state` in class fields (whether public or private):
@@ -85,7 +91,42 @@ class Todo {
8591
}
8692
```
8793

88-
> [!NOTE] The compiler transforms `done` and `text` into `get`/`set` methods on the class prototype referencing private fields.
94+
> [!NOTE] The compiler transforms `done` and `text` into `get`/`set` methods on the class prototype referencing private fields. This means the properties are not enumerable.
95+
96+
When calling methods in JavaScript, the value of [`this`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) matters. This won't work, because `this` inside the `reset` method will be the `<button>` rather than the `Todo`:
97+
98+
```svelte
99+
<button onclick={todo.reset}>
100+
reset
101+
</button>
102+
```
103+
104+
You can either use an inline function...
105+
106+
```svelte
107+
<button onclick=+++{() => todo.reset()}>+++
108+
reset
109+
</button>
110+
```
111+
112+
...or use an arrow function in the class definition:
113+
114+
```js
115+
// @errors: 7006 2554
116+
class Todo {
117+
done = $state(false);
118+
text = $state();
119+
120+
constructor(text) {
121+
this.text = text;
122+
}
123+
124+
+++reset = () => {+++
125+
this.text = '';
126+
this.done = false;
127+
}
128+
}
129+
```
89130

90131
## `$state.raw`
91132

@@ -127,3 +168,90 @@ To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snaps
127168
```
128169

129170
This is handy when you want to pass some state to an external library or API that doesn't expect a proxy, such as `structuredClone`.
171+
172+
## Passing state into functions
173+
174+
JavaScript is a _pass-by-value_ language — when you call a function, the arguments are the _values_ rather than the _variables_. In other words:
175+
176+
```js
177+
/// file: index.js
178+
// @filename: index.js
179+
// ---cut---
180+
/**
181+
* @param {number} a
182+
* @param {number} b
183+
*/
184+
function add(a, b) {
185+
return a + b;
186+
}
187+
188+
let a = 1;
189+
let b = 2;
190+
let total = add(a, b);
191+
console.log(total); // 3
192+
193+
a = 3;
194+
b = 4;
195+
console.log(total); // still 3!
196+
```
197+
198+
If `add` wanted to have access to the _current_ values of `a` and `b`, and to return the current `total` value, you would need to use functions instead:
199+
200+
```js
201+
/// file: index.js
202+
// @filename: index.js
203+
// ---cut---
204+
/**
205+
* @param {() => number} getA
206+
* @param {() => number} getB
207+
*/
208+
function add(+++getA, getB+++) {
209+
return +++() => getA() + getB()+++;
210+
}
211+
212+
let a = 1;
213+
let b = 2;
214+
let total = add+++(() => a, () => b)+++;
215+
console.log(+++total()+++); // 3
216+
217+
a = 3;
218+
b = 4;
219+
console.log(+++total()+++); // 7
220+
```
221+
222+
State in Svelte is no different — when you reference something declared with the `$state` rune...
223+
224+
```js
225+
let a = +++$state(1)+++;
226+
let b = +++$state(2)+++;
227+
```
228+
229+
...you're accessing its _current value_.
230+
231+
Note that 'functions' is broad — it encompasses properties of proxies and [`get`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)/[`set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) properties...
232+
233+
```js
234+
/// file: index.js
235+
// @filename: index.js
236+
// ---cut---
237+
/**
238+
* @param {{ a: number, b: number }} input
239+
*/
240+
function add(input) {
241+
return {
242+
get value() {
243+
return input.a + input.b;
244+
}
245+
};
246+
}
247+
248+
let input = $state({ a: 1, b: 2 });
249+
let total = add(input);
250+
console.log(total.value); // 3
251+
252+
input.a = 3;
253+
input.b = 4;
254+
console.log(total.value); // 7
255+
```
256+
257+
...though if you find yourself writing code like that, consider using [classes](#Classes) instead.

documentation/docs/03-template-syntax/11-bind.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ In the case of a numeric input (`type="number"` or `type="range"`), the value wi
5353

5454
If the input is empty or invalid (in the case of `type="number"`), the value is `undefined`.
5555

56+
Since 5.6.0, if an `<input>` has a `defaultValue` and is part of a form, it will revert to that value instead of the empty string when the form is reset. Note that for the initial render the value of the binding takes precedence unless it is `null` or `undefined`.
57+
58+
```svelte
59+
<script>
60+
let value = $state('');
61+
</script>
62+
63+
<form>
64+
<input bind:value defaultValue="not the empty string">
65+
<input type="reset" value="Reset">
66+
</form>
67+
```
68+
69+
> [!NOTE]
70+
> Use reset buttons sparingly, and ensure that users won't accidentally click them while trying to submit the form.
71+
5672
## `<input bind:checked>`
5773

5874
Checkbox and radio inputs can be bound with `bind:checked`:
@@ -64,16 +80,29 @@ Checkbox and radio inputs can be bound with `bind:checked`:
6480
</label>
6581
```
6682

83+
Since 5.6.0, if an `<input>` has a `defaultChecked` attribute and is part of a form, it will revert to that value instead of `false` when the form is reset. Note that for the initial render the value of the binding takes precedence unless it is `null` or `undefined`.
84+
85+
```svelte
86+
<script>
87+
let checked = $state(true);
88+
</script>
89+
90+
<form>
91+
<input type="checkbox" bind:checked defaultChecked={true}>
92+
<input type="reset" value="Reset">
93+
</form>
94+
```
95+
6796
## `<input bind:group>`
6897

6998
Inputs that work together can use `bind:group`.
7099

71100
```svelte
72101
<script>
73-
let tortilla = 'Plain';
102+
let tortilla = $state('Plain');
74103
75104
/** @type {Array<string>} */
76-
let fillings = [];
105+
let fillings = $state([]);
77106
</script>
78107
79108
<!-- grouped radio inputs are mutually exclusive -->
@@ -146,6 +175,16 @@ When the value of an `<option>` matches its text content, the attribute can be o
146175
</select>
147176
```
148177

178+
You can give the `<select>` a default value by adding a `selected` attribute to the`<option>` (or options, in the case of `<select multiple>`) that should be initially selected. If the `<select>` is part of a form, it will revert to that selection when the form is reset. Note that for the initial render the value of the binding takes precedence if it's not `undefined`.
179+
180+
```svelte
181+
<select bind:value={selected}>
182+
<option value={a}>a</option>
183+
<option value={b} selected>b</option>
184+
<option value={c}>c</option>
185+
</select>
186+
```
187+
149188
## `<audio>`
150189

151190
`<audio>` elements have their own set of bindings — five two-way ones...

documentation/docs/07-misc/07-v5-migration-guide.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ The `foreign` namespace was only useful for Svelte Native, which we're planning
823823

824824
`afterUpdate` callbacks in a parent component will now run after `afterUpdate` callbacks in any child components.
825825

826+
`beforeUpdate/afterUpdate` no longer run when the component contains a `<slot>` and its content is updated.
827+
826828
Both functions are disallowed in runes mode — use `$effect.pre(...)` and `$effect(...)` instead.
827829

828830
### `contenteditable` behavior change

documentation/docs/07-misc/99-faq.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ If you need hash-based routing on the client side, check out [svelte-spa-router]
102102

103103
You can see a [community-maintained list of routers on sveltesociety.dev](https://sveltesociety.dev/packages?category=routers).
104104

105+
## How do I write a mobile app with Svelte?
106+
107+
While most mobile apps are written without using JavaScript, if you'd like to leverage your existing Svelte components and knowledge of Svelte when building mobile apps, you can turn a [SvelteKit SPA](https://kit.svelte.dev/docs/single-page-apps) into a mobile app with [Tauri](https://v2.tauri.app/start/frontend/sveltekit/) or [Capacitor](https://capacitorjs.com/solution/svelte). Mobile features like the camera, geolocation, and push notifications are available via plugins for both platforms.
108+
109+
Svelte Native was an option available for Svelte 4, but note that Svelte 5 does not currently support it. Svelte Native lets you write NativeScript apps using Svelte components that contain [NativeScript UI components](https://docs.nativescript.org/ui/) rather than DOM elements, which may be familiar for users coming from React Native.
110+
105111
## Can I tell Svelte not to remove my unused styles?
106112

107113
No. Svelte removes the styles from the component and warns you about them in order to prevent issues that would otherwise arise.

documentation/docs/98-reference/.generated/client-warnings.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
<!-- This file is generated by scripts/process-messages/index.js. Do not edit! -->
22

3+
### assignment_value_stale
4+
5+
```
6+
Assignment to `%property%` property (%location%) will evaluate to the right-hand side, not the value of `%property%` following the assignment. This may result in unexpected behaviour.
7+
```
8+
9+
Given a case like this...
10+
11+
```svelte
12+
<script>
13+
let object = $state({ array: null });
14+
15+
function add() {
16+
(object.array ??= []).push(object.array.length);
17+
}
18+
</script>
19+
20+
<button onclick={add}>add</button>
21+
<p>items: {JSON.stringify(object.items)}</p>
22+
```
23+
24+
...the array being pushed to when the button is first clicked is the `[]` on the right-hand side of the assignment, but the resulting value of `object.array` is an empty state proxy. As a result, the pushed value will be discarded.
25+
26+
You can fix this by separating it into two statements:
27+
28+
```js
29+
let object = { array: [0] };
30+
// ---cut---
31+
function add() {
32+
object.array ??= [];
33+
object.array.push(object.array.length);
34+
}
35+
```
36+
337
### binding_property_non_reactive
438
539
```
@@ -86,6 +120,46 @@ Mutating a value outside the component that created it is strongly discouraged.
86120
%component% mutated a value owned by %owner%. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead
87121
```
88122

123+
### reactive_declaration_non_reactive_property
124+
125+
```
126+
A `$:` statement (%location%) read reactive state that was not visible to the compiler. Updates to this state will not cause the statement to re-run. The behaviour of this code will change if you migrate it to runes mode
127+
```
128+
129+
In legacy mode, a `$:` [reactive statement](https://svelte.dev/docs/svelte/legacy-reactive-assignments) re-runs when the state it _references_ changes. This is determined at compile time, by analysing the code.
130+
131+
In runes mode, effects and deriveds re-run when there are changes to the values that are read during the function's _execution_.
132+
133+
Often, the result is the same — for example these can be considered equivalent:
134+
135+
```js
136+
let a = 1, b = 2, sum = 3;
137+
// ---cut---
138+
$: sum = a + b;
139+
```
140+
141+
```js
142+
let a = 1, b = 2;
143+
// ---cut---
144+
const sum = $derived(a + b);
145+
```
146+
147+
In some cases — such as the one that triggered the above warning — they are _not_ the same:
148+
149+
```js
150+
let a = 1, b = 2, sum = 3;
151+
// ---cut---
152+
const add = () => a + b;
153+
154+
// the compiler can't 'see' that `sum` depends on `a` and `b`, but
155+
// they _would_ be read while executing the `$derived` version
156+
$: sum = add();
157+
```
158+
159+
Similarly, reactive properties of [deep state](https://svelte.dev/docs/svelte/$state#Deep-state) are not visible to the compiler. As such, changes to these properties will cause effects and deriveds to re-run but will _not_ cause `$:` statements to re-run.
160+
161+
When you [migrate this component](https://svelte.dev/docs/svelte/v5-migration-guide) to runes mode, the behaviour will change accordingly.
162+
89163
### state_proxy_equality_mismatch
90164

91165
```

documentation/docs/98-reference/.generated/compile-errors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,12 @@ A component cannot have a default export
487487
### node_invalid_placement
488488

489489
```
490-
%thing% is invalid inside `<%parent%>`
490+
%message%. The browser will 'repair' the HTML (by moving, removing, or inserting elements) which breaks Svelte's assumptions about the structure of your components.
491491
```
492492

493493
HTML restricts where certain elements can appear. In case of a violation the browser will 'repair' the HTML in a way that breaks Svelte's assumptions about the structure of your components. Some examples:
494494

495-
- `<p>hello <div>world</div></p>` will result in `<p>hello </p><div>world</div><p></p>` for example (the `<div>` autoclosed the `<p>` because `<p>` cannot contain block-level elements)
495+
- `<p>hello <div>world</div></p>` will result in `<p>hello </p><div>world</div><p></p>` (the `<div>` autoclosed the `<p>` because `<p>` cannot contain block-level elements)
496496
- `<option><div>option a</div></option>` will result in `<option>option a</option>` (the `<div>` is removed)
497497
- `<table><tr><td>cell</td></tr></table>` will result in `<table><tbody><tr><td>cell</td></tr></tbody></table>` (a `<tbody>` is auto-inserted)
498498

documentation/docs/98-reference/.generated/compile-warnings.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,12 @@ Svelte 5 components are no longer classes. Instantiate them using `mount` or `hy
643643
### node_invalid_placement_ssr
644644

645645
```
646-
%thing% is invalid inside `<%parent%>`. When rendering this component on the server, the resulting HTML will be modified by the browser, likely resulting in a `hydration_mismatch` warning
646+
%message%. When rendering this component on the server, the resulting HTML will be modified by the browser (by moving, removing, or inserting elements), likely resulting in a `hydration_mismatch` warning
647647
```
648648

649649
HTML restricts where certain elements can appear. In case of a violation the browser will 'repair' the HTML in a way that breaks Svelte's assumptions about the structure of your components. Some examples:
650650

651-
- `<p>hello <div>world</div></p>` will result in `<p>hello </p><div>world</div><p></p>` for example (the `<div>` autoclosed the `<p>` because `<p>` cannot contain block-level elements)
651+
- `<p>hello <div>world</div></p>` will result in `<p>hello </p><div>world</div><p></p>` (the `<div>` autoclosed the `<p>` because `<p>` cannot contain block-level elements)
652652
- `<option><div>option a</div></option>` will result in `<option>option a</option>` (the `<div>` is removed)
653653
- `<table><tr><td>cell</td></tr></table>` will result in `<table><tbody><tr><td>cell</td></tr></tbody></table>` (a `<tbody>` is auto-inserted)
654654

@@ -726,12 +726,6 @@ Reactive declarations only exist at the top level of the instance script
726726
Reassignments of module-level declarations will not cause reactive statements to update
727727
```
728728

729-
### reactive_declaration_non_reactive_property
730-
731-
```
732-
Properties of objects and arrays are not reactive unless in runes mode. Changes to this property will not cause the reactive statement to update
733-
```
734-
735729
### script_context_deprecated
736730

737731
```

0 commit comments

Comments
 (0)