Created
May 5, 2020 16:17
-
-
Save daniele-zurico/5733b39f3c0c49e0509e2f0451944f93 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 React from 'react'; | |
interface ThemeProps { | |
color: string; | |
backgroundColor: string; | |
} | |
interface Theme { | |
theme: ThemeProps; | |
} | |
const themes = { | |
light: { | |
color: '#504b4b', | |
backgroundColor: '#ffffff', | |
}, | |
dark: { | |
color: '#ffffff', | |
backgroundColor: '#504b4b', | |
}, | |
brown: { | |
color: '#ffffff', | |
backgroundColor: '#795548', | |
}, | |
}; | |
const setLightTheme = () => themes.light; | |
const setDarkTheme = () => themes.dark; | |
const setBrownTheme = () => themes.brown; | |
type Dispatch = (theme: any) => void; | |
const ThemeStateContext = React.createContext<Theme | undefined>(undefined); | |
const ThemeDispatchContext = React.createContext<Dispatch | undefined>(undefined); | |
function ThemeProvider({ children }: any) { | |
const [theme, setTheme] = React.useState({ theme: themes.light }); | |
return ( | |
<ThemeStateContext.Provider value={theme}> | |
<ThemeDispatchContext.Provider value={setTheme}>{children}</ThemeDispatchContext.Provider> | |
</ThemeStateContext.Provider> | |
); | |
} | |
function useThemeState() { | |
const context = React.useContext(ThemeStateContext); | |
if (context === undefined) { | |
throw new Error('useCountState must be used within a CountProvider'); | |
} | |
return context; | |
} | |
function useThemeDispatch() { | |
const context = React.useContext(ThemeDispatchContext); | |
if (context === undefined) { | |
throw new Error('useCountDispatch must be used within a CountProvider'); | |
} | |
return context; | |
} | |
export { ThemeProvider, useThemeState, useThemeDispatch, Theme, setLightTheme, setDarkTheme, setBrownTheme }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment