Skip to content

Commit bf5fda6

Browse files
authored
docs: add svelte:element/document chapters (sveltejs#270)
from old tutorial - also updates some existing content
1 parent 8430140 commit bf5fda6

File tree

38 files changed

+158
-110
lines changed

38 files changed

+158
-110
lines changed

content/tutorial/03-advanced-svelte/09-special-elements/01-svelte-self/app-a/src/lib/Folder.svelte

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
}
1111
</script>
1212

13-
<span class:expanded on:click={toggle}
14-
>{name}</span
15-
>
13+
<button class:expanded on:click={toggle}>{name}</button>
1614

1715
{#if expanded}
1816
<ul>
@@ -29,13 +27,14 @@
2927
{/if}
3028

3129
<style>
32-
span {
30+
button {
3331
padding: 0 0 0 1.5em;
34-
background: url(/tutorial/icons/folder.svg) 0
35-
0.1em no-repeat;
32+
background: url(/tutorial/icons/folder.svg) 0 0.1em no-repeat;
3633
background-size: 1em 1em;
3734
font-weight: bold;
3835
cursor: pointer;
36+
border: none;
37+
margin: 0;
3938
}
4039
4140
.expanded {

content/tutorial/03-advanced-svelte/09-special-elements/01-svelte-self/app-b/src/lib/File.svelte

Lines changed: 0 additions & 17 deletions
This file was deleted.

content/tutorial/03-advanced-svelte/09-special-elements/01-svelte-self/app-b/src/lib/Folder.svelte

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
}
1111
</script>
1212

13-
<span class:expanded on:click={toggle}
14-
>{name}</span
15-
>
13+
<button class:expanded on:click={toggle}>{name}</button>
1614

1715
{#if expanded}
1816
<ul>
@@ -29,13 +27,14 @@
2927
{/if}
3028

3129
<style>
32-
span {
30+
button {
3331
padding: 0 0 0 1.5em;
34-
background: url(/tutorial/icons/folder.svg) 0
35-
0.1em no-repeat;
32+
background: url(/tutorial/icons/folder.svg) 0 0.1em no-repeat;
3633
background-size: 1em 1em;
3734
font-weight: bold;
3835
cursor: pointer;
36+
border: none;
37+
margin: 0;
3938
}
4039
4140
.expanded {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: <svelte:element>
3+
---
4+
5+
Sometimes we don't know in advance what kind of DOM element to render. `<svelte:element>` comes in handy here. Instead of a sequence of `if` blocks...
6+
7+
```html
8+
{#if selected === 'h1'}
9+
<h1>I'm a h1 tag</h1>
10+
{:else if selected === 'h3'}
11+
<h3>I'm a h3 tag</h3>
12+
{:else if selected === 'p'}
13+
<p>I'm a p tag</p>
14+
{/if}
15+
```
16+
17+
...we can have a single dynamic component:
18+
19+
```html
20+
<svelte:element this={selected}>I'm a {selected} tag</svelte:element>
21+
```
22+
23+
The `this` value can be any string, or a falsy value — if it's falsy, no element is rendered.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
const options = ['h1', 'h3', 'p'];
3+
let selected = options[0];
4+
</script>
5+
6+
<select bind:value={selected}>
7+
{#each options as option}
8+
<option value={option}>{option}</option>
9+
{/each}
10+
</select>
11+
12+
{#if selected === 'h1'}
13+
<h1>I'm a h1 tag</h1>
14+
{:else if selected === 'h3'}
15+
<h3>I'm a h3 tag</h3>
16+
{:else if selected === 'p'}
17+
<p>I'm a p tag</p>
18+
{/if}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
const options = ['h1', 'h3', 'p'];
3+
let selected = options[0];
4+
</script>
5+
6+
<select bind:value={selected}>
7+
{#each options as option}
8+
<option value={option}>{option}</option>
9+
{/each}
10+
</select>
11+
12+
<svelte:element this={selected}>I'm a {selected} tag</svelte:element>

0 commit comments

Comments
 (0)