-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Dynamic elements implementation <svelte:element> (#6898)
Closes #2324 Co-authored-by: Alfred Ringstad <alfred.ringstad@hyperlab.se> Co-authored-by: Simon Holthausen <simon.holthausen@accso.de> Co-authored-by: tanhauhau <lhtan93@gmail.com>
- Loading branch information
1 parent
54197c5
commit e0d9325
Showing
99 changed files
with
1,170 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
site/content/tutorial/16-special-elements/03-svelte-element/app-a/App.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<script> | ||
const options = ['h1', 'h3', 'p']; | ||
let selected = options[0]; | ||
</script> | ||
|
||
<select bind:value={selected}> | ||
{#each options as option} | ||
<option value={option}>{option}</option> | ||
{/each} | ||
</select> | ||
|
||
{#if selected === 'h1'} | ||
<h1>I'm a h1 tag</h1> | ||
{:else if selected === 'h3'} | ||
<h3>I'm a h3 tag</h3> | ||
{:else if selected === 'p'} | ||
<p>I'm a p tag</p> | ||
{/if} |
12 changes: 12 additions & 0 deletions
12
site/content/tutorial/16-special-elements/03-svelte-element/app-b/App.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script> | ||
const options = ['h1', 'h3', 'p']; | ||
let selected = options[0]; | ||
</script> | ||
|
||
<select bind:value={selected}> | ||
{#each options as option} | ||
<option value={option}>{option}</option> | ||
{/each} | ||
</select> | ||
|
||
<svelte:element this={selected}>I'm a {selected} tag</svelte:element> |
23 changes: 23 additions & 0 deletions
23
site/content/tutorial/16-special-elements/03-svelte-element/text.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: <svelte:element> | ||
--- | ||
|
||
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... | ||
|
||
```html | ||
{#if selected === 'h1'} | ||
<h1>I'm a h1 tag</h1> | ||
{:else if selected === 'h3'} | ||
<h3>I'm a h3 tag</h3> | ||
{:else if selected === 'p'} | ||
<p>I'm a p tag</p> | ||
{/if} | ||
``` | ||
|
||
...we can have a single dynamic component: | ||
|
||
```html | ||
<svelte:element this={selected}>I'm a {selected} tag</svelte:element> | ||
``` | ||
|
||
The `this` value can be any string, or a falsy value — if it's falsy, no element is rendered. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.