feat(site, templates): use HTMLElement.dataset 🤗

This commit is contained in:
Ayo 2023-11-17 19:15:56 +01:00
parent 9ebd6e7f3b
commit b4c7bab3df
4 changed files with 26 additions and 21 deletions

View file

@ -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) {

View file

@ -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

View file

@ -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) {
@ -15,4 +18,4 @@ class HelloWorld extends HTMLElement {
this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`; this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`;
} }
} }
} }

View file

@ -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 {