feat: update all examples to use latest features
This commit is contained in:
parent
eb115d25ea
commit
aa59785806
13 changed files with 68 additions and 98 deletions
|
@ -1,11 +1,11 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
import { WebComponent, attachEffect } from "../../src/index.js";
|
import { WebComponent, attachEffect, html } from "../../src/index.js";
|
||||||
export class Counter extends WebComponent {
|
export class Counter extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
count: 0,
|
count: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
onInit() {
|
onInit() {
|
||||||
this.onclick = () => ++this.props.count;
|
|
||||||
attachEffect(this.props.count, (count) => console.log(count));
|
attachEffect(this.props.count, (count) => console.log(count));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ export class Counter extends WebComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<button id="btn">${this.props.count}</button>`;
|
return html`<button onclick=${() => ++this.props.count} id="btn">${this.props.count}</button>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
import { WebComponent, attachEffect } from "../../src/index.js";
|
import { WebComponent, attachEffect, html } from "../../src/index.js";
|
||||||
|
|
||||||
export class Decrease extends WebComponent {
|
export class Decrease extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
|
@ -7,7 +7,6 @@ export class Decrease extends WebComponent {
|
||||||
};
|
};
|
||||||
|
|
||||||
onInit() {
|
onInit() {
|
||||||
this.onclick = () => --this.props.count;
|
|
||||||
attachEffect(this.props.count, (count) => console.log(count));
|
attachEffect(this.props.count, (count) => console.log(count));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +15,7 @@ export class Decrease extends WebComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<button id="btn">${this.props.count}</button>`;
|
return html`<button onclick=${() => --this.props.count} id="btn">${this.props.count}</button>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { WebComponent } from "../../src/index.js";
|
import { html, WebComponent } from "../../src/index.js";
|
||||||
|
|
||||||
export class BooleanPropTest extends WebComponent {
|
export class BooleanPropTest extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
|
@ -7,7 +7,9 @@ export class BooleanPropTest extends WebComponent {
|
||||||
};
|
};
|
||||||
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<p>is-inline: ${this.props.isInline}</p><p>another-one: ${this.props.anotherone}</p>`;
|
return html`
|
||||||
|
<p>is-inline: ${this.props.isInline}</p><p>another-one: ${this.props.anotherone}</p>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
import { WebComponent } from "../../src/index.js";
|
import { html, WebComponent } from "../../src/index.js";
|
||||||
|
|
||||||
export class HelloWorld extends WebComponent {
|
export class HelloWorld extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
|
@ -9,32 +9,15 @@ export class HelloWorld extends WebComponent {
|
||||||
|
|
||||||
onInit() {
|
onInit() {
|
||||||
this.props.count = 0;
|
this.props.count = 0;
|
||||||
this.onclick = () => ++this.props.count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get template() {
|
get template() {
|
||||||
const label = this.props.count ? `Clicked ${this.props.count}` : "World";
|
const label = this.props.count ? `Clicked ${this.props.count}` : "World";
|
||||||
const emote = this.props.emotion === "sad" ? ". 😭" : "! 🙌";
|
const emote = this.props.emotion === "sad" ? ". 😭" : "! 🙌";
|
||||||
|
|
||||||
const button = document.createElement("button");
|
return html`
|
||||||
button.innerText = `Hello ${label}${emote}`;
|
<button onclick=${() => ++this.props.count}>Hello ${label}${emote}</button>
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
import { WebComponent } from "../../src/index.js";
|
import { html, WebComponent } from "../../src/index.js";
|
||||||
|
|
||||||
class SimpleText extends WebComponent {
|
class SimpleText extends WebComponent {
|
||||||
clickCallback() {
|
clickCallback() {
|
||||||
console.log(">>> click!");
|
console.log(">>> click!");
|
||||||
}
|
}
|
||||||
onInit() {
|
|
||||||
this.onclick = this.clickCallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
onDestroy() {
|
onDestroy() {
|
||||||
console.log(">>> removing event listener");
|
console.log(">>> removing event listener");
|
||||||
|
@ -15,7 +12,7 @@ class SimpleText extends WebComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<span style="cursor:pointer">Click me!</span>`;
|
return html`<span onclick=${this.clickCallback} style="cursor:pointer">Click me!</span>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
import {
|
import {
|
||||||
WebComponent,
|
WebComponent,
|
||||||
html,
|
html,
|
||||||
} from "https://unpkg.com/web-component-base@latest/index.js";
|
} from "https://esm.sh/web-component-base@latest";
|
||||||
|
|
||||||
export class Counter extends WebComponent {
|
export class Counter extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
|
@ -30,11 +30,11 @@
|
||||||
static props = {
|
static props = {
|
||||||
toggle: false,
|
toggle: false,
|
||||||
};
|
};
|
||||||
onInit() {
|
|
||||||
this.onclick = () => (this.props.toggle = !this.props.toggle);
|
clickFn = () => (this.props.toggle = !this.props.toggle);
|
||||||
}
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<button>${this.props.toggle ? "On" : "Off"}</button>`;
|
return html`<button onclick=${this.clickFn}>${this.props.toggle ? "On" : "Off"}</button>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import { WebComponent } from "../../src/index.js";
|
import { html, WebComponent } from "../../src/index.js";
|
||||||
|
|
||||||
export class HelloWorld extends WebComponent {
|
export class HelloWorld extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
myName: "World",
|
myName: "World",
|
||||||
};
|
};
|
||||||
get template() {
|
get template() {
|
||||||
return `<p>Hello ${this.props.myName}</p>`;
|
return html`<p>Hello ${this.props.myName}</p>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
import { WebComponent } from "../../src/index.js";
|
import { html, WebComponent } from "../../src/index.js";
|
||||||
|
|
||||||
export class Counter extends WebComponent {
|
export class Counter extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
count: 123,
|
count: 123,
|
||||||
};
|
};
|
||||||
onInit() {
|
|
||||||
this.onclick = () => ++this.props.count;
|
|
||||||
}
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<button id="btn">${this.props.count}</button>`;
|
return html`<button onclick=${() => ++this.props.count} id="btn">${this.props.count}</button>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,12 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
import { WebComponent } from "../../src/index.js";
|
import { html, WebComponent } from "../../src/index.js";
|
||||||
|
|
||||||
export class Counter extends WebComponent {
|
export class Counter extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
count: 1,
|
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() {
|
get template() {
|
||||||
return `<button>${this.props.count}</button>`;
|
return html`<button onclick=${() => ++this.props.count}>${this.props.count}</button>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
import { WebComponent } from "../../src/index.js";
|
import { html, WebComponent } from "../../src/index.js";
|
||||||
|
|
||||||
export class HelloWorld extends WebComponent {
|
export class HelloWorld extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
name: "a",
|
name: "a",
|
||||||
};
|
};
|
||||||
onInit() {
|
addA = () => (this.props.name += "a");
|
||||||
this.onclick = () => (this.props.name += "a");
|
|
||||||
}
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<button>W${this.props.name}h!</button>`;
|
return html`<button onclick=${this.addA}>W${this.props.name}h!</button>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
export class ObjectText extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
object: {
|
object: {
|
||||||
|
@ -11,38 +14,37 @@ export class ObjectText extends WebComponent {
|
||||||
console.log(">>> object", this.props.object);
|
console.log(">>> object", this.props.object);
|
||||||
}
|
}
|
||||||
get template() {
|
get template() {
|
||||||
const greeting = document.createElement("textarea");
|
return html`
|
||||||
greeting.innerHTML = this.props.object.hello;
|
<form>
|
||||||
greeting.setAttribute("id", "greeting-field");
|
<label for="greeting-field">Hello</label>
|
||||||
const greetingLabel = document.createElement("label");
|
<textarea
|
||||||
greetingLabel.setAttribute("for", "greeting-field");
|
onkeyup=${
|
||||||
greetingLabel.textContent = "Hello";
|
(event) => {
|
||||||
greeting.onkeyup = () => {
|
|
||||||
this.props.object = {
|
this.props.object = {
|
||||||
...this.props.object,
|
...this.props.object,
|
||||||
hello: greeting.value,
|
hello: event.target.value,
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
const ageField = document.createElement("input");
|
}
|
||||||
ageField.value = this.props.object.age;
|
id="greeting-field">
|
||||||
ageField.setAttribute("id", "age-field");
|
${this.props.object.hello}
|
||||||
const ageLabel = document.createElement("label");
|
</textarea>
|
||||||
ageLabel.setAttribute("for", "age-field");
|
<label for="age-field">Age</label>
|
||||||
ageLabel.textContent = "Age";
|
<input
|
||||||
ageField.onkeyup = () => {
|
onkeyup=${
|
||||||
|
(event) => {
|
||||||
this.props.object = {
|
this.props.object = {
|
||||||
...this.props.object,
|
...this.props.object,
|
||||||
age: ageField.value,
|
age: event.target.value,
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
const form = document.createElement("form");
|
}
|
||||||
form.append(greetingLabel, greeting, ageLabel, ageField);
|
id="age-field" value=${this.props.object.age} />
|
||||||
return form;
|
</form>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
|
||||||
if (this.children.length === 0) this.replaceChildren(this.template);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
customElements.define("my-object", ObjectText);
|
customElements.define("my-object", ObjectText);
|
||||||
|
|
|
@ -1,18 +1,17 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
import { WebComponent } from "../../src/index.js";
|
import { html, WebComponent } from "../../src/index.js";
|
||||||
|
|
||||||
export class Toggle extends WebComponent {
|
export class Toggle extends WebComponent {
|
||||||
static props = {
|
static props = {
|
||||||
toggle: false,
|
toggle: false,
|
||||||
};
|
};
|
||||||
onInit() {
|
|
||||||
this.onclick = () => this.handleToggle();
|
|
||||||
}
|
|
||||||
handleToggle() {
|
handleToggle() {
|
||||||
this.props.toggle = !this.props.toggle;
|
this.props.toggle = !this.props.toggle;
|
||||||
}
|
}
|
||||||
get template() {
|
get template() {
|
||||||
return `<button id="toggle">${this.props.toggle ? "On" : "Off"}</button>`;
|
return html`
|
||||||
|
<button onclick=${() => this.handleToggle()} id="toggle">${this.props.toggle ? "On" : "Off"}</button>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ export class Counter extends WebComponent {
|
||||||
count: 123,
|
count: 123,
|
||||||
};
|
};
|
||||||
static shadowRootInit = {
|
static shadowRootInit = {
|
||||||
mode: "closed",
|
mode: "open",
|
||||||
};
|
};
|
||||||
|
|
||||||
get template() {
|
get template() {
|
||||||
|
|
Loading…
Reference in a new issue