|
2 | 2 | title: <slot> |
3 | 3 | --- |
4 | 4 |
|
5 | | -```svelte |
6 | | -<slot><!-- optional fallback --></slot> |
7 | | -``` |
| 5 | +In Svelte 5, content can be passed to components in the form of [snippets](snippet) and rendered using [render tags](@render). |
8 | 6 |
|
9 | | -```svelte |
10 | | -<slot name="x"><!-- optional fallback --></slot> |
11 | | -``` |
| 7 | +In legacy mode, content inside component tags is considered _slotted content_, which can be rendered by the component using a `<slot>` element: |
12 | 8 |
|
13 | 9 | ```svelte |
14 | | -<slot prop={value} /> |
15 | | -``` |
16 | | - |
17 | | -Components can have child content, in the same way that elements can. |
| 10 | +<!--- file: App.svelte ---> |
| 11 | +<script> |
| 12 | + import Modal from './Modal.svelte'; |
| 13 | +</script> |
18 | 14 |
|
19 | | -The content is exposed in the child component using the `<slot>` element, which can contain fallback content that is rendered if no children are provided. |
| 15 | +<Modal>This is some slotted content</Modal> |
| 16 | +``` |
20 | 17 |
|
21 | 18 | ```svelte |
22 | | -<!-- Widget.svelte --> |
23 | | -<div> |
24 | | - <slot> |
25 | | - this fallback content will be rendered when no content is provided, like in the first example |
26 | | - </slot> |
| 19 | +<!--- file: Modal.svelte ---> |
| 20 | +<div class="modal"> |
| 21 | + <slot></slot> |
27 | 22 | </div> |
28 | | -
|
29 | | -<!-- App.svelte --> |
30 | | -<Widget /> |
31 | | -<!-- this component will render the default content --> |
32 | | -
|
33 | | -<Widget> |
34 | | - <p>this is some child content that will overwrite the default slot content</p> |
35 | | -</Widget> |
36 | 23 | ``` |
37 | 24 |
|
38 | | -Note: If you want to render regular `<slot>` element, You can use `<svelte:element this="slot" />`. |
39 | | - |
40 | | -> [!NOTE] |
41 | | -> In Svelte 5+, use snippets instead |
| 25 | +> [!NOTE] If you want to render a regular `<slot>` element, you can use `<svelte:element this={'slot'} />`. |
42 | 26 |
|
43 | | -## `<slot name="`_name_`">` |
| 27 | +## Named slots |
44 | 28 |
|
45 | | -Named slots allow consumers to target specific areas. They can also have fallback content. |
| 29 | +A component can have _named_ slots in addition to the default slot. On the parent side, add a `slot="..."` attribute to an element, component or [`<svelte:fragment>`](legacy-svelte-fragment) directly inside the component tags. |
46 | 30 |
|
47 | 31 | ```svelte |
48 | | -<!-- Widget.svelte --> |
49 | | -<div> |
50 | | - <slot name="header">No header was provided</slot> |
51 | | - <p>Some content between header and footer</p> |
52 | | - <slot name="footer" /> |
53 | | -</div> |
54 | | -
|
55 | | -<!-- App.svelte --> |
56 | | -<Widget> |
57 | | - <h1 slot="header">Hello</h1> |
58 | | - <p slot="footer">Copyright (c) 2019 Svelte Industries</p> |
59 | | -</Widget> |
| 32 | +<!--- file: App.svelte ---> |
| 33 | +<script> |
| 34 | + import Modal from './Modal.svelte'; |
| 35 | +
|
| 36 | + let open = true; |
| 37 | +</script> |
| 38 | +
|
| 39 | +{#if open} |
| 40 | + <Modal> |
| 41 | + This is some slotted content |
| 42 | +
|
| 43 | + +++<div slot="buttons">+++ |
| 44 | + <button on:click={() => open = false}> |
| 45 | + close |
| 46 | + </button> |
| 47 | + +++</div>+++ |
| 48 | + </Modal> |
| 49 | +{/if} |
60 | 50 | ``` |
61 | 51 |
|
62 | | -Components can be placed in a named slot using the syntax `<Component slot="name" />`. |
63 | | -In order to place content in a slot without using a wrapper element, you can use the special element `<svelte:fragment>`. |
| 52 | +On the child side, add a corresponding `<slot name="...">` element: |
64 | 53 |
|
65 | 54 | ```svelte |
66 | | -<!-- Widget.svelte --> |
67 | | -<div> |
68 | | - <slot name="header">No header was provided</slot> |
69 | | - <p>Some content between header and footer</p> |
70 | | - <slot name="footer" /> |
| 55 | +<!--- file: Modal.svelte ---> |
| 56 | +<div class="modal"> |
| 57 | + <slot></slot> |
| 58 | + <hr> |
| 59 | + +++<slot name="buttons"></slot>+++ |
71 | 60 | </div> |
| 61 | +``` |
72 | 62 |
|
73 | | -<!-- App.svelte --> |
74 | | -<Widget> |
75 | | - <HeaderComponent slot="header" /> |
76 | | - <svelte:fragment slot="footer"> |
77 | | - <p>All rights reserved.</p> |
78 | | - <p>Copyright (c) 2019 Svelte Industries</p> |
79 | | - </svelte:fragment> |
80 | | -</Widget> |
| 63 | +## Fallback content |
| 64 | + |
| 65 | +If no slotted content is provided, a component can define fallback content by putting it inside the `<slot>` element: |
| 66 | + |
| 67 | +```svelte |
| 68 | +<slot> |
| 69 | + This will be rendered if no slotted content is provided |
| 70 | +</slot> |
81 | 71 | ``` |
82 | 72 |
|
83 | | -## `<slot key={`_value_`}>` |
| 73 | +## Passing data to slotted content |
84 | 74 |
|
85 | 75 | 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. |
86 | 76 |
|
@@ -122,3 +112,5 @@ Named slots can also expose values. The `let:` directive goes on the element wit |
122 | 112 | <p slot="footer">Copyright (c) 2019 Svelte Industries</p> |
123 | 113 | </FancyList> |
124 | 114 | ``` |
| 115 | + |
| 116 | + |
0 commit comments