Skip to content

Commit 382d710

Browse files
author
Rich Harris
committed
fixes
1 parent 194905c commit 382d710

File tree

4 files changed

+54
-61
lines changed

4 files changed

+54
-61
lines changed

content/tutorial/02-advanced-svelte/09-special-elements/10-svelte-fragment/README.md

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,27 @@
22
title: <svelte:fragment>
33
---
44

5-
The `<svelte:fragment>` element allows you to place content in a named slot without wrapping it in a container DOM element. This keeps the flow layout of your document intact.
5+
The `<svelte:fragment>` element allows you to place content in a named slot without wrapping it in a container DOM element.
66

7-
In the example notice how we applied a flex layout with a gap of `1em` to the box.
7+
In this exercise, we're making a Tic-Tac-Toe game. To form a grid, the `<button>` elements in `App.svelte` should be direct descendants of the `<div class="board">` element in `Board.svelte`.
88

9-
```svelte
10-
/// file: Box.svelte
11-
<div class="box">
12-
<slot name="header">No header was provided</slot>
13-
<p>Some content between header and footer</p>
14-
<slot name="footer"></slot>
15-
</div>
16-
17-
<style>
18-
.box {
19-
display: flex;
20-
flex-direction: column;
21-
gap: 1em;
22-
}
23-
</style>
24-
```
25-
26-
However, the content in the footer is not spaced out according to this rhythm because wrapping it in a div created a new flow layout.
27-
28-
We can solve this by changing `<div slot="footer">` in the `App` component. Replace the `<div>` with `<svelte:fragment>`:
9+
At the moment, it's horribly broken, because they're children of `<div slot="game">` instead. Let's fix it:
2910

3011
```svelte
3112
/// file: App.svelte
32-
<svelte:fragment slot="footer">
33-
<p>All rights reserved.</p>
34-
<p>Copyright (c) 2019 Svelte Industries</p>
35-
</svelte:fragment>
36-
```
13+
<+++svelte:fragment+++ slot="game">
14+
{#each squares as square, i}
15+
<button
16+
class="square"
17+
class:winning={winningLine?.includes(i)}
18+
disabled={square}
19+
on:click={() => {
20+
squares[i] = next;
21+
next = next === 'x' ? 'o' : 'x';
22+
}}
23+
>
24+
{square}
25+
</button>
26+
{/each}
27+
</+++svelte:fragment+++>
28+
```

content/tutorial/02-advanced-svelte/09-special-elements/10-svelte-fragment/app-a/src/lib/App.svelte

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,16 @@
11
<script>
22
import Board from './Board.svelte';
3+
import { getWinningLine } from './utils.js';
34
45
let squares = Array(9).fill('');
56
let next = 'x';
67
7-
function getLine(squares) {
8-
const lines = [
9-
[0, 1, 2],
10-
[3, 4, 5],
11-
[6, 7, 8],
12-
[0, 3, 6],
13-
[1, 4, 7],
14-
[2, 5, 8],
15-
[0, 4, 8],
16-
[2, 4, 6]
17-
];
18-
19-
for (const line of lines) {
20-
const [a, b, c] = line;
21-
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
22-
return line;
23-
}
24-
}
25-
26-
return null;
27-
}
28-
29-
$: winningLine = getLine(squares);
8+
$: winningLine = getWinningLine(squares);
309
</script>
3110

3211
<div class="container">
3312
<Board size={3}>
34-
<svelte:fragment slot="game">
13+
<div slot="game">
3514
{#each squares as square, i}
3615
<button
3716
class="square"
@@ -45,15 +24,17 @@
4524
{square}
4625
</button>
4726
{/each}
48-
</svelte:fragment>
49-
</Board>
27+
</div>
5028

51-
<button on:click={() => {
52-
squares = Array(9).fill('');
53-
next = 'x';
54-
}}>
55-
Reset
56-
</button>
29+
<div slot="controls">
30+
<button on:click={() => {
31+
squares = Array(9).fill('');
32+
next = 'x';
33+
}}>
34+
Reset
35+
</button>
36+
</div>
37+
</Board>
5738
</div>
5839

5940
<style>
@@ -63,7 +44,6 @@
6344
gap: 1em;
6445
align-items: center;
6546
justify-content: center;
66-
width: min(calc(100vmin - 2em), 20em);
6747
height: 100%;
6848
margin: 0 auto;
6949
}
@@ -74,6 +54,7 @@
7454
color: #222;
7555
opacity: 1;
7656
font-size: 2em;
57+
padding: 0;
7758
}
7859
7960
.winning {

content/tutorial/02-advanced-svelte/09-special-elements/10-svelte-fragment/app-a/src/lib/Board.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
display: grid;
1414
grid-template-columns: repeat(var(--size), 1fr);
1515
grid-template-rows: repeat(var(--size), 1fr);
16-
width: 100%;
16+
height: 100%;
17+
max-height: 20em;
1718
aspect-ratio: 1;
1819
background: black;
1920
gap: 1px;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const lines = [
2+
[0, 1, 2],
3+
[3, 4, 5],
4+
[6, 7, 8],
5+
[0, 3, 6],
6+
[1, 4, 7],
7+
[2, 5, 8],
8+
[0, 4, 8],
9+
[2, 4, 6]
10+
];
11+
12+
export function getWinningLine(squares) {
13+
for (const line of lines) {
14+
const [a, b, c] = line;
15+
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
16+
return line;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)