Skip to content
Open
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
114 changes: 57 additions & 57 deletions 03-slot-card/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Summary

In React we are used to the _children_ property of a componente, when we work
In React we are used to the _children_ property of a component, when we work
with Svelte we get _Slots_ and this includes some extra goodies:

- We can have a single slot or multiple named slots.
Expand All @@ -9,7 +9,7 @@ with Svelte we get _Slots_ and this includes some extra goodies:

# Step By Step Guide

- This example will take a starting point _00-scratch-typescript_
- This example will take a starting point _00-boiler-typescript_

- Let's install the packages.

Expand Down Expand Up @@ -60,9 +60,9 @@ _./src/common/index.ts_
export { default as CardComponent } from "./card.svelte";
```

Let's instantiate two instance of the component in our main _app_
Let's add two instances of the component in our main _App_:

_./src/app.svelte_
_./src/App.svelte_

```sv
<script lang="ts">
Expand All @@ -82,46 +82,46 @@ _./src/app.svelte_
</style>
```

- Cool,time to play with slots, we want to display in each card a different body.
- Cool, time to play with slots, we want to display in each card a different body.

- Let's create in our card componente slot for the body.
- Let's create in our card component a slot for the body.

_./src/common/card.svelte_

```diff
<div class="root">
<!-- header -->
<div class="card-header">
<h2>Default header</h2>
<div class="root">
<!-- header -->
<div class="card-header">
<h2>Default header</h2>
</div>
<!-- body -->
<div class="card-body">
+ <slot>
<h3>Default body</h3>
+ </slot>
</div>
</div>
<!-- body -->
<div class="card-body">
+ <slot>
<h3>Default body</h3>
+ </slot>
</div>
</div>
```

- Now let's define the body content for our card components instantiated in our app.

_./src/app.svelte_

```diff
<script lang="ts">
import { CardComponent } from "./common";
</script>

<main>
- <CardComponent/>
- <CardComponent/>
+ <CardComponent>
+ <h3>Body Card A</h3>
+ </CardComponent>
+ <CardComponent>
+ <h3>Body Card B</h3>
+ </CardComponent>
</main>
<script lang="ts">
import { CardComponent } from "./common";
</script>

<main>
- <CardComponent/>
- <CardComponent/>
+ <CardComponent>
+ <h3>Body Card A</h3>
+ </CardComponent>
+ <CardComponent>
+ <h3>Body Card B</h3>
+ </CardComponent>
</main>
```

- So far so good, but what if we want to include a new slot for the card heading?
Expand All @@ -130,40 +130,40 @@ _./src/app.svelte_
_./src/common/card.svelte_

```diff
<div class="root">
<!-- header -->
<div class="card-header">
+ <slot name="header">
<h2>Default header</h2>
+ </slot>
<div class="root">
<!-- header -->
<div class="card-header">
+ <slot name="header">
<h2>Default header</h2>
+ </slot>
</div>
<!-- body -->
<div class="card-body">
- <slot>
+ <slot name="body">
<h3>Default body</h3>
</slot>
</div>
</div>
<!-- body -->
<div class="card-body">
- <slot>
+ <slot name="body">
<h3>Default body</h3>
</slot>
</div>
</div>
```

- Now we got to named slots, let's define them in our card:

_./src/app.svelte_

```diff
<main>
<CardComponent>
+ <h2 slot="header">Header Card A</h2>
- <h3>Body Card A</h3>
+ <h3 slot="body">Body Card A</h3>
</CardComponent>
<CardComponent>
+ <h2 slot="header">Header Card B</h2>
- <h3>Body Card B</h3>
+ <h3 slot="body">Body Card B</h3>
</CardComponent>
</main>
<main>
<CardComponent>
+ <h2 slot="header">Header Card A</h2>
- <h3>Body Card A</h3>
+ <h3 slot="body">Body Card A</h3>
</CardComponent>
<CardComponent>
+ <h2 slot="header">Header Card B</h2>
- <h3>Body Card B</h3>
+ <h3 slot="body">Body Card B</h3>
</CardComponent>
</main>
```

We can have some additional control:
Expand Down
14 changes: 7 additions & 7 deletions 03-slot-card/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 03-slot-card/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@sveltejs/vite-plugin-svelte": "^1.0.1",
"@tsconfig/svelte": "^3.0.0",
"prettier-plugin-svelte": "^2.7.0",
"svelte": "^3.49.0",
"svelte": "^3.50.1",
"svelte-check": "^2.8.0",
"svelte-preprocess": "^4.10.7",
"tslib": "^2.4.0",
Expand Down