From aa597858068317e1838f8106956ea994fe399e0a Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Tue, 3 Sep 2024 00:52:38 +0200 Subject: [PATCH] feat: update all examples to use latest features --- examples/attach-effect/Counter.mjs | 6 +-- examples/attach-effect/Decrease.mjs | 5 +- examples/demo/BooleanPropTest.mjs | 6 ++- examples/demo/HelloWorld.mjs | 25 ++-------- examples/demo/SimpleText.mjs | 7 +-- examples/pens/counter-toggle.html | 10 ++-- examples/props-blueprint/hello-world.js | 4 +- examples/props-blueprint/index.js | 7 +-- examples/type-restore/Counter.mjs | 12 +---- examples/type-restore/HelloWorld.mjs | 9 ++-- examples/type-restore/Object.mjs | 64 +++++++++++++------------ examples/type-restore/Toggle.mjs | 9 ++-- examples/use-shadow/index.js | 2 +- 13 files changed, 68 insertions(+), 98 deletions(-) diff --git a/examples/attach-effect/Counter.mjs b/examples/attach-effect/Counter.mjs index 5897e1d..3275173 100644 --- a/examples/attach-effect/Counter.mjs +++ b/examples/attach-effect/Counter.mjs @@ -1,11 +1,11 @@ // @ts-check -import { WebComponent, attachEffect } from "../../src/index.js"; +import { WebComponent, attachEffect, html } from "../../src/index.js"; export class Counter extends WebComponent { static props = { count: 0, }; + onInit() { - this.onclick = () => ++this.props.count; attachEffect(this.props.count, (count) => console.log(count)); } @@ -14,7 +14,7 @@ export class Counter extends WebComponent { } get template() { - return ``; + return html``; } } diff --git a/examples/attach-effect/Decrease.mjs b/examples/attach-effect/Decrease.mjs index 70c7d56..5f7d319 100644 --- a/examples/attach-effect/Decrease.mjs +++ b/examples/attach-effect/Decrease.mjs @@ -1,5 +1,5 @@ // @ts-check -import { WebComponent, attachEffect } from "../../src/index.js"; +import { WebComponent, attachEffect, html } from "../../src/index.js"; export class Decrease extends WebComponent { static props = { @@ -7,7 +7,6 @@ export class Decrease extends WebComponent { }; onInit() { - this.onclick = () => --this.props.count; attachEffect(this.props.count, (count) => console.log(count)); } @@ -16,7 +15,7 @@ export class Decrease extends WebComponent { } get template() { - return ``; + return html``; } } diff --git a/examples/demo/BooleanPropTest.mjs b/examples/demo/BooleanPropTest.mjs index 956ee31..f975aa2 100644 --- a/examples/demo/BooleanPropTest.mjs +++ b/examples/demo/BooleanPropTest.mjs @@ -1,4 +1,4 @@ -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; export class BooleanPropTest extends WebComponent { static props = { @@ -7,7 +7,9 @@ export class BooleanPropTest extends WebComponent { }; get template() { - return `

is-inline: ${this.props.isInline}

another-one: ${this.props.anotherone}

`; + return html` +

is-inline: ${this.props.isInline}

another-one: ${this.props.anotherone}

+ `; } } diff --git a/examples/demo/HelloWorld.mjs b/examples/demo/HelloWorld.mjs index 30ee97b..9d2e734 100644 --- a/examples/demo/HelloWorld.mjs +++ b/examples/demo/HelloWorld.mjs @@ -1,5 +1,5 @@ // @ts-check -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; export class HelloWorld extends WebComponent { static props = { @@ -9,32 +9,15 @@ export class HelloWorld extends WebComponent { onInit() { this.props.count = 0; - this.onclick = () => ++this.props.count; } get template() { const label = this.props.count ? `Clicked ${this.props.count}` : "World"; const emote = this.props.emotion === "sad" ? ". 😭" : "! 🙌"; - const button = document.createElement("button"); - button.innerText = `Hello ${label}${emote}`; - const paragraph = document.createElement("p"); - paragraph.innerText = "Oh what, dynamic DOM?"; - - return [button, paragraph, "no way"]; - } - - render() { - if (typeof this.template === "string") { - this.innerHTML = this.template; - } else if (this.template instanceof Node) { - this.replaceChildren(this.template); - } else if ( - Array.isArray(this.template) && - this.template.every((t) => t instanceof Node || typeof t === "string") - ) { - this.replaceChildren(...this.template); - } + return html` + + ` } } diff --git a/examples/demo/SimpleText.mjs b/examples/demo/SimpleText.mjs index fa9efa1..54fffd8 100644 --- a/examples/demo/SimpleText.mjs +++ b/examples/demo/SimpleText.mjs @@ -1,13 +1,10 @@ // @ts-check -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; class SimpleText extends WebComponent { clickCallback() { console.log(">>> click!"); } - onInit() { - this.onclick = this.clickCallback; - } onDestroy() { console.log(">>> removing event listener"); @@ -15,7 +12,7 @@ class SimpleText extends WebComponent { } get template() { - return `Click me!`; + return html`Click me!`; } } diff --git a/examples/pens/counter-toggle.html b/examples/pens/counter-toggle.html index 20cde20..a234cb6 100644 --- a/examples/pens/counter-toggle.html +++ b/examples/pens/counter-toggle.html @@ -13,7 +13,7 @@ import { WebComponent, html, - } from "https://unpkg.com/web-component-base@latest/index.js"; + } from "https://esm.sh/web-component-base@latest"; export class Counter extends WebComponent { static props = { @@ -30,11 +30,11 @@ static props = { toggle: false, }; - onInit() { - this.onclick = () => (this.props.toggle = !this.props.toggle); - } + + clickFn = () => (this.props.toggle = !this.props.toggle); + get template() { - return ``; + return html``; } } diff --git a/examples/props-blueprint/hello-world.js b/examples/props-blueprint/hello-world.js index c0ca4dc..df9398f 100644 --- a/examples/props-blueprint/hello-world.js +++ b/examples/props-blueprint/hello-world.js @@ -1,11 +1,11 @@ -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; export class HelloWorld extends WebComponent { static props = { myName: "World", }; get template() { - return `

Hello ${this.props.myName}

`; + return html`

Hello ${this.props.myName}

`; } } diff --git a/examples/props-blueprint/index.js b/examples/props-blueprint/index.js index 622ad35..cea7cad 100644 --- a/examples/props-blueprint/index.js +++ b/examples/props-blueprint/index.js @@ -1,14 +1,11 @@ -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; export class Counter extends WebComponent { static props = { count: 123, }; - onInit() { - this.onclick = () => ++this.props.count; - } get template() { - return ``; + return html``; } } diff --git a/examples/type-restore/Counter.mjs b/examples/type-restore/Counter.mjs index bcabbdf..3f167a8 100644 --- a/examples/type-restore/Counter.mjs +++ b/examples/type-restore/Counter.mjs @@ -1,20 +1,12 @@ // @ts-check -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; export class Counter extends WebComponent { static props = { count: 1, }; - onInit() { - let i = 1; - this.onclick = () => ++this.props.count; - let double = () => i * 2; - console.log(double()); - i = 3; - console.log(double()); - } get template() { - return ``; + return html``; } } diff --git a/examples/type-restore/HelloWorld.mjs b/examples/type-restore/HelloWorld.mjs index 23d6c40..ef0d2f3 100644 --- a/examples/type-restore/HelloWorld.mjs +++ b/examples/type-restore/HelloWorld.mjs @@ -1,14 +1,13 @@ -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; export class HelloWorld extends WebComponent { static props = { name: "a", }; - onInit() { - this.onclick = () => (this.props.name += "a"); - } + addA = () => (this.props.name += "a"); + get template() { - return ``; + return html``; } } diff --git a/examples/type-restore/Object.mjs b/examples/type-restore/Object.mjs index b05ecf6..7290c82 100644 --- a/examples/type-restore/Object.mjs +++ b/examples/type-restore/Object.mjs @@ -1,5 +1,8 @@ -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; +/** + * TODO: rendering currently wipes all children so focus gets removed on fields + */ export class ObjectText extends WebComponent { static props = { object: { @@ -11,38 +14,37 @@ export class ObjectText extends WebComponent { console.log(">>> object", this.props.object); } get template() { - const greeting = document.createElement("textarea"); - greeting.innerHTML = this.props.object.hello; - greeting.setAttribute("id", "greeting-field"); - const greetingLabel = document.createElement("label"); - greetingLabel.setAttribute("for", "greeting-field"); - greetingLabel.textContent = "Hello"; - greeting.onkeyup = () => { - this.props.object = { - ...this.props.object, - hello: greeting.value, - }; - }; - const ageField = document.createElement("input"); - ageField.value = this.props.object.age; - ageField.setAttribute("id", "age-field"); - const ageLabel = document.createElement("label"); - ageLabel.setAttribute("for", "age-field"); - ageLabel.textContent = "Age"; - ageField.onkeyup = () => { - this.props.object = { - ...this.props.object, - age: ageField.value, - }; - }; - const form = document.createElement("form"); - form.append(greetingLabel, greeting, ageLabel, ageField); - return form; + return html` +
+ + + + { + this.props.object = { + ...this.props.object, + age: event.target.value, + }; + } + } + id="age-field" value=${this.props.object.age} /> +
+ `; } - render() { - if (this.children.length === 0) this.replaceChildren(this.template); - } + } customElements.define("my-object", ObjectText); diff --git a/examples/type-restore/Toggle.mjs b/examples/type-restore/Toggle.mjs index 7beeaa3..a4ff160 100644 --- a/examples/type-restore/Toggle.mjs +++ b/examples/type-restore/Toggle.mjs @@ -1,18 +1,17 @@ // @ts-check -import { WebComponent } from "../../src/index.js"; +import { html, WebComponent } from "../../src/index.js"; export class Toggle extends WebComponent { static props = { toggle: false, }; - onInit() { - this.onclick = () => this.handleToggle(); - } handleToggle() { this.props.toggle = !this.props.toggle; } get template() { - return ``; + return html` + + `; } } diff --git a/examples/use-shadow/index.js b/examples/use-shadow/index.js index 5057c56..63e2a5e 100644 --- a/examples/use-shadow/index.js +++ b/examples/use-shadow/index.js @@ -6,7 +6,7 @@ export class Counter extends WebComponent { count: 123, }; static shadowRootInit = { - mode: "closed", + mode: "open", }; get template() {