Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is my <MyButton /> Component

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/). However, I want the color of the "ripple" to be blue when the button is clicked.

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?

like image 978
Stophface Avatar asked May 16 '19 13:05

Stophface


2 Answers

Here's an example showing how to modify the button ripple for v4 (v5 example further down):

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

Edit button ripple

The default opacity of the ripple is 0.3. In the example I have increased this to 0.5 to make it easier to verify that the ripple is blue. 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

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

JSS keyframes documentation: https://cssinjs.org/jss-syntax/?v=v10.3.0#keyframes-animation


Here's a similar example for MUI v5:

import { styled } from "@mui/material/styles";
import Button from "@mui/material/Button";
import { keyframes } from "@emotion/react";

const enterKeyframe = keyframes`
  0% {
    transform: scale(0);
    opacity: 0.1;
  }
  100% {
    transform: scale(1);
    opacity: 0.5;
  }
`;
const StyledButton = styled(Button)`
  background-color: green;
  &:hover {
    background-color: red;
  }
  && .MuiTouchRipple-child {
    background-color: blue;
  }
  && .MuiTouchRipple-rippleVisible {
    opacity: 0.5;
    animation-name: ${enterKeyframe};
    animation-duration: 550ms;
    animation-timing-function: ${({ theme }) =>
      theme.transitions.easing.easeInOut};
  }
`;
export default StyledButton;

Edit button ripple

v5 TouchRipple source code: https://github.com/mui/material-ui/blob/v5.4.0/packages/mui-material/src/ButtonBase/TouchRipple.js#L92

Emotion keyframes documentation: https://emotion.sh/docs/keyframes

Answer demonstrating use of keyframes: How to apply custom animation effect @keyframes in MUI?

like image 172
Ryan Cogswell Avatar answered Oct 23 '22 15:10

Ryan Cogswell


Here is the hack for Material-UI 4.9.10

Add a class to button and then add the below code to given class

 Btn:{
    "& .MuiTouchRipple-root span":{
      backgroundColor: 'red!important',
      opacity: .3,
    },
},
like image 43
Muhammad Muzamil Avatar answered Oct 23 '22 17:10

Muhammad Muzamil