Spaces:
Sleeping
Sleeping
File size: 2,796 Bytes
7644eac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
// Initialize theme immediately to prevent flash
(function() {
const storedTheme = localStorage.getItem('theme');
const root = document.documentElement;
if (storedTheme === 'light') {
root.classList.remove('dark');
} else {
// Default to dark theme
root.classList.add('dark');
}
})();
document.addEventListener('DOMContentLoaded', function () {
// Ensure Tailwind uses class strategy
if (window.tailwind && window.tailwind.config) {
window.tailwind.config.darkMode = 'class';
}
const themeToggleDesktop = document.getElementById('theme-toggle');
const themeToggleMobile = document.getElementById('theme-toggle-mobile');
const root = document.documentElement;
// Helpers to swap icon visibility
function showDarkIcons() {
document.querySelectorAll('#theme-toggle-light-icon, #theme-toggle-mobile-light-icon').forEach(el => {
if (el) el.classList.add('hidden');
});
document.querySelectorAll('#theme-toggle-dark-icon, #theme-toggle-mobile-dark-icon').forEach(el => {
if (el) el.classList.remove('hidden');
});
}
function showLightIcons() {
document.querySelectorAll('#theme-toggle-dark-icon, #theme-toggle-mobile-dark-icon').forEach(el => {
if (el) el.classList.add('hidden');
});
document.querySelectorAll('#theme-toggle-light-icon, #theme-toggle-mobile-light-icon').forEach(el => {
if (el) el.classList.remove('hidden');
});
}
// Set initial theme and icons
const storedTheme = localStorage.getItem('theme');
const isDark = storedTheme !== 'light'; // Default to dark
if (isDark) {
root.classList.add('dark');
showDarkIcons();
} else {
root.classList.remove('dark');
showLightIcons();
}
function toggleTheme() {
root.classList.toggle('dark');
const isDark = root.classList.contains('dark');
if (isDark) {
localStorage.setItem('theme', 'dark');
showDarkIcons();
console.log('Switched to dark mode');
} else {
localStorage.setItem('theme', 'light');
showLightIcons();
console.log('Switched to light mode');
}
}
if (themeToggleDesktop) {
themeToggleDesktop.addEventListener('click', function(e) {
e.preventDefault();
toggleTheme();
});
}
if (themeToggleMobile) {
themeToggleMobile.addEventListener('click', function(e) {
e.preventDefault();
toggleTheme();
});
}
console.log('Theme system initialized. Current theme:', root.classList.contains('dark') ? 'dark' : 'light');
});
|