Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Rombs opened this issue Nov 22, 2018 · 13 comments
Closed

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

Rombs opened this issue Nov 22, 2018 · 13 comments
Labels
component: list This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement

Comments

@Rombs
Copy link

Rombs commented Nov 22, 2018

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.
@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Nov 22, 2018
@oliviertassinari
Copy link
Member

oliviertassinari commented Nov 22, 2018

@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.

@laurance-nguyen

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@TidyIQ

This comment has been minimized.

@ryancogswell

This comment has been minimized.

@TidyIQ

This comment has been minimized.

@ndeviant

This comment has been minimized.

@ryancogswell

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@tommybernaciak

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@oliviertassinari oliviertassinari added the component: list This is the name of the generic UI component, not the React module! label Aug 15, 2020
@rofrol
Copy link

rofrol commented Mar 17, 2021

That worked for me to also change background color when on no-hover device:

import { withStyles, ListItem as MuiListItem } from '@material-ui/core'

export const ListItem = withStyles({
  root: {
    backgroundColor: '#fafafa',
    '&:not(:last-child)': {
      borderBottom: '1px solid rgba(0, 0, 0, 0.12)',
    },
    '@media (hover: none)': {
      '&:hover': {
        backgroundColor: '#fafafa',
      },
    },
  },
})(MuiListItem)

@JamieShelley
Copy link

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

*withStyles-> now resides in : import { withStyles } from '@mui/styles';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: list This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

9 participants