diff --git a/public/js/theme.js b/public/js/theme.js index 9f2705a..a57db20 100644 --- a/public/js/theme.js +++ b/public/js/theme.js @@ -11,12 +11,11 @@ // Initialize theme immediately to prevent flash of wrong theme initThemeEarly(); - // Then initialize everything else when DOM is ready - if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', initializeThemeSystem); - } else { + // Wait for components to be loaded before initializing the theme system + document.addEventListener('componentsLoaded', function() { + console.log('Components loaded, initializing theme system...'); initializeThemeSystem(); - } + }); // Functions function initThemeEarly() { @@ -35,12 +34,16 @@ } function initializeThemeSystem() { + console.log('Theme system initializing...'); try { // Set up theme toggle button const themeToggle = document.querySelector('.theme-toggle'); + console.log('Theme toggle button found:', themeToggle); + if (themeToggle) { themeToggle.addEventListener('click', toggleTheme); updateThemeIcon(getCurrentTheme()); + console.log('Theme toggle event listener added'); } // Set up system theme change listener @@ -67,6 +70,10 @@ try { const currentTheme = getCurrentTheme(); const newTheme = currentTheme === THEMES.DARK ? THEMES.LIGHT : THEMES.DARK; + + console.log('Theme Toggle clicked!'); + console.log('Current theme:', currentTheme); + console.log('New theme:', newTheme); setTheme(newTheme); localStorage.setItem(THEME_STORAGE_KEY, newTheme); @@ -78,11 +85,15 @@ function updateThemeIcon(theme) { try { - const themeIcon = document.querySelector('.theme-toggle i'); - if (themeIcon) { - themeIcon.className = theme === THEMES.DARK - ? 'fas fa-sun' - : 'fas fa-moon'; + const themeToggle = document.querySelector('.theme-toggle'); + if (themeToggle) { + // Ensure both icons exist + if (!themeToggle.querySelector('.fa-sun')) { + themeToggle.innerHTML = ` + + + `; + } } } catch (error) { console.warn('Theme icon update error:', error); @@ -111,4 +122,4 @@ console.warn('Theme persistence error:', error); } } -})(); +})(); \ No newline at end of file