Skip to content

Commit 6af62bc

Browse files
authored
Merge pull request #651 from dzcode-io/fix/fallback-to-user-pref-insteadof-dark-mode
fix: fallback to user prefered mode instead of assuming dark mode for first-time theme load
2 parents d3cab58 + 0b1dec1 commit 6af62bc

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

web/src/components/top-bar.tsx

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { useMemo, useLayoutEffect, useState, useCallback } from "react";
2+
import { useMemo, useLayoutEffect, useState } from "react";
33
import { useLocation } from "react-router-dom";
44
import logoWide from "src/assets/svg/logo-wide.svg";
55
import logoSquare from "src/assets/svg/logo-square.svg";
@@ -18,6 +18,8 @@ export interface TopBarProps {
1818
links: Array<{ localeKey: DictionaryKeys<"navbar-section">; href: string }>;
1919
}
2020

21+
type Theme = "light" | "dark";
22+
2123
export function TopBar({ version, links }: TopBarProps): JSX.Element {
2224
const { pathname } = useLocation();
2325
const languageLessPathname = useMemo(() => stripLanguageCodeFromHRef(pathname), [pathname]);
@@ -41,25 +43,17 @@ export function TopBar({ version, links }: TopBarProps): JSX.Element {
4143

4244
const { localize } = useLocale();
4345

44-
const [isDark, setIsDark] = useState(() => {
45-
const savedTheme = localStorage.getItem("theme");
46-
const isDark = savedTheme === "dark";
47-
return savedTheme ? isDark : true; // default should be dark
46+
const [theme, setTheme] = useState((): Theme => {
47+
const savedTheme = localStorage.getItem("theme") || "";
48+
if (["light", "dark"].includes(savedTheme)) return savedTheme as Theme;
49+
50+
return window.matchMedia?.("(prefers-color-scheme: light)")?.matches ? "light" : "dark";
4851
});
4952

5053
useLayoutEffect(() => {
51-
const theme = localStorage.getItem("theme");
52-
if (theme) {
53-
document.documentElement.setAttribute("data-theme", theme);
54-
}
55-
}, []);
56-
57-
const toggleTheme = useCallback(() => {
58-
const newTheme = isDark ? "light" : "dark";
59-
setIsDark(!isDark);
60-
localStorage.setItem("theme", newTheme);
61-
document.documentElement.setAttribute("data-theme", newTheme);
62-
}, [isDark, setIsDark]);
54+
localStorage.setItem("theme", theme);
55+
document.documentElement.setAttribute("data-theme", theme);
56+
}, [theme]);
6357

6458
return (
6559
<div className="bg-neutral">
@@ -133,12 +127,12 @@ export function TopBar({ version, links }: TopBarProps): JSX.Element {
133127
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
134128
</svg>
135129
<input
136-
onClick={toggleTheme}
130+
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
137131
id="theme-toggle"
138132
type="checkbox"
139133
value="dzcodeLight"
140134
className="theme-controller toggle"
141-
checked={!isDark}
135+
checked={theme === "light"}
142136
/>
143137
<svg
144138
xmlns="http://www.w3.org/2000/svg"

0 commit comments

Comments
 (0)