idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | import { PaletteMode } from '@mui/material'; |
| 19 | import { ThemeProvider } from '@mui/material/styles'; |
Ziwei Wang | ea4c139 | 2023-01-05 14:50:45 -0500 | [diff] [blame] | 20 | import useMediaQuery from '@mui/material/useMediaQuery'; |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 21 | import { createContext, useCallback, useEffect, useMemo, useState } from 'react'; |
| 22 | |
| 23 | import { buildDefaultTheme } from '../themes/Default'; |
| 24 | import { WithChildren } from '../utils/utils'; |
| 25 | |
| 26 | interface ICustomThemeContext { |
| 27 | mode: PaletteMode; |
| 28 | toggleMode: () => void; |
| 29 | } |
| 30 | |
| 31 | export const CustomThemeContext = createContext<ICustomThemeContext>(undefined!); |
| 32 | |
| 33 | export default ({ children }: WithChildren) => { |
| 34 | const [mode, setMode] = useState<PaletteMode>('light'); |
Ziwei Wang | ea4c139 | 2023-01-05 14:50:45 -0500 | [diff] [blame] | 35 | const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)'); |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 36 | |
| 37 | useEffect(() => { |
Ziwei Wang | cd687a4 | 2023-01-04 16:40:53 -0500 | [diff] [blame] | 38 | const themeModeInStorage = localStorage.getItem('themeMode'); |
| 39 | let themeModeValue = 'light'; |
| 40 | // localStorage returns null if no value is found for the given key |
| 41 | if (typeof themeModeInStorage === 'string') { |
| 42 | themeModeValue = JSON.parse(themeModeInStorage); |
Ziwei Wang | ea4c139 | 2023-01-05 14:50:45 -0500 | [diff] [blame] | 43 | setMode(themeModeValue === 'dark' ? 'dark' : 'light'); |
| 44 | } else { |
| 45 | //when there is no theme mode stored in the localStorage, use the mode in OS preference |
| 46 | setMode(prefersDarkMode ? 'dark' : 'light'); |
Ziwei Wang | cd687a4 | 2023-01-04 16:40:53 -0500 | [diff] [blame] | 47 | } |
Ziwei Wang | ea4c139 | 2023-01-05 14:50:45 -0500 | [diff] [blame] | 48 | }, [prefersDarkMode]); |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 49 | |
Ziwei Wang | cd687a4 | 2023-01-04 16:40:53 -0500 | [diff] [blame] | 50 | const toggleMode = useCallback(() => { |
| 51 | setMode((mode) => { |
| 52 | const newMode = mode === 'light' ? 'dark' : 'light'; |
| 53 | localStorage.setItem('themeMode', JSON.stringify(newMode)); |
| 54 | return newMode; |
| 55 | }); |
| 56 | }, [setMode]); |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 57 | |
| 58 | const theme = useMemo(() => buildDefaultTheme(mode), [mode]); |
| 59 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 60 | const value = useMemo( |
| 61 | () => ({ |
| 62 | mode, |
| 63 | toggleMode, |
| 64 | }), |
| 65 | [mode, toggleMode] |
| 66 | ); |
| 67 | |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 68 | return ( |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 69 | <CustomThemeContext.Provider value={value}> |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 70 | <ThemeProvider theme={theme}>{children}</ThemeProvider> |
| 71 | </CustomThemeContext.Provider> |
| 72 | ); |
| 73 | }; |