forked from homa31/CSE134B-HW4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonCount.js
34 lines (27 loc) · 975 Bytes
/
ButtonCount.js
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
class ButtonCount extends HTMLElement {
constructor() {
super();
// Create a shadow root
this.attachShadow({ mode: 'open' });
let count = 0;
const button = document.createElement('button');
button.textContent = `Times Clicked: ${count}`;
button.addEventListener('click', () => {
count++;
button.textContent = `Times Clicked: ${count}`;
this.shadowRoot.appendChild(button);
});
// Append the button to the shadow root
this.shadowRoot.appendChild(button);
}
// Render the count value inside the shadow DOM
render(count) {
const countElement = document.createElement('p');
countElement.textContent = `You clicked me ${count} times!`;
// Clear the shadow root before appending the new count element
this.shadowRoot.innerHTML = '';
this.shadowRoot.appendChild(countElement);
}
}
// Define the element in the custom elements registry
customElements.define('button-count', ButtonCount);