Skip to content

Commit 1fcf595

Browse files
committed
Change svelte example
1 parent a667d8e commit 1fcf595

File tree

1 file changed

+126
-85
lines changed

1 file changed

+126
-85
lines changed
Lines changed: 126 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<StepHead>
22

33
```svelte App.svelte
4-
<h1>Hello Svelte</h1>
4+
<h1>Svelte</h1>
55
```
66

77
</StepHead>
88

9-
^0
9+
Svelte uses a custom file format, similar to HTML
1010

1111
<StepHead>
1212

@@ -15,16 +15,57 @@
1515
let name = "Svelte"
1616
</script>
1717
18-
<h1>{name}</h1>
18+
<h1>Hello {name}</h1>
1919
```
2020

2121
</StepHead>
2222

23-
^1
23+
You can use curly braces to render data.
2424

2525
<StepHead>
2626

2727
```svelte App.svelte
28+
29+
```
30+
31+
---
32+
33+
```svelte MyComponent.svelte
34+
<div>
35+
<button>Hello</button>
36+
</div>
37+
```
38+
39+
</StepHead>
40+
41+
Each svelte file is a different component
42+
43+
<StepHead>
44+
45+
```svelte App.svelte focus=2,5,6
46+
<script>
47+
import MyComponent from './MyComponent.svelte'
48+
</script>
49+
50+
<MyComponent />
51+
<MyComponent />
52+
```
53+
54+
---
55+
56+
```svelte MyComponent.svelte focus=2[1]
57+
<div>
58+
<button>Hello</button>
59+
</div>
60+
```
61+
62+
</StepHead>
63+
64+
You can add imports inside the script tag
65+
66+
<StepHead>
67+
68+
```svelte App.svelte focus=5[14:26],6[14:28]
2869
<script>
2970
import MyComponent from './MyComponent.svelte'
3071
</script>
@@ -33,19 +74,32 @@
3374
<MyComponent name="Ronaldo" />
3475
```
3576

77+
---
78+
79+
```svelte MyComponent.svelte focus=2[1]
80+
<div>
81+
<button>Hello</button>
82+
</div>
83+
```
84+
3685
</StepHead>
3786

38-
^2
87+
To share data with children components
3988

4089
<StepHead>
4190

42-
```svelte App.svelte focus=2
91+
```svelte App.svelte focus=5[14:26],6[14:28]
92+
<script>
93+
import MyComponent from './MyComponent.svelte'
94+
</script>
4395
96+
<MyComponent name="Messi" />
97+
<MyComponent name="Ronaldo" />
4498
```
4599

46100
---
47101

48-
```svelte MyComponent.svelte
102+
```svelte MyComponent.svelte focus=2,6
49103
<script>
50104
export let name;
51105
</script>
@@ -57,45 +111,47 @@
57111

58112
</StepHead>
59113

60-
^3 Each svelte file is a different component
114+
you can create props using the export keyword
61115

62116
<StepHead>
63117

64-
```svelte App.svelte
65-
66-
```
67-
68-
```svelte MyComponent.svelte focus=2 active
118+
```svelte MyComponent.svelte focus=3,8
69119
<script>
70120
export let name;
121+
let goalCount = 2;
71122
</script>
72123
73124
<div>
74125
<button>{name}</button>
126+
{"⚽".repeat(goalCount)}
75127
</div>
76128
```
77129

78130
</StepHead>
79131

80-
^4 You can declare props with the export keyword.
132+
Inside curly braces you can put any javascript expression
81133

82134
<StepHead>
83135

84-
```svelte MyComponent.svelte focus=3,8
136+
```svelte MyComponent.svelte focus=11[11:32]
85137
<script>
86138
export let name;
87139
let goalCount = 2;
140+
141+
function handleClick() {
142+
// TODO
143+
}
88144
</script>
89145
90146
<div>
91-
<button>{name}</button>
147+
<button on:click={handleClick}>{name}</button>
92148
{"⚽".repeat(goalCount)}
93149
</div>
94150
```
95151

96152
</StepHead>
97153

98-
^5 using js inside curly braces
154+
With the on directive you can listen to events
99155

100156
<StepHead>
101157

@@ -117,7 +173,7 @@
117173

118174
</StepHead>
119175

120-
^6 adding event handlers
176+
here, when the goalcount changes after a click, svelte will rerender the component (show)
121177

122178
<StepHead>
123179

@@ -143,11 +199,11 @@
143199

144200
</StepHead>
145201

146-
^7 firing custom events
202+
You can also use a custom event to share data with the parent
147203

148204
<StepHead>
149205

150-
```svelte App.svelte focus=4:6,9[27:42],10[29:44]
206+
```svelte App.svelte focus=9[27:42],10[29:44]
151207
<script>
152208
import MyComponent from './MyComponent.svelte'
153209
@@ -160,123 +216,108 @@
160216
<MyComponent name="Ronaldo" on:goal={onGoal}/>
161217
```
162218

219+
---
220+
221+
```svelte MyComponent.svelte focus=10
222+
223+
```
224+
163225
</StepHead>
164226

165-
handling custom events
227+
and use the on directive again to handle it
166228

167229
<StepHead>
168230

169-
```svelte App.svelte focus=3,9,12
231+
```svelte App.svelte focus=4:6,9[27:42],10[29:44]
170232
<script>
171233
import MyComponent from './MyComponent.svelte'
172-
import MyBox from './MyBox.svelte'
234+
173235
function onGoal(event) {
174236
console.log(event.detail.player)
175237
}
176238
</script>
177239
178-
<MyBox>
179-
<MyComponent name="Messi" on:goal={onGoal}/>
180-
<MyComponent name="Ronaldo" on:goal={onGoal}/>
181-
</MyBox>
182-
240+
<MyComponent name="Messi" on:goal={onGoal}/>
241+
<MyComponent name="Ronaldo" on:goal={onGoal}/>
183242
```
184243

185-
---
186-
187244
</StepHead>
188245

189-
adding style
246+
and do something with the data
190247

191248
<StepHead>
192249

193-
```svelte App.svelte focus=9,12
194-
195-
```
196-
197-
---
198-
199-
```svelte MyBox.svelte
200-
<div>
201-
something
202-
</div>
203-
204-
<style>
205-
div {
206-
border: 8px solid hotpink;
250+
```svelte App.svelte focus=3
251+
<script>
252+
import MyComponent from './MyComponent.svelte'
253+
const players = ['Messi', 'Ronaldo', 'Laspada']
254+
function onGoal(event) {
255+
console.log(event.detail.player)
207256
}
208-
</style>
209-
```
210-
211-
</StepHead>
212-
213-
...
214-
215-
<StepHead>
216-
217-
```svelte App.svelte focus=9,12
218-
219-
```
220-
221-
---
222-
223-
```svelte MyBox.svelte focus=2
224-
<div>
225-
<slot />
226-
</div>
257+
</script>
227258
228-
<style>
229-
div {
230-
border: 8px solid hotpink;
231-
}
232-
</style>
259+
<MyComponent name="Messi" on:goal={onGoal}/>
260+
<MyComponent name="Ronaldo" on:goal={onGoal}/>
233261
```
234262

235263
</StepHead>
236264

237-
using slot
265+
If you want to render a list
238266

239267
<StepHead>
240268

241-
```svelte App.svelte focus=4
269+
```svelte App.svelte focus=3,9,10[16:28],11
242270
<script>
243271
import MyComponent from './MyComponent.svelte'
244-
import MyBox from './MyBox.svelte'
245272
const players = ['Messi', 'Ronaldo', 'Laspada']
246273
function onGoal(event) {
247274
console.log(event.detail.player)
248275
}
249276
</script>
250277
251-
<MyBox>
252-
<MyComponent name="Messi" on:goal={onGoal}/>
253-
<MyComponent name="Ronaldo" on:goal={onGoal}/>
254-
</MyBox>
278+
{#each players as player}
279+
<MyComponent name={player} on:goal={onGoal}/>
280+
{/each}
255281
```
256282

257283
</StepHead>
258284

259-
rendering lists
285+
you can use the each block.
260286

261287
<StepHead>
262288

263-
```svelte App.svelte focus=4,11,12[18:30],13
289+
```svelte App.svelte focus=9:13
264290
<script>
265291
import MyComponent from './MyComponent.svelte'
266-
import MyBox from './MyBox.svelte'
267292
const players = ['Messi', 'Ronaldo', 'Laspada']
268293
function onGoal(event) {
269294
console.log(event.detail.player)
270295
}
271296
</script>
272297
273-
<MyBox>
274-
{#each players as player}
275-
<MyComponent name={player} on:goal={onGoal}/>
276-
{/each}
277-
</MyBox>
298+
<style>
299+
div {
300+
border: 8px solid hotpink;
301+
}
302+
</style>
303+
304+
<div>
305+
{#each players as player}
306+
<MyComponent name={player} on:goal={onGoal}/>
307+
{/each}
308+
</div>
309+
```
310+
311+
</StepHead>
312+
313+
Svelte also support style tags,
314+
315+
<StepHead>
316+
317+
```svelte App.svelte focus=10:12,15,19
318+
278319
```
279320

280321
</StepHead>
281322

282-
...
323+
Here the style only applies to the div from the current component and not others.

0 commit comments

Comments
 (0)