Skip to content

Commit

Permalink
[article] Create a lit cheat sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
e111077 committed Nov 23, 2024
1 parent 42d1ed5 commit b82f44e
Show file tree
Hide file tree
Showing 123 changed files with 2,491 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./my-element.js"></script>

<my-element></my-element>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
}
`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "/samples/v3-base.json",
"files": {
"my-element.ts": {},
"index.html": {}
},
"previewHeight": "100px"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./my-element.js"></script>

<my-element></my-element>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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;
}
`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "/samples/v3-base.json",
"files": {
"my-element.ts": {},
"index.html": {}
},
"previewHeight": "100px"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./my-element.js"></script>

<my-element></my-element>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { html, LitElement } from 'lit';
import { customElement, state } from 'lit/decorators.js';

@customElement('my-element')
export class MyElement extends LitElement {
@state() private someBoolean = false;

render() {
let someText = html`<p>Some text</p>`;

if (this.someBoolean) {
someText = html`<p>Some other text</p>`;
}

return html`
<button
@click=${() => {this.someBoolean = !this.someBoolean}}>
Toggle template
</button>
<div>This is an inline ternary conditional</div>
${this.someBoolean ? html`<p>Some other text</p>` : html`<p>Some text</p>`}
<div>This is a variable conditional</div>
${someText}
`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "/samples/v3-base.json",
"files": {
"my-element.ts": {},
"index.html": {}
},
"previewHeight": "200px"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script type="module" src="./my-element.js"></script>
<style>
.styled my-element::part(paragraph) {
color: yellow;
border-color: white;
padding: 8px;
margin: 2px;
}

.styled {
background-color: black;
}

div {
padding: 4px;
}
</style>

<div class="styled">
<my-element></my-element>
</div>
<div>
<my-element></my-element>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { html, LitElement, css } from 'lit';
import { customElement } from 'lit/decorators.js';

@customElement('my-element')
export class MyElement extends LitElement {
static styles = css`
p {
color: blue;
border: 1px solid black;
padding: 4px;
margin-block: 4px;
}
`;

render() {
return html`<p part="paragraph">This is in a shadow root!</p>`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "/samples/v3-base.json",
"files": {
"my-element.ts": {},
"index.html": {}
},
"previewHeight": "120px"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./my-element.js"></script>

<my-element array='1,"2",3,4,"5"'></my-element>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';import {ComplexAttributeConverter} from 'lit';

/**
* Bidirectionally converts an array from an attribute to a property of the
* following format:
*
* array-attribute='1, "2", 3' to [1, '2', 3]
*/
export const arrayConverter: ComplexAttributeConverter<Array<unknown>> = {
toAttribute: (array: Array<unknown>) => {
return JSON.stringify(array).substring(1, JSON.stringify(array).length - 1);
},
fromAttribute: (value: string) => {
try {
return JSON.parse(`[${value}]`);
} catch {
return [];
}
}
};

@customElement('my-element')
export class MyElement extends LitElement {
@property({ converter: arrayConverter, reflect: true })
array: Array<number|string> = [];

render() {
return this.array.map((item) =>
html`<div>${typeof item}: ${item}</div>`
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "/samples/v3-base.json",
"files": {
"my-element.ts": {},
"index.html": {}
},
"previewHeight": "200px"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';

export type ScoreEvent = CustomEvent<number>;

@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: number) {
this.dispatchEvent(new CustomEvent('score', { detail: points, bubbles: true }));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./score-board.js"></script>

<score-board></score-board>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "/samples/v3-base.json",
"files": {
"score-board.ts": {},
"game-player.ts": {},
"index.html": {}
},
"previewHeight": "250px"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { html, LitElement } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import './game-player.js';
import type {ScoreEvent} from './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: ScoreEvent) => this.playerOneScore += e.detail}></game-player>
<h2>Player 2</h2>
<game-player @score=${(e: ScoreEvent) => this.playerTwoScore += e.detail}></game-player>
`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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>`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./hello-world.js"></script>

<hello-world></hello-world>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "/samples/v3-base.json",
"files": {
"hello-world.ts": {},
"index.html": {}
},
"previewHeight": "100px"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./my-element.js"></script>

<my-element></my-element>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { html, LitElement, css } from 'lit';
import { customElement, state, query } from 'lit/decorators.js';

@customElement('my-element')
export class MyElement extends LitElement {
@state() private canvasText = 'Hello World!';
// You can use any querySelector selector
@query('#canvasId') private canvasEl!: HTMLCanvasElement;

render() {
return html`
<canvas id="canvasId"></canvas>
<label>
Enter text to draw in canvas:
<input @input=${this.handleInput} .value=${this.canvasText}>
</label>
`;
}

private setCanvasText(text: string) {
this.canvasText = text;
// Access the canvas elemtnwith this.canvasEl
const ctx = this.canvasEl.getContext("2d");

if (!ctx) {
return;
}

ctx.clearRect(0, 0, this.canvasEl.width, this.canvasEl.height);
ctx.font = "50px Arial";
ctx.fillText(this.canvasText,10,80);
}

private handleInput(event: Event) {
this.setCanvasText((event.target as HTMLInputElement).value);
}

protected firstUpdated() {
// DOM is typically ready for the first time by firstUpdated()
this.setCanvasText(this.canvasText);
}

static styles = css`
canvas {
border: 1px solid black;
}
label {
display: block;
margin-block-start: 1em;
}
`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "/samples/v3-base.json",
"files": {
"my-element.ts": {},
"index.html": {}
},
"previewHeight": "230px"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./my-element.js"></script>

<my-element></my-element>
Loading

0 comments on commit b82f44e

Please sign in to comment.