-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
534 lines (447 loc) · 14.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Learn Lit</title>
<meta name="description" content="A cheat sheet for learning the basics of Lit, the JavaScript library for building web components.">
<script type="module" src="/build/index.js"></script>
<link rel="stylesheet" href="/styles.css"/>
<!-- Roboto Mono font -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="/images/flame-favicon.svg">
<link rel="mask-icon" href="/images/flame-favicon.svg" color="#324fff">
<meta property="og:url" content="https://lit.how/">
<meta property="og:title" content="Learn Lit">
<meta property="og:description" content="A cheat sheet for learning the basics of Lit, the JavaScript library for building web components.">
<meta property="og:site_name" content="lit.how">
<meta property="og:image" content="https://lit.how/images/screenshot.png">
<meta property="og:image:alt" content="Lit Logo">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@straversi1">
</head>
<body>
<div id="header">
<h1>Learn Lit</h1>
<p>A cheat sheet for learning the basics of <a href="https://lit.dev">Lit</a>, the JavaScript library for building web components. By <a href="https://twitter.com/straversi1">Steven Traversi</a>.</p>
</div>
<ol>
<li><a href="#component-definition">Component definition</a></li>
<li><a href="#styles">Styles</a></li>
<li><a href="#properties-and-state">Properties and state</a></li>
<li><a href="#event-handlers">Event handlers</a></li>
<li><a href="#pass-data-down">Pass data down</a></li>
<li><a href="#pass-data-up">Pass data up</a></li>
</ol>
<!----------------------
-- Create a component --
----------------------->
<h2 id="component-definition">Component definition</h2>
<h3>Create a component</h3>
<div class="group">
<p>
<code>LitElement</code> is the base class for all components.
<br><br>
<code>@customElement</code> is where you define the name of your component.
<br><br>
<code>render</code> is where you define your component's view.
<br><br>
Write HTML in <code>html`...`</code>.
</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="hello-world.ts">
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('hello-world')
export class HelloWorld extends LitElement {
render() {
return html`<p>Hello, world!</p>`;
}
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./hello-world.js"></script>
<hello-world></hello-world>
</script>
</playground-ide>
</div>
<h3>Import a component</h3>
<div class="group">
<p>To use a component, import its file.</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="hello-world.ts">
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
import './another-component.js'
@customElement('hello-world')
export class HelloWorld extends LitElement {
render() {
return html`Hello, world! <another-component></another-component>`;
}
}
</script>
<script type="sample/ts" filename="another-component.ts">
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('another-component')
export class AnotherComponent extends LitElement {
render() {
return html`(I'm another component.)`;
}
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./hello-world.js"></script>
<hello-world></hello-world>
</script>
</playground-ide>
</div>
<h4>👉 Important rules</h4>
<p class="important">Components are global HTML elements, you can't have more than one with the same name on a page. People are working on that though!</p>
<p class="important">Components must have dashes in their name (defined using <code>@customElement</code>).</p>
<!--------------
-- Add styles --
--------------->
<h2 id="styles">Styles</h2>
<h3>Add styles</h3>
<div class="group">
<p>Add styles by defining the <code>static styles</code> property. Write CSS in the <code>css</code> tag.</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="my-element.ts">
import { html, LitElement, css } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-element')
export class MyElement extends LitElement {
render() {
return html`<p>I'm blue</p><div>I'm red</div>`;
}
static styles = css`
p {
color: blue;
}
div {
color: red;
}
`;
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./my-element.js"></script>
<my-element></my-element>
</script>
</playground-ide>
</div>
<h3>Styles are scoped</h3>
<div class="group">
<p>Styles <i>only</i> apply to the current element. That means you can feel free to use super generic selectors that you'd normally have to make up class names for.</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="my-element.ts">
import { html, LitElement, css } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-element')
export class MyElement extends LitElement {
render() {
return html`<p>I'm blue</p>`;
}
static styles = css`
p {
color: blue;
}
`;
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./my-element.js"></script>
<my-element></my-element>
<p>I'm also a p, but I'm not blue.</p>
</script>
</playground-ide>
</div>
<h3>Add classes</h3>
<div class="group">
<p>To conditionally apply styles, use <code>classMap</code>.</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="my-element.ts">
import { html, LitElement, css } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
@customElement('my-element')
export class MyElement extends LitElement {
@state() counter = 0
firstUpdated() {
setInterval(() => this.counter += 1 , 1000);
}
render() {
const classes = {
red: this.counter % 2 === 0,
blue: this.counter % 2 === 1
};
return html`<p class=${classMap(classes)}>Hello!</p>`;
}
static styles = css`
.red {
color: red;
}
.blue {
color: blue;
}
`;
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./my-element.js"></script>
<my-element></my-element>
</script>
</playground-ide>
</div>
<!------------------------
-- Properties and State --
------------------------->
<h2 id="properties-and-state">Properties and State</h2>
<h3>Define state</h3>
<div class="group">
<p>
Use <code>@state</code> to define a state variable.
<br><br>
When the state changes, Lit will re-render any part of the component that references the state.
</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="my-counter.ts">
import { html, LitElement } from 'lit';
import { customElement, state } from 'lit/decorators.js';
@customElement('my-counter')
export class MyCounter extends LitElement {
@state() count = 0;
render() {
return html`
<p>Clicks: ${this.count}</p>
<button @click=${() => this.count++}> + </button>
`;
}
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./my-counter.js"></script>
<my-counter></my-counter>
</script>
</playground-ide>
</div>
<h3>Define a property</h3>
<div class="group">
<p>
Use <code>@property</code> to define a state variable that can be accessed externally.
<br><br>
When the property changes, Lit will re-render any part of the component that references the property (just like state).
</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="index.ts">
import { html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
@customElement('counter-app')
export class CounterApp extends LitElement {
@state() count = 0;
render() {
return html`
<number-display .number=${this.count}></number-display>
<button @click=${() => this.count++}> + </button>
`;
}
}
@customElement('number-display')
export class NumberDisplay extends LitElement {
@property({ type: Number }) number = 0;
render() {
return html`
<code>${this.number}</code>
`;
}
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./index.js"></script>
<counter-app></counter-app>
</script>
</playground-ide>
</div>
<!------------------
-- Event handlers --
------------------->
<h2 id="event-handlers">Event handlers</h2>
<h3>Add an event handler</h3>
<div class="group">
<p>
Add an event handler to a component using <code>@click=${handler}</code>. This is the same as adding an event listener like this: <code>el.addEventListener('click', handler)</code>. The handler will be called with the event object as the first argument.
</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="my-counter.ts">
import { html, LitElement } from 'lit';
import { customElement, state } from 'lit/decorators.js';
@customElement('my-counter')
export class MyCounter extends LitElement {
@state() count = 0;
render() {
return html`
<p>Clicks: ${this.count}</p>
<button @click=${this.increment}> + </button>
`;
}
increment() {
this.count++;
}
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./my-counter.js"></script>
<my-counter></my-counter>
</script>
</playground-ide>
</div>
<h3>Add an event handler to the component itself</h3>
<div class="group">
<p>
Add event handlers to the component itself in the <code>constructor()</code> method.
</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="my-counter.ts">
import { html, css, LitElement } from 'lit';
import { customElement, state } from 'lit/decorators.js';
@customElement('my-counter')
export class MyCounter extends LitElement {
@state() count = 0;
constructor() {
super();
this.addEventListener('mouseover', this.increment);
}
render() {
return html`
<p>Mouseover to increment</p>
<p>Count: ${this.count}</p>
`;
}
increment() {
this.count++;
}
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./my-counter.js"></script>
<my-counter></my-counter>
</script>
</playground-ide>
</div>
<!------------------
-- Pass Data Down --
------------------->
<h2 id="pass-data-down">Pass Data Down</h2>
<h3>Properties</h3>
<div class="group">
<p>
Pass data to child elements using property bindings, like this: <code>.name=${"Steven"}</code>. The value can be any expression.
<br><br>
For boolean properties, use a question mark instead of a period, like this: <code>?programmer=${true}</code>.
<br><br>
You can only bind to properties declared with <code>@property</code>, not <code>@state</code>.
</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="my-wallet.ts">
import { html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import './id-card.js';
@customElement('my-wallet')
export class MyWallet extends LitElement {
render() {
return html`
<id-card .name=${"Steven"} .age=${27} ?programmer=${true}></id-card>
`;
}
}
</script>
<script type="sample/ts" filename="id-card.ts">
import { html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
@customElement('id-card')
export class IdCard extends LitElement {
@property({ type: String }) name: string
@property({ type: Number }) age: number
@property({ type: Boolean }) programmer: boolean
render() {
return html`
<h3>${this.name}</h3>
<p>Age: ${this.age}</p>
<label>
<input disabled type="checkbox" ?checked=${this.programmer}>
Programmer
</label>
`;
}
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./my-wallet.js"></script>
<my-wallet></my-wallet>
</script>
</playground-ide>
</div>
<!------------------
-- Pass Data Up --
------------------->
<h2 id="pass-data-up">Pass Data Up</h2>
<h3>Events</h3>
<div class="group">
<p>
Pass data to ancestor elements using events. To emit an event, use <code>this.dispatchEvent</code>.
<br><br>
<code>dispatchEvent</code> takes an event object as the first argument.
<br><br>
Construct an event object like this: <code>new CustomEvent('event-name', { detail: data, bubbles: true })</code>.
<br><br>
Provide data you want to pass to ancestors in the <code>detail</code> property of the event.
<br><br>
Ancestors can react to the event by adding an event listener to the component, like this: <code>@event-name=${e => this.handler(e)}</code>.
</p>
<playground-ide editable-file-system line-numbers resizable>
<script type="sample/ts" filename="score-board.ts">
import { html, LitElement } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import './game-player.js';
@customElement('score-board')
export class ScoreBoard extends LitElement {
@state() playerOneScore = 0;
@state() playerTwoScore = 0;
render() {
return html`
<h1>${this.playerOneScore} - ${this.playerTwoScore}</h1>
<h2>Player 1</h2>
<game-player @score=${e => this.playerOneScore += e.detail}></game-player>
<h2>Player 2</h2>
<game-player @score=${e => this.playerTwoScore += e.detail}></game-player>
`;
}
}
</script>
<script type="sample/ts" filename="game-player.ts">
import { html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
@customElement('game-player')
export class GamePlayer extends LitElement {
render() {
return html`
<button @click=${() => this.handleScore(7)}>Touchdown!</button>
<button @click=${() => this.handleScore(3)}>Field goal!</button>
`;
}
handleScore(points) {
this.dispatchEvent(new CustomEvent('score', { detail: points, bubbles: true }));
}
}
</script>
<script type="sample/html" filename="index.html">
<script type="module" src="./score-board.js"></script>
<score-board></score-board>
</script>
</playground-ide>
</div>
</body>
</html>