Home > OS >  How to override material-ui disabled button style keeping the background color provided to the Butto
How to override material-ui disabled button style keeping the background color provided to the Butto

Time:09-27

I want to change the global style of the disabled Button component from Material-ui. But the problem is I am unable to keep the original color scheme of the button.

Consider this button:

<Button color="secondary" disabled={isLoading}>Create Account</Button>

Now by default Mui-disabled would be attached to this button. Whose color and the background-color are taken from theme.palatte.action property. So this disabled button would be having CSS as:

color: rgba(0,0,0,0.26);
box-shadow: none;
background-color: rgba(0,0,0,0.12);

But I want my disabled button to maintain its original color ("primary, secondary", "error" etc.) added with an opacity of 0.7. By default, the cursor events are set to none by MUI.

I tried it using the custom theme but I don't know how to maintain the original color of the button. E.g if the Button is primary keep the primary color, if the Button is secondary keep the secondary colors.

    MuiButton: {
        styleOverrides: {
            root: {
                textTransform: "none",
                boxShadow: "none",
                "&.Mui-disabled": {
                    // background: "initial",
                    // color: "initial",
                    opacity: .7
                }
            },
        }
    }

Of course, I don't want to write a custom code for each code. I can do this even by creating a wrapper around the MUI's button and use that wrapper everywhere in my code. But I want to do it the MUI way by overriding the theme.

How can I implement a global solution?

CodePudding user response:

With the way that the disabled state is handled in the default styles, I think you need to redefine the default colors within your override in order for it to work.

In the source code you can find how the default colors are defined for the Edit ColorButtons override disabled colors

CodePudding user response:

Is this what you want? just don't set the disabledBackground color in the palette:

import Button, { buttonClasses } from "@mui/material/Button";
import { createTheme, ThemeProvider } from "@mui/material/styles";

const defaultTheme = createTheme();
const theme = createTheme({
  palette: {
    action: {
      disabledBackground: "", // don't set the disable background color
      disabled: "white", // set the disable foreground color
    }
  },
  components: {
    MuiButtonBase: {
      styleOverrides: {
        root: {
          [`&.${buttonClasses.disabled}`]: {
            opacity: 0.5
          },
          // Fix ButtonGroup disabled styles.
          [`&.${toggleButtonClasses.root}.${buttonClasses.disabled}`]: {
            color: defaultTheme.palette.action.disabled,
            borderColor: defaultTheme.palette.action.disabledBackground
          }
        }
      }
    }
  }
);

At the current time I'm writing. There is also other Codesandbox Demo

  • Related