简体   繁体   English

单击 Material UI 更改波纹颜色

[英]Change Ripple Color on click of Material UI <Button />

This is my <MyButton /> Component这是我的<MyButton />组件

import React from 'react';
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';

const styles = theme => ({
  button: {
    backgroundColor: 'green',
    "&:hover": {
      backgroundColor: "red"
    },
  },
});

function MyButton(props) {

  return (
    <Button
      className={props.classes.button} >
      {props.text}
    </Button>
  );
}

export default withStyles(styles)(MyButton);

Currently, there is the default click effect on the button, brightening/lightening it up on click (see here: https://material-ui.com/demos/buttons/ ).目前,按钮上有默认的点击效果,点击时使其变亮/变亮(请参阅此处: https : //material-ui.com/demos/buttons/ )。 However, I want the color of the "ripple" to be blue when the button is clicked.但是,我希望单击按钮时“波纹”的颜色为blue

I tried我试过

"&:click": {
    backgroundColor: "blue"
},

and

"&:active": {
    backgroundColor: "blue"
},

No luck though.虽然没有运气。 How do I change the color of the ripple when the button is clicked?单击按钮时如何更改波纹的颜色?

Here's an example showing how to modify the button ripple:这是一个显示如何修改按钮波纹的示例:

import React from "react";
import { withStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";

const styles = theme => ({
  button: {
    backgroundColor: "green",
    "&:hover": {
      backgroundColor: "red"
    }
  },
  child: {
    backgroundColor: "blue"
  },
  rippleVisible: {
    opacity: 0.5,
    animation: `$enter 550ms ${theme.transitions.easing.easeInOut}`
  },
  "@keyframes enter": {
    "0%": {
      transform: "scale(0)",
      opacity: 0.1
    },
    "100%": {
      transform: "scale(1)",
      opacity: 0.5
    }
  }
});

function MyButton({ classes, ...other }) {
  const { button: buttonClass, ...rippleClasses } = classes;
  return (
    <Button
      TouchRippleProps={{ classes: rippleClasses }}
      className={buttonClass}
      {...other}
    />
  );
}

export default withStyles(styles)(MyButton);

编辑按钮波纹

The default opacity of the ripple is 0.3.波纹的默认不透明度为 0.3。 In the example I have increased this to 0.5 to make it easier to verify that the ripple is blue.在示例中,我将其增加到 0.5,以便更容易验证波纹是否为蓝色。 Since the button background is red (due to the hover styling), the result is purple.由于按钮背景为红色(由于悬停样式),因此结果为紫色。 You'll get a different overall effect if you move to the button via the keyboard since the blue will be layered on top of the button's green background.如果您通过键盘移动到按钮,您将获得不同的整体效果,因为蓝色将叠加在按钮的绿色背景之上。

You'll find some additional background in my answer here: How to set MuiButton text and active color你会在我的回答中找到一些额外的背景: How to set MuiButton text and active color

The main resource for the styling of the ripple is its source code: https://github.com/mui-org/material-ui/blob/v4.10.2/packages/material-ui/src/ButtonBase/TouchRipple.js波纹样式的主要资源是它的源代码: https : //github.com/mui-org/material-ui/blob/v4.10.2/packages/material-ui/src/ButtonBase/TouchRipple.js

JSS keyframes documentation: https://cssinjs.org/jss-syntax/?v=v10.3.0#keyframes-animation JSS 关键帧文档: https ://cssinjs.org/jss-syntax/ ?v= v10.3.0#keyframes-animation

Answer demonstrating use of keyframes: How to apply custom animation effect @keyframes in MaterialUI using makestyles()回答演示关键帧的使用: 如何使用 makestyles() 在 MaterialUI 中应用自定义动画效果 @keyframes

Here is the hack for Material-UI 4.9.10这是Material-UI 4.9.10的 hack

Add a class to button and then add the below code to given class添加一个类到按钮,然后将下面的代码添加到给定的类

 Btn:{
    "& .MuiTouchRipple-root span":{
      backgroundColor: 'red!important',
      opacity: .3,
    },
},

Using CSS - can be tricky to identify the class for the MUI component at times, but for our usage of the <ListItem> element which is rendered like a button:使用 CSS - 有时可能很难识别 MUI 组件的类,但对于我们使用的<ListItem>元素来说,它呈现为一个按钮:

.MuiListItem-button:hover {
  background-color: rgba(23, 93, 131, 0.12) !important;
}
.MuiTouchRipple-rippleVisible {
  color: #005d83 !important;
}

Note: we use a dark blue and customized the background-color attribute with some opacity.注意:我们使用深蓝色并使用一些不透明度自定义background-color属性。

I've adapted the solution from this answer to work with styled components and emotion/styled .我已经从这个答案中调整了解决方案,以使用styled componentsemotion/styled

const ButtonStyledWithSC = styledSC(Button)`
  && {
    background-color: magenta;
    width: 10rem;
    height: 3rem;
    margin: 10px;

    &:hover {
      background-color: white;
    }

    > .MuiTouchRipple-root span {
      background-color: cyan;
    }
  }
`;

启发式-feistel-0znpi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vuejs 中按钮单击时的 Material UI 涟漪效应,我哪里出错了? (捕获鼠标位置,但不触发波纹) - Material UI ripple effect on button click in Vuejs, where am I going wrong? (Capturing mouse position, but ripple not firing) 如何更改 Material UI Table 中特定单元格中的按钮颜色和名称 - How to change Button Color and Name in specific cell in Material UI Table 如何使用“&amp;:active”更改材质ui中按钮的颜色:? - How to change color of button in material ui using "&:active":? 更改激活时 material-ui 按钮的颜色 - Change the color of a material-ui Button when it's active 更改颜色 InputLabel Material UI - Change color InputLabel Material UI 如何更改按钮上的波纹背景颜色? - How do I change the ripple background color on Button? Material UI中的嵌套按钮:如何在单击子按钮时禁用容器按钮的波纹效果? - Nesting buttons in Material UI: how to disable ripple effect of container button while clicking on a child button? 当我们在 React 中使用 Material-UI 单击它时,如何更改侧边栏菜单项的颜色? - How to change the menu item's color of sidebar when we click on it with Material-UI in React? 更改纸张颜色 Material-UI - Change paper color Material-UI 分隔线颜色变化 React Material Ui - Divider color change React Material Ui
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM