class ThemeToggle extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = `
`; this.button = this.shadowRoot.querySelector('.toggle-btn'); this.sunIcon = this.shadowRoot.querySelector('#sun-icon'); this.moonIcon = this.shadowRoot.querySelector('#moon-icon'); this.button.addEventListener('click', () => this.toggleTheme()); this.initTheme(); } initTheme() { const savedTheme = localStorage.getItem('theme') || 'light'; document.documentElement.setAttribute('data-theme', savedTheme); this.updateUI(savedTheme); } toggleTheme() { const currentTheme = document.documentElement.getAttribute('data-theme'); const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; document.documentElement.setAttribute('data-theme', newTheme); localStorage.setItem('theme', newTheme); this.updateUI(newTheme); } updateUI(theme) { if (theme === 'dark') { this.sunIcon.style.display = 'none'; this.moonIcon.style.display = 'block'; } else { this.sunIcon.style.display = 'block'; this.moonIcon.style.display = 'none'; } } } customElements.define('custom-theme-toggle', ThemeToggle);