Skip to content

Commit 4c54849

Browse files
Sync svelte docs (sveltejs#1101)
sync svelte docs Co-authored-by: Rich-Harris <[email protected]>
1 parent ab7481b commit 4c54849

File tree

2 files changed

+69
-50
lines changed

2 files changed

+69
-50
lines changed

apps/svelte.dev/content/docs/svelte/07-misc/07-v5-migration-guide.md

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,31 @@ When spreading props, local event handlers must go _after_ the spread, or they r
340340

341341
In Svelte 4, content can be passed to components using slots. Svelte 5 replaces them with snippets which are more powerful and flexible, and as such slots are deprecated in Svelte 5.
342342

343-
They continue to work, however, and you can mix and match snippets and slots in your components.
343+
They continue to work, however, and you can pass snippets to a component that uses slots:
344+
345+
```svelte
346+
<!--- file: Child.svelte --->
347+
<slot />
348+
<hr />
349+
<slot name="foo" message="hello" />
350+
```
351+
352+
```svelte
353+
<!--- file: Parent.svelte --->
354+
<script>
355+
import Child from './Child.svelte';
356+
</script>
357+
358+
<Child>
359+
default child content
360+
361+
{#snippet foo({ message })}
362+
message from child: {message}
363+
{/snippet}
364+
</Child>
365+
```
366+
367+
(The reverse is not true — you cannot pass slotted content to a component that uses [`{@render ...}`](/docs/svelte/@render) tags.)
344368

345369
When using custom elements, you should still use `<slot />` like before. In a future version, when Svelte removes its internal version of slots, it will leave those slots as-is, i.e. output a regular DOM tag instead of transforming it.
346370

@@ -598,28 +622,57 @@ export declare const MyComponent: Component<{
598622

599623
To declare that a component of a certain type is required:
600624

625+
```js
626+
import { ComponentA, ComponentB } from 'component-library';
627+
---import type { SvelteComponent } from 'svelte';---
628+
+++import type { Component } from 'svelte';+++
629+
630+
---let C: typeof SvelteComponent<{ foo: string }> = $state(---
631+
+++let C: Component<{ foo: string }> = $state(+++
632+
Math.random() ? ComponentA : ComponentB
633+
);
634+
```
635+
636+
The two utility types `ComponentEvents` and `ComponentType` are also deprecated. `ComponentEvents` is obsolete because events are defined as callback props now, and `ComponentType` is obsolete because the new `Component` type is the component type already (i.e. `ComponentType<SvelteComponent<{ prop: string }>>` is equivalent to `Component<{ prop: string }>`).
637+
638+
### bind:this changes
639+
640+
Because components are no longer classes, using `bind:this` no longer returns a class instance with `$set`, `$on` and `$destroy` methods on it. It only returns the instance exports (`export function/const`) and, if you're using the `accessors` option, a getter/setter-pair for each property.
641+
642+
## `<svelte:component>` is no longer necessary
643+
644+
In Svelte 4, components are _static_ — if you render `<Thing>`, and the value of `Thing` changes, [nothing happens](/playground/7f1fa24f0ab44c1089dcbb03568f8dfa?version=4.2.18). To make it dynamic you had to use `<svelte:component>`.
645+
646+
This is no longer true in Svelte 5:
647+
601648
```svelte
602-
<script lang="ts">
603-
import type { ---SvelteComponent--- +++Component+++ } from 'svelte';
604-
import {
605-
ComponentA,
606-
ComponentB
607-
} from 'component-library';
649+
<script>
650+
import A from './A.svelte';
651+
import B from './B.svelte';
608652

609-
---let component: typeof SvelteComponent<{ foo: string }>---
610-
+++let component: Component<{ foo: string }>+++ = $state(
611-
Math.random() ? ComponentA : ComponentB
612-
);
653+
let Thing = $state();
613654
</script>
614655

615-
<svelte:component this={component} foo="bar" />
656+
<select bind:value={Thing}>
657+
<option value={A}>A</option>
658+
<option value={B}>B</option>
659+
</select>
660+
661+
<!-- these are equivalent -->
662+
<Thing />
663+
<svelte:component this={Thing} />
616664
```
665+
While migrating, keep in mind that your component's name should be capitalized (`Thing`) to distinguish it from elements, unless using dot notation.
617666
618-
The two utility types `ComponentEvents` and `ComponentType` are also deprecated. `ComponentEvents` is obsolete because events are defined as callback props now, and `ComponentType` is obsolete because the new `Component` type is the component type already (e.g. `ComponentType<SvelteComponent<{ prop: string }>>` == `Component<{ prop: string }>`).
667+
### Dot notation indicates a component
619668
620-
### bind:this changes
669+
In Svelte 4, `<foo.bar>` would create an element with a tag name of `"foo.bar"`. In Svelte 5, `foo.bar` is treated as a component instead. This is particularly useful inside `each` blocks:
621670
622-
Because components are no longer classes, using `bind:this` no longer returns a class instance with `$set`, `$on` and `$destroy` methods on it. It only returns the instance exports (`export function/const`) and, if you're using the `accessors` option, a getter/setter-pair for each property.
671+
```svelte
672+
{#each items as item}
673+
<item.component {...item.props} />
674+
{/each}
675+
```
623676
624677
## Whitespace handling changed
625678
@@ -654,16 +707,6 @@ The `legacy` compiler option, which generated bulkier but IE-friendly code, no l
654707
655708
Content inside component tags becomes a snippet prop called `children`. You cannot have a separate prop by that name.
656709
657-
## Dot notation indicates a component
658-
659-
In Svelte 4, `<foo.bar>` would create an element with a tag name of `"foo.bar"`. In Svelte 5, `foo.bar` is treated as a component instead. This is particularly useful inside `each` blocks:
660-
661-
```svelte
662-
{#each items as item}
663-
<item.component {...item.props} />
664-
{/each}
665-
```
666-
667710
## Breaking changes in runes mode
668711
669712
Some breaking changes only apply once your component is in runes mode.
@@ -701,30 +744,6 @@ In Svelte 4, doing the following triggered reactivity:
701744
702745
This is because the Svelte compiler treated the assignment to `foo.value` as an instruction to update anything that referenced `foo`. In Svelte 5, reactivity is determined at runtime rather than compile time, so you should define `value` as a reactive `$state` field on the `Foo` class. Wrapping `new Foo()` with `$state(...)` will have no effect — only vanilla objects and arrays are made deeply reactive.
703746
704-
### `<svelte:component>` is no longer necessary
705-
706-
In Svelte 4, components are _static_ — if you render `<Thing>`, and the value of `Thing` changes, [nothing happens](/playground/7f1fa24f0ab44c1089dcbb03568f8dfa?version=4.2.18). To make it dynamic you must use `<svelte:component>`.
707-
708-
This is no longer true in Svelte 5:
709-
710-
```svelte
711-
<script>
712-
import A from './A.svelte';
713-
import B from './B.svelte';
714-
715-
let Thing = $state();
716-
</script>
717-
718-
<select bind:value={Thing}>
719-
<option value={A}>A</option>
720-
<option value={B}>B</option>
721-
</select>
722-
723-
<!-- these are equivalent -->
724-
<Thing />
725-
<svelte:component this={Thing} />
726-
```
727-
728747
### Touch and wheel events are passive
729748
730749
When using `onwheel`, `onmousewheel`, `ontouchstart` and `ontouchmove` event attributes, the handlers are [passive](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#using_passive_listeners) to align with browser defaults. This greatly improves responsiveness by allowing the browser to scroll the document immediately, rather than waiting to see if the event handler calls `event.preventDefault()`.

apps/svelte.dev/content/docs/svelte/99-legacy/30-legacy-svelte-component.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-doc
33
title: <svelte:component>
44
---
55

6-
In runes mode, `<MyComponent>` will re-render if the value of `MyComponent` changes. See the [Svelte 5 migration guide](/docs/svelte/v5-migration-guide#Breaking-changes-in-runes-mode-svelte:component-is-no-longer-necessary) for an example.
6+
In runes mode, `<MyComponent>` will re-render if the value of `MyComponent` changes. See the [Svelte 5 migration guide](/docs/svelte/v5-migration-guide#svelte:component-is-no-longer-necessary) for an example.
77

88
In legacy mode, it won't — we must use `<svelte:component>`, which destroys and recreates the component instance when the value of its `this` expression changes:
99

0 commit comments

Comments
 (0)