refactor(site): different prop access examples

- HTMLElement.dataset for vanilla
- WebComponent camelCase counterpart for custom base
This commit is contained in:
Ayo 2023-11-17 20:53:41 +01:00
parent ac1e839ca4
commit 5d4702b1a6
3 changed files with 9 additions and 5 deletions

View file

@ -3,15 +3,16 @@
* @see https://ayco.io/n/web-component-base * @see https://ayco.io/n/web-component-base
*/ */
class HelloWorld extends WebComponent { class HelloWorld extends WebComponent {
dataName = 'World';
static properties = ["data-name"]; static properties = ["data-name"];
onInit() { onInit() {
let count = 0; let count = 0;
this.dataset.name = this.dataset.name ?? 'World';
this.onclick = () => this.dataset.name = `Clicked ${++count}x`; this.onclick = () => this.dataset.name = `Clicked ${++count}x`;
} }
get template() { get template() {
return `<button style="cursor:pointer">Hello ${this.dataset.name}!</button>`; return `<button style="cursor:pointer">Hello ${this.dataName}!</button>`;
} }
} }

View file

@ -5,7 +5,11 @@ class HelloWorld extends HTMLElement {
connectedCallback() { connectedCallback() {
let count = 0; let count = 0;
this.dataset.name = this.dataset.name ?? 'World';
if (!('name' in this.dataset)) {
this.dataset.name = 'World'
}
this.onclick = () => this.dataset.name = `Clicked ${++count}x`; this.onclick = () => this.dataset.name = `Clicked ${++count}x`;
} }

View file

@ -33,8 +33,7 @@
</p> </p>
<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 />
<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