/**
* Theme Toggle — shared across all pages.
* Reads/writes 'theme' key in localStorage.
* Injects an animated moon/sun button into the page.
*/
(() => {
'use strict';
const STORAGE_KEY = 'theme';
/* ---------- SVG icons ---------- */
const moonSVG = ``;
const sunSVG = ``;
/* ---------- Apply saved theme BEFORE paint ---------- */
const saved = localStorage.getItem(STORAGE_KEY);
if (saved) {
document.documentElement.setAttribute('data-theme', saved);
}
/* ---------- Create button when DOM ready ---------- */
function init() {
const isDark = () => document.documentElement.getAttribute('data-theme') !== 'light';
const btn = document.createElement('button');
btn.className = 'theme-toggle';
btn.setAttribute('aria-label', 'Переключить тему');
btn.innerHTML = isDark() ? moonSVG : sunSVG;
btn.addEventListener('click', () => {
const goLight = isDark();
document.documentElement.setAttribute('data-theme', goLight ? 'light' : 'dark');
localStorage.setItem(STORAGE_KEY, goLight ? 'light' : 'dark');
/* Swap icon immediately and trigger a gentle rotate via CSS */
btn.innerHTML = goLight ? sunSVG : moonSVG;
const svg = btn.querySelector('svg');
svg.style.transition = 'none';
svg.style.transform = 'rotate(-90deg) scale(0.5)';
svg.style.opacity = '0';
requestAnimationFrame(() => {
svg.style.transition = 'transform 0.4s ease, opacity 0.3s ease';
svg.style.transform = 'rotate(0deg) scale(1)';
svg.style.opacity = '1';
});
});
/* Where to place the button */
const topbar = document.querySelector('.topbar');
if (topbar) {
/* Admin-style pages: insert into topbar (CSS handles flex layout) */
topbar.appendChild(btn);
} else {
/* Login / placeholder pages: fixed position */
btn.classList.add('theme-toggle--fixed');
document.body.appendChild(btn);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();