question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ListItem] Custom background color always has overridden by MUI style

See original GitHub issue

When I’m trying override background color of root node for ListItem( the attribute selected = {true}), MUI is overriding it by default class from theme (theme.palette.action.selected). I’d like to know is it normal behaviour or is it a bug (I has fixed it in my local repo and can make pull request)? Thx

  • [v] This is not a v0.x issue.
  • [v] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

I’d like that background color of ListItem was ‘red’.

Current Behavior 😯

The background color of ListItem is ‘rgba(0, 0, 0, 0.14)’.

Steps to Reproduce 🕹

import List from '@material-ui/core/List'
import ListItem from '@material-ui/core/ListItem'
import { withStyles } from '@material-ui/core/styles'

const styles = theme => ({
  root: {
  },
  selected: {
    backgroundColor: 'red',
  },
})

function Demo(props) {
  const { classes } = props
  return (
    <List component="nav">
      <ListItem selected={true}
        classes={{
          selected: classes.selected
      }}>
        TEST
      </ListItem>
    </List>
  )
}

export default withStyles(styles)(Demo)

Edit p9ll5rw7w7

demo

Context 🔦

Your Environment 🌎

Tech Version
Material-UI v3.5.1
React 16.6.3
Browser Chrome, Safari
TypeScript No
etc.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:22
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

42reactions
oliviertassinaricommented, Aug 15, 2020

@Rombs The correct solution is the following:

import React from "react";
import { List, ListItem, withStyles } from "@material-ui/core";

const StyledListItem = withStyles({
  root: {
    backgroundColor: "blue",
    "&.Mui-selected": {
      backgroundColor: "red"
    }
  },
})(ListItem);

function Demo() {
  return (
    <List component="nav">
      <StyledListItem>default</StyledListItem>
      <StyledListItem selected>selected pseudo-class</StyledListItem>
    </List>
  );
}

export default Demo;

https://codesandbox.io/s/pk5w045pp7?file=/src/demo.js

Capture d’écran 2019-05-03 à 18 02 46

You can learn more in https://material-ui.com/customization/components/#pseudo-classes.

11reactions
oliviertassinaricommented, Aug 15, 2020

With v4.0.0-beta.1 you can do:

import React from "react";
import { List, ListItem, styled } from "@material-ui/core";

const StyledListItem = styled(ListItem)({
  backgroundColor: "blue",
  "&.Mui-selected": {
    backgroundColor: "red"
  }
});

function Demo() {
  return (
    <List component="nav">
      <StyledListItem>default</StyledListItem>
      <StyledListItem selected>selected pseudo-class</StyledListItem>
    </List>
  );
}

export default Demo;

https://codesandbox.io/s/xwor26oqo

Capture d’écran 2019-05-03 à 18 02 46

cc @iossocket @ShogunRoss @iwknow @chpinan1128 @ndibek @jay-almaraz @HorizonShadow @TidyIQ I would love to learn more what would be a great solution for you guys.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[ListItem] Custom background color always has overridden by ...
When I'm trying override background color of root node for ListItem( the attribute selected = {true}), MUI is overriding it by default class ......
Read more >
How to change background color of a selected ItemList ...
const muiTheme = createMuiTheme({ overrides: { MuiListItem: { root: ... Change default selected gray color by passing selected style like this. <ListItem ......
Read more >
How to customize - Material UI - MUI
Learn how to customize Material UI components by taking advantage of different strategies for specific use cases.
Read more >
Overrides - Material-UI
The first way to override the style of a component is to use class names. Every component provides a className property which is...
Read more >
Style library interoperability - Material UI - MUI
Note: If you are using Emotion and have a custom cache in your app, that one will override the one coming from MUI....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found