Implement a hover effect for the Button component in StoryBook

Is there a way to implement a hover effect on my button Storybook function?

To include the button in my Storybook documentation, I follow these steps:

import ButtonClose from './ButtonClose';

const StoryMeta = {
  title: `Components/Button/ButtonClose`,
  component: ButtonClose,
};

export default StoryMeta;

const Template = (args) => <ButtonClose {...args} />;

export const Default = Template.bind({});

I am struggling with activating the hover state of my button, which is defined as:

import React from 'react';
import { IconButton } from '@mui/material';
import SvgIcons from '../SvgIcons';

const ButtonClose = ({ onClick, className }) => (
  <IconButton
    sx={{
      width: '40px',
      height: '40px',
      '&:hover': {
        bgcolor: palette.primary.dark,
    }}
    onClick={onClick}
    className={className}
  >
    <SvgIcons icon="close-button" />
  </IconButton>
);

export default ButtonClose;

Answer №1

Consider using the onMouseEnter event handler on your button element. You can create a boolean state variable and use onMouseEnter and onMouseLeave to toggle the value of this state. Then, you can implement conditional logic to change the background color based on this state.

Here's an example:

<Button
   onMouseEnter={()=>setIsHover(true)}
   onMouseLeave={()=>setIsHover(false)}
   style={{
        width: '50px',
        height: '30px',
        backgroundColor:(theme)=>isHover? theme.palette.secondary.dark : theme.palette.secondary.light
  }}
 >

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

Find unique numbers within a specified range using NodeJS

I need to update my arts on an NFT test via OpenSea API, but I'm facing an issue where the numbers are being repeated. Is there a way to select a number within a range that does not repeat? Here is my current code: const opensea = require("opense ...

Issue with an external library in Angular 2

After generating my Angular 2 library using the yeoman generator and adding it to my main Angular project, I encountered errors when running the app in production mode. The specific errors include: WARNING in ./src/$$_gendir/app/services/main/main.compone ...

How can I assign a property value for a class in one cell from another cell within the same notebook using Observablehq?

I'm brand new to .js classes and my goal is to set a property value for a class that I define in one cell, from within another cell in the same notebook. The cells only contain the code shown here. Below is the test class in cell 1: class hist_class ...

determining the selected "child" checkbox in ReactJS

As a newcomer to React, I have a question that may not be the most experienced. Let me provide some context: within my ExerciseCard component, there is a checkbox (among other things). This component is rendered inside the CreateNewWorkout component where ...

As attempting to make changes, the system generates an error stating that it cannot read the property '0' of null

Encountering an error message "Cannot read property '0' of null" while attempting to modify any of the text fields, and the image is not updating in the database. Attached is a screenshot for reference. The error Type: TypeError: Cannot read pro ...

How do I utilize JSON to transmit a string to my server with Tornado?

Currently, I am facing an issue with my HTML input box and a JavaScript function that is triggered upon clicking a submit button. My goal is to send the data entered by the user in the input box to my Tornado server. Despite trying various options, I have ...

Error: Unable to access the 'CustomerId' property because it is undefined

Recently, I made some updates to my website. As part of the upgrade process, I switched to AngularJS v1.7.6 and JQuery v1.12.1. However, after making these changes, I encountered an error in my console log. TypeError: Cannot read property 'CustomerId ...

How can an array be concatenated using tabs?

I am currently in the process of integrating with an old system that requires a value to be sent via a GET request as a tab delimited string. I have my data stored in an array, but I am facing difficulty when attempting to properly format it using the join ...

#React How to include an image within a component

Currently, I am attempting to define my "Logo" as an image. I attempted to create a logo.js component, defining it as an image and calling it here, but I am at a loss on how to solve this issue. Below is my banner component, import React from 're ...

Sticky positioning causes elements to stick to the window as the

https://i.stack.imgur.com/nv3vU.png I am facing an issue with a position sticky block that can vary in height relative to the window size. Sometimes, the block is taller than the viewport, making it impossible to scroll to see all the content. Is there a ...

How can we briefly make the list item color and size return to default after clicking the Bootstrap navbar-toggler button?

.navbar .collapse .navbar-nav .nav-item .nav-link { font-family: 'yekan'; color: #fff; opacity: .8; font-size: .9rem; padding-left: 15px; padding-right: 15px } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery. ...

Using values from a select menu in a math calculation without including a decimal point

I am working with a dropdown menu that contains various values... <select id="number1a" onChange="Addition()"> <option value="0" selected>-</option> <option value="10">10</option> <option value="7.5">7.5</optio ...

Converting Dates in Javascript to the Format YYYYMMDDHHMMSS

Can you guide me on the process of transforming the date format Mon Apr 16 2018 19:00:00 GMT-0500 (Central Daylight Time) into 20180416190000 (YYYYMMDDHHMMSS)? ...

Ways to evaluate a date against a string in JavaScript

I am currently working on developing a custom filter to showcase the most recent file in my system using AngularJS for data filtering. The challenge I am facing is that the date information is saved as a string in my json file, and I'm unsure how to ...

React does not accept objects as valid children. If you want to render a group of children, make sure to use an array instead

I am in the process of developing a system for document verification using ReactJS and solidity smart contract. My goal is to showcase the outcome of the get().call() method from my smart contract on the frontend, either through a popup or simply as text d ...

The network tab is failing to display the CSS styles being applied to the selectors

Being new to markup languages, I encountered an issue when trying to apply my first CSS file to my index.html. Here is how I included the link tag in the HTML file for the CSS file: index.html code Upon checking the network tab in developer tools, I notic ...

Issue with loading animated Three.js file causing it to not display correct

I created an animation in Blender and exported it as a Three.js file to incorporate it into my Three.js code. Below is the JavaScript code snippet: var loader = new THREE.JSONLoader(); loader.load( "obj/dog5.js", function( geometry ) { mesh = new THR ...

Tips for transferring chosen radio button to controller using AngularJS

Is there a way to use console.log() to output the selected radio button? I attempted to set a default selection with checked="checked", but it didn't function as expected. <body ng-app="app"> <div class="container" ng-controller="radioCon ...

The Jquery command fails to reveal the hidden <p> tag

Confession time: I am diving headfirst into the world of java script and jQuery for the first time. My goal? To smoothly transition from displaying one <p> </p> tag to another with a fade-out effect, but it seems like fate has other plans. Desp ...

Encountered an error in AWS Lambda (Node 14.x): SyntaxError - Unexpected token 'export'

As I work on developing a straightforward login and registration system with AWS, I encountered an issue in AWS Lambda while testing my backend using Postman. The error code is detailed below: { "errorType": "Runtime.UserCodeSyntaxError& ...