Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Material UI: How to give a button a custom color when disabled?

I'm building a React App using Material UI.

If the button is disabled, it is grey and opaque. I'd like it to be in my themes primary color and opaque.

So here is what I tried:

import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";

const styles = theme => ({
  button: {
    ":disabled": {
      backgroundColor: theme.palette.primary || 'red'
    }
  }
});

function ContainedButtons(props) {
  const { classes } = props;
  return (
    <div>
      <Button
        variant="contained"
        color="secondary"
        disabled
        className={classes.button}
      >
        Disabled
      </Button>
    </div>
  );
}

ContainedButtons.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(ContainedButtons);

But the button stays grey. How can you change the style of a disabled element?

like image 229
J. Hesters Avatar asked Apr 09 '19 06:04

J. Hesters


People also ask

How do you style a disabled button in React?

To style the disabled button, we can use the :disabled pseudo-class selector in css.


2 Answers

Or you can try createMuiTheme and customize the following property:

import { createMuiTheme } from '@material-ui/core/styles'

const theme = createMuiTheme({
  palette: {
    action: {
      disabledBackground: 'set color of background here',
      disabled: 'set color of text here'
    }
  }
}
like image 59
Francis Nepomuceno Avatar answered Oct 07 '22 05:10

Francis Nepomuceno


You can define a class to be applied for disabled buttons:

const styles = theme => ({
  disabledButton: {
    backgroundColor: theme.palette.primary || 'red'
  }
});

And then, use it like this:

<Button
  variant="contained"
  color="secondary"
  disabled
  classes={{ disabled: classes.disabledButton }}
>
  Disabled
</Button>

You can find in the documentation all the classes that you can override.

like image 32
Arnaud Christ Avatar answered Oct 07 '22 05:10

Arnaud Christ