Тема оформления: переключатель дня, ночи и авто-режима

This commit is contained in:
dev
2026-11-28 12:43:21 +03:00
parent 468ab0a5fd
commit e5526617fe
36 changed files with 143 additions and 15 deletions
+52
View File
@@ -0,0 +1,52 @@
(function () {
const KEY = 'whatidoTheme';
const MODES = ['auto', 'day', 'night'];
const LABELS = { auto: 'Авто', day: 'День', night: 'Ночь' };
const ICONS = { auto: 'monitor', day: 'sun', night: 'moon' };
const media = window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null;
let link = document.getElementById('themeCss');
function get() {
let value = null;
try { value = localStorage.getItem(KEY); } catch {}
return MODES.includes(value) ? value : 'auto';
}
function isDark(mode) {
return mode === 'night' || (mode === 'auto' && !!(media && media.matches));
}
function apply() {
const mode = get();
const dark = isDark(mode);
document.documentElement.classList.toggle('midnight-neon', dark);
if (!link) {
link = document.createElement('link');
link.id = 'themeCss';
link.rel = 'stylesheet';
link.href = '/admin-dark.css';
document.head.appendChild(link);
}
link.disabled = !dark;
document.dispatchEvent(new CustomEvent('whatido:themechange', { detail: { mode: mode, dark: dark } }));
return mode;
}
function set(mode) {
if (!MODES.includes(mode)) return get();
try { localStorage.setItem(KEY, mode); } catch {}
return apply();
}
function next() {
return set(MODES[(MODES.indexOf(get()) + 1) % MODES.length]);
}
window.WhatIDoTheme = { get: get, set: set, next: next, apply: apply, modes: MODES, labels: LABELS, icons: ICONS };
apply();
if (media) {
const onChange = () => { if (get() === 'auto') apply(); };
if (media.addEventListener) media.addEventListener('change', onChange);
else if (media.addListener) media.addListener(onChange);
}
})();