Skip to content

Commit 7fcd4b6

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

17 files changed

Lines changed: 216 additions & 50 deletions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ In rare cases, you may need to run code _before_ the DOM updates. For this we ca
206206
</div>
207207
```
208208

209+
`$effect.pre` runs before DOM updates that are scheduled after it, not before every DOM mutation in the flush - DOM of parent components may already be updated. When using [await expressions](await-expressions), block updates like `{#if ...}` and `{#each ...}` in the same component also run before `$effect.pre`.
210+
209211
Apart from the timing, `$effect.pre` works exactly like `$effect`.
210212

211213
## `$effect.tracking`

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ In this case, you can specify a fallback value for when no prop is passed at all
5353
/// file: FancyInput.svelte
5454
let { value = $bindable('fallback'), ...props } = $props();
5555
```
56+
57+
When a bindable prop has a fallback value, the parent must pass a value other than `undefined` if it uses `bind:`. This avoids ambiguity about which value should apply, since the parent and child should share the same value for a binding.

apps/svelte.dev/content/docs/svelte/03-template-syntax/01-basic-markup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ You can add a special comment starting with `@component` that will show up when
203203
- You can also use code blocks here.
204204
- Usage:
205205
```html
206-
<Main name="Arethra">
206+
<Main name="Aretha">
207207
```
208208
-->
209209
<script>

apps/svelte.dev/content/docs/svelte/03-template-syntax/11-declaration-tags.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Declaration tags define local variables inside markup with `const` or `let`:
1414
1515
{#each boxes as box}
1616
{const area = box.width * box.height}
17-
{const label = `${box.width} ${box.height} = ${area}`}
17+
{const label = `${box.width} × ${box.height} = ${area}`}
1818
1919
<p>{label}</p>
2020
{/each}

apps/svelte.dev/content/docs/svelte/03-template-syntax/12-bind.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ You can give the `<select>` a default value by adding a `selected` attribute to
252252
</select>
253253
```
254254

255+
Since 5.57.0, if a `<select>` 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`.
256+
257+
```svelte
258+
<form>
259+
<select bind:value defaultValue="b">
260+
<option>a</option>
261+
<option>b</option>
262+
<option>c</option>
263+
</select>
264+
<input type="reset" value="Reset">
265+
</form>
266+
```
267+
255268
## `<audio>`
256269

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

apps/svelte.dev/content/docs/svelte/06-runtime/02-context.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ Svelte will warn you if you get it wrong.
166166

167167
Similarly, to pass primitive values through context, use functions as described in [Passing state into functions]($state#Passing-state-into-functions).
168168

169-
## Component testing
169+
## Mounting components with context
170170

171-
When writing [component tests](testing#Unit-and-component-tests-with-Vitest-Component-testing), it can be useful to create a wrapper component that sets the context in order to check the behaviour of a component that uses it. As of version 5.49, you can do this sort of thing:
171+
To mount a component with specific context, create a wrapper component that sets the context before rendering the component. This is useful for [component tests](testing#Unit-and-component-tests-with-Vitest-Component-testing), or any other scenario that needs to provide context through `mount`. As of version 5.49, you can do this sort of thing:
172172

173173
```js
174174
import { mount, unmount } from 'svelte';
@@ -194,6 +194,8 @@ test('MyComponent', () => {
194194

195195
This approach also works with [`hydrate`](imperative-component-api#hydrate) and [`render`](imperative-component-api#render).
196196

197+
The context set by the wrapper only applies to that mounted component tree. Each call to `mount`, `hydrate` or `render` creates a separate wrapper instance, so the context does not leak into other mounted components.
198+
197199
## Replacing global state
198200

199201
When you have state shared by many different components, you might be tempted to put it in its own module and just import it wherever it's needed:

apps/svelte.dev/content/docs/svelte/06-runtime/03-lifecycle-hooks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ To implement a chat window that autoscrolls to the bottom when new messages appe
103103

104104
In Svelte 4, we do this with `beforeUpdate`, but this is a flawed approach — it fires before _every_ update, whether it's relevant or not. In the example below, we need to introduce checks like `updatingMessages` to make sure we don't mess with the scroll position when someone toggles dark mode.
105105

106-
With runes, we can use `$effect.pre`, which behaves the same as `$effect` but runs before the DOM is updated. As long as we explicitly reference `messages` inside the effect body, it will run whenever `messages` changes, but _not_ when `theme` changes.
106+
With runes, we can use `$effect.pre`, which behaves the same as `$effect` but runs before DOM updates scheduled after it (see [$effect.pre]($effect#$effect.pre) for the exact ordering). As long as we explicitly reference `messages` inside the effect body, it will run whenever `messages` changes, but _not_ when `theme` changes.
107107

108108
`beforeUpdate`, and its equally troublesome counterpart `afterUpdate`, are therefore deprecated in Svelte 5.
109109

apps/svelte.dev/content/docs/svelte/07-misc/03-typescript.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ If you don't give `$state` an initial value, part of its types will be `undefine
171171
let count: number = $state();
172172
```
173173

174+
You can pass the type directly as a generic parameter to safely handle this. TypeScript will infer the variable as `number | undefined`.
175+
176+
```ts
177+
let count = $state<number>();
178+
```
179+
174180
If you know that the variable _will_ be defined before you first use it, use an `as` casting. This is especially useful in the context of classes:
175181

176182
```ts

apps/svelte.dev/content/docs/svelte/07-misc/99-faq.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ However, you can use any router library. A sampling of available routers are hig
100100

101101
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.
102102

103-
Some work has been completed towards [custom renderer support in Svelte 5](https://github.com/sveltejs/svelte/issues/15470), but this feature is not yet available. The custom rendering API would support additional mobile frameworks like Lynx JS and Svelte Native. Svelte Native was an option available for Svelte 4, but 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.
103+
You can also write apps in Svelte that compiles to native components by using [Symbiote Native](https://docs.symbiote-native.dev/), which leverages the infrastructure provided by React Native.
104+
105+
Work has been completed towards [custom renderer support in Svelte 5](https://github.com/sveltejs/svelte/issues/15470), but this feature is not yet merged. The custom rendering API will allow support in additional mobile frameworks like Lynx JS and Svelte Native. Symbiote Native will also adopt this API. Svelte Native was an option available for Svelte 4, but 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.
104106

105107
## Can I tell Svelte not to remove my unused styles?
106108

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -339,27 +339,6 @@ Reactive `$state(...)` proxies and the values they proxy have different identiti
339339
340340
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.
341341
342-
### state_proxy_unmount
343-
344-
```
345-
Tried to unmount a state proxy, rather than a component
346-
```
347-
348-
`unmount` was called with a state proxy:
349-
350-
```js
351-
import { mount, unmount } from 'svelte';
352-
import Component from './Component.svelte';
353-
let target = document.body;
354-
// ---cut---
355-
let component = $state(mount(Component, { target }));
356-
357-
// later...
358-
unmount(component);
359-
```
360-
361-
Avoid using `$state` here. If `component` _does_ need to be reactive for some reason, use `$state.raw` instead.
362-
363342
### svelte_boundary_reset_noop
364343
365344
```

0 commit comments

Comments
 (0)