|
21 | 21 | </div> |
22 | 22 |
|
23 | 23 | <script type="text/javascript"> |
24 | | -const theme = localStorage.getItem('theme'); |
25 | | - |
26 | | -if (theme === "light") { |
27 | | - document.documentElement.setAttribute('data-theme', 'light'); |
28 | | -} else { |
29 | | - document.documentElement.setAttribute('data-theme', 'dark'); |
30 | | -} |
31 | | -const userPrefers = getComputedStyle(document.documentElement).getPropertyValue('content'); |
| 24 | +// Get theme from localStorage, default to dark if not set |
| 25 | +const theme = localStorage.getItem('theme') || 'dark'; |
32 | 26 |
|
33 | 27 | function activateDarkTheme() { |
34 | | - document.getElementById('theme-toggle').checked = true; |
35 | 28 | document.documentElement.setAttribute('data-theme', 'dark'); |
36 | 29 | document.documentElement.classList.add('theme--dark'); |
37 | 30 | document.documentElement.classList.remove('theme--light'); |
38 | | - document.getElementById("theme-toggle").className = 'light'; |
39 | | - window.localStorage.setItem('theme', 'dark'); |
| 31 | + window.localStorage.setItem('theme', 'dark'); |
40 | 32 | } |
41 | 33 |
|
42 | 34 | function activateLightTheme() { |
43 | | - document.getElementById('theme-toggle').checked = false; |
44 | 35 | document.documentElement.setAttribute('data-theme', 'light'); |
45 | 36 | document.documentElement.classList.add('theme--light'); |
46 | 37 | document.documentElement.classList.remove('theme--dark'); |
47 | | - document.getElementById("theme-toggle").className = 'dark'; |
48 | | - window.localStorage.setItem('theme', 'light'); |
| 38 | + window.localStorage.setItem('theme', 'light'); |
49 | 39 | } |
50 | 40 |
|
51 | | -if (theme === "dark") { |
52 | | - activateDarkTheme(); |
53 | | -} else if (theme === "light") { |
| 41 | +// Apply theme immediately to prevent flash |
| 42 | +if (theme === "light") { |
54 | 43 | activateLightTheme(); |
55 | | -} else if (userPrefers === "light") { |
56 | | - activateDarkTheme(); |
57 | 44 | } else { |
| 45 | + // Default to dark theme |
58 | 46 | activateDarkTheme(); |
59 | 47 | } |
60 | 48 |
|
61 | 49 | function modeSwitcher() { |
62 | | - let currentMode = document.documentElement.getAttribute('data-theme'); |
63 | | - if (currentMode === "dark") { |
64 | | - activateLightTheme(); |
65 | | - } else { |
66 | | - activateDarkTheme(); |
67 | | - } |
| 50 | + let currentMode = document.documentElement.getAttribute('data-theme'); |
| 51 | + if (currentMode === "dark") { |
| 52 | + activateLightTheme(); |
| 53 | + } else { |
| 54 | + activateDarkTheme(); |
| 55 | + } |
68 | 56 | } |
69 | 57 | </script> |
0 commit comments