feat(site, templates): use HTMLElement.dataset 🤗
This commit is contained in:
parent
9ebd6e7f3b
commit
b4c7bab3df
4 changed files with 26 additions and 21 deletions
|
@ -5,9 +5,12 @@ class HelloWorld extends HTMLElement {
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
this.onclick = () => {
|
|
||||||
this.setAttribute("data-name", `Clicked ${++count}x`);
|
if (!('name' in this.dataset)) {
|
||||||
};
|
this.dataset.name = 'World'
|
||||||
|
}
|
||||||
|
|
||||||
|
this.onclick = () => this.dataset.name = `Clicked ${++count}x`;
|
||||||
}
|
}
|
||||||
|
|
||||||
attributeChangedCallback(property, previousValue, currentValue) {
|
attributeChangedCallback(property, previousValue, currentValue) {
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
<code-block>npm create <strong style="color: var(--color-blue)">mcfly</strong>@latest</code-block>
|
<code-block>npm create <strong style="color: var(--color-blue)">mcfly</strong>@latest</code-block>
|
||||||
<p>
|
<p>
|
||||||
Here's a sample interactive custom element:
|
Here's a sample interactive custom element:
|
||||||
<vanilla-hello-world data-name="McFly"></vanilla-hello-world>
|
<vanilla-hello-world></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
|
||||||
|
|
|
@ -1,18 +1,21 @@
|
||||||
class HelloWorld extends HTMLElement {
|
class HelloWorld extends HTMLElement {
|
||||||
static get observedAttributes() {
|
static get observedAttributes() {
|
||||||
return ["data-name"];
|
return ["data-name"];
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
if (!('name' in this.dataset)) {
|
||||||
|
this.dataset.name = 'World'
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
this.onclick = () => this.dataset.name = `Clicked ${++count}x`;
|
||||||
let count = 0;
|
}
|
||||||
this.onclick = () => {
|
|
||||||
this.setAttribute("data-name", `Clicked ${++count}x`);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
attributeChangedCallback(property, previousValue, currentValue) {
|
attributeChangedCallback(property, previousValue, currentValue) {
|
||||||
if (property === "data-name" && 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>`;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -25,8 +25,7 @@
|
||||||
<main>
|
<main>
|
||||||
<h2>Welcome to {{ project.name }}</h2>
|
<h2>Welcome to {{ project.name }}</h2>
|
||||||
<p>
|
<p>
|
||||||
Here's a vanilla custom element:
|
Here's a vanilla custom element: <vanilla-hello-world/>
|
||||||
<vanilla-hello-world data-name="{{project.name}}" />
|
|
||||||
</p>
|
</p>
|
||||||
<code-block language="js">
|
<code-block language="js">
|
||||||
class HelloWorld extends HTMLElement {
|
class HelloWorld extends HTMLElement {
|
||||||
|
|
Loading…
Reference in a new issue