使用 props 设置 '&:hover' 背景颜色

mat*_*lev 6 css jsx reactjs material-ui

我正在包装 Material-UI 的芯片组件,以便我可以为道具传递“主要”和“次要”以外的值colors。如果芯片可点击,我还想保持悬停效果,以便当光标位于芯片上方时芯片会转换为不同的颜色。颜色作为 props 传入,因此设置backgroundColor和很容易color

<Chip
  style={{
    backgroundColor: props.backgroundColor,
    color: props.color
  }}
/> 
Run Code Online (Sandbox Code Playgroud)

但是,由于我还想将悬停颜色作为道具传递,因此我需要执行以下操作:

<Chip
  style={{
    backgroundColor: props.backgroundColor,
    color: props.color,
    '&:hover': {
      backgroundColor: props.hoverBackgroundColor,
      color: props.hoverColor
    }
  }}
/> 
Run Code Online (Sandbox Code Playgroud)

但是,&:hover(据我所知)不能在style道具内部使用。通常,&:hover将在传递到 的样式对象内部使用withStyles,但我无法从那里访问道具。有什么建议么?

Rya*_*ell 30

您可以通过创建自己的定制芯片组件来实现这一点。为了能够使用 props 来控制样式,您可以使用makeStyles. 该makeStyles函数返回一个可以接受对象参数的钩子,以便为您的样式提供变量。

这是一个可能的 CustomChip 实现:

import React from "react";
import Chip from "@material-ui/core/Chip";
import { makeStyles } from "@material-ui/core/styles";
import { emphasize } from "@material-ui/core/styles/colorManipulator";

const useChipStyles = makeStyles({
  chip: {
    color: ({ color }) => color,
    backgroundColor: ({ backgroundColor }) => backgroundColor,
    "&:hover, &:focus": {
      backgroundColor: ({ hoverBackgroundColor, backgroundColor }) =>
        hoverBackgroundColor
          ? hoverBackgroundColor
          : emphasize(backgroundColor, 0.08)
    },
    "&:active": {
      backgroundColor: ({ hoverBackgroundColor, backgroundColor }) =>
        emphasize(
          hoverBackgroundColor ? hoverBackgroundColor : backgroundColor,
          0.12
        )
    }
  }
});
const CustomChip = ({
  color,
  backgroundColor,
  hoverBackgroundColor,
  ...rest
}) => {
  const classes = useChipStyles({
    color,
    backgroundColor,
    hoverBackgroundColor
  });
  return <Chip className={classes.chip} {...rest} />;
};
export default CustomChip;
Run Code Online (Sandbox Code Playgroud)

样式方法(包括使用emphasize生成悬停颜色和活动颜色的函数)基于Chip.

然后可以这样使用:

      <CustomChip
        label="Custom Chip 1"
        color="green"
        backgroundColor="#ccf"
        onClick={() => {
          console.log("clicked 1");
        }}
      />
      <CustomChip
        label="Custom Chip 2"
        color="#f0f"
        backgroundColor="#fcc"
        hoverBackgroundColor="#afa"
        onClick={() => {
          console.log("clicked 2");
        }}
      />
Run Code Online (Sandbox Code Playgroud)

这是一个 CodeSandbox 演示了这一点:

编辑芯片颜色(分叉)


以下是该示例的 Material-UI v5 版本:

import Chip from "@material-ui/core/Chip";
import { styled } from "@material-ui/core/styles";
import { emphasize } from "@material-ui/core/styles";
import { shouldForwardProp } from "@material-ui/system";
function customShouldForwardProp(prop) {
  return (
    prop !== "color" &&
    prop !== "backgroundColor" &&
    prop !== "hoverBackgroundColor" &&
    shouldForwardProp(prop)
  );
}
const CustomChip = styled(Chip, { shouldForwardProp: customShouldForwardProp })(
  ({ color, backgroundColor, hoverBackgroundColor }) => ({
    color: color,
    backgroundColor: backgroundColor,
    "&:hover, &:focus": {
      backgroundColor: hoverBackgroundColor
        ? hoverBackgroundColor
        : emphasize(backgroundColor, 0.08)
    },
    "&:active": {
      backgroundColor: emphasize(
        hoverBackgroundColor ? hoverBackgroundColor : backgroundColor,
        0.12
      )
    }
  })
);

export default CustomChip;
Run Code Online (Sandbox Code Playgroud)

编辑芯片颜色