Fix initial system theme
This commit is contained in:
parent
d170b3e792
commit
8acf3f6ba6
1 changed files with 8 additions and 5 deletions
|
|
@ -31,11 +31,14 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||||
// Initialize theme as early as possible to prevent flash
|
// Initialize theme as early as possible to prevent flash
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
const stored = localStorage.getItem('theme') as Theme;
|
const stored = localStorage.getItem('theme') as Theme;
|
||||||
if (stored && THEMES.includes(stored)) {
|
const initialTheme = stored && THEMES.includes(stored) ? stored : 'system';
|
||||||
setTheme(stored);
|
setTheme(initialTheme);
|
||||||
const effectiveTheme = getEffectiveTheme(stored);
|
const effectiveTheme = getEffectiveTheme(initialTheme);
|
||||||
document.documentElement.classList.add(effectiveTheme);
|
document.documentElement.classList.remove(...THEMES);
|
||||||
document.documentElement.style.colorScheme = effectiveTheme;
|
document.documentElement.classList.add(effectiveTheme);
|
||||||
|
document.documentElement.style.colorScheme = effectiveTheme;
|
||||||
|
if (!stored) {
|
||||||
|
localStorage.setItem('theme', initialTheme);
|
||||||
}
|
}
|
||||||
setMounted(true);
|
setMounted(true);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue