Skip to content

Commit d6d9a5e

Browse files
authored
Merge pull request sveltejs#2425 from sveltejs/sveltejsgh-2423
update svelte:options tutorial chapter, remove example
2 parents 8b9c4f3 + ab84aee commit d6d9a5e

File tree

16 files changed

+182
-108
lines changed

16 files changed

+182
-108
lines changed

site/content/examples/16-special-elements/06-svelte-options/App.svelte

-14
This file was deleted.

site/content/examples/16-special-elements/06-svelte-options/Square.svelte

-15
This file was deleted.

site/content/examples/16-special-elements/06-svelte-options/meta.json

-3
This file was deleted.

site/content/examples/20-miscellaneous/02-immutable-data/App.svelte

+2-8
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@
2929

3030
<h2>Immutable</h2>
3131
{#each todos as todo}
32-
<label on:click="{() => toggle(todo.id)}">
33-
<span>{todo.done ? "😎": "🙁"}</span>
34-
<ImmutableTodo {todo}/>
35-
</label>
32+
<ImmutableTodo {todo} on:click="{() => toggle(todo.id)}"/>
3633
{/each}
3734

3835
<h2>Mutable</h2>
3936
{#each todos as todo}
40-
<label on:click="{() => toggle(todo.id)}">
41-
<span>{todo.done ? "😎": "🙁"}</span>
42-
<MutableTodo {todo}/>
43-
</label>
37+
<MutableTodo {todo} on:click="{() => toggle(todo.id)}"/>
4438
{/each}

site/content/examples/20-miscellaneous/02-immutable-data/ImmutableTodo.svelte

+17-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@
55
import flash from './flash.js';
66
77
export let todo;
8-
let span;
8+
export let toggle;
99
10-
flash(() => span);
10+
let div;
11+
12+
afterUpdate(() => {
13+
flash(div);
14+
});
1115
</script>
1216

17+
<style>
18+
div {
19+
cursor: pointer;
20+
line-height: 1.5;
21+
}
22+
</style>
23+
1324
<!-- the text will flash red whenever
14-
the `todo` object changes -->
15-
<span bind:this={span}>{todo.text}</span>
25+
the `todo` object changes -->
26+
<div bind:this={div} on:click>
27+
{todo.done ? '👍': ''} {todo.text}
28+
</div>

site/content/examples/20-miscellaneous/02-immutable-data/MutableTodo.svelte

+17-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@
33
import flash from './flash.js';
44
55
export let todo;
6-
let span;
6+
export let toggle;
77
8-
flash(() => span);
8+
let div;
9+
10+
afterUpdate(() => {
11+
flash(div);
12+
});
913
</script>
1014

15+
<style>
16+
div {
17+
cursor: pointer;
18+
line-height: 1.5;
19+
}
20+
</style>
21+
1122
<!-- the text will flash red whenever
12-
the `todo` object changes -->
13-
<span bind:this={span}>{todo.text}</span>
23+
the `todo` object changes -->
24+
<div bind:this={div} on:click>
25+
{todo.done ? '👍': ''} {todo.text}
26+
</div>
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { afterUpdate } from 'svelte';
1+
export default function flash(element) {
2+
element.style.transition = 'none';
3+
element.style.color = 'rgba(255,62,0,1)';
4+
element.style.backgroundColor = 'rgba(255,62,0,0.2)';
25

3-
export default function flash(fn) {
4-
afterUpdate(() => {
5-
const span = fn();
6-
7-
span.style.color = 'red';
8-
9-
setTimeout(() => {
10-
span.style.color = 'black';
11-
}, 400);
6+
setTimeout(() => {
7+
element.style.transition = 'color 1s, background 1s';
8+
element.style.color = '';
9+
element.style.backgroundColor = '';
1210
});
1311
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
<script>
2-
import Square from './Square.svelte';
3-
</script>
2+
import Todo from './Todo.svelte';
3+
4+
let todos = [
5+
{ id: 1, done: true, text: 'wash the car' },
6+
{ id: 2, done: false, text: 'take the dog for a walk' },
7+
{ id: 3, done: false, text: 'mow the lawn' }
8+
];
49
5-
<style>
6-
svg {
7-
width: 100%;
8-
height: 100%;
10+
function toggle(toggled) {
11+
todos = todos.map(todo => {
12+
if (todo === toggled) {
13+
// return a new object
14+
return {
15+
id: todo.id,
16+
text: todo.text,
17+
done: !todo.done
18+
};
19+
}
20+
21+
// return the same object
22+
return todo;
23+
});
924
}
10-
</style>
25+
</script>
1126

12-
<svg viewBox="0 0 100 100">
13-
<Square cx={50} cy={50} size={40} style="fill: #ff3e00"/>
14-
</svg>
27+
<h2>Todos</h2>
28+
{#each todos as todo}
29+
<Todo {todo} on:click={() => toggle(todo)}/>
30+
{/each}

site/content/tutorial/16-special-elements/07-svelte-options/app-a/Square.svelte

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<script>
2+
import { afterUpdate } from 'svelte';
3+
import flash from './flash.js';
4+
5+
export let todo;
6+
export let toggle;
7+
8+
let div;
9+
10+
afterUpdate(() => {
11+
flash(div);
12+
});
13+
</script>
14+
15+
<style>
16+
div {
17+
cursor: pointer;
18+
line-height: 1.5;
19+
}
20+
</style>
21+
22+
<!-- the text will flash red whenever
23+
the `todo` object changes -->
24+
<div bind:this={div} on:click>
25+
{todo.done ? '👍': ''} {todo.text}
26+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function flash(element) {
2+
element.style.transition = 'none';
3+
element.style.color = 'rgba(255,62,0,1)';
4+
element.style.backgroundColor = 'rgba(255,62,0,0.2)';
5+
6+
setTimeout(() => {
7+
element.style.transition = 'color 1s, background 1s';
8+
element.style.color = '';
9+
element.style.backgroundColor = '';
10+
});
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
<script>
2-
import Square from './Square.svelte';
3-
</script>
2+
import Todo from './Todo.svelte';
3+
4+
let todos = [
5+
{ id: 1, done: true, text: 'wash the car' },
6+
{ id: 2, done: false, text: 'take the dog for a walk' },
7+
{ id: 3, done: false, text: 'mow the lawn' }
8+
];
49
5-
<style>
6-
svg {
7-
width: 100%;
8-
height: 100%;
10+
function toggle(toggled) {
11+
todos = todos.map(todo => {
12+
if (todo === toggled) {
13+
// return a new object
14+
return {
15+
id: todo.id,
16+
text: todo.text,
17+
done: !todo.done
18+
};
19+
}
20+
21+
// return the same object
22+
return todo;
23+
});
924
}
10-
</style>
25+
</script>
1126

12-
<svg viewBox="0 0 100 100">
13-
<Square cx={50} cy={50} size={40} style="fill: #ff3e00"/>
14-
</svg>
27+
<h2>Todos</h2>
28+
{#each todos as todo}
29+
<Todo {todo} on:click={() => toggle(todo)}/>
30+
{/each}

site/content/tutorial/16-special-elements/07-svelte-options/app-b/Square.svelte

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<svelte:options immutable={true}/>
2+
3+
<script>
4+
import { afterUpdate } from 'svelte';
5+
import flash from './flash.js';
6+
7+
export let todo;
8+
export let toggle;
9+
10+
let div;
11+
12+
afterUpdate(() => {
13+
flash(div);
14+
});
15+
</script>
16+
17+
<style>
18+
div {
19+
cursor: pointer;
20+
line-height: 1.5;
21+
}
22+
</style>
23+
24+
<!-- the text will flash red whenever
25+
the `todo` object changes -->
26+
<div bind:this={div} on:click>
27+
{todo.done ? '👍': ''} {todo.text}
28+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function flash(element) {
2+
element.style.transition = 'none';
3+
element.style.color = 'rgba(255,62,0,1)';
4+
element.style.backgroundColor = 'rgba(255,62,0,0.2)';
5+
6+
setTimeout(() => {
7+
element.style.transition = 'color 1s, background 1s';
8+
element.style.color = '';
9+
element.style.backgroundColor = '';
10+
});
11+
}

site/content/tutorial/16-special-elements/07-svelte-options/text.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ title: <svelte:options>
44

55
Lastly, `<svelte:options>` allows you to specify compiler options.
66

7-
Here, we have a component inside an `<svg>` element. Unless we tell Svelte otherwise, it will compile `Square.svelte` to a module that generates HTML nodes instead of SVG nodes. We can correct that by adding this to the top of `Square.svelte`:
7+
We'll use the `immutable` option as an example. In this app, the `<Todo>` component flashes whenever it receives new data. Clicking on one of the items toggles its `done` state by creating an updated `todos` array. This causes the *other* `<Todo>` items to flash, even though they don't end up making any changes to the DOM.
8+
9+
We can optimise this by telling the `<Todo>` component to expect *immutable* data. This means that we're promising never to *mutate* the `todo` prop, but will instead create new todo objects whenever things change.
10+
11+
Add this to the top of the `Todo.svelte` file:
812

913
```html
10-
<svelte:options namespace="svg"/>
14+
<svelte:options immutable={true}/>
1115
```
1216

17+
> You can shorten this to `<svelte:options immutable/>` if you prefer.
18+
19+
Now, when you toggle todos by clicking on them, only the updated component flashes.
20+
1321
The options that can be set here are:
1422

1523
* `immutable={true}` — you never use mutable data, so the compiler can do simple referential equality checks to determine if values have changed

0 commit comments

Comments
 (0)