-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight-dom-events.html
65 lines (61 loc) · 1.85 KB
/
light-dom-events.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
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: Light DOM Events</title>
<meta name="author" title="Eugene Kashida" href="mailto:[email protected]">
</head>
<body>
<script>
class MyParent extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
}
connectedCallback() {
this._shadowRoot.innerHTML = `
<div>
<my-child>
<button>slotted button</button>
</my-child>
<button>parent button</button>
</div>
`;
this._shadowRoot.querySelector('div').addEventListener('click', function (event) {
console.log('click handler on div');
});
this._shadowRoot.querySelector('my-child').addEventListener('click', function (event) {
console.log('click handler on my-child');
});
this._shadowRoot.addEventListener('click', function (event) {
console.log('click handler on parent shadow root');
});
}
}
class MyChild extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
}
connectedCallback() {
this._shadowRoot.innerHTML = `
<button>child button</button>
<slot></slot>
`;
this._shadowRoot.addEventListener('click', function (event) {
console.log('click handler on child shadow root');
});
this._shadowRoot.querySelector('slot').addEventListener('click', function (event) {
console.log('click handler on slot');
});
}
}
customElements.define('my-parent', MyParent);
customElements.define('my-child', MyChild);
var div = document.createElement('div');
div.innerHTML = `
<my-parent></my-parent>
`;
document.body.appendChild(div);
</script>
</body>
</html>