|
23 | 23 |
|
24 | 24 | // Dark mode toggle functionality |
25 | 25 | function initDarkMode() { |
26 | | - const isDark = localStorage.getItem('darkMode') === 'true' || |
| 26 | + // Check if user has previously set a preference |
| 27 | + const isDark = localStorage.getItem('darkMode') === 'true' || |
27 | 28 | (!localStorage.getItem('darkMode') && window.matchMedia('(prefers-color-scheme: dark)').matches); |
28 | | - |
29 | | - document.documentElement.classList.toggle('dark', isDark); |
| 29 | +
|
| 30 | + if (isDark) { |
| 31 | + document.documentElement.classList.add('dark'); |
| 32 | + } else { |
| 33 | + document.documentElement.classList.remove('dark'); |
| 34 | + } |
30 | 35 | updateDarkModeIcon(isDark); |
31 | 36 | } |
32 | 37 |
|
|
39 | 44 | function updateDarkModeIcon(isDark) { |
40 | 45 | const moonIcon = document.getElementById('moon-icon'); |
41 | 46 | const sunIcon = document.getElementById('sun-icon'); |
42 | | - |
| 47 | +
|
43 | 48 | if (isDark) { |
44 | 49 | moonIcon.classList.add('hidden'); |
45 | 50 | sunIcon.classList.remove('hidden'); |
|
50 | 55 | } |
51 | 56 |
|
52 | 57 | // Initialize dark mode on page load |
53 | | - document.addEventListener('DOMContentLoaded', initDarkMode); |
| 58 | + if (document.readyState === 'loading') { |
| 59 | + document.addEventListener('DOMContentLoaded', initDarkMode); |
| 60 | + } else { |
| 61 | + initDarkMode(); |
| 62 | + } |
54 | 63 | </script> |
55 | 64 | <header class="sticky top-0 bg-[#FFFDF8] dark:bg-gray-900 border-b-[3px] border-gray-900 dark:border-gray-400 z-50"> |
56 | 65 | <div class="max-w-4xl mx-auto px-4"> |
|
90 | 99 | <span id="github-stars">0</span> |
91 | 100 | </div> |
92 | 101 | </div> |
93 | | - <button onclick="toggleDarkMode()" |
| 102 | + <button onclick="toggleDarkMode()" |
94 | 103 | class="p-2 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors" |
95 | 104 | aria-label="Toggle dark mode"> |
96 | 105 | <!-- Moon icon --> |
97 | | - <svg id="moon-icon" class="w-5 h-5 text-gray-900 dark:text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
98 | | - <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"></path> |
| 106 | + <svg id="moon-icon" |
| 107 | + class="w-5 h-5 text-gray-900 dark:text-white" |
| 108 | + fill="none" |
| 109 | + stroke="currentColor" |
| 110 | + viewBox="0 0 24 24"> |
| 111 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"> |
| 112 | + </path> |
99 | 113 | </svg> |
100 | 114 | <!-- Sun icon --> |
101 | | - <svg id="sun-icon" class="hidden w-5 h-5 text-gray-900 dark:text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
102 | | - <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"></path> |
| 115 | + <svg id="sun-icon" |
| 116 | + class="hidden w-5 h-5 text-gray-900 dark:text-white" |
| 117 | + fill="none" |
| 118 | + stroke="currentColor" |
| 119 | + viewBox="0 0 24 24"> |
| 120 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"> |
| 121 | + </path> |
103 | 122 | </svg> |
104 | 123 | </button> |
105 | 124 | </nav> |
|
0 commit comments