Skip to content

Commit 66121df

Browse files
Sync svelte docs (#1098)
sync svelte docs Co-authored-by: Rich-Harris <[email protected]>
1 parent d366515 commit 66121df

File tree

5 files changed

+91
-49
lines changed

5 files changed

+91
-49
lines changed

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

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -194,53 +194,7 @@ The `$effect.tracking` rune is an advanced feature that tells you whether or not
194194
<p>in template: {$effect.tracking()}</p> <!-- true -->
195195
```
196196

197-
This allows you to (for example) add things like subscriptions without causing memory leaks, by putting them in child effects. Here's a `readable` function that listens to changes from a callback function as long as it's inside a tracking context:
198-
199-
```ts
200-
import { tick } from 'svelte';
201-
202-
export default function readable<T>(
203-
initial_value: T,
204-
start: (callback: (update: (v: T) => T) => T) => () => void
205-
) {
206-
let value = $state(initial_value);
207-
208-
let subscribers = 0;
209-
let stop: null | (() => void) = null;
210-
211-
return {
212-
get value() {
213-
// If in a tracking context ...
214-
if ($effect.tracking()) {
215-
$effect(() => {
216-
// ...and there's no subscribers yet...
217-
if (subscribers === 0) {
218-
// ...invoke the function and listen to changes to update state
219-
stop = start((fn) => (value = fn(value)));
220-
}
221-
222-
subscribers++;
223-
224-
// The return callback is called once a listener unlistens
225-
return () => {
226-
tick().then(() => {
227-
subscribers--;
228-
// If it was the last subscriber...
229-
if (subscribers === 0) {
230-
// ...stop listening to changes
231-
stop?.();
232-
stop = null;
233-
}
234-
});
235-
};
236-
});
237-
}
238-
239-
return value;
240-
}
241-
};
242-
}
243-
```
197+
It is used to implement abstractions like [`createSubscriber`](/docs/svelte/svelte-reactivity#createSubscriber), which will create listeners to update reactive values but _only_ if those values are being tracked (rather than, for example, read inside an event handler).
244198

245199
## `$effect.root`
246200

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,15 @@ Reactive `$state(...)` proxies and the values they proxy have different identiti
222222
```
223223
224224
To resolve this, ensure you're comparing values where both values were created with `$state(...)`, or neither were. Note that `$state.raw(...)` will _not_ create a state proxy.
225+
226+
### transition_slide_display
227+
228+
```
229+
The `slide` transition does not work correctly for elements with `display: %value%`
230+
```
231+
232+
The [slide](/docs/svelte/svelte-transition#slide) transition works by animating the `height` of the element, which requires a `display` style like `block`, `flex` or `grid`. It does not work for:
233+
234+
- `display: inline` (which is the default for elements like `<span>`), and its variants like `inline-block`, `inline-flex` and `inline-grid`
235+
- `display: table` and `table-[name]`, which are the defaults for elements like `<table>` and `<tr>`
236+
- `display: contents`

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,39 @@ The $ prefix is reserved, and cannot be used for variables and imports
331331
### each_item_invalid_assignment
332332

333333
```
334-
Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`)
334+
Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`, or `bind:value={array[i]}` instead of `bind:value={entry}`)
335+
```
336+
337+
In legacy mode, it was possible to reassign or bind to the each block argument itself:
338+
339+
```svelte
340+
<script>
341+
let array = [1, 2, 3];
342+
</script>
343+
344+
{#each array as entry}
345+
<!-- reassignment -->
346+
<button on:click={() => entry = 4}>change</button>
347+
348+
<!-- binding -->
349+
<input bind:value={entry}>
350+
{/each}
351+
```
352+
353+
This turned out to be buggy and unpredictable, particularly when working with derived values (such as `array.map(...)`), and as such is forbidden in runes mode. You can achieve the same outcome by using the index instead:
354+
355+
```svelte
356+
<script>
357+
let array = $state([1, 2, 3]);
358+
</script>
359+
360+
{#each array as entry, i}
361+
<!-- reassignment -->
362+
<button onclick={() => array[i] = 4}>change</button>
363+
364+
<!-- binding -->
365+
<input bind:value={array[i]}>
366+
{/each}
335367
```
336368

337369
### effect_invalid_placement

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,39 @@ The $ prefix is reserved, and cannot be used for variables and imports
336336
### each_item_invalid_assignment
337337

338338
```
339-
Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`)
339+
Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`, or `bind:value={array[i]}` instead of `bind:value={entry}`)
340+
```
341+
342+
In legacy mode, it was possible to reassign or bind to the each block argument itself:
343+
344+
```svelte
345+
<script>
346+
let array = [1, 2, 3];
347+
</script>
348+
349+
{#each array as entry}
350+
<!-- reassignment -->
351+
<button on:click={() => entry = 4}>change</button>
352+
353+
<!-- binding -->
354+
<input bind:value={entry}>
355+
{/each}
356+
```
357+
358+
This turned out to be buggy and unpredictable, particularly when working with derived values (such as `array.map(...)`), and as such is forbidden in runes mode. You can achieve the same outcome by using the index instead:
359+
360+
```svelte
361+
<script>
362+
let array = $state([1, 2, 3]);
363+
</script>
364+
365+
{#each array as entry, i}
366+
<!-- reassignment -->
367+
<button onclick={() => array[i] = 4}>change</button>
368+
369+
<!-- binding -->
370+
<input bind:value={array[i]}>
371+
{/each}
340372
```
341373

342374
### effect_invalid_placement

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ Reactive `$state(...)` proxies and the values they proxy have different identiti
230230
231231
To resolve this, ensure you're comparing values where both values were created with `$state(...)`, or neither were. Note that `$state.raw(...)` will _not_ create a state proxy.
232232
233+
### transition_slide_display
234+
235+
```
236+
The `slide` transition does not work correctly for elements with `display: %value%`
237+
```
238+
239+
The [slide](/docs/svelte/svelte-transition#slide) transition works by animating the `height` of the element, which requires a `display` style like `block`, `flex` or `grid`. It does not work for:
240+
241+
- `display: inline` (which is the default for elements like `<span>`), and its variants like `inline-block`, `inline-flex` and `inline-grid`
242+
- `display: table` and `table-[name]`, which are the defaults for elements like `<table>` and `<tr>`
243+
- `display: contents`
244+
233245
234246
## Shared warnings
235247

0 commit comments

Comments
 (0)