diff --git a/resources/js/theme-switcher.js b/resources/js/theme-switcher.js index e381e80..bf0a56c 100644 --- a/resources/js/theme-switcher.js +++ b/resources/js/theme-switcher.js @@ -1,38 +1,72 @@ -document.addEventListener('DOMContentLoaded', function() { - const themeSwitcher = document.getElementById('themeSwitcher'); - const themeIcon = themeSwitcher.querySelector('.iconify'); - const themeText = themeSwitcher.querySelector('.theme-text'); - - // Check for saved theme preference or use system preference +// Apply theme immediately on script load (before DOM is ready) to prevent flash +(function() { const savedTheme = localStorage.getItem('theme'); const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; - - if (savedTheme === 'dark' || (!savedTheme && systemPrefersDark)) { - document.body.classList.add('dark-mode'); - themeIcon.setAttribute('data-icon', 'mdi-weather-night'); - themeText.textContent = 'Dark Mode'; + + // Wait for body to be available + if (document.body) { + applyInitialTheme(); } else { - document.body.classList.add('light-mode'); - themeIcon.setAttribute('data-icon', 'mdi-weather-sunny'); - themeText.textContent = 'Light Mode'; + document.addEventListener('DOMContentLoaded', applyInitialTheme); } - + + function applyInitialTheme() { + // Remove any existing theme classes first + document.body.classList.remove('dark-mode', 'light-mode'); + + if (savedTheme === 'dark' || (!savedTheme && systemPrefersDark)) { + document.body.classList.add('dark-mode'); + } else { + document.body.classList.add('light-mode'); + } + } +})(); + +document.addEventListener('DOMContentLoaded', function() { + const themeSwitcher = document.getElementById('themeSwitcher'); + + if (!themeSwitcher) { + console.warn('Theme switcher button not found'); + return; + } + + const themeIcon = themeSwitcher.querySelector('.iconify'); + const themeText = themeSwitcher.querySelector('.theme-text'); + + // Function to apply theme + function applyTheme(theme) { + // Remove both classes first + document.body.classList.remove('dark-mode', 'light-mode'); + + if (theme === 'dark') { + document.body.classList.add('dark-mode'); + if (themeIcon) themeIcon.setAttribute('data-icon', 'mdi-weather-night'); + if (themeText) themeText.textContent = 'Dark Mode'; + localStorage.setItem('theme', 'dark'); + } else { + document.body.classList.add('light-mode'); + if (themeIcon) themeIcon.setAttribute('data-icon', 'mdi-weather-sunny'); + if (themeText) themeText.textContent = 'Light Mode'; + localStorage.setItem('theme', 'light'); + } + } + + // Set initial theme UI based on current state + const savedTheme = localStorage.getItem('theme'); + const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; + const currentTheme = savedTheme || (systemPrefersDark ? 'dark' : 'light'); + + if (currentTheme === 'dark') { + if (themeIcon) themeIcon.setAttribute('data-icon', 'mdi-weather-night'); + if (themeText) themeText.textContent = 'Dark Mode'; + } else { + if (themeIcon) themeIcon.setAttribute('data-icon', 'mdi-weather-sunny'); + if (themeText) themeText.textContent = 'Light Mode'; + } + // Theme switcher click handler themeSwitcher.addEventListener('click', function() { - if (document.body.classList.contains('dark-mode')) { - // Switch to light mode - document.body.classList.remove('dark-mode'); - document.body.classList.add('light-mode'); - themeIcon.setAttribute('data-icon', 'mdi-weather-sunny'); - themeText.textContent = 'Light Mode'; - localStorage.setItem('theme', 'light'); - } else { - // Switch to dark mode - document.body.classList.remove('light-mode'); - document.body.classList.add('dark-mode'); - themeIcon.setAttribute('data-icon', 'mdi-weather-night'); - themeText.textContent = 'Dark Mode'; - localStorage.setItem('theme', 'dark'); - } + const isDarkMode = document.body.classList.contains('dark-mode'); + applyTheme(isDarkMode ? 'light' : 'dark'); }); }); \ No newline at end of file diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 6456725..729c5dc 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -24,7 +24,7 @@ @livewireStyles @bukStyles -
+