|
1 | 1 |
|
2 |
| -<!-- Javascript Start --> |
| 2 | +<!-- Javascript Start - Theme Flicker Fix v1 --> |
3 | 3 |
|
4 | 4 | <!-- jQuery -->
|
5 | 5 | <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
|
|
27 | 27 | // State management
|
28 | 28 | this.isScrolled = false;
|
29 | 29 | this.isMobileMenuOpen = false;
|
30 |
| - this.currentTheme = localStorage.getItem('theme') || 'dark'; |
| 30 | + // Use pre-applied theme from critical script, fallback to localStorage or dark |
| 31 | + this.currentTheme = window.initialTheme || localStorage.getItem('theme') || 'dark'; |
31 | 32 |
|
32 | 33 | // Performance optimization
|
33 | 34 | this.scrollTicking = false;
|
|
144 | 145 |
|
145 | 146 | // 🌓 Theme Management
|
146 | 147 | initializeTheme() {
|
147 |
| - this.setTheme(this.currentTheme, false); |
| 148 | + // Theme was already applied in head.html critical script |
| 149 | + // Just sync the toggle button state with the current theme |
| 150 | + this.syncThemeToggleState(); |
148 | 151 | }
|
149 | 152 |
|
150 | 153 | toggleTheme() {
|
151 | 154 | const newTheme = this.currentTheme === 'dark' ? 'light' : 'dark';
|
152 | 155 | this.setTheme(newTheme, true);
|
153 | 156 | }
|
154 | 157 |
|
155 |
| - setTheme(theme, animate = true) { |
156 |
| - this.currentTheme = theme; |
157 |
| - |
158 |
| - // Update DOM |
159 |
| - document.body.setAttribute('data-theme', theme); |
160 |
| - |
161 |
| - // Update theme toggle button |
| 158 | + syncThemeToggleState() { |
| 159 | + // Only update the toggle button state - theme is already applied |
162 | 160 | if (this.themeToggle) {
|
163 |
| - this.themeToggle.setAttribute('aria-pressed', theme === 'light' ? 'true' : 'false'); |
| 161 | + this.themeToggle.setAttribute('aria-pressed', this.currentTheme === 'light' ? 'true' : 'false'); |
164 | 162 |
|
165 | 163 | // Update icon in the handle
|
166 | 164 | const toggleIcon = this.themeToggle.querySelector('.toggle-icon');
|
167 | 165 | if (toggleIcon) {
|
168 |
| - toggleIcon.className = theme === 'light' ? |
| 166 | + toggleIcon.className = this.currentTheme === 'light' ? |
169 | 167 | 'toggle-icon fa fa-sun' :
|
170 | 168 | 'toggle-icon fa fa-moon';
|
171 | 169 | }
|
172 | 170 | }
|
| 171 | + } |
| 172 | + |
| 173 | + setTheme(theme, animate = true) { |
| 174 | + this.currentTheme = theme; |
| 175 | + |
| 176 | + // Update DOM (both html and body for maximum compatibility) |
| 177 | + document.documentElement.setAttribute('data-theme', theme); |
| 178 | + document.body.setAttribute('data-theme', theme); |
| 179 | + |
| 180 | + // Update theme toggle button |
| 181 | + this.syncThemeToggleState(); |
173 | 182 |
|
174 | 183 | // Store preference
|
175 | 184 | localStorage.setItem('theme', theme);
|
|
0 commit comments