|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | + |
| 19 | +import React, { createContext, useContext, useState, useEffect } from 'react' |
| 20 | +import { ThemeProvider } from '@mui/material/styles' |
| 21 | +import { CssBaseline } from '@mui/material' |
| 22 | +import { lightTheme, darkTheme } from '../theme/themes' |
| 23 | + |
| 24 | +type ThemeMode = 'light' | 'dark' | 'system' |
| 25 | + |
| 26 | +const ThemeContext = createContext<{ |
| 27 | + themeMode: ThemeMode |
| 28 | + setThemeMode: (mode: ThemeMode) => void |
| 29 | +}>({ |
| 30 | + themeMode: 'system', |
| 31 | + setThemeMode: () => {} |
| 32 | +}) |
| 33 | + |
| 34 | +export const useTheme = () => useContext(ThemeContext) |
| 35 | + |
| 36 | +export const CustomThemeProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { |
| 37 | + const [themeMode, setThemeMode] = useState<ThemeMode>('system') |
| 38 | + const [systemPrefersDark, setSystemPrefersDark] = useState(false) |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (typeof window !== 'undefined' && window.localStorage) { |
| 42 | + const saved = localStorage.getItem('theme-mode') as ThemeMode |
| 43 | + if (saved) setThemeMode(saved) |
| 44 | + } |
| 45 | + if (typeof window !== 'undefined' && window.matchMedia) { |
| 46 | + setSystemPrefersDark(window.matchMedia('(prefers-color-scheme: dark)').matches) |
| 47 | + } |
| 48 | + }, []) |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + if (typeof window !== 'undefined' && window.localStorage) { |
| 52 | + localStorage.setItem('theme-mode', themeMode) |
| 53 | + } |
| 54 | + }, [themeMode]) |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + if (typeof window !== 'undefined' && window.matchMedia) { |
| 58 | + const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)') |
| 59 | + const handler = (e: MediaQueryListEvent) => setSystemPrefersDark(e.matches) |
| 60 | + mediaQuery.addEventListener('change', handler) |
| 61 | + return () => mediaQuery.removeEventListener('change', handler) |
| 62 | + } |
| 63 | + }, []) |
| 64 | + |
| 65 | + const isDark = themeMode === 'dark' || (themeMode === 'system' && systemPrefersDark) |
| 66 | + const currentTheme = isDark ? darkTheme : lightTheme |
| 67 | + |
| 68 | + return ( |
| 69 | + <ThemeContext.Provider value={{ themeMode, setThemeMode }}> |
| 70 | + <ThemeProvider theme={currentTheme}> |
| 71 | + <CssBaseline /> |
| 72 | + {children} |
| 73 | + </ThemeProvider> |
| 74 | + </ThemeContext.Provider> |
| 75 | + ) |
| 76 | +} |
0 commit comments