Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between main, light and dark variances of a Material-UI Palette

Assuming I am using a Material-UI palette as follows:

  palette: {
    primary: {
      main: "#039be5",
      light: "#63ccff",
      dark: "#C218006db35B",
      contrastText: "#fafafa"
    },
    secondary: {
      main: "#f50057",
      light: "#ff5983",
      dark: "#bb002f",
      contrastText: "#f9fbe7"
    },
    error: {
      main: "#f50057",
      light: "#ff5983",
      dark: "#bb002f",
      contrastText: "#f9fbe7"
    }
  },

And let's say that I am using a number of Material-UI components such as the <AppBar />, <Button />, etc. and each of them I want to give them different accents of the primary palette object - ex. <AppBar /> will be primary.main and <Button /> will be primary.light.

How can I do it?

Using something like <AppBar position="static" color="primary.light"> does not work, and throws an error.

like image 364
James Avatar asked May 22 '18 15:05

James


People also ask

How do I change primary and secondary color in UI?

const theme = createMuiTheme({ palette: { primary: { main: "#ff8f00" // This is an orange looking color }, secondary: { main: "#ffcc80" //Another orange-ish color } },fontFamily: font // as an aside, highly recommend importing roboto font for Material UI projects!

How do I change to dark theme in material UI?

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 .

What is primary and secondary in material UI?

primary - used to represent primary interface elements for a user. It's the color displayed most frequently across your app's screens and components. secondary - used to represent secondary interface elements for a user. It provides more ways to accent and distinguish your product.


1 Answers

Unfortunately the docs are unclear. Here is the only hacky work-around I found:

<AppBar position="static" className="primaryLight">

const styles = theme => ({
  primaryLight: {
    backgroundColor: theme.palette.primary.light
  }
}

This library is convoluted... at least it's consistent with React!

like image 61
awc737 Avatar answered Sep 20 '22 15:09

awc737