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?
To style the disabled button, we can use the :disabled pseudo-class selector in css.
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'
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With