class LanguageSwitcher extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
${this.getAttribute('current').toUpperCase()}
`;
const toggleBtn = this.shadowRoot.getElementById('toggleBtn');
toggleBtn.addEventListener('click', () => {
this.toggleAttribute('open');
});
const options = this.shadowRoot.querySelectorAll('.language-option');
options.forEach(option => {
option.addEventListener('click', () => {
const lang = option.getAttribute('data-lang');
this.setAttribute('current', lang);
this.removeAttribute('open');
this.dispatchEvent(new CustomEvent('language-change', {
detail: { language: lang },
bubbles: true,
composed: true
}));
});
});
}
}
customElements.define('language-switcher', LanguageSwitcher);