-
Notifications
You must be signed in to change notification settings - Fork 0
/
listening-for-events.html
54 lines (50 loc) · 1.51 KB
/
listening-for-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
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: Listening for events</title>
<meta name="author" title="Eugene Kashida" href="mailto:[email protected]">
</head>
<body>
<script>
class MyCustomElement extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
[
'dispatched-on-custom-element-from-inside',
'dispatched-on-custom-element-from-outside',
'dispatched-on-shadow-root-composed',
'dispatched-on-shadow-root-not-composed',
].forEach(eventName => {
this.addEventListener(eventName, () => console.log(`${eventName}`));
});
}
connectedCallback() {
this._shadowRoot.innerHTML = `
<div>
`;
this._shadowRoot.dispatchEvent(
new CustomEvent('dispatched-on-shadow-root-composed', {
bubbles: true,
composed: true,
})
);
this._shadowRoot.dispatchEvent(
new CustomEvent('dispatched-on-shadow-root-not-composed', {
bubbles: true,
})
);
this.dispatchEvent(
new CustomEvent('dispatched-on-custom-element-from-inside')
);
}
}
customElements.define('my-custom-element', MyCustomElement);
var instance = document.createElement('my-custom-element');
document.body.appendChild(instance);
instance.dispatchEvent(
new CustomEvent('dispatched-on-custom-element-from-outside')
);
</script>
</body>
</html>