Sascha Schulz
2024-02-05 904d9655997b1cb46bb8044843465c8045092af8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class HelloSayerComponent extends HTMLElement {
    static observedAttributes = ["name"];
 
    constructor() {
        super();
 
        this.root = this.attachShadow({mode: "open"});
 
        this.root.innerHTML = `
            <style>
                div {
                    background-color: coral;
                    color: white;
                }
            </style>
            <div>Hello!</div>
        `;
    }
 
    attributeChangedCallback(name, oldValue, newValue) {
        this.root.querySelector("div").innerHTML = `Hello ${newValue}!`;
    }
}
 
window.customElements.define("hello-sayer", HelloSayerComponent);
 
const names = ["Joe", "Allie"];
let counter = 0;
 
setInterval(() => document.querySelector("hello-sayer").setAttribute("name", names[counter++ % 2]), 2000);