css - 如何在 Material UI 中设置 ListItem 的选择和悬停颜色?

标签 css reactjs material-ui

无法获得“选定”或“悬停颜色”以适用于 ListItem。对于选定的尝试设置其类,如:

<ListItem selected button key="home" classes={{ selected: classes.listItemSelected }} >
            <ListItemText primary="Hi"/>
/ListItem>

然后将样式设置为:
const useStyles = makeStyles((theme) => ({

  listItemSelected:{
    backgroundColor: "#ff0000",
  },

}));

但它没有做任何事情,此处的 ListItem 组件 API 中描述了“选定”:https://material-ui.com/api/list-item/

您如何为 ListItem 设置 选择的 悬停 的颜色?

最佳答案

下面是 default ListItem styles 处理背景颜色的部分:

export const styles = (theme) => ({
  /* Styles applied to the (normally root) `component` element. May be wrapped by a `container`. */
  root: {
    '&$focusVisible': {
      backgroundColor: theme.palette.action.selected,
    },
    '&$selected, &$selected:hover': {
      backgroundColor: theme.palette.action.selected,
    },
    '&$disabled': {
      opacity: 0.5,
    },
  },
  /* Pseudo-class applied to the `component`'s `focusVisibleClassName` prop if `button={true}`. */
  focusVisible: {},
  /* Styles applied to the inner `component` element if `button={true}`. */
  button: {
    transition: theme.transitions.create('background-color', {
      duration: theme.transitions.duration.shortest,
    }),
    '&:hover': {
      textDecoration: 'none',
      backgroundColor: theme.palette.action.hover,
      // Reset on touch devices, it doesn't add specificity
      '@media (hover: none)': {
        backgroundColor: 'transparent',
      },
    },
  },
  /* Pseudo-class applied to the root element if `selected={true}`. */
  selected: {},
});
需要注意的重要一点是,所选样式是通过两个类( rootselected )的组合完成的,因此如果您尝试使用单个类覆盖它,您将没有足够的 specificity
下面是一个示例,显示了一种覆盖选定和悬停状态的方法:
import React from "react";
import { makeStyles, withStyles } from "@material-ui/core/styles";
import List from "@material-ui/core/List";
import MuiListItem from "@material-ui/core/ListItem";
import ListItemIcon from "@material-ui/core/ListItemIcon";
import ListItemText from "@material-ui/core/ListItemText";
import Divider from "@material-ui/core/Divider";
import InboxIcon from "@material-ui/icons/Inbox";
import DraftsIcon from "@material-ui/icons/Drafts";

const useStyles = makeStyles((theme) => ({
  root: {
    width: "100%",
    maxWidth: 360,
    backgroundColor: theme.palette.background.paper
  }
}));

const ListItem = withStyles({
  root: {
    "&$selected": {
      backgroundColor: "red",
      color: "white",
      "& .MuiListItemIcon-root": {
        color: "white"
      }
    },
    "&$selected:hover": {
      backgroundColor: "purple",
      color: "white",
      "& .MuiListItemIcon-root": {
        color: "white"
      }
    },
    "&:hover": {
      backgroundColor: "blue",
      color: "white",
      "& .MuiListItemIcon-root": {
        color: "white"
      }
    }
  },
  selected: {}
})(MuiListItem);

export default function SelectedListItem() {
  const classes = useStyles();
  const [selectedIndex, setSelectedIndex] = React.useState(1);

  const handleListItemClick = (event, index) => {
    setSelectedIndex(index);
  };

  return (
    <div className={classes.root}>
      <List component="nav" aria-label="main mailbox folders">
        <ListItem
          button
          selected={selectedIndex === 0}
          onClick={(event) => handleListItemClick(event, 0)}
        >
          <ListItemIcon>
            <InboxIcon />
          </ListItemIcon>
          <ListItemText primary="Inbox" />
        </ListItem>
        <ListItem
          button
          selected={selectedIndex === 1}
          onClick={(event) => handleListItemClick(event, 1)}
        >
          <ListItemIcon>
            <DraftsIcon />
          </ListItemIcon>
          <ListItemText primary="Drafts" />
        </ListItem>
      </List>
      <Divider />
      <List component="nav" aria-label="secondary mailbox folder">
        <ListItem
          button
          selected={selectedIndex === 2}
          onClick={(event) => handleListItemClick(event, 2)}
        >
          <ListItemText primary="Trash" />
        </ListItem>
        <ListItem
          button
          selected={selectedIndex === 3}
          onClick={(event) => handleListItemClick(event, 3)}
        >
          <ListItemText primary="Spam" />
        </ListItem>
      </List>
    </div>
  );
}
Edit ListItem selected and hover
相关回答:
  • How to overried the selected classes in menuItem in material ui REACTjs?
  • How to change the styles of ListItem element with the "onclick" event?
  • 关于css - 如何在 Material UI 中设置 ListItem 的选择和悬停颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61486061/

    相关文章:

    css - 如何使用css选择li中的文本

    html - 导航栏背景图像仍然被背景颜色覆盖

    reactjs - React Material UI + Formik FieldArray 自动完成控制值在移除时保持不变

    html - 创建一个带有标签的垂直分隔符

    css - Bootstrap 布局媒体查询在 Chrome 上无法以 767px 运行

    javascript - React.js - 输入在重新渲染时失去焦点

    javascript - “this”不是代码分割中的函数

    javascript - 当我的数据是 bool 值时,在 mui-datatables 的 switch 中显示表数据

    javascript - 仅在生产环境中使用 Gatsby 和 Material UI 时,首次访问页面时 HTML 消失

    javascript - React setState() 更新浏览器中的所有组件