|
6 | 6 | </div>
|
7 | 7 |
|
8 | 8 | <script>
|
9 |
| -// Dark mode functionality |
| 9 | +// Dark mode functionality - apply immediately to avoid flash |
10 | 10 | (function() {
|
11 |
| - const toggle = document.getElementById('dark-mode-toggle'); |
12 |
| - const body = document.body; |
13 |
| - const icon = toggle.querySelector('.toggle-icon'); |
14 |
| - |
15 |
| - // Check for saved theme preference or default to light mode |
| 11 | + // Apply theme immediately from localStorage |
16 | 12 | const savedTheme = localStorage.getItem('crup-theme') || 'light';
|
| 13 | + document.documentElement.setAttribute('data-theme', savedTheme); |
| 14 | + document.body.setAttribute('data-theme', savedTheme); |
17 | 15 |
|
18 |
| - function setTheme(theme) { |
19 |
| - if (theme === 'dark') { |
20 |
| - body.setAttribute('data-theme', 'dark'); |
21 |
| - icon.textContent = '☀️'; |
22 |
| - localStorage.setItem('crup-theme', 'dark'); |
23 |
| - } else { |
24 |
| - body.setAttribute('data-theme', 'light'); |
25 |
| - icon.textContent = '🌙'; |
26 |
| - localStorage.setItem('crup-theme', 'light'); |
| 16 | + // Wait for DOM to load for the toggle functionality |
| 17 | + document.addEventListener('DOMContentLoaded', function() { |
| 18 | + // Prevent multiple initializations |
| 19 | + if (window.darkModeInitialized) return; |
| 20 | + window.darkModeInitialized = true; |
| 21 | + |
| 22 | + const toggle = document.getElementById('dark-mode-toggle'); |
| 23 | + if (!toggle) { |
| 24 | + console.warn('Dark mode toggle button not found'); |
| 25 | + return; |
27 | 26 | }
|
28 |
| - } |
29 |
| - |
30 |
| - // Apply saved theme on page load |
31 |
| - setTheme(savedTheme); |
32 |
| - |
33 |
| - // Toggle theme on button click |
34 |
| - toggle.addEventListener('click', function() { |
35 |
| - const currentTheme = body.getAttribute('data-theme'); |
36 |
| - const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; |
37 |
| - setTheme(newTheme); |
38 |
| - }); |
39 |
| - |
40 |
| - // Listen for system theme changes |
41 |
| - if (window.matchMedia) { |
42 |
| - window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function(e) { |
43 |
| - if (!localStorage.getItem('crup-theme')) { |
44 |
| - setTheme(e.matches ? 'dark' : 'light'); |
| 27 | + |
| 28 | + const body = document.body; |
| 29 | + const html = document.documentElement; |
| 30 | + const icon = toggle.querySelector('.toggle-icon'); |
| 31 | + |
| 32 | + if (!icon) { |
| 33 | + console.warn('Dark mode toggle icon not found'); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + function setTheme(theme) { |
| 38 | + if (theme === 'dark') { |
| 39 | + body.setAttribute('data-theme', 'dark'); |
| 40 | + html.setAttribute('data-theme', 'dark'); |
| 41 | + body.classList.add('dark-theme'); // Additional class for fallback |
| 42 | + html.classList.add('dark-theme'); |
| 43 | + icon.textContent = '☀️'; |
| 44 | + localStorage.setItem('crup-theme', 'dark'); |
| 45 | + console.log('Dark theme applied successfully'); |
| 46 | + |
| 47 | + // Force a style recalculation |
| 48 | + body.style.backgroundColor = '#0d1117'; |
| 49 | + body.style.color = '#f0f6fc'; |
| 50 | + } else { |
| 51 | + body.setAttribute('data-theme', 'light'); |
| 52 | + html.setAttribute('data-theme', 'light'); |
| 53 | + body.classList.remove('dark-theme'); |
| 54 | + html.classList.remove('dark-theme'); |
| 55 | + icon.textContent = '🌙'; |
| 56 | + localStorage.setItem('crup-theme', 'light'); |
| 57 | + console.log('Light theme applied successfully'); |
| 58 | + |
| 59 | + // Reset to CSS variables |
| 60 | + body.style.backgroundColor = ''; |
| 61 | + body.style.color = ''; |
45 | 62 | }
|
| 63 | + } |
| 64 | + |
| 65 | + // Apply saved theme and set icon |
| 66 | + setTheme(savedTheme); |
| 67 | + |
| 68 | + // Toggle theme on button click |
| 69 | + toggle.addEventListener('click', function(e) { |
| 70 | + e.preventDefault(); |
| 71 | + const currentTheme = body.getAttribute('data-theme'); |
| 72 | + const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; |
| 73 | + setTheme(newTheme); |
| 74 | + console.log(`Theme toggled from ${currentTheme} to ${newTheme}`); |
46 | 75 | });
|
47 |
| - } |
| 76 | + |
| 77 | + console.log('Dark mode toggle initialized successfully'); |
| 78 | + }); |
48 | 79 | })();
|
49 | 80 | </script>
|
0 commit comments