question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can we add new property for color in palette in MuiTheme

See original GitHub issue

Like MuiTheme already provided a way to override colors of 3 property

  • Primary

  • Secondary

  • error

This is the below way to change the colors of existing properties.

palette: {
  primary: {
    main: palette.primary[500],
  },
  secondary: {
    main: palette.secondary.A400,
  },
  error: {
    main: palette.error[500],
  },
},

What If we need to define new property for color like

palette: {
  primary: {
    main: palette.primary[500],
  },
  secondary: {
    main: palette.secondary.A400,
  },
  error: {
    main: palette.error[500],
  },

>   success : {
>     main : "#bac778"
>   }

},

Is that possible ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:38
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

106reactions
oliviertassinaricommented, Mar 2, 2019

@davidcalhoun Yes, you are right, this issue is about extending the Button implementation so people can just do:

const theme = createMuiTheme({
  palette: {
    success: {
      main : "#bac778",
    },
  },
})

then have a working:

<Button color="success" />

implementation.

The best solution right now is to add the color in the theme, then to create a wrapping component:

import React from "react";
import { withStyles, ThemeProvider } from "@material-ui/styles";
import MuiButton from "@material-ui/core/Button";
import { createMuiTheme } from "@material-ui/core/styles";

const Button = withStyles(theme => ({
  root: props =>
    props.color === "success" && props.variant === "contained"
      ? {
          color: theme.palette.success.contrastText,
          backgroundColor: theme.palette.success.main,
          "&:hover": {
            backgroundColor: theme.palette.success.dark,
            // Reset on touch devices, it doesn't add specificity
            "@media (hover: none)": {
              backgroundColor: theme.palette.success.main
            }
          }
        }
      : {}
}))(MuiButton);

const theme = createMuiTheme();

theme.palette.success = theme.palette.augmentColor({
  main: "#689f38"
});

function Theming() {
  return (
    <ThemeProvider theme={theme}>
      <Button variant="contained" color="primary">
        primary
      </Button>
      <Button variant="contained" color="success">
        success
      </Button>
    </ThemeProvider>
  );
}

export default Theming;

https://codesandbox.io/s/7zmw6ox8m1

capture d ecran 2019-03-02 a 19 13 54


By the way, we are planing on extending the color to primary | secondary | success | warning | error/danger? | info. Would that help in your case?

66reactions
kushal-techcommented, Jan 15, 2019

Yes I can but I cannot use it like <Button color="success">Login</Button>

It gives warning about to use only primary default secondary

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can we add new property for color in palette in MuiTheme
No, you can't use the success color property directly. You need to create a wrapping component to use the theme values.
Read more >
How to add custom MUI palette colors - Stack Overflow
The MUI palette is extendable, but you need to do a couple of things to create a new color and apply to your...
Read more >
MUI Custom Colors: How to Customize Colors and Themes in ...
In this tutorial, we will learn how to customize colors in Material UI by creating a new theme and adding a set of...
Read more >
Palette - Material UI - MUI
Adding new colors. You can add new colors inside and outside the palette of the theme as follows:.
Read more >
Customizing the Material-UI Theme Color Palette (MUI v5)
This demo shows how to override palette colors, add new categories to the palette, and create a component that uses the custom colors....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found