-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc-button.js
More file actions
39 lines (36 loc) · 1.34 KB
/
c-button.js
File metadata and controls
39 lines (36 loc) · 1.34 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
class cButton extends HTMLElement {
constructor() {
super();
// Detect whether we have SSR content already:
if (this.shadowRoot) {
// A Declarative Shadow Root exists!
// wire up event listeners, references, etc.:
const button = this.shadowRoot.querySelector('button');
button.addEventListener('click', this._handleClick.bind(this));
} else {
// A Declarative Shadow Root doesn't exist.
// Create a new shadow root and populate it:
const shadow = this.attachShadow({ mode: 'open' });
shadow.innerHTML = `<button><slot></slot></button>`;
shadow.firstChild.addEventListener('click', this._handleClick.bind(this));
}
this.handleAnalytics = this._sendAnalytics.bind(this);
}
_handleClick() {
console.log('clicked');
this._sendAnalytics('toggle');
}
_sendAnalytics(message) {
setTimeout(() => {
console.log('Sending analytics data...', message);
}, 500);
}
connectedCallback() {
console.log('Custom c-button added to page.');
}
disconnectedCallback() {
window.removeEventListener('click', this._handleClick);
console.log('Custom c-button removed from page.');
}
}
customElements.define('c-button', cButton);