-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCustomElement.html
127 lines (97 loc) · 3.43 KB
/
CustomElement.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
// convenience functions
// get a random integer value between min and max with (min < max)
const random = (min,max) => Math.floor(Math.random()*(max-min+1)+min);
// Create a class for the element
class Square extends HTMLElement {
// Specify observed attributes so that
// attributeChangedCallback will work
static get observedAttributes() {return ['extend','fill-color']; }
constructor() {
// Always call super first in constructor
super();
const shadow = this.attachShadow({mode: 'open'});
const styleElement = document.createElement('style');
this.shadowStyle = styleElement;
shadow.appendChild(styleElement);
const shadowDiv = document.createElement('div');
shadow.appendChild(shadowDiv);
}
connectedCallback() {
console.log('Custom square element added.');
this.updateStyle();
}
disconnectedCallback() {
console.log('Custom square element removed.');
}
adoptedCallback() {
console.log('Custom square element moved to new page.');
}
attributeChangedCallback(name, oldValue, newValue) {
console.log('Custom square element attributes changed.');
this.updateStyle();
}
// updating the style for changes of "extend" or "fill-color" attributes
updateStyle() {
this.shadowStyle.textContent =
'div {' +
' width: ' + this.getAttribute('extend') + 'px;' +
' height: ' + this.getAttribute('extend') + 'px;' +
' background-color: ' + this.getAttribute('fill-color') + ';' +
' }';
}
}
customElements.define('custom-square', Square);
</script>
<p>Use of web component as html element </p>
<script>
function onCustomUpdate(elem) {
elem.setAttribute('extend',random(50,200));
elem.setAttribute('fill-color','rgb(' + random(0,255) + ',' + random(0,255) + ',' + random(0,255) + ')');
}
</script>
<custom-square extend="100" fill-color="black" onclick="onCustomUpdate(this)">
</custom-square>
<p>Use of web component when added dynamically</p>
<div class="add" >add</div>
<div class="update">update</div>
<div class="remove">remove</div>
<script>
const add = document.querySelector('.add');
const update = document.querySelector('.update');
const remove = document.querySelector('.remove');
let square = null;
update.disabled = true;
remove.disabled = true;
add.onclick = function() {
// Create a custom square element
square = document.createElement('custom-square');
square.setAttribute('extend','100');
square.setAttribute('fill-color','red');
document.body.appendChild(square);
update.disabled = false;
remove.disabled = false;
add.disabled = true;
};
update.onclick = function() {
// Randomly update square's attributes
square.setAttribute('extend',random(50,200));
square.setAttribute('fill-color','rgb(' + random(0,255) + ',' + random(0,255) + ',' + random(0,255) + ')');
};
remove.onclick = function() {
// Remove the square
document.body.removeChild(square);
update.disabled = true;
remove.disabled = true;
add.disabled = false;
};
</script>
</body>
</html>