Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i set primary light/dark color to components of Material-ui ? i am using custom theme like here

here is link of docs of how to customize theme

The above link has Object that we can configure the theme object. But default it take primary-main color, what if i want to access primary-dark . how to access primary dark ?

like image 568
Montu Patel Avatar asked Feb 22 '18 10:02

Montu Patel


People also ask

How do you use primary light color in MUI?

Make a class that specifies the colour you want and provide the hex colour code as the background colour. (also not ideal). Make a JSX class using makeStyles that takes the app's theme as an argument and provide the primary. light colour directly from your theme.

How do I set material UI to dark mode?

Adding <CssBaseline /> inside of the <ThemeProvider> component will also enable dark mode for the app's background. Note: setting the dark mode this way only works if you are using the default palette. If you have a custom palette, make sure that you have the correct values based on the mode .

How do I change primary and secondary color in material UI?

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 I customize colors 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.


1 Answers

You could set the dark theme to default like this:

// ... imports ... 
const theme = createMuiTheme({
  palette: {
    type: 'dark',
  }
});

ReactDOM.render(
  <MuiThemeProvider theme={theme}>
    <App />
  </MuiThemeProvider>,
  document.getElementById('root')
);

For each theme you have primary and secondary colors. For primary e.g. primary.light, primary.main and primary.dark.

In your component, you can access the theme variables like this:

// ... imports ... 
const styles = theme => ({
  darkColor: {
    color: theme.palette.primary.dark // or theme.palette.primary.main
  } 
})

const StatelessMyComponent = ({ classes }) => 
  <div className={classes.darkColor}>Look at my dark color! :)</div>;

export withStyles(styles)(StatelessMyComponent);
like image 99
wiesson Avatar answered Oct 21 '22 17:10

wiesson