Skip to content

fix: allow theme state to be persisted #645

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 7 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"@sentry/react": "^8.27.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-helmet-async": "^2.0.5"
"react-helmet-async": "^2.0.5",
"theme-change": "^2.5.0"
},
"devDependencies": {
"@loadable/component": "^5.16.4",
Expand Down
27 changes: 25 additions & 2 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 } 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 @@ -12,7 +12,7 @@ import { useAppSelector } from "src/redux/store";
import { stripLanguageCodeFromHRef } from "src/utils/website-language";
import { useSearchModal } from "src/utils/search-modal";
import { Language, LANGUAGES } from "@dzcode.io/models/dist/language";

import { themeChange } from "theme-change";
export interface TopBarProps {
version: string;
links: Array<{ localeKey: DictionaryKeys<"navbar-section">; href: string }>;
Expand Down Expand Up @@ -41,6 +41,27 @@ 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
});

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

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

return (
<div className="bg-neutral">
<div className="m-auto flex max-w-7xl flex-row gap-4 p-4 items-center">
Expand Down Expand Up @@ -113,10 +134,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}
id="theme-toggle"
type="checkbox"
value="dzcodeLight"
className="theme-controller toggle"
checked={!isDark}
/>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
Loading