【问题标题】:Material UI Custom Hover Color材质 UI 自定义悬停颜色
【发布时间】:2021-05-21 17:18:48
【问题描述】:

之前没有做过这个功能,你可以改变按钮的悬停颜色。

我已经制作了一个功能,可以使用颜色选择器通过滑块、背景颜色和字体颜色更改半径。但是,我注意到悬停(用于背景和字体)可能会更好。

代码如下:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Grid from "@material-ui/core/Grid";
import Slider from "@material-ui/core/Slider";
import Input from "@material-ui/core/Input";
import Button from "@material-ui/core/Button";
import { ChromePicker } from "react-color";

const useStyles = makeStyles((theme) => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  },
  Button: {
    width: 150,
    height: 50,
    borderRadius: "var(--borderRadius)"
  },
  color: {
    width: "36px",
    height: "14px",
    borderRadius: "2px"
  },
  swatch: {
    padding: "5px",
    background: "#fff",
    borderRadius: "1px",
    display: "inline-block",
    cursor: "pointer"
  },
  popover: {
    position: "absolute",
    zIndex: "2"
  },
  cover: {
    position: "fixed",
    top: "0px",
    right: "0px",
    bottom: "0px",
    left: "0px"
  }
}));

export default function InputSlider() {
  const classes = useStyles();
  const [value, setValue] = React.useState(30);
  const [color, setColor] = React.useState({ r: 0, g: 0, b: 0, a: 1 });

  const [fontColor, setFontColor] = React.useState({
    r: 255,
    g: 255,
    b: 255,
    a: 1
  });
  const [displayColorPicker, setDisplayColorPicker] = React.useState(true);

  const handleSliderChange = (event, newValue) => {
    setValue(newValue);
  };

  const handleInputChange = (event) => {
    setValue(event.target.value === "" ? "" : Number(event.target.value));
  };

  const handleBlur = () => {
    if (value < 0) {
      setValue(0);
    } else if (value > 30) {
      setValue(30);
    }
  };

  const handleClick = () => {
    setDisplayColorPicker(!displayColorPicker);
  };

  const handleClose = () => {
    setDisplayColorPicker(false);
  };

  const handleChange = (color) => {
    setColor(color.rgb);
  };

  const handleFontColorChange = (color) => {
    setFontColor(color.rgb);
  };

  return (
    <div className={classes.root}>
      <style>
        {`:root {
          --borderRadius = ${value}px;
        }`}
      </style>
      <Button
        style={{
          borderRadius: value,
          background: `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`,
          color: `rgba(${fontColor.r}, ${fontColor.g}, ${fontColor.b}, ${fontColor.a})`
        }}
        variant="contained"
        color="primary"
        value="value"
        onChange={handleSliderChange}
        className={classes.Button}
      >
        Fire laser
      </Button>

      <Grid container spacing={2}>
        <Grid item xs>
          <Slider
            value={typeof value === "number" ? value : 0}
            onChange={handleSliderChange}
            aria-labelledby="input-slider"
          />
        </Grid>
        <Grid item>
          <Input
            value={value}
            margin="dense"
            onChange={handleInputChange}
            onBlur={handleBlur}
            inputProps={{
              step: 10,
              min: 0,
              max: 24,
              type: "number"
            }}
          />
        </Grid>
      </Grid>
      <div>
        <div style={useStyles.swatch} onClick={handleClick}>
          {displayColorPicker} <p class="h4">Background</p>
          <div style={useStyles.color} />
        </div>

        {displayColorPicker ? (
          <div style={useStyles.popover}>
            <div style={useStyles.cover} onClick={handleClose}></div>
            <ChromePicker color={color} onChange={handleChange} />
          </div>
        ) : null}
      </div>

      <div>
        <div style={useStyles.swatch} onClick={handleClick}>
          {displayColorPicker} <p class="h4">Font</p>
          <div style={useStyles.color} />
        </div>

        {displayColorPicker ? (
          <div style={useStyles.popover}>
            <div style={useStyles.cover} onClick={handleClose}></div>
            <ChromePicker color={fontColor} onChange={handleFontColorChange} />
          </div>
        ) : null}
      </div>
    </div>
  );
}

这里是沙盒 - https://codesandbox.io/s/material-demo-forked-t8xut?file=/demo.js

有什么建议吗?

有没有人有一篇很好的 Material UI 文章来编辑/很酷的功能和项目来玩?

【问题讨论】:

  • 请检查我的答案,让我知道它是否适合你:)
  • 完美!非常感谢你,星期五快乐!
  • 谢谢!也适合你

标签: reactjs material-ui hover color-picker


【解决方案1】:

您需要将道具传递给makeStyles。 首先,在声明类时传递fontColor变量如下:

const classes = useStyles({ hoverBackgroundColor, hoverFontColor })();

然后在useStyles中,你可以访问fontColor作为prop,如下:

const useStyles = ({ hoverBackgroundColor, hoverFontColor }) =>
makeStyles((theme) => ({
Button: {
  width: 150,
  height: 50,
  borderRadius: "var(--borderRadius)",
  "&:hover": {
    backgroundColor: `rgba(${hoverBackgroundColor.r}, ${hoverBackgroundColor.g}, ${hoverBackgroundColor.b}, ${hoverBackgroundColor.a}) !important`,
    color: `rgba(${hoverFontColor.r}, ${hoverFontColor.g}, ${hoverFontColor.b}, ${hoverFontColor.a}) !important`
  }
},

sandbox

【讨论】:

  • 嗨@ali-sasani 检查了沙箱并感到困惑。现在没有任何问题,但悬停背景的颜色选择器与未触发的按钮背景颜色匹配。
  • 抱歉,我无法理解您的意思。你能解释一下吗?您的目的是根据为Hover Font 选择的颜色更改按钮的背景颜色吗?我说的对吗?
  • 关闭!我想允许用户选择他们的按钮悬停颜色。所以他们将拥有: - 按钮颜色(完成) - 按钮字体颜色(完成) - 按钮悬停颜色。 (进行中) - 当鼠标移到按钮上时,颜色应显示选择的颜色 - 按钮悬停字体颜色(与上述相同,但带有字体)。 (TODO)这样更有意义吗?
  • 我更新了答案和沙盒链接。我想这就是你要找的东西
猜你喜欢
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
  • 2021-10-03
  • 2018-02-05
相关资源
最近更新 更多