Created
January 11, 2020 02:52
-
-
Save praveen001/baacb1e3e952b7c3ae97cd9088bfac8e to your computer and use it in GitHub Desktop.
Building React App using Material UI with Support for Multiple Switchable Themes
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 React, { useContext } from 'react'; | |
import { Button } from '@material-ui/core'; | |
import { ThemeContext} from './ThemeProvider'; | |
const App: React.FC = () => { | |
// Get the setter function from context | |
const setThemeName = useContext(ThemeContext) | |
return ( | |
<div className="App"> | |
<Button | |
variant="contained" | |
color="primary" | |
onClick={() => setThemeName("lightTheme")} | |
> | |
Set Light Theme | |
</Button> | |
<Button | |
variant="contained" | |
color="secondary" | |
onClick={() => setThemeName("darkTheme")} | |
> | |
Set Dark Theme | |
</Button> | |
</div> | |
); | |
} | |
export default App; |
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 { Theme } from "@material-ui/core"; | |
import { lightTheme } from "./light"; | |
import { darkTheme } from "./dark"; | |
export function getThemeByName(theme: string): Theme { | |
return themeMap[theme]; | |
} | |
const themeMap: { [key: string]: Theme } = { | |
lightTheme, | |
darkTheme | |
}; |
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 { createMuiTheme } from "@material-ui/core"; | |
export const lightTheme = createMuiTheme({ | |
palette: { | |
type: "dark", | |
primary: { | |
main: "#000" | |
} | |
} | |
}); |
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 React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import App from './App'; | |
import ThemeProvider from './ThemeProvider'; | |
ReactDOM.render( | |
<ThemeProvider> | |
<App /> | |
</ThemeProvider>, | |
document.getElementById("root") | |
); |
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 { createMuiTheme } from '@material-ui/core'; | |
export const lightTheme = createMuiTheme({ | |
palette: { | |
type: 'light', | |
primary: { | |
main: '#4167B2' | |
} | |
} | |
}); |
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 React, { useState } from 'react'; | |
import { MuiThemeProvider } from "@material-ui/core"; | |
import { getThemeByName } from './themes/base'; | |
const ThemeProvider: React.FC = (props) => { | |
// State to hold the selected theme name | |
const [themeName, _setThemeName] = useState('lightTheme'); | |
// Retrieve the theme object by theme name | |
const theme = getThemeByName(themeName); | |
return ( | |
<MuiThemeProvider theme={theme}>{props.children}</MuiThemeProvider> | |
); | |
} | |
export default ThemeProvider; |
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
export const ThemeContext = React.createContext((themeName: string): void => {}); |
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
return ( | |
<ThemeContext.Provider value={_setThemeName}> | |
<MuiThemeProvider theme={theme}>{props.children}</MuiThemeProvider> | |
</ThemeContext.Provider> | |
); |
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
// Read current theme from localStorage or maybe from an api | |
const curThemeName = localStorage.getItem("appTheme") || "lightTheme"; | |
// State to hold the selected theme name | |
const [themeName, _setThemeName] = useState(curThemeName); |
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
// Wrap _setThemeName to store new theme names in localStorage | |
const setThemeName = (themeName: string): void => { | |
localStorage.setItem("appTheme", themeName); | |
_setThemeName(themeName); | |
} | |
return ( | |
<ThemeContext.Provider value={setThemeName}> | |
<MuiThemeProvider theme={theme}>{props.children}</MuiThemeProvider> | |
</ThemeContext.Provider> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment