I want to render an element when I hover hover a button when it is disabled.
My app component:
import React, { useState } from "react";
function App() {
const [hover, setHover] = useState(false);
const onHover = () => {
setHover(true);
};
const onLeave = () => {
setHover(false);
};
return (
<div className="App" style={appStyles}>
<StyledGlButton
className = "dis-btn"
onMouseEnter={onHover}
onMouseLeave={onLeave}
disabled = {isDisabled}
>
Red
</StyledGlButton>
{(hover && isDisabled) && (
<h1>Disabled Button!!</h1>
)}
</div>
);
}
export default App;
and my StyledGlButton:
export const StyledGlButton = styled(GlButton)<GlButtonProps>`
&.gl-cta {
font-size: 14px;
height: 3rem;
min-height: auto;
pointer-events: all !important;
}`;
I tried adding pointer-events: all !important;
in the css of the button but it is not working.
2
Answers
You can surround the
button
withspan
and give that buttonpointer-events: none
, so the span can listen to the events.I may well be that React simply disables the
mouseover
event of a [disabled] element by giving itpointer-events: none
. I don’t use React, but this is what I expect their solution to be.Worse would be React catching the event in JS and doing nothing with it (would probably go against W3C specs). In that case the solution by @Mina would be best to use.
pointer-events: all
as you mention is meant for SVG elements only, usepointer-events: auto
instead.Here’s a little snippet that shows two
disabled
buttons and a hidden element that only shows when hovered overdisabled
button two.