(() => { const picker = document.querySelector('[data-theme-picker]'); if (!picker) return; const select = picker.querySelector('[data-theme-select]'); const status = picker.querySelector('[data-theme-status]'); const themeLink = [...document.querySelectorAll('link[rel="stylesheet"]')] .find(link => link.getAttribute('href') === picker.dataset.currentCss); if (!themeLink) { picker.hidden = true; return; } let current = picker.dataset.currentId; select.addEventListener('change', () => { const option = select.selectedOptions[0]; const candidate = document.createElement('link'); candidate.rel = 'stylesheet'; candidate.href = option.dataset.css; select.disabled = true; status.textContent = ''; // Load before replacing, so failure cannot leave an unstyled page. Keeping // the link's position preserves editor/component overrides and avoids reload. candidate.media = 'not all'; candidate.onload = () => { themeLink.href = candidate.href; candidate.remove(); current = option.value; const secure = location.protocol === 'https:' ? '; Secure' : ''; document.cookie = 'pageswiki_theme=' + encodeURIComponent(current) + '; Path=/; SameSite=Lax; Max-Age=' + (current ? 31536000 : 0) + secure; select.disabled = false; document.dispatchEvent(new Event('pageswiki:theme')); }; candidate.onerror = () => { candidate.remove(); select.value = current; select.disabled = false; status.textContent = picker.dataset.failed; }; document.head.append(candidate); }); document.addEventListener('pointerdown', event => { if (!picker.contains(event.target)) picker.open = false; }); picker.addEventListener('keydown', event => { if (event.key === 'Escape') { picker.open = false; picker.querySelector('summary').focus(); } }); })();