Skip to content

Commit

Permalink
fix(ui): fix uninitialized theme classes
Browse files Browse the repository at this point in the history
  • Loading branch information
skjsjhb committed Jan 22, 2025
1 parent 08450f1 commit 4f558ac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import events from "node:events";
import os from "node:os";
import { pathToFileURL } from "node:url";
import path from "path";
import dedent from "ts-dedent";
import { dedent } from "ts-dedent";
import pkg from "~/package.json";
import { runInstrumentedTest } from "~/test/instrumented/entry";
import "v8-compile-cache";
Expand Down Expand Up @@ -210,6 +210,7 @@ function injectDevToolsStyles(w: BrowserWindow) {
if (os.platform() !== "win32") return;

w.webContents.on("devtools-opened", () => {
console.log(dedent);
const css = dedent`
:root {
--source-code-font-family: 'JetBrains Mono', Consolas, 'Courier New', monospace !important;
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { i18n } from "@/renderer/i18n/i18n";
import { useTheme } from "@/renderer/theme";
import { Header } from "@components/Header";
import { HeroUIProvider } from "@heroui/react";
import { About } from "@pages/about/About";
Expand All @@ -12,6 +13,7 @@ import pkg from "~/package.json";
*/
export const App: FC = () => {
const [, navigate] = useLocation();
useTheme();

i18n.useAutoFontClass();

Expand Down
19 changes: 12 additions & 7 deletions src/renderer/theme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isTruthy } from "@/main/util/misc";
import type { ConfigTheme } from "@heroui/react";
import { type Dispatch, type SetStateAction, useEffect, useRef, useState } from "react";
import { type Dispatch, type SetStateAction, useEffect, useRef } from "react";
import { useLocalStorage } from "react-use";
import themes from "~/themes";

Expand All @@ -13,14 +12,20 @@ function isDark(th: string) {
}

export function useTheme() {
useState();
const [theme, setTheme] = useLocalStorage("theme", "dark") as [string, Dispatch<SetStateAction<string>>, () => void];
const originalTheme = useRef(theme);
const originalTheme = useRef<string | null>(null);

useEffect(() => {
const clazz = [theme, isDark(theme) && "dark"].filter(isTruthy);
document.documentElement.classList.remove(originalTheme.current, "dark");
document.documentElement.classList.add(...clazz);
if (originalTheme.current) {
document.documentElement.classList.remove(originalTheme.current, "dark");
}

console.log(`Adding theme: ${theme}`);
document.documentElement.classList.add(theme);
if (isDark(theme)) {
document.documentElement.classList.add("dark");
}

originalTheme.current = theme;
}, [theme]);

Expand Down

0 comments on commit 4f558ac

Please sign in to comment.