Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override Material-UI MenuItem selected background color?

Currently I am struggling with setting the background color of a MenuItem component which is selected to a different color. (without having to using !important to force it)

The component code:

<MenuItem
 classes={{
  root: this.props.classes.root,
  selected: this.props.classes.selected
 }}
 value={10}>
  Alfabetical
</MenuItem>

This is the css:

const homePageStyle = (theme) => ({
  root: {
    width: "300px"
  },
  selected: {
    backgroundColor: "turquoise !important",
    color: "white",
    fontWeight: 600
  }
});

What do I want to achieve?

I would like to set the backgroundColor of the MenuItem without having to set the !important flag. I've done that with plenty of components but this seems not work around at the moment.

I am using version "@material-ui/core": "^1.0.0-rc.0",

like image 825
Kevin Vugts Avatar asked May 16 '18 12:05

Kevin Vugts


People also ask

How do I change the background color in material UI?

To set a background color on Material UI's Paper, you simply need to apply the background-color CSS property to the root element of the Paper. Setting the styles on the root element of any Material UI component can be done in multiple ways, but the most common is to use the useStyles hook.

How do you change the material UI select focus color?

So to overcome this create a custom listItem using withStyles . const CustomMenuItem = withStyles((theme) =>createStyles({ root:{ "&$selected": { backgroundColor: "red", "&:hover": { backgroundColor: "green" } }, '&:hover':{ backgroundColor:'blue' } }, selected:{ } }) )(MenuItem);

How do you override material UI classes?

To override the styles of a specific part of the component, use the global classes provided by Material UI, as described in the previous section "Overriding nested component styles" under the sx prop section.


2 Answers

I just made a working example here

For your selected class to be taken into account, you have to set the selected property of your MenuItem component to true

<MenuItem
  onClick={this.handleClose}
  selected
  classes={{ selected: classes.selected }}
>
like image 188
Arnaud Christ Avatar answered Oct 03 '22 00:10

Arnaud Christ


I'm doing it this way to change the MenuItem background of selected. (selected prop provided by material UI).

export default createMuiTheme({
  overrides: {
    MuiMenuItem: { // For ListItem, change this to MuiListItem
      root: {
        "&$selected": {       // this is to refer to the prop provided by M-UI
          backgroundColor: "black", // updated backgroundColor
        },
      },
    },
  },
});

These are the defaults that can be overridden https://material-ui.com/customization/default-theme/#default-theme

Reference: https://material-ui.com/customization/components/#global-theme-override

Note: I'm using Material-UI version 4.9.11

like image 33
Sandeep Amarnath Avatar answered Oct 03 '22 01:10

Sandeep Amarnath