Skip to content

Commit

Permalink
Merge pull request #1279 from mousumi2002/issues/1262
Browse files Browse the repository at this point in the history
Fix: Prevent flash on dark theme
  • Loading branch information
dartpain authored Oct 8, 2024
2 parents 862ea5f + 68f4975 commit 2395785
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function MainLayout() {
}

export default function App() {
useDarkTheme();
const [,,componentMounted] = useDarkTheme();
if(!componentMounted) {
return <div />
}
return (
<div className="h-full relative overflow-auto">
<Routes>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function useDarkTheme() {
};

const [isDarkTheme, setIsDarkTheme] = useState<boolean>(getInitialTheme());
const [componentMounted, setComponentMounted] = useState(false);

useEffect(() => {
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
Expand All @@ -102,11 +103,12 @@ export function useDarkTheme() {
} else {
document.body?.classList.remove('dark');
}
setComponentMounted(true);
}, [isDarkTheme]);

const toggleTheme = () => {
setIsDarkTheme(!isDarkTheme);
};

return [isDarkTheme, toggleTheme] as const;
return [isDarkTheme, toggleTheme, componentMounted] as const;
}

0 comments on commit 2395785

Please sign in to comment.