vanilla example

This commit is contained in:
Ayo 2023-10-19 16:09:11 +02:00
parent 9225fa662e
commit 8a66e3b9ad
3 changed files with 39 additions and 16 deletions

View file

@ -1,7 +1,7 @@
class HelloWorld extends WebComponent { class HelloWorld extends HTMLElement {
static properties = ["name"]; static observedAttributes = ["name"];
onInit() { connectedCallback() {
console.log(`Greeting for ${this.name} initialized`); console.log(`Greeting for ${this.name} initialized`);
let count = 0; let count = 0;
this.onclick = () => { this.onclick = () => {
@ -11,11 +11,10 @@ class HelloWorld extends WebComponent {
this.setAttribute("title", "Click me please"); this.setAttribute("title", "Click me please");
} }
onChanges(changes) { attributeChangedCallback(property, previousValue, currentValue) {
console.log(changes); if (previousValue !== currentValue) {
} this[property] = currentValue;
this.innerHTML = `<button style="cursor:pointer">Hello ${this.name}!</button>`;
get template() { }
return `<button style="cursor:pointer">Hello ${this.name}!</button>`;
} }
} }

View file

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

View file

@ -14,21 +14,24 @@
</p> </p>
<code-block class="language-js line-numbers" data-line="2"> <code-block class="language-js line-numbers" data-line="2">
<pre> <pre>
class HelloWorld extends WebComponent { class HelloWorld extends HTMLElement {
static properties = ["name"]; static observedAttributes = ["name"];
onInit() { connectedCallback() {
let count = 0; let count = 0;
this.onclick = () => { this.onclick = () => {
this.setAttribute("name", `Clicked ${++count}x`); this.setAttribute("name", `Clicked ${++count}x`);
}; };
this.setAttribute("title", "Click me please");
} }
get template() { attributeChangedCallback(property, previousValue, currentValue) {
return `<button style="cursor:pointer">Hello ${this.name}!</button>`; if (previousValue !== currentValue) {
this[property] = currentValue;
this.innerHTML = `<button style="cursor:pointer">Hello ${this.name}!</button>`;
}
} }
} }</pre>
</pre>
</code-block> </code-block>
<p> <p>
Start at the very basic of writing HTML files and enhance with standard Start at the very basic of writing HTML files and enhance with standard