Button Hover Effect: Transition with Style

I am trying to implement a one-second transition from the default color scheme to the hover color for a Button element in Material UI. I am using the

transitions.create(props, options)
method as described in this article: https://medium.com/@octaviocoria/custom-css-transitions-with-react-material-ui-5d41cb2e7c5. The transition does work, but the hover color appears immediately when the mouse hovers over the button. How can I delay the color change until the transition is finished?

Here is the relevant code snippet:

function Mbutton({ classes }) {
  return (
    <Button variant="outlined" className={classes.cart} disableRipple>
      Add to Cart
    </Button>
  );
}

const styles = theme => ({
  cart: {
    padding: "6px 16px",
    borderRadius: 0,
    border: "2px solid #000",
    "&:hover": {
      transition: theme.transitions.create(["backgroundColor", "color"], {
        duration: 1000
      }),
      backgroundColor: "#000",
      color: "#fff"
    }
  }
});

You can also view and test it on Code Sandbox: https://codesandbox.io/s/mbutton-fogco

Thank you.

Answer №1

Transform this into plain CSS format


.cart {
    padding: 6px 16px;
    border-radius: 0;
    border: 2px solid #000;
    background-color: white;
    color: black;
    transition: background 1s, color 1s;
}

.cart:hover {
    background-color: #000;
    color: #fff;
}

View the Code on CodeSandbox

The theme parameter is not necessary in this context. It should be used when utilizing the default Material-UI theme object for transition styles, which is not needed in this particular case.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Jquery's .load function will temporarily deactivate all other functions

Season's Greetings everyone! I have a unique personal messaging system on my website that utilizes jQuery for message display and manipulation. Let's delve into the specific file that controls this functionality: <!-- Fetching and displaying ...

Use CSS to apply hover effects to multiple sections of text at the same time

Is there a way in CSS to apply the hover state to one part of HTML text when another part is hovered over, both sharing the same class? I have a block of text in HTML that is broken down into individual words, each word linked to a specific CSS class. It ...

How can I fix the error "rootRef.current.contains is not a recognized function" when using the Transition Component in react-transition-group with the latest versions

I am in the process of updating a project from Material-UI 4 to MUI v5, and I have also upgraded react to v18. We utilize the Transition component from react-transition-group within a Modal in one of our components. However, I encountered an error stating ...

Having trouble getting custom Themes to apply in Material UI version 5

I meticulously followed the documentation to create a custom theme, but unfortunately, I'm not seeing any changes reflected in my UI. Both the primary and secondary colors remain the same as the default, despite my efforts. Additionally, the warning, ...

Avoid unnecessary re-rendering of MUI DatePicker when switching between years

I need to find a way to prevent the onChange function from triggering when the user switches from one year to another using the DatePicker. Currently, whenever the user selects a new year, the component re-renders before an actual date is selected. https: ...

Is it possible to asynchronously access a JSON object that has been retrieved from a local file on a global scale using the XMLHttpRequest method and

Having some trouble manipulating data from a local JSON file using this technique (no jQuery) as I can't seem to access the object on a global scale: var actual_JSON; function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.o ...

Display the Header in each group only once

I'm attempting to create a dropdown similar to the one shown in this example.. Here's the code I have so far: const data = [ { id: 1, header:1, value:1 }, { id: 2, header:2, value:2 }, { id: 3, header:2, ...

Challenges Encountered when Making Multiple API Requests

I've encountered a puzzling issue with an ngrx effect I developed to fetch data from multiple API calls. Strangely, while some calls return data successfully, others are returning null for no apparent reason. Effect: @Effect() loadMoveList$: Obse ...

I am looking to conceal the y-axis labels and tooltip within the react chart

I am currently working with react-chart-2. I have a line graph that displays a tooltip when hovered over, but I would like to hide this tooltip feature. Additionally, I want to remove the numbers 0, 0.1, 0.2 up to 1 on the left side (y-axis) of the gra ...

Tips for incorporating images into an `.mdx` file located outside of the `public/` directory with the `next-mdx-remote` package in Next JS

I am currently developing a blog using next-mdx-remote and I am facing an issue with incorporating images in the .mdx file that are located outside of the public/ folder. If you would like to check out the complete code for my blog project, it is availabl ...

Use jQuery to assign a value of "true" when a checkbox is

Can you guide me on how to use jQuery to implement a click function that sets the status value to 'true' if a checkbox is checked, and 'false' if it's not checked? If Checkbox 1 is checked, Status 1 should be set to true. Similarl ...

I'm receiving a Reference Error stating that 'next()' is not defined, despite the fact that I have defined it in my JavaScript code. What could be causing this issue?

I have a javascript function called next() to process information and then call the function from HTML. However, my Firefox console is showing a ReferenceError: 'next' is not defined. I am aware that there is an error in my code, but I cannot pin ...

Halting execution: Trying to use the keyword 'import' which is not allowed here

0. April 2023: error cannot be reproduced anymore see here The error is no longer replicable due to bug fixes in react-scripts 5.0.1. 1 Even though the error is gone, the question and my self-answer still seem relevant to Angular users and others as we ...

Is there a way to include a different component without it automatically displaying within a div element?

Is there a way to make the Torrent component render without directly binding it to a DOM element? I am facing an issue with my Torrent Table Component as I want it to be populated with Torrent Components based on API data, but it's not rendering beca ...

What is the syntax for implementing a nested for loop in JavaScript within this particular scenario?

var entries = [ { "item": "something", "steps": [{ "node": {"name": "test0"}, "status": {"name": "test"}, "time": {"name": "test"} },{ "node": {"name": "test1"}, ...

A guide to accurately fetching the transform properties of an SVG element within a d3.transition

Currently, I am experimenting with d3 animations using d3.transitions specifically involving circles. Consider the circle animation example below (d3.transition()): animationTime = 500; svg = d3.select('#svg'); // Locate th ...

Tips for circumventing CORS in an ajax request using various browser extensions or add-ons

I am encountering a cross-origin problem with my WCF service. Despite adding an extension in chrome to bypass CORS, the issue persists. Is there any other extension/add-on/plugin available to test the service locally and bypass the CORS problem? Edit Aft ...

Exploring the world of Node.js callback parameter passing

Currently, I am diving into the world of Node callbacks and JavaScript in general. As I delve deeper, I find myself puzzled by the following snippet: var request = require('request'); request('http://www.google.com', function (error, ...

Error: Unable to access 'name' property in reactjs due to undefined value

Here is the frontend code snippet: { (opp||[]).map(opps=>{ return( <div className="card OppCard"> <div className="card-body" > <h4>{opps.title}</h4> <p><b&g ...

Creating a dropdown menu utilizing JavaScript/jQuery arrays

Looking to create an HTML select menu using a JavaScript array where the keys are used as values for options. The challenge is when entering numbers as keys, they should be accepted in the code. a([selected="No of rooms", 1="1", 2="2", 3="3", 4="4", 5="5" ...