Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MUI Button hover background color and text color

I have created an Appbar component in React.js with 3 buttons in it but I would like to change the color when I hover over those buttons. The background color is #3c52b2 and the text color is #fff. I would like the background color and text color exchange when I hover over the button.

I've tried the code below but still not working.

Button: {
  '&:hover': {
    backgroundColor: '#ffffff',
    boxShadow: 'none',
  },
  '&:active': {
    boxShadow: 'none',
    backgroundColor: '#3c52b2',
  },
},
like image 679
John Avatar asked Nov 24 '20 09:11

John


People also ask

How do I change text color in Mui button?

import React from 'react'; import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles'; import { lightBlue } from 'material-ui/colors'; import styled from 'styled-components'; const theme = createMuiTheme({ palette: { primary: lightBlue, // works }, raisedButton: { color: '#ffffff', // doesn't work }, ...

How do you change the hover effect in MUI?

MUI Styled Components Code Here's a simple styled button: const StyledButton = styled(Button)` background-color: grey; color: #fff; padding: 6px 12px; &:hover { background-color: #a9a9a9; } &:focus { background-color: green; } `; The hover selector is simple to add.

How do you give a custom color to a button in Materialui?

According to the official Doc: Customize MUI with your theme, you need to use ThemeProvider and createTheme . First, customize the primary color like this, import {ThemeProvider, createTheme} from '@mui/material/styles'; const theme = createTheme({ palette: { primary: { main: '#000000', }, }, });


2 Answers

You can do that in MUI v5 using sx prop:

<Button
  variant="text"
  sx={{
    ':hover': {
      bgcolor: 'primary.main', // theme.palette.primary.main
      color: 'white',
    },
  }}
>
  Text
</Button>

Or styled() if you want to create a reusable component:

const StyledButton = styled(Button)(({ theme, color = 'primary' }) => ({
  ':hover': {
    color: theme.palette[color].main,
    backgroundColor: 'white',
  },
}));
<StyledButton variant="contained" color="primary">
  Contained
</StyledButton>
<StyledButton variant="contained" color="secondary">
  Contained
</StyledButton>

Live Demo

Codesandbox Demo

like image 86
NearHuscarl Avatar answered Oct 23 '22 06:10

NearHuscarl


You probably don't want to change the button's :active state but the default and the :hover state. The following sets the button color to #fff and the backgroundColor to #3c52b2 and switch them on :hover.

I'm not sure how you applied the updated styles (or how you tried to override the default styles), I created this snippet below with makeStyles() but the idea is the same with the withStyles() HOC.

const { 
  AppBar,
  Button,
  makeStyles,
  Toolbar,
  Typography,
} = MaterialUI

const useStyles = makeStyles({
  flexGrow: {
    flex: '1',
  },
  button: {
    backgroundColor: '#3c52b2',
    color: '#fff',
    '&:hover': {
      backgroundColor: '#fff',
      color: '#3c52b2',
  },
}})

function AppBarWithButtons() {
  const classes = useStyles()
  
  return (
    <AppBar>
      <Toolbar>
        <Typography>
          YourApp
        </Typography>
        <div className={classes.flexGrow} />
        <Button className={classes.button}>
          Button 1
        </Button>
        <Button className={classes.button}>
          Button 2
        </Button>
      </Toolbar>
    </AppBar>
  );
};

ReactDOM.render(
  <React.StrictMode>
    <AppBarWithButtons />
  </React.StrictMode>,
  document.getElementById("root")
)
<div id="root"></div>
<script src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@material-ui/core@latest/umd/material-ui.production.min.js"></script>

You could also just create a new styled button component:

const StyledButton = withStyles({
  root: {
    backgroundColor: '#3c52b2',
    color: '#fff',
    '&:hover': {
      backgroundColor: '#fff',
      color: '#3c52b2',
  },
}})(Button);

const { 
  AppBar,
  Button,
  Toolbar,
  Typography,
  withStyles
} = MaterialUI

const StyledButton = withStyles({
  root: {
    backgroundColor: '#3c52b2',
    color: '#fff',
    '&:hover': {
      backgroundColor: '#fff',
      color: '#3c52b2',
  },
}})(Button);

function AppBarWithButtons() {
  return (
    <AppBar>
      <Toolbar>
        <Typography>
          YourApp
        </Typography>
        <div style={{flex: '1'}} />
        <StyledButton>
          Button 1
        </StyledButton>
        <StyledButton>
          Button 2
        </StyledButton>
      </Toolbar>
    </AppBar>
  );
};

ReactDOM.render(
  <React.StrictMode>
    <AppBarWithButtons />
  </React.StrictMode>,
  document.getElementById("root")
)
<div id="root"></div>
<script src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@material-ui/core@latest/umd/material-ui.production.min.js"></script>
like image 34
Zsolt Meszaros Avatar answered Oct 23 '22 07:10

Zsolt Meszaros