Skip to content

Commit a3dde04

Browse files
Sync svelte docs (#1280)
sync svelte docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com>
1 parent b37676e commit a3dde04

File tree

4 files changed

+119
-26
lines changed

4 files changed

+119
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: .svelte.js and .svelte.ts files
55

66
Besides `.svelte` files, Svelte also operates on `.svelte.js` and `.svelte.ts` files.
77

8-
These behave like any other `.js` or `.ts` module, except that you can use runes. This is useful for creating reusable reactive logic, or sharing reactive state across your app.
8+
These behave like any other `.js` or `.ts` module, except that you can use runes. This is useful for creating reusable reactive logic, or sharing reactive state across your app (though note that you [cannot export reassigned state]($state#Passing-state-across-modules)).
99

1010
> [!LEGACY]
1111
> This is a concept that didn't exist prior to Svelte 5

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,83 @@ console.log(total.value); // 7
251251
```
252252

253253
...though if you find yourself writing code like that, consider using [classes](#Classes) instead.
254+
255+
## Passing state across modules
256+
257+
You can declare state in `.svelte.js` and `.svelte.ts` files, but you can only _export_ that state if it's not directly reassigned. In other words you can't do this:
258+
259+
```js
260+
/// file: state.svelte.js
261+
export let count = $state(0);
262+
263+
export function increment() {
264+
count += 1;
265+
}
266+
```
267+
268+
That's because every reference to `count` is transformed by the Svelte compiler — the code above is roughly equivalent to this:
269+
270+
```js
271+
/// file: state.svelte.js (compiler output)
272+
// @filename: index.ts
273+
interface Signal<T> {
274+
value: T;
275+
}
276+
277+
interface Svelte {
278+
state<T>(value?: T): Signal<T>;
279+
get<T>(source: Signal<T>): T;
280+
set<T>(source: Signal<T>, value: T): void;
281+
}
282+
declare const $: Svelte;
283+
// ---cut---
284+
export let count = $.state(0);
285+
286+
export function increment() {
287+
$.set(count, $.get(count) + 1);
288+
}
289+
```
290+
291+
> [!NOTE] You can see the code Svelte generates by clicking the 'JS Output' tab in the [playground](/playground).
292+
293+
Since the compiler only operates on one file at a time, if another file imports `count` Svelte doesn't know that it needs to wrap each reference in `$.get` and `$.set`:
294+
295+
```js
296+
// @filename: state.svelte.js
297+
export let count = 0;
298+
299+
// @filename: index.js
300+
// ---cut---
301+
import { count } from './state.svelte.js';
302+
303+
console.log(typeof count); // 'object', not 'number'
304+
```
305+
306+
This leaves you with two options for sharing state between modules — either don't reassign it...
307+
308+
```js
309+
// This is allowed — since we're updating
310+
// `counter.count` rather than `counter`,
311+
// Svelte doesn't wrap it in `$.state`
312+
export const counter = $state({
313+
count: 0
314+
});
315+
316+
export function increment() {
317+
counter.count += 1;
318+
}
319+
```
320+
321+
...or don't directly export it:
322+
323+
```js
324+
let count = $state(0);
325+
326+
export function getCount() {
327+
return count;
328+
}
329+
330+
export function increment() {
331+
count += 1;
332+
}
333+
```

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

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ You can create an effect with the `$effect` rune ([demo](/playground/untitled#H4
2626
});
2727
</script>
2828
29-
<canvas bind:this={canvas} width="100" height="100" />
29+
<canvas bind:this={canvas} width="100" height="100"></canvas>
3030
```
3131

3232
When Svelte runs an effect function, it tracks which pieces of state (and derived state) are accessed (unless accessed inside [`untrack`](svelte#untrack)), and re-runs the function when that state later changes.
@@ -136,19 +136,33 @@ An effect only reruns when the object it reads changes, not when a property insi
136136

137137
An effect only depends on the values that it read the last time it ran. This has interesting implications for effects that have conditional code.
138138

139-
For instance, if `a` is `true` in the code snippet below, the code inside the `if` block will run and `b` will be evaluated. As such, changes to either `a` or `b` [will cause the effect to re-run](/playground/untitled#H4sIAAAAAAAAE3VQzWrDMAx-FdUU4kBp71li6EPstOxge0ox8-QQK2PD-N1nLy2F0Z2Evj9_chKkP1B04pnYscc3cRCT8xhF95IEf8-Vq0DBr8rzPB_jJ3qumNERH-E2ECNxiRF9tIubWY00lgcYNAywj6wZJS8rtk83wjwgCrXHaULLUrYwKEgVGrnkx-Dx6MNFNstK5OjSbFGbwE0gdXuT_zGYrjmAuco515Hr1p_uXak3K3MgCGS9s-9D2grU-judlQYXIencnzad-tdR79qZrMyvw9wd5Z8Yv1h09dz8mn8AkM7Pfo0BAAA=).
139+
For instance, if `condition` is `true` in the code snippet below, the code inside the `if` block will run and `color` will be evaluated. As such, changes to either `condition` or `color` [will cause the effect to re-run](/playground/untitled#H4sIAAAAAAAAE21RQW6DMBD8ytaNBJHaJFLViwNIVZ8RcnBgXVk1xsILTYT4e20TQg89IOPZ2fHM7siMaJBx9tmaWpFqjQNlAKXEihx7YVJpdIyfRkY3G4gB8Pi97cPanRtQU8AuwuF_eNUaQuPlOMtc1SlLRWlKUo1tOwJflUikQHZtA0klzCDc64Imx0ANn8bInV1CDhtHgjClrsftcSXotluLybOUb3g4JJHhOZs5WZpuIS9gjNqkJKQP5e2ClrR4SMdZ13E4xZ8zTPOTJU2A2uE_PQ9COCI926_hTVarIU4hu_REPlBrKq2q73ycrf1N-vS4TMUsulaVg3EtR8H9rFgsg8uUsT1B2F9eshigZHBRpuaD0D3mY8Qm2BfB5N2YyRzdNEYVDy0Ja-WsFjcOUuP1HvFLWA6H3XuHTUSmmDV2--0TXonxsKbp7G9C6R__NONS-MFNvxj_d6mBAgAA).
140140

141-
Conversely, if `a` is `false`, `b` will not be evaluated, and the effect will _only_ re-run when `a` changes.
141+
Conversely, if `condition` is `false`, `color` will not be evaluated, and the effect will _only_ re-run again when `condition` changes.
142142

143143
```ts
144-
let a = false;
145-
let b = false;
144+
// @filename: ambient.d.ts
145+
declare module 'canvas-confetti' {
146+
interface ConfettiOptions {
147+
colors: string[];
148+
}
149+
150+
function confetti(opts?: ConfettiOptions): void;
151+
export default confetti;
152+
}
153+
154+
// @filename: index.js
146155
// ---cut---
147-
$effect(() => {
148-
console.log('running');
156+
import confetti from 'canvas-confetti';
149157

150-
if (a) {
151-
console.log('b:', b);
158+
let condition = $state(true);
159+
let color = $state('#ff3e00');
160+
161+
$effect(() => {
162+
if (condition) {
163+
confetti({ colors: [color] });
164+
} else {
165+
confetti();
152166
}
153167
});
154168
```
@@ -212,20 +226,19 @@ It is used to implement abstractions like [`createSubscriber`](/docs/svelte/svel
212226

213227
The `$effect.root` rune is an advanced feature that creates a non-tracked scope that doesn't auto-cleanup. This is useful for nested effects that you want to manually control. This rune also allows for the creation of effects outside of the component initialisation phase.
214228

215-
```svelte
216-
<script>
217-
let count = $state(0);
229+
```js
230+
const destroy = $effect.root(() => {
231+
$effect(() => {
232+
// setup
233+
});
218234

219-
const cleanup = $effect.root(() => {
220-
$effect(() => {
221-
console.log(count);
222-
});
235+
return () => {
236+
// cleanup
237+
};
238+
});
223239

224-
return () => {
225-
console.log('effect root cleanup');
226-
};
227-
});
228-
</script>
240+
// later...
241+
destroy();
229242
```
230243

231244
## When not to use `$effect`

apps/svelte.dev/content/docs/svelte/03-template-syntax/13-transition.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ The `transition:` directive indicates a _bidirectional_ transition, which means
2323
{/if}
2424
```
2525

26-
## Built-in transitions
27-
28-
A selection of built-in transitions can be imported from the [`svelte/transition`](svelte-transition) module.
29-
3026
## Local vs global
3127

3228
Transitions are local by default. Local transitions only play when the block they belong to is created or destroyed, _not_ when parent blocks are created or destroyed.
@@ -41,6 +37,10 @@ Transitions are local by default. Local transitions only play when the block the
4137
{/if}
4238
```
4339

40+
## Built-in transitions
41+
42+
A selection of built-in transitions can be imported from the [`svelte/transition`](svelte-transition) module.
43+
4444
## Transition parameters
4545

4646
Transitions can have parameters.

0 commit comments

Comments
 (0)