Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change custom theme Material-UI

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?

like image 976
Kumar Avatar asked Aug 31 '18 05:08

Kumar


People also ask

How do you use custom themes in MUI?

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.

How do you override styles in material UI?

If you want to override a component's styles using custom classes, you can use the className prop, available on each component.

How do I add custom colors to material UI?

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.


1 Answers

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:

  • MuiThemeProvider
  • Color

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')
    );

like image 188
Araz Babayev Avatar answered Oct 22 '22 19:10

Araz Babayev