Skip to content

docs: add svelte:element/document chapters #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2023
Merged
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
Expand Up @@ -10,9 +10,7 @@
}
</script>

<span class:expanded on:click={toggle}
>{name}</span
>
<button class:expanded on:click={toggle}>{name}</button>

{#if expanded}
<ul>
Expand All @@ -29,13 +27,14 @@
{/if}

<style>
span {
button {
padding: 0 0 0 1.5em;
background: url(https://github.com/tutorial/icons/folder.svg) 0
0.1em no-repeat;
background: url(https://github.com/tutorial/icons/folder.svg) 0 0.1em no-repeat;
background-size: 1em 1em;
font-weight: bold;
cursor: pointer;
border: none;
margin: 0;
}

.expanded {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
}
</script>

<span class:expanded on:click={toggle}
>{name}</span
>
<button class:expanded on:click={toggle}>{name}</button>

{#if expanded}
<ul>
Expand All @@ -29,13 +27,14 @@
{/if}

<style>
span {
button {
padding: 0 0 0 1.5em;
background: url(https://github.com/tutorial/icons/folder.svg) 0
0.1em no-repeat;
background: url(https://github.com/tutorial/icons/folder.svg) 0 0.1em no-repeat;
background-size: 1em 1em;
font-weight: bold;
cursor: pointer;
border: none;
margin: 0;
}

.expanded {
Expand Down
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.
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}
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: <svelte:document>
---

Similar to `<svelte:window>`, the `<svelte:document>` element allows you to listen for events that fire on `document`. This is useful with events like `selectionchange`, which doesn't fire on `window`.

Add the `selectionchange` handler to the `<svelte:document>` tag:

```html
<svelte:document on:selectionchange={handleSelectionChange} />
```

> Avoid `mouseenter` and `mouseleave` handlers on this element, these events are not fired on `document` in all browsers. Use `<svelte:body>` for this instead.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
let selection = '';

const handleSelectionChange = (e) => selection = document.getSelection();
</script>

<svelte:body />

<p>Select this text to fire events</p>
<p>Selection: {selection}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
let selection = '';

const handleSelectionChange = (e) => selection = document.getSelection();
</script>

<svelte:document on:selectionchange={handleSelectionChange} />

<p>Select this text to fire events</p>
<p>Selection: {selection}</p>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@

export let todo;

let div;
let button;

afterUpdate(() => {
flash(div);
flash(button);
});
</script>

<!-- the text will flash red whenever
the `todo` object changes -->
<div bind:this={div} on:click>
<button bind:this={button} type="button" on:click>
{todo.done ? '👍' : ''}
{todo.text}
</div>
</button>

<style>
div {
button {
all: unset;
display: block;
cursor: pointer;
line-height: 1.5;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@

export let todo;

let div;
let button;

afterUpdate(() => {
flash(div);
flash(button);
});
</script>

<!-- the text will flash red whenever
the `todo` object changes -->
<div bind:this={div} on:click>
<button bind:this={button} type="button" on:click>
{todo.done ? '👍' : ''}
{todo.text}
</div>
</button>

<style>
div {
button {
all: unset;
display: block;
cursor: pointer;
line-height: 1.5;
}
Expand Down
14 changes: 7 additions & 7 deletions content/tutorial/common/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 content/tutorial/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"devDependencies": {
"@sveltejs/kit": "^1.7.2",
"esbuild-wasm": "^0.17.9",
"svelte": "^3.55.1",
"svelte": "^3.57.0",
"vite": "^4.1.3"
},
"type": "module"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"esbuild": "^0.17.11",
"prettier": "^2.8.4",
"prettier-plugin-svelte": "^2.9.0",
"svelte": "^3.56.0",
"svelte": "^3.57.0",
"svelte-check": "^3.1.1",
"tiny-glob": "^0.2.9",
"typescript": "~4.9.5",
Expand Down
Loading