-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-component.html
59 lines (51 loc) · 1.28 KB
/
new-component.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
49
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>FMI component</title>
</head>
<body>
<fmi-counter count="0"></fmi-counter>
<script type="module" >
class FmiCounter extends HTMLElement {
constructor() {
super();
this.shadow = this.attachShadow({ mode:"open" });
}
static get observedAttributes() {
return ["count"];
}
get count() {
return this.getAttribute("count");
}
set count(val) {
this.setAttribute("count", val);
}
attributeChangedCallback(prop, oldVal, newVal) {
if (prop === "count") {
this.render();
const button = this.shadow.querySelector('#btn');
button.addEventListener("click", this.incrementCounter.bind(this));
}
}
connectedCallback() {
this.render();
const button = this.shadow.querySelector('#btn');
button.addEventListener("click", this.incrementCounter.bind(this));
}
incrementCounter() {
this.count++;
}
render() {
this.shadow.innerHTML = `
<h1> FMI Counter </h1>
${this.count}
<button id='btn'> Add </button>
`;
}
}
customElements.define('fmi-counter', FmiCounter);
</script>
</body>
</html>