-
Notifications
You must be signed in to change notification settings - Fork 0
/
component-lifecycle.html
78 lines (74 loc) · 1.85 KB
/
component-lifecycle.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
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: Slot Assigned Nodes</title>
<meta name="author" title="Eugene Kashida" href="mailto:[email protected]">
</head>
<body>
<script>
class MySlotted extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
this._shadowRoot.innerHTML = `
<p>slotted</p>
`;
}
connectedCallback() {
console.log('slotted connectedCallback');
}
disconnectedCallback() {
console.log('slotted disconnectedCallback');
}
}
class MyParent extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
this._shadowRoot.innerHTML = `
<div>
<my-child>
<slot></slot>
</my-child>
</div>
`;
}
connectedCallback() {
console.log('parent connectedCallback');
}
disconnectedCallback() {
console.log('parent disconnectedCallback');
}
}
class MyChild extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
this._shadowRoot.innerHTML = `
<div>
<button>foo</button>
<slot></slot>
</div>
`;
}
connectedCallback() {
console.log('child connectedCallback');
}
disconnectedCallback() {
console.log('child disconnectedCallback');
}
}
customElements.define('my-slotted', MySlotted);
customElements.define('my-parent', MyParent);
customElements.define('my-child', MyChild);
var div = document.createElement('div');
div.innerHTML = `
<my-parent>
<my-slotted></my-slotted>
</my-parent>
`;
document.body.appendChild(div);
document.body.removeChild(div);
</script>
</body>
</html>