Skip to content

Instantly share code, notes, and snippets.

@praveen001
Created January 11, 2020 02:52
Show Gist options
  • Save praveen001/baacb1e3e952b7c3ae97cd9088bfac8e to your computer and use it in GitHub Desktop.
Save praveen001/baacb1e3e952b7c3ae97cd9088bfac8e to your computer and use it in GitHub Desktop.
Building React App using Material UI with Support for Multiple Switchable Themes
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;
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
};
import { createMuiTheme } from "@material-ui/core";
export const lightTheme = createMuiTheme({
palette: {
type: "dark",
primary: {
main: "#000"
}
}
});
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")
);
import { createMuiTheme } from '@material-ui/core';
export const lightTheme = createMuiTheme({
palette: {
type: 'light',
primary: {
main: '#4167B2'
}
}
});
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;
export const ThemeContext = React.createContext((themeName: string): void => {});
return (
<ThemeContext.Provider value={_setThemeName}>
<MuiThemeProvider theme={theme}>{props.children}</MuiThemeProvider>
</ThemeContext.Provider>
);
// 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);
// 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