-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx-counter-1.html
48 lines (39 loc) · 935 Bytes
/
x-counter-1.html
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<template id="tpl">
<style>
span {
color: green;
font-weight: bold;
}
</style>
<span> click me! </span>
</template>
<script>
const owner = document.currentScript.ownerDocument;
const tpl = owner.querySelector('#tpl');
class XCounter extends HTMLElement {
constructor() {
super();
this.count = 0;
this.shadow = this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.shadow.appendChild(tpl.content.cloneNode(true));
this.addEventListener('click', this.increment.bind(this));
this.render();
}
increment() {
this.count += 1;
this.render();
}
attributeChangedCallback(name, oldValue, newValue) {
if (attr === 'count') {
this.render();
}
}
render() {
this.shadow.querySelector('span').textContent = this.count;
}
}
customElements.define('x-counter-1', XCounter);
</script>
<x-counter-1 count="10">Hello</x-counter-1>