How to disable the hover effect of material-ui button inside of a styled component
Asked Answered
A

9

64

I added the css hover property to disable the button's hover effect, but it seems not work for my case, how should I fix this?

import Button from 'material-ui/Button'
import styled from 'styled-components'

const StyledButton = styled(Button)`
  &:hover {
    background: none;
  }
`
export const SubmitButton = ({ onClick }) => {
  return (
    <StyledButton
      variant="raised"
      onClick={onClick}>
      login
    </StyledButton>
  )
}
Anthem answered 10/5, 2018 at 2:32 Comment(1)
sx={{ "&:hover": { backgroundColor: "transparent" }} } should work in your case.Prop
A
68

You can solve this problem by adding an inline style

export const SubmitButton = ({ onClick }) => {
  return (
    <StyledButton
      variant="raised"
      onClick={onClick}
      style={{ backgroundColor: 'transparent' }} >
      login
    </StyledButton>
  )
}
Anthem answered 15/5, 2018 at 5:28 Comment(2)
also you have a major typo, you could have at least wrote style={{ backgroundColor: 'transparent' }}Nuristan
Yeah, one more dirty trick needed, while they could just have it optional.Lysimachus
P
52

Try setting it to the same color as the background:

root = {
    backgroundColor: "#FFF"
    "&:hover": {
        //you want this to be the same as the backgroundColor above
        backgroundColor: "#FFF"
    }
}
Partner answered 28/6, 2018 at 0:2 Comment(0)
S
35

this is solution for v5 if anyone needs it

         <IconButton
            disableElevation
            disableRipple
            size="small"
            sx={{
              ml: 1,
              "&.MuiButtonBase-root:hover": {
                bgcolor: "transparent"
              }
            }}
          >

          </IconButton>
Shorten answered 28/9, 2021 at 23:44 Comment(2)
Btw: This also is the way to go for the MUI Radio component because it inherits ButtonBase API.Timothea
It still works for MUI V6!Borglum
A
11

You can try setting the background of the button as none

button: {    
    '&:hover': {
        background: 'none',
    },
}
Anneliese answered 27/4, 2021 at 20:17 Comment(0)
C
0

If you used the origin Button component with className instead, you could have added disableRipple to the button like that. <Button disableRipple>

Cuckoopint answered 16/6, 2020 at 8:12 Comment(1)
This does disable the effect, but not the hover.Hokku
Z
0

You can just override it via a styled component:

const StyledButton = styled(Button)`
    &:hover {
      background-color: transparent;
    }
`;
Zanthoxylum answered 9/3, 2022 at 11:7 Comment(0)
E
0

This should work

const StyledButton = styled(Button)`
  &&.MuiButton-root {
    &:hover {
      background: none;
    }
  }
`
Ehf answered 18/8, 2022 at 18:59 Comment(0)
D
0

We can able to disable material-ui button hover effect in following way by adding these two details in the component In my case I did for Switch button.

disableRipple

style={{ backgroundColor: "transparent" }}

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

const styles = {
  
};

class SwitchLabels extends React.Component {
  state = {
    checked: true
  };

  handleChange = (event) => {
    this.setState({ checked: event.target.checked });
  };

  render() {
    return (
      <FormControlLabel
        control={
          <Switch
            classes={this.props.classes}
            checked={this.state.checked}
            onChange={this.handleChange}
            disableElevation
            disableRipple
            style={{ backgroundColor: "transparent" }}
            value="checked"
            color="primary"
          />
        }
        labelPlacement="start"
        label={this.state.checked ? "On" : 'Off'}
      />
    );
  }
}

export default withStyles(styles)(SwitchLabels);
Dart answered 12/3, 2023 at 1:52 Comment(0)
C
-1

Work for me

 <Button 
    sx={{
      color:'#000000', 
      background:'rgba(0, 0, 0, 0.065)', 
      width:'84px', 
      height:'51px', 
      boxShadow: '0px 0px 4px 0px #00000040',
      fontSize:'20px',
      '&:hover': {
           background:'rgba(0, 0, 0, 0.065)'
      },
    }} 
    variant="contained">
    +50$
   </Button>
Cassis answered 16/8, 2023 at 15:53 Comment(1)
OP is looking to style a button created using styled utility.Instead

© 2022 - 2024 — McMap. All rights reserved.