refactor: examples to use data-name prop

This commit is contained in:
Ayo 2023-10-20 11:23:57 +02:00
parent be8bdf7bc8
commit 9b6043b219
4 changed files with 8 additions and 8 deletions

View file

@ -1,15 +1,15 @@
class HelloWorld extends WebComponent { class HelloWorld extends WebComponent {
static properties = ["name"]; static properties = ["data-name"];
onInit() { onInit() {
let count = 0; let count = 0;
this.onclick = () => { this.onclick = () => {
this.setAttribute("name", `Clicked ${++count}x`); this.setAttribute("data-name", `Clicked ${++count}x`);
}; };
this.setAttribute("title", "Click me please"); this.setAttribute("title", "Click me please");
} }
get template() { get template() {
return `<button style="cursor:pointer">Hello ${this.name}!</button>`; return `<button style="cursor:pointer">Hello ${this["data-name"]}!</button>`;
} }
} }

View file

@ -1,17 +1,17 @@
class HelloWorld extends HTMLElement { class HelloWorld extends HTMLElement {
static get observedAttributes() { static get observedAttributes() {
return ["name"]; return ["data-name"];
} }
connectedCallback() { connectedCallback() {
let count = 0; let count = 0;
this.onclick = () => { this.onclick = () => {
this.setAttribute("name", `Clicked ${++count}x`); this.setAttribute("data-name", `Clicked ${++count}x`);
}; };
} }
attributeChangedCallback(property, previousValue, currentValue) { attributeChangedCallback(property, previousValue, currentValue) {
if (previousValue !== currentValue) { if (property === "data-name" && previousValue !== currentValue) {
this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`; this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`;
} }
} }

View file

@ -46,7 +46,7 @@
<h2>Demo Page</h2> <h2>Demo Page</h2>
<a href="/demo/about">{{ count }}</a> <a href="/demo/about">{{ count }}</a>
<div> <div>
<my-hello-world name="{{name }}"></my-hello-world> <my-hello-world data-name="{{name }}"></my-hello-world>
</div> </div>
<code-block language="js">const name = "Nitro"</code-block> <code-block language="js">const name = "Nitro"</code-block>
<p>some text: {{greeting}}</p> <p>some text: {{greeting}}</p>

View file

@ -31,7 +31,7 @@
</p> </p>
<p> <p>
Here's a sample interactive custom element: Here's a sample interactive custom element:
<vanilla-hello-world name="McFly"></vanilla-hello-world> <vanilla-hello-world data-name="McFly"></vanilla-hello-world>
</p> </p>
<p> <p>
Start at the very basic of writing HTML files and enhance with Start at the very basic of writing HTML files and enhance with