@@ -148,18 +148,35 @@ import {
148148 modeTitle.textContent = "Dark mode";
149149 }
150150
151- function setMode(mode: Mode, animateModeSvg = true) {
152- // Once the user has set a mode preference, follow that instead of the
153- // system default.
154- darkModeMedia.removeEventListener("change", setModeTitleBasedOnMedia);
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+ }
155164
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) {
156173 switch (mode) {
157174 case "light": {
158175 rootStyle.setProperty("--c-foreground", colors.black);
159176 rootStyle.setProperty("--c-background", colors.white);
160177 rootStyle.setProperty("--c-accent", colors.grayDark);
161178
162- if (animateModeSvg ) {
179+ if (!isSetModeFirstRun ) {
163180 modeMoon.classList.add("rotate-appear");
164181 modeSun.classList.add("rotate-vanish");
165182 }
@@ -173,7 +190,7 @@ import {
173190 rootStyle.setProperty("--c-background", colors.black);
174191 rootStyle.setProperty("--c-accent", colors.grayLight);
175192
176- if (animateModeSvg ) {
193+ if (!isSetModeFirstRun ) {
177194 modeMoon.classList.add("rotate-vanish");
178195 modeSun.classList.add("rotate-appear");
179196 }
@@ -183,32 +200,19 @@ import {
183200 break;
184201 }
185202 }
186- }
187203
188- function resetModeSvgState(mode: Mode) {
189- switch (mode) {
190- case "light": {
191- modeMoon.style.opacity = "1";
192- modeSun.style.opacity = "0";
193- break;
194- }
195- case "dark": {
196- modeMoon.style.opacity = "0";
197- modeSun.style.opacity = "1";
198- break;
199- }
200- }
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);
201210
202- modeMoon.classList.remove("rotate-appear");
203- modeMoon.classList.remove("rotate-vanish");
204- modeSun.classList.remove("rotate-appear");
205- modeSun.classList.remove("rotate-vanish");
211+ isSetModeFirstRun = false;
212+ }
206213 }
207214
208215 darkModeMedia.addEventListener("change", setModeTitleBasedOnMedia);
209- modeButton.addEventListener("click", () => {
210- setMode(modeTitle.textContent === "Light mode" ? "light" : "dark");
211- });
212216 // Mode moon and sun animations end at the same time so put 'animationend'
213217 // handling on only one of them.
214218 modeMoon.addEventListener("animationend", () => {
@@ -218,6 +222,9 @@ import {
218222 resetModeSvgState(storedMode);
219223 }
220224 });
225+ modeButton.addEventListener("click", () => {
226+ setMode(modeTitle.textContent === "Light mode" ? "light" : "dark");
227+ });
221228
222229 addEventListener("DOMContentLoaded", () => {
223230 const storedMode = localStorage.getItem("mode") as Mode | undefined;
@@ -227,8 +234,6 @@ import {
227234 return;
228235 }
229236
230- // Do not animate mode SVG on the first run, it is already in the correct
231- // state.
232- setMode(storedMode, false);
237+ setMode(storedMode);
233238 });
234239</script >
0 commit comments