|
36 | 36 | })(window,document,'script','dataLayer','GTM-K5HG9JT');</script>
|
37 | 37 |
|
38 | 38 | <script>
|
39 |
| - |
40 |
| - let storedTheme = null; |
41 |
| - try { |
42 |
| - storedTheme = localStorage.getItem("theme"); |
43 |
| - } catch (e) { |
44 |
| - // Some environments block storage (privacy mode, etc.) |
45 |
| - } |
46 |
| - |
47 |
| - const mql = window.matchMedia |
48 |
| - ? window.matchMedia("(prefers-color-scheme: dark)") |
49 |
| - : null; |
50 |
| - const initialTheme = storedTheme || (mql && mql.matches ? "dark" : "light"); |
51 |
| - document.documentElement.setAttribute("data-theme", initialTheme); |
52 |
| - |
53 |
| - document.addEventListener("DOMContentLoaded", () => { |
54 |
| - const toggle = document.getElementById("theme-toggle"); |
55 |
| - if (!toggle) return; |
56 |
| - |
57 |
| - toggle.checked = |
58 |
| - document.documentElement.getAttribute("data-theme") === "dark"; |
59 |
| - |
60 |
| - toggle.addEventListener("change", () => { |
61 |
| - const theme = toggle.checked ? "dark" : "light"; |
62 |
| - document.documentElement.setAttribute("data-theme", theme); |
63 |
| - try { |
64 |
| - localStorage.setItem("theme", theme); |
65 |
| - } catch (e) {} |
66 |
| - }); |
67 |
| - |
68 |
| - // Follow system preference only until the user picks a theme |
69 |
| - let hasUserChoice = false; |
| 39 | + let storedTheme = null; |
| 40 | + try { |
| 41 | + storedTheme = localStorage.getItem('theme'); |
| 42 | + } catch (e) { |
| 43 | + console.warn('Unable to read theme from localStorage:', e); |
| 44 | + } |
| 45 | + |
| 46 | + const mql = window.matchMedia?.('(prefers-color-scheme: dark)'); |
| 47 | + const initialTheme = storedTheme || (mql?.matches ? 'dark' : 'light'); |
| 48 | + document.documentElement.setAttribute('data-theme', initialTheme); |
| 49 | + |
| 50 | + document.addEventListener('DOMContentLoaded', () => { |
| 51 | + const toggle = document.getElementById('theme-toggle'); |
| 52 | + if (!toggle) return; |
| 53 | + |
| 54 | + toggle.addEventListener('click', () => { |
| 55 | + const currentTheme = document.documentElement.getAttribute('data-theme'); |
| 56 | + const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; |
| 57 | + document.documentElement.setAttribute('data-theme', newTheme); |
70 | 58 | try {
|
71 |
| - hasUserChoice = localStorage.getItem("theme") !== null; |
| 59 | + localStorage.setItem('theme', newTheme); |
72 | 60 | } catch (e) {
|
73 |
| - hasUserChoice = false; |
| 61 | + console.warn('Failed to write theme to localStorage:', e); |
74 | 62 | }
|
| 63 | + }); |
| 64 | + |
| 65 | + const hasUserChoice = storedTheme !== null; |
75 | 66 |
|
76 |
| - if (!hasUserChoice && mql) { |
77 |
| - const onSystemChange = (e) => { |
78 |
| - try { |
79 |
| - if (localStorage.getItem("theme") !== null) return; |
80 |
| - } catch (err) { |
81 |
| - // continue following system if storage is blocked |
82 |
| - } |
83 |
| - const theme = e.matches ? "dark" : "light"; |
84 |
| - document.documentElement.setAttribute("data-theme", theme); |
85 |
| - toggle.checked = theme === "dark"; |
86 |
| - }; |
87 |
| - |
88 |
| - if (typeof mql.addEventListener === "function") { |
89 |
| - mql.addEventListener("change", onSystemChange); |
90 |
| - } else if (typeof mql.addListener === "function") { |
91 |
| - mql.addListener(onSystemChange); |
| 67 | + if (!hasUserChoice && mql) { |
| 68 | + const onSystemChange = (e) => { |
| 69 | + try { |
| 70 | + if (localStorage.getItem('theme') !== null) return; |
| 71 | + } catch (err) { |
| 72 | + console.warn('Unable to read theme from localStorage:', err); |
92 | 73 | }
|
93 |
| - } |
94 |
| - }); |
| 74 | + const theme = e.matches ? 'dark' : 'light'; |
| 75 | + document.documentElement.setAttribute('data-theme', theme); |
| 76 | + }; |
| 77 | + mql.addEventListener('change', onSystemChange); |
| 78 | + } |
| 79 | + }); |
95 | 80 |
|
96 |
| - // Keep multiple tabs in sync so a change in one reflects in others |
97 |
| - window.addEventListener("storage", (e) => { |
98 |
| - if (e.key !== "theme") return; |
99 |
| - const theme = e.newValue || (mql && mql.matches ? "dark" : "light"); |
100 |
| - document.documentElement.setAttribute("data-theme", theme); |
101 |
| - const toggle = document.getElementById("theme-toggle"); |
102 |
| - if (toggle) toggle.checked = theme === "dark"; |
103 |
| - }); |
| 81 | + window.addEventListener('storage', (e) => { |
| 82 | + if (e.key !== 'theme') return; |
| 83 | + const theme = e.newValue || (mql?.matches ? 'dark' : 'light'); |
| 84 | + document.documentElement.setAttribute('data-theme', theme); |
| 85 | + }); |
104 | 86 |
|
105 | 87 | </script>
|
106 | 88 | </head>
|
|
0 commit comments