按钮中字体的反应颜色选择器

问题描述

把我的头撞到墙上一点。

我有一个按钮可以更改其半径边框和背景颜色。我的下一个目标是更改字体颜色,以便如果用户想要浅色背景,他们可以与字体形成对比。

这是我正在处理的代码:

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 [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);
  };

  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})`
        }}
        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={color} onChange={handleChange} />
          </div>
        ) : null}
      </div>
    </div>
  );
}

这是我还没有破坏的沙箱(还) - https://codesandbox.io/s/material-demo-forked-t8xut?file=/demo.js

有人对如何解决这个问题有任何建议吗?睡得很少,撞到墙上。 TGIF。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...