Skip to content

Commit 2a46ae3

Browse files
author
Rich Harris
committed
tidy up
1 parent 8c3856e commit 2a46ae3

File tree

2 files changed

+12
-19
lines changed
  • content/tutorial/03-advanced-svelte/09-special-elements/03-svelte-element

2 files changed

+12
-19
lines changed

content/tutorial/03-advanced-svelte/09-special-elements/03-svelte-element/README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22
title: <svelte:element>
33
---
44

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...
5+
Similarly, we don't always know in advance what kind of DOM element to render. `<svelte:element>` comes in handy here. As with the [previous exercise](svelte-component), we can replace a long sequence of `if` blocks with a single dynamic element:
66

77
```svelte
88
/// file: App.svelte
9-
{#if selected === 'h1'}
10-
<h1>I'm a h1 tag</h1>
11-
{:else if selected === 'h3'}
12-
<h3>I'm a h3 tag</h3>
13-
{:else if selected === 'p'}
14-
<p>I'm a p tag</p>
15-
{/if}
16-
```
17-
18-
...we can have a single dynamic component:
9+
<select bind:value={selected}>
10+
{#each options as option}
11+
<option value={option}>{option}</option>
12+
{/each}
13+
</select>
1914
20-
```svelte
21-
/// file: App.svelte
22-
<svelte:element this={selected}>I'm a {selected} tag</svelte:element>
15+
+++<svelte:element this={selected}>
16+
I'm a {selected} tag
17+
</svelte:element>+++
2318
```
2419

2520
The `this` value can be any string, or a falsy value — if it's falsy, no element is rendered.
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
const options = ['h1', 'h3', 'p'];
2+
const options = ['h1', 'h2', 'h3', 'p', 'marquee'];
33
let selected = options[0];
44
</script>
55

@@ -11,8 +11,6 @@
1111

1212
{#if selected === 'h1'}
1313
<h1>I'm a <code>&lt;h1&gt;</code></h1>
14-
{:else if selected === 'h3'}
15-
<h3>I'm a <code>&lt;h3&gt;</code></h3>
16-
{:else if selected === 'p'}
17-
<p>I'm a <code>&lt;p&gt;</code></p>
14+
{:else}
15+
<p>TODO others</p>
1816
{/if}

0 commit comments

Comments
 (0)