Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color of a selected ItemList Material-Ui

I have created a selectable component using Material-UI

let SelectableInfiniteList = makeSelectable(Infinite);

and put ListItem inside, now I want to change the default grayish color of a selected item, but I don't know how, if I give it a className

<ListItem className="list-item" primaryText={i}/>

and use list-item:focus selector I can change the background color as long as it is focused (but if I click somewhere else in the app) the focus is lost and the gray color shows up on the (still) selected item,

is there a way to change the selected item background color?

like image 948
high incompetance Avatar asked Nov 14 '16 14:11

high incompetance


People also ask

How do I change the arrow color in select material UI?

To change the color of Select component's border and arrow icon with React Material UI, we can use the '&:before' and '&:after' selectors and apply the styles for them. We call makeStyles with a function that returns an object with the select property set to an object with the drop down styles.


3 Answers

I had to use Global Theme override: https://material-ui.com/customization/components/#global-theme-override

const muiTheme = createMuiTheme({
  overrides: {
    MuiListItem: {
      root: {
        "&$selected": {
          backgroundColor: "red",
          "&:hover": {
            backgroundColor: "orange",
          },
        },
      },
      button: {
        "&:hover": {
          backgroundColor: "yellow",
        },
      },
    },
  },
});
like image 160
thisismydesign Avatar answered Oct 19 '22 09:10

thisismydesign


Change default selected gray color by passing selected style like this.

<ListItem
        button
        selected={true}
        classes={{ selected: classes.active }}>
  <ListItemText primary={item.name} />
</ListItem>

Style object should be like this.

active: {
  backgroundColor: "red"
}
like image 28
Amruth Avatar answered Oct 19 '22 11:10

Amruth


Using Material UI v4 and this worked for me:

<ListItem button classes={{ root: classes.listItemRoot }}>
  ...
</ListItem>

with:

const useStyles = makeStyles((theme) => ({
  listItemRoot: {
    "&.Mui-selected": {
        backgroundColor: 'red',
    }
  },
}));

like image 21
chickahoona Avatar answered Oct 19 '22 10:10

chickahoona