Spaces:
Running
Running
File size: 1,164 Bytes
2ad4002 |
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 |
class ThemeToggle extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
button {
background: transparent;
border: none;
cursor: pointer;
color: inherit;
display: flex;
align-items: center;
gap: 0.5rem;
}
</style>
<button>
<i data-feather="moon"></i>
<span>Dark Mode</span>
</button>
`;
this.shadowRoot.querySelector('button').addEventListener('click', () => {
document.documentElement.classList.toggle('light-mode');
this.updateIcon();
});
this.updateIcon();
}
updateIcon() {
const icon = this.shadowRoot.querySelector('i');
const span = this.shadowRoot.querySelector('span');
if (document.documentElement.classList.contains('light-mode')) {
icon.setAttribute('data-feather', 'sun');
span.textContent = 'Light Mode';
} else {
icon.setAttribute('data-feather', 'moon');
span.textContent = 'Dark Mode';
}
feather.replace();
}
}
customElements.define('theme-toggle', ThemeToggle); |