blob: 63f4e8124d471bfd8cb400892c4a7e1db1ce3974 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import { ReactNode, createContext, useMemo, useState } from 'react';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import useMediaQuery from '@mui/material/useMediaQuery';
type ThemeContextType = {
toggleColorMode: () => void;
}
type ThemeProviderProps = {
children: ReactNode;
}
export const ThemeContext = createContext<ThemeContextType>({
toggleColorMode: () => {}
});
function ThemeContextProvider({ children }: ThemeProviderProps) {
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)');
const [mode, setMode] = useState<'light' | 'dark'>(
prefersDarkMode ? 'dark' : 'light'
);
const toggleColorMode = () => {
setMode((prevMode) => (prevMode === 'light' ? 'dark' : 'light'));
};
const theme = useMemo(
() =>
createTheme({
palette: {
mode,
}
}),
[mode]
);
return (
<ThemeContext.Provider value={{ toggleColorMode }}>
<ThemeProvider theme={theme}>
{children}
</ThemeProvider>
</ThemeContext.Provider>
);
}
export default ThemeContextProvider;
|