Skip to content

Commit

Permalink
Fix dark theme on initial page load (astral-sh#13077)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Aug 23, 2024
1 parent 1f2cb09 commit 2d5fe9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 0 additions & 2 deletions playground/src/Editor/Chrome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ async function startPlayground(): Promise<{
await initRuff();
const monaco = await loader.init();

console.log(monaco);

setupMonaco(monaco);

const response = await restore();
Expand Down
22 changes: 14 additions & 8 deletions playground/src/Editor/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import { useState } from "react";
export type Theme = "dark" | "light";

export function useTheme(): [Theme, (theme: Theme) => void] {
const [localTheme, setLocalTheme] = useState<Theme>(() =>
detectInitialTheme(),
);
const [localTheme, setLocalTheme] = useState<Theme>(() => {
const theme = detectInitialTheme();
toggleTheme(theme);
return theme;
});

const setTheme = (mode: Theme) => {
if (mode === "dark") {
document.body.classList.add("dark");
} else {
document.body.classList.remove("dark");
}
toggleTheme(mode);
localStorage.setItem("theme", mode);
setLocalTheme(mode);
};
Expand All @@ -35,3 +33,11 @@ function detectInitialTheme(): Theme {
return "light";
}
}

function toggleTheme(theme: Theme) {
if (theme === "dark") {
document.body.classList.add("dark");
} else {
document.body.classList.remove("dark");
}
}

0 comments on commit 2d5fe9a

Please sign in to comment.