Created
December 4, 2021 19:22
-
-
Save Momotoculteur/319c313240d296eb2e537866c39ff813 to your computer and use it in GitHub Desktop.
custom dark light theme switch system for react app build with material ui design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import { ThemeProvider } from '@emotion/react'; | |
import ThemeContext from './ThemeContext' | |
import { LOCAL_STORAGE_KEY, DARK_THEME, LIGHT_THEME } from "./Theme"; | |
function ThemeHandler(props) { | |
const [isDark, setDark] = React.useState(localStorage.getItem(LOCAL_STORAGE_KEY) === 'true'); | |
const ctxValue = { | |
isDark: isDark, | |
toggleTheme: toggleTheme | |
} | |
function toggleTheme (){ | |
setDark(!isDark) | |
localStorage.setItem(LOCAL_STORAGE_KEY, !isDark); | |
} | |
function getTheme(){ | |
if(isDark){ | |
return DARK_THEME; | |
} else { | |
return LIGHT_THEME; | |
} | |
} | |
return ( | |
<ThemeContext.Provider value={ctxValue}> | |
<ThemeProvider theme={getTheme}> | |
{props.children} | |
</ThemeProvider> | |
</ThemeContext.Provider> | |
) | |
} | |
export default ThemeHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment