Skip to content

Commit 1c9516f

Browse files
committed
Fix mode button weird behavior and refactor
1 parent af4d84c commit 1c9516f

1 file changed

Lines changed: 40 additions & 43 deletions

File tree

src/layout/body/header.astro

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ import {
139139
) as unknown as SVGGElement;
140140
const modeSun = document.getElementById(modeSunId) as unknown as SVGGElement;
141141

142+
darkModeMedia.addEventListener("change", setModeTitleBasedOnMedia);
142143
function setModeTitleBasedOnMedia() {
143144
if (darkModeMedia.matches) {
144145
modeTitle.textContent = "Light mode";
@@ -148,37 +149,22 @@ import {
148149
modeTitle.textContent = "Dark mode";
149150
}
150151

151-
function resetModeSvgState(mode: Mode) {
152-
switch (mode) {
153-
case "light": {
154-
modeMoon.style.opacity = "1";
155-
modeSun.style.opacity = "0";
156-
break;
157-
}
158-
case "dark": {
159-
modeMoon.style.opacity = "0";
160-
modeSun.style.opacity = "1";
161-
break;
162-
}
163-
}
164-
165-
modeMoon.classList.remove("rotate-appear");
166-
modeMoon.classList.remove("rotate-vanish");
167-
modeSun.classList.remove("rotate-appear");
168-
modeSun.classList.remove("rotate-vanish");
169-
}
170-
171-
let isSetModeFirstRun = true;
172-
function setMode(mode: Mode) {
152+
modeButton.addEventListener("click", () => {
153+
setMode(modeTitle.textContent === "Light mode" ? "light" : "dark");
154+
});
155+
function setMode(mode: Mode, animateModeSvg = true) {
173156
switch (mode) {
174157
case "light": {
175158
rootStyle.setProperty("--c-foreground", colors.black);
176159
rootStyle.setProperty("--c-background", colors.white);
177160
rootStyle.setProperty("--c-accent", colors.grayDark);
178161

179-
if (!isSetModeFirstRun) {
162+
if (animateModeSvg) {
180163
modeMoon.classList.add("rotate-appear");
181164
modeSun.classList.add("rotate-vanish");
165+
} else {
166+
modeMoon.style.opacity = "1";
167+
modeSun.style.opacity = "0";
182168
}
183169

184170
modeTitle.textContent = "Dark mode";
@@ -190,9 +176,12 @@ import {
190176
rootStyle.setProperty("--c-background", colors.black);
191177
rootStyle.setProperty("--c-accent", colors.grayLight);
192178

193-
if (!isSetModeFirstRun) {
179+
if (animateModeSvg) {
194180
modeMoon.classList.add("rotate-vanish");
195181
modeSun.classList.add("rotate-appear");
182+
} else {
183+
modeMoon.style.opacity = "0";
184+
modeSun.style.opacity = "1";
196185
}
197186

198187
modeTitle.textContent = "Light mode";
@@ -201,39 +190,47 @@ import {
201190
}
202191
}
203192

204-
if (isSetModeFirstRun) {
205-
// Once the user has set a mode preference, follow that instead of the
206-
// system default.
207-
darkModeMedia.removeEventListener("change", setModeTitleBasedOnMedia);
208-
// On initial load, shows the moon on the mode button for light mode.
209-
resetModeSvgState(mode);
210-
211-
isSetModeFirstRun = false;
212-
}
193+
// Once the user has set a mode preference, follow that instead of the
194+
// system default.
195+
darkModeMedia.removeEventListener("change", setModeTitleBasedOnMedia);
213196
}
214197

215-
darkModeMedia.addEventListener("change", setModeTitleBasedOnMedia);
216198
// Mode moon and sun animations end at the same time so put 'animationend'
217199
// handling on only one of them.
218200
modeMoon.addEventListener("animationend", () => {
219-
const storedMode = localStorage.getItem("mode") as Mode | undefined;
201+
const storedMode = localStorage.getItem("mode") as Mode | null;
220202

221-
if (typeof storedMode !== "undefined") {
222-
resetModeSvgState(storedMode);
203+
if (storedMode === null) {
204+
return;
223205
}
224-
});
225-
modeButton.addEventListener("click", () => {
226-
setMode(modeTitle.textContent === "Light mode" ? "light" : "dark");
206+
207+
switch (storedMode) {
208+
case "light": {
209+
modeMoon.style.opacity = "1";
210+
modeSun.style.opacity = "0";
211+
break;
212+
}
213+
case "dark": {
214+
modeMoon.style.opacity = "0";
215+
modeSun.style.opacity = "1";
216+
break;
217+
}
218+
}
219+
220+
modeMoon.classList.remove("rotate-appear");
221+
modeMoon.classList.remove("rotate-vanish");
222+
modeSun.classList.remove("rotate-appear");
223+
modeSun.classList.remove("rotate-vanish");
227224
});
228225

229226
addEventListener("DOMContentLoaded", () => {
230-
const storedMode = localStorage.getItem("mode") as Mode | undefined;
227+
const storedMode = localStorage.getItem("mode") as Mode | null;
231228

232-
if (typeof storedMode === "undefined") {
229+
if (storedMode === null) {
233230
setModeTitleBasedOnMedia();
234231
return;
235232
}
236233

237-
setMode(storedMode);
234+
setMode(storedMode, false);
238235
});
239236
</script>

0 commit comments

Comments
 (0)