Merge pull request #1279 from mousumi2002/issues/1262

Fix: Prevent flash on dark theme
This commit is contained in:
Alex
2024-10-08 23:30:00 +01:00
committed by GitHub
2 changed files with 7 additions and 2 deletions

View File

@@ -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>

View File

@@ -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)');
@@ -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;
}