-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
82 lines (69 loc) · 2.03 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<head>
<script type="module">
import { HydrateElement } from "./hydrate.js";
// example from https://webcomponents.dev/edit/i72WfCCVWQQpeXD2IDDI
const template = document.createElement("template");
template.innerHTML = /*html*/`
<style>
* {
font-size: 200%;
}
span {
width: 4rem;
display: inline-block;
text-align: center;
}
button {
width: 4rem;
height: 4rem;
border: none;
border-radius: 10px;
background-color: seagreen;
color: white;
}
</style>
<button id="dec">-</button>
<span id="count"></span>
<button id="inc">+</button>
`;
class MyCounter extends HydrateElement {
constructor() {
super();
this.count = 0;
this.attachShadow({ mode: "open" }); // TODO only works when mode is "open"
}
async connectedCallback() {
await this.rehydrate(this, async () => {
this.count = parseInt(this.querySelector('#count').textContent, 10);
});
this.shadowRoot.appendChild(template.content.cloneNode(true));
this.shadowRoot.getElementById("inc").onclick = () => this.inc();
this.shadowRoot.getElementById("dec").onclick = () => this.dec();
this.update();
}
inc() {
this.update(++this.count);
}
dec() {
this.update(--this.count);
}
update(count) {
this.shadowRoot.getElementById("count").innerHTML = count || this.count;
}
}
customElements.define("my-counter", MyCounter);
</script>
</head>
<body>
<h1>Counter (empty)</h1>
<my-counter></my-counter>
<h1>Counter (hydrated)</h1>
<my-counter>
<button id="dec">-</button>
<span id="count">33</span>
<button id="inc">+</button>
</my-counter>
</body>
</html>