I am using Material-UI for a React project. However, I'm not sure as to how to apply a theme globally.
Here I have tried for individual components
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import CardCommon from '../../common/card/CardCommon';
import purple from '@material-ui/core/colors/purple';
import Button from '@material-ui/core/Button';
import { Link } from 'react-router-dom';
//const primary = red[500]; // #F44336
import { Paths } from '../../../Routes';
const theme = createMuiTheme({
palette: {
primary: { main: purple[500] }, // Purple and green play nicely together.
secondary: { main: '#11cb5f' }, // This is just green.A700 as hex.
},
});
So how can I change the primary and secondary colors globally?
If you wish to customize the theme, you need to use the ThemeProvider component in order to inject a theme into your application. However, this is optional; MUI components come with a default theme.
If you want to override a component's styles using custom classes, you can use the className prop, available on each component.
You only need to set the main color, as Material-UI will automatically set the light and dark colors for us based on the value of the main color. It will look something like this. }); Please note that in the Material-UI documentation, they show an example of importing purple and green from the Material-UI core package.
You can structure your app like this. Wrap the child components inside a MuiThemeProvider
and pass a createMuiTheme
object to it as a theme
property value.
Also typography: {useNextVariants: true }
fixes the following error:
(Warning: Material-UI: you are using the deprecated typography variants that will be removed in the next major release.
)
The official Material UI docs have more detailed information on this:
Edit the index.js file as follows
import React from 'react';
import ReactDOM from 'react-dom';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import './index.css';
import App from './App';
const theme = createMuiTheme({
palette: {
primary: {
light: '#fff',
main: 'rgb(23, 105, 170)',
dark: '#000'
},
secondary: {
main: '#f44336',
},
},
typography: {
useNextVariants: true
}
});
ReactDOM.render(
<MuiThemeProvider theme = { theme }>
<App />
</MuiThemeProvider>,
document.getElementById('root')
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With