1
|
import { PaletteMode } from '@mui/material'
|
2
|
import { createTheme, Theme, ThemeProvider } from '@mui/material/styles'
|
3
|
import { FunctionComponent, ReactNode, useEffect, useState } from 'react'
|
4
|
import { useSelector } from 'react-redux'
|
5
|
import { RootState } from '../redux/store'
|
6
|
|
7
|
export interface ThemeWrapperProps {
|
8
|
children: ReactNode
|
9
|
}
|
10
|
|
11
|
const ThemeWrapper: FunctionComponent<ThemeWrapperProps> = ({ children }) => {
|
12
|
const lightThemePalette = {
|
13
|
primary: {
|
14
|
main: '#A28253',
|
15
|
},
|
16
|
secondary: {
|
17
|
main: '#E3DBCF',
|
18
|
},
|
19
|
}
|
20
|
|
21
|
const darkThemePalette = {
|
22
|
primary: {
|
23
|
main: '#e0bb84',
|
24
|
},
|
25
|
secondary: {
|
26
|
main: '#E3DBCF',
|
27
|
},
|
28
|
}
|
29
|
|
30
|
const getPalette = (paletteMode: PaletteMode) => {
|
31
|
switch (paletteMode) {
|
32
|
case 'light':
|
33
|
return lightThemePalette
|
34
|
case 'dark':
|
35
|
return darkThemePalette
|
36
|
default:
|
37
|
return lightThemePalette
|
38
|
}
|
39
|
}
|
40
|
|
41
|
const buildTheme = (paletteMode: PaletteMode) =>
|
42
|
createTheme({
|
43
|
palette: {
|
44
|
mode: paletteMode,
|
45
|
...getPalette(paletteMode),
|
46
|
},
|
47
|
shape: {
|
48
|
borderRadius: 8,
|
49
|
},
|
50
|
typography: {
|
51
|
fontFamily: [
|
52
|
'-apple-system',
|
53
|
'BlinkMacSystemFont',
|
54
|
'"Segoe UI"',
|
55
|
'Roboto',
|
56
|
'"Helvetica Neue"',
|
57
|
'Arial',
|
58
|
'sans-serif',
|
59
|
'"Apple Color Emoji"',
|
60
|
'"Segoe UI Emoji"',
|
61
|
'"Segoe UI Symbol"',
|
62
|
].join(','),
|
63
|
},
|
64
|
})
|
65
|
|
66
|
const paletteMode = useSelector(
|
67
|
(state: RootState) => state.theme.paletteMode
|
68
|
)
|
69
|
|
70
|
const [theme, setTheme] = useState<Theme>(buildTheme(paletteMode))
|
71
|
useEffect(() => {
|
72
|
setTheme(() => {
|
73
|
return buildTheme(paletteMode)
|
74
|
})
|
75
|
}, [paletteMode])
|
76
|
|
77
|
return <ThemeProvider theme={theme}>{children}</ThemeProvider>
|
78
|
}
|
79
|
|
80
|
export default ThemeWrapper
|