Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<script>
const { children } = $props();
</script>

<div class="card">
<slot />
{@render children()}
</div>

<style>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Slots
title: Snippets
---

Just like elements can have children...
Expand All @@ -11,12 +11,15 @@ Just like elements can have children...
</div>
```

...so can components. Before a component can accept children, though, it needs to know where to put them. We do this with the `<slot>` element. Put this inside `Card.svelte`:
...so can components. Before a component can accept children, though, it needs to know where to put them. We do this with the `children` prop and the render tag `{@render children()}` – which is a bit like a function call. Put this inside `Card.svelte`:

```svelte
/// file: Card.svelte
+++<script>+++
+++ const { children } = $props();+++
+++</script>+++
<div class="card">
+++<slot></slot>+++
+++{@render children()}+++
</div>
```

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script>
import Card from './Card.svelte';
</script>

<main>
<Card>
<span>Patrick BATEMAN</span>
<span>Vice President</span>

{#snippet telephone()}
<span>212 555 6342</span>
{/snippet}

{#snippet company()}
<span>
Pierce &amp; Pierce
<small>Mergers and Aquisitions</small>
</span>
{/snippet}

{#snippet address()}
<span>358 Exchange Place, New York, N.Y. 10099 fax 212 555 6390 telex 10 4534</span>
{/snippet}
</Card>
</main>

<style>
main {
display: grid;
place-items: center;
height: 100%;
background: url(./wood.svg);
}
</style>
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<script>
const { children } = $props();
</script>

<div class="card">
<slot />
{@render children()}
</div>

<style>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
<span>Patrick BATEMAN</span>
<span>Vice President</span>

<span slot="telephone">212 555 6342</span>
{#snippet telephone()}
<span>212 555 6342</span>
{/snippet}

<span slot="company">
Pierce &amp; Pierce
<small>Mergers and Aquisitions</small>
</span>

<span slot="address">358 Exchange Place, New York, N.Y. 10099 fax 212 555 6390 telex 10 4534</span>
{#snippet company()}
<span>
Pierce &amp; Pierce
<small>Mergers and Aquisitions</small>
</span>
{/snippet}

{#snippet address()}
<span>358 Exchange Place, New York, N.Y. 10099 fax 212 555 6390 telex 10 4534</span>
{/snippet}
</Card>
</main>

Expand All @@ -31,4 +37,4 @@
font-size: 0.6em;
text-align: right;
}
</style>
</style>
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<script>
const { children, address, company, telephone } = $props();
</script>

<div class="card">
<header>
<slot name="telephone" />
<slot name="company" />
{@render telephone()}
{@render company()}
</header>

<slot />
{@render children()}

<footer>
<slot name="address" />
{@render address()}
</footer>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
---
title: Named slots
title: Named snippets
---

The previous example contained a _default slot_, which renders the direct children of a component. Sometimes you will need more control over placement. In those cases, we can use _named slots_.
The previous example contained a _default snippet_, which renders the direct children of a component. Sometimes you will need more control over placement. In those cases, we can use _named snippets_.

Inside `App.svelte`, we're rendering a `<Card>` component that contains `<span slot="telephone">` and others for `company` and `address`. Let's add the corresponding named slots in `Card.svelte`:
Inside `App.svelte`, we're rendering a `<Card>` component that contains `{#snippet telephone()}` and others for `company` and `address`. Let's add the corresponding named snippets in `Card.svelte`:

```svelte
/// file: Card.svelte
<script>
const { children+++, address, company, telephone+++ } = $props();
</script>

<div class="card">
+++ <header>
<slot name="telephone"></slot>
<slot name="company"></slot>
{@render telephone()}
{@render company()}
</header>+++

<slot></slot>
{@render children()}

+++ <footer>
<slot name="address"></slot>
{@render address()}
</footer>+++
</div>
```
Expand Down
Loading