class CustomButton extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.render();
}
toggleTheme() {
const html = document.documentElement;
html.classList.toggle('dark');
// Save preference to localStorage
const isDark = html.classList.contains('dark');
localStorage.setItem('theme', isDark ? 'dark' : 'light');
// Add pulse animation
const button = this.shadowRoot.querySelector('button');
button.classList.add('animate-pulse-once');
setTimeout(() => {
button.classList.remove('animate-pulse-once');
}, 500);
// Update icon
this.updateIcon();
}
updateIcon() {
const icon = this.shadowRoot.querySelector('i');
const isDark = document.documentElement.classList.contains('dark');
icon.setAttribute('data-feather', isDark ? 'sun' : 'moon');
feather.replace();
}
render() {
const isDark = document.documentElement.classList.contains('dark');
this.shadowRoot.innerHTML = `
`;
const button = this.shadowRoot.querySelector('button');
button.addEventListener('click', () => this.toggleTheme());
}
}
customElements.define('custom-button', CustomButton);