48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
// Dark Mode Controller — Unified for all Chatc2 pages
|
|
(function() {
|
|
var osPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
|
|
var manualOverride = localStorage.getItem('chatc2_dark_mode_manual');
|
|
|
|
function getInitialTheme() {
|
|
if (manualOverride !== null) return manualOverride === 'true';
|
|
return osPrefersDark.matches;
|
|
}
|
|
|
|
function applyTheme(enable, isManual) {
|
|
if (document.body) {
|
|
document.body.classList.toggle('dark-mode', enable);
|
|
}
|
|
localStorage.setItem('chatc2_dark_mode', enable ? 'true' : 'false');
|
|
if (isManual) {
|
|
localStorage.setItem('chatc2_dark_mode_manual', enable ? 'true' : 'false');
|
|
}
|
|
document.querySelectorAll('.dark-mode-toggle').forEach(function(btn) {
|
|
btn.innerHTML = enable ? '☀️ Claro' : '🌙 Escuro';
|
|
});
|
|
}
|
|
|
|
window.darkModeToggle = function() {
|
|
var isDark = localStorage.getItem('chatc2_dark_mode') === 'true';
|
|
applyTheme(!isDark, true);
|
|
};
|
|
|
|
window.darkModeApply = function(enable) { applyTheme(enable, false); };
|
|
|
|
window.darkModeIsDark = function() {
|
|
return localStorage.getItem('chatc2_dark_mode') === 'true';
|
|
};
|
|
|
|
// Reage a mudanças no SO (só sem override manual)
|
|
osPrefersDark.addEventListener('change', function(e) {
|
|
if (localStorage.getItem('chatc2_dark_mode_manual') === null) {
|
|
applyTheme(e.matches, false);
|
|
}
|
|
});
|
|
|
|
// Aplica imediatamente (sem esperar DOMContentLoaded)
|
|
// Sincroniza html.dark (setado pelo script inline no <head>) com body.dark-mode
|
|
var htmlDark = document.documentElement.classList.contains('dark');
|
|
applyTheme(htmlDark || getInitialTheme(), false);
|
|
document.documentElement.classList.remove('dark');
|
|
})();
|