Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change primary color dynamically in MUI theme

There is requirement, I want to give access to user can select their primary color from list like Blue, Orange Green. I have used latest MUI for front-end.

Now I am able to change light to dark theme but my requirement is change primary color also. please help me for same how to code for same.

Please check attached screen:

enter image description here

like image 654
Nitin Shinde Avatar asked Sep 18 '18 04:09

Nitin Shinde


People also ask

How do I change primary color in MUI?

If you want to use a custom color, put it in the main property of an object. MUI will use that color to calculate the dark, light and contrastText values besides the main one. dark , light : a darker and lighter versions of the main color. contrastText : the color of the text if the background color is the main color.

How do you set color in material UI?

Picking colors The Material Design team has also built an awesome palette configuration tool: material.io/resources/color/. This can help you create a color palette for your UI, as well as measure the accessibility level of any color combination.

Is createMuiTheme deprecated?

Developers - createMuiTheme was deprecated -


1 Answers

import React from 'react';
import { render } from 'react-dom';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import Root from './Root';

import lightTheme from 'your-light-theme-path';
import darkTheme from 'your-dark-them-path';

const theme1 = createMuiTheme(lightTheme);
const theme2 = createMuiTheme(darkTheme)

class App extends React.Component {
  state = {
    isThemeLight: true;
  }
  onChange = () => {
    this.setState=({ isThemeLight: false })
  }
  render() {
     const { isThemeLight } = this.state;
     return (
       <MuiThemeProvider theme={isThemeLight ? theme1 : theme2}>
         <Root /> // your app here
         <button onClick={this.onChange}>Change Dark</button>
       </MuiThemeProvider>
     );
  }
}

render(<App />, document.querySelector('#app'));

Where your lightTheme or darkTheme can be a file like this

export default {
  direction: 'ltr',
  palette: {
    type: 'light',
     primary: {
       main: '#37b44e',
     },
     secondary: {
       main: '#000',
     },
  },
};

You can see all the list of theme configurable in Material UI Docs Theme Configuration

Approach 2 (For Theme Change Runtime)

import React from 'react';
import { render } from 'react-dom';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import Root from './Root';

const theme1 = createMuiTheme(lightTheme);
const theme2 = createMuiTheme(darkTheme)

class App extends React.Component {
  state = {
    theme1: {
       palette: {
         type: 'light',
         primary: { main: '#37b44e' },
         secondary: { main: '#000' },
       },
    };
    theme2: {
       palette: {
         type: 'light',
         primary: { main: '#37b44e' },
         secondary: { main: '#000' },
       },
    };
    isThemeLight: true;
  }
  onChange = () => {
    this.setState=({ isThemeLight: false })
  }
  onChangeTheme1 = () => {
    this.setState(({theme1}) => ({
       theme1: { 
        ...theme1,
        primary: { main: 'red' },
       }
    }));
  }
  render() {
     const { theme1, theme2, isThemeLight } = this.state;
     return (
       <MuiThemeProvider 
         theme={isThemeLight ? createMuiTheme(theme1) : createMuiTheme(theme2)}
       >
         <Root /> // your app here
         <button onClick={this.onChange}>Change Dark</button>
         <button onClick={this.onChangeTheme1}>Change Palette Theme 1</button>
       </MuiThemeProvider>
     );
  }
}

render(<App />, document.querySelector('#app'));
like image 59
Adeel Imran Avatar answered Sep 30 '22 07:09

Adeel Imran