mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-29 11:23:57 +01:00
fix: wrap watcher into useEffect
This commit is contained in:
parent
8130de23ff
commit
1ee6b5f974
@ -1,7 +1,7 @@
|
|||||||
import themes from 'themes/index';
|
import themes from 'themes/index';
|
||||||
import useLocalStorage from 'hooks/useLocalStorage/index';
|
import useLocalStorage from 'hooks/useLocalStorage/index';
|
||||||
|
|
||||||
import { createContext, useContext, useState } from 'react';
|
import { createContext, useContext, useEffect, useState } from 'react';
|
||||||
import { ThemeProvider as SCThemeProvider } from 'styled-components';
|
import { ThemeProvider as SCThemeProvider } from 'styled-components';
|
||||||
|
|
||||||
export const ThemeContext = createContext();
|
export const ThemeContext = createContext();
|
||||||
@ -10,10 +10,12 @@ export const ThemeProvider = (props) => {
|
|||||||
const [displayedTheme, setDisplayedTheme] = useState(isBrowserThemeLight ? 'light' : 'dark');
|
const [displayedTheme, setDisplayedTheme] = useState(isBrowserThemeLight ? 'light' : 'dark');
|
||||||
const [storedTheme, setStoredTheme] = useLocalStorage('bruno.theme', 'system');
|
const [storedTheme, setStoredTheme] = useLocalStorage('bruno.theme', 'system');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', (e) => {
|
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', (e) => {
|
||||||
if (storedTheme !== 'system') return;
|
if (storedTheme !== 'system') return;
|
||||||
setDisplayedTheme(e.matches ? 'light' : 'dark');
|
setDisplayedTheme(e.matches ? 'light' : 'dark');
|
||||||
});
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
const theme = storedTheme === 'system' ? themes[displayedTheme] : themes[storedTheme];
|
const theme = storedTheme === 'system' ? themes[displayedTheme] : themes[storedTheme];
|
||||||
const themeOptions = Object.keys(themes);
|
const themeOptions = Object.keys(themes);
|
||||||
|
Loading…
Reference in New Issue
Block a user