-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincrementDecrement.html
More file actions
87 lines (73 loc) · 1.94 KB
/
incrementDecrement.html
File metadata and controls
87 lines (73 loc) · 1.94 KB
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
83
84
85
86
87
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Increment/Decrement web component</title>
<script type="module" src="../src/polyfill.js"></script>
<template id="template">
<button id="decrement">-</button>
<span id="value"></span>
<button id="increment">+</button>
</template>
<script type="module">
class IncrementDecrement extends HTMLElement {
constructor() {
super();
this.state = {
max: 5,
min: -5,
value: 0
};
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
const instance = document.importNode(template.content, true);
instance.querySelector('#decrement').addEventListener('click', () => {
this.value--;
});
instance.querySelector('#increment').addEventListener('click', () => {
this.value++;
});
this.shadowRoot.appendChild(instance);
this.render();
}
render() {
const value = this.state.value;
this.shadowRoot.applyPropertiesById({
decrement: {
attributes: {
disabled: value <= this.state.min
}
},
increment: {
attributes: {
disabled: value >= this.state.max
}
},
value: {
style: {
color: value < 0 ? 'red' : null
},
textContent: value
}
});
}
setState(changes) {
Object.assign(this.state, changes);
this.render();
}
get value() {
return this.state.value;
}
set value(value) {
this.setState({ value });
}
}
customElements.define('increment-decrement', IncrementDecrement);
</script>
</head>
<body>
<increment-decrement></increment-decrement>
</body>
</html>