Skip to content

Commit cbf9f29

Browse files
Sync svelte docs (#928)
sync svelte docs Co-authored-by: Rich-Harris <[email protected]>
1 parent 42bd7b8 commit cbf9f29

File tree

6 files changed

+66
-21
lines changed

6 files changed

+66
-21
lines changed

apps/svelte.dev/content/docs/svelte/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...

apps/svelte.dev/content/docs/svelte/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

apps/svelte.dev/content/docs/svelte/98-reference/.generated/compile-warnings.md

Lines changed: 2 additions & 2 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

apps/svelte.dev/content/docs/svelte/98-reference/30-compiler-errors.md

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

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

497497
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:
498498

499-
- `<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)
499+
- `<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)
500500
- `<option><div>option a</div></option>` will result in `<option>option a</option>` (the `<div>` is removed)
501501
- `<table><tr><td>cell</td></tr></table>` will result in `<table><tbody><tr><td>cell</td></tr></tbody></table>` (a `<tbody>` is auto-inserted)
502502

apps/svelte.dev/content/docs/svelte/98-reference/30-compiler-warnings.md

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

665665
```
666-
%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
666+
%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
667667
```
668668

669669
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:
670670

671-
- `<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)
671+
- `<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)
672672
- `<option><div>option a</div></option>` will result in `<option>option a</option>` (the `<div>` is removed)
673673
- `<table><tr><td>cell</td></tr></table>` will result in `<table><tbody><tr><td>cell</td></tr></tbody></table>` (a `<tbody>` is auto-inserted)
674674

apps/svelte.dev/content/docs/svelte/99-legacy/20-legacy-slots.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,39 +74,45 @@ If no slotted content is provided, a component can define fallback content by pu
7474

7575
Slots can be rendered zero or more times and can pass values _back_ to the parent using props. The parent exposes the values to the slot template using the `let:` directive.
7676

77-
The usual shorthand rules apply — `let:item` is equivalent to `let:item={item}`, and `<slot {item}>` is equivalent to `<slot item={item}>`.
78-
7977
```svelte
80-
<!-- FancyList.svelte -->
78+
<!--- file: FancyList.svelte --->
8179
<ul>
82-
{#each items as item}
80+
{#each items as data}
8381
<li class="fancy">
84-
<slot prop={item} />
82+
<!-- 'item' here... -->
83+
<slot item={process(data)} />
8584
</li>
8685
{/each}
8786
</ul>
87+
```
8888

89-
<!-- App.svelte -->
90-
<FancyList {items} let:prop={thing}>
91-
<div>{thing.text}</div>
89+
```svelte
90+
<!--- file: App.svelte --->
91+
<!-- ...corresponds to 'item' here: -->
92+
<FancyList {items} let:item={processed}>
93+
<div>{processed.text}</div>
9294
</FancyList>
9395
```
9496

97+
The usual shorthand rules apply — `let:item` is equivalent to `let:item={item}`, and `<slot {item}>` is equivalent to `<slot item={item}>`.
98+
9599
Named slots can also expose values. The `let:` directive goes on the element with the `slot` attribute.
96100

97101
```svelte
98-
<!-- FancyList.svelte -->
102+
<!--- file: FancyList.svelte --->
99103
<ul>
100104
{#each items as item}
101105
<li class="fancy">
102-
<slot name="item" {item} />
106+
<slot name="item" item={process(data)} />
103107
</li>
104108
{/each}
105109
</ul>
106110
107111
<slot name="footer" />
112+
```
108113

109-
<!-- App.svelte -->
114+
```svelte
115+
<!--- file: App.svelte --->
110116
<FancyList {items}>
111117
<div slot="item" let:item>{item.text}</div>
112118
<p slot="footer">Copyright (c) 2019 Svelte Industries</p>

0 commit comments

Comments
 (0)