Skip to content

fix: fallback to user prefered mode instead of assuming dark mode for first-time theme load #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions web/src/components/top-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { useMemo, useLayoutEffect, useState, useCallback } from "react";
import { useMemo, useLayoutEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import logoWide from "src/assets/svg/logo-wide.svg";
import logoSquare from "src/assets/svg/logo-square.svg";
Expand All @@ -18,6 +18,8 @@ export interface TopBarProps {
links: Array<{ localeKey: DictionaryKeys<"navbar-section">; href: string }>;
}

type Theme = "light" | "dark";

export function TopBar({ version, links }: TopBarProps): JSX.Element {
const { pathname } = useLocation();
const languageLessPathname = useMemo(() => stripLanguageCodeFromHRef(pathname), [pathname]);
Expand All @@ -41,25 +43,17 @@ export function TopBar({ version, links }: TopBarProps): JSX.Element {

const { localize } = useLocale();

const [isDark, setIsDark] = useState(() => {
const savedTheme = localStorage.getItem("theme");
const isDark = savedTheme === "dark";
return savedTheme ? isDark : true; // default should be dark
const [theme, setTheme] = useState((): Theme => {
const savedTheme = localStorage.getItem("theme") || "";
if (["light", "dark"].includes(savedTheme)) return savedTheme as Theme;

return window.matchMedia?.("(prefers-color-scheme: light)")?.matches ? "light" : "dark";
});

useLayoutEffect(() => {
const theme = localStorage.getItem("theme");
if (theme) {
document.documentElement.setAttribute("data-theme", theme);
}
}, []);

const toggleTheme = useCallback(() => {
const newTheme = isDark ? "light" : "dark";
setIsDark(!isDark);
localStorage.setItem("theme", newTheme);
document.documentElement.setAttribute("data-theme", newTheme);
}, [isDark, setIsDark]);
localStorage.setItem("theme", theme);
document.documentElement.setAttribute("data-theme", theme);
}, [theme]);

return (
<div className="bg-neutral">
Expand Down Expand Up @@ -133,12 +127,12 @@ export function TopBar({ version, links }: TopBarProps): JSX.Element {
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
</svg>
<input
onClick={toggleTheme}
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
id="theme-toggle"
type="checkbox"
value="dzcodeLight"
className="theme-controller toggle"
checked={!isDark}
checked={theme === "light"}
/>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
Loading