Skip to content

Commit d481b84

Browse files
committed
fix toggling button
Signed-off-by: Huabing (Robin) Zhao <zhaohuabing@gmail.com>
1 parent 3d49c3e commit d481b84

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

layouts/_partials/nav.html

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,39 @@
4444
</li>
4545
{{ if not (eq .Site.Params.dark_mode_toggle false) }}
4646
<li>
47-
<a href="#" id="theme-toggle" title="Toggle dark mode">
47+
<a href="#" id="theme-toggle" title="Toggle dark mode" style="opacity: 0;">
4848
<i class="fa fa-moon"></i>
49+
<i class="fa fa-sun" style="display: none;"></i>
4950
</a>
5051
</li>
52+
<script>
53+
(function() {
54+
// Set correct icon immediately to prevent flash
55+
var theme = localStorage.getItem('cleanwhite-theme') ||
56+
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
57+
var toggleBtn = document.getElementById('theme-toggle');
58+
if (toggleBtn) {
59+
var moonIcon = toggleBtn.querySelector('.fa-moon');
60+
var sunIcon = toggleBtn.querySelector('.fa-sun');
61+
62+
if (theme === 'dark') {
63+
if (moonIcon) moonIcon.style.display = 'none';
64+
if (sunIcon) sunIcon.style.display = 'inline';
65+
toggleBtn.setAttribute('title', 'Switch to light mode');
66+
} else {
67+
if (moonIcon) moonIcon.style.display = 'inline';
68+
if (sunIcon) sunIcon.style.display = 'none';
69+
toggleBtn.setAttribute('title', 'Switch to dark mode');
70+
}
71+
72+
// Show button with correct icon
73+
requestAnimationFrame(function() {
74+
toggleBtn.style.transition = 'opacity 0.2s ease';
75+
toggleBtn.style.opacity = '1';
76+
});
77+
}
78+
})();
79+
</script>
5180
{{ end }}
5281
</ul>
5382
</div>

static/js/theme-toggle.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ const ThemeManager = (function() {
122122
const toggleBtn = document.getElementById('theme-toggle');
123123
if (!toggleBtn) return;
124124

125+
const moonIcon = toggleBtn.querySelector('.fa-moon');
126+
const sunIcon = toggleBtn.querySelector('.fa-sun');
127+
128+
// Handle new method (both icons present, show/hide)
129+
if (moonIcon && sunIcon) {
130+
if (theme === THEMES.DARK) {
131+
moonIcon.style.display = 'none';
132+
sunIcon.style.display = 'inline';
133+
toggleBtn.setAttribute('title', 'Switch to light mode');
134+
} else {
135+
moonIcon.style.display = 'inline';
136+
sunIcon.style.display = 'none';
137+
toggleBtn.setAttribute('title', 'Switch to dark mode');
138+
}
139+
return;
140+
}
141+
142+
// Handle old method (single icon, swap classes)
125143
const icon = toggleBtn.querySelector('i');
126144
if (!icon) return;
127145

0 commit comments

Comments
 (0)