[BUG] onHoverStart and onHoverEnd triggered multiple times when used with drag
See original GitHub issueEDIT: I’ve just tested this on Firefox and it works fine. The only appears only on Chrome and Edge (since it is Chromium as well). (Windows 10)
I needed to get access to the hover state of my motion component so I used onHoverStart
and onHoverEnd
to set the state accordingly. This worked as expected until drag
is enabled, then after the first drag the hover state is triggered multiple times when the mouse is entering and exiting the element.
CodeSandbox Example: https://codesandbox.io/s/vigorous-wozniak-gninp?file=/src/App.tsx
- Try without
drag
enabled and see the hover state triggers as expected as mouse hovers over the element - Enable
drag
. Drag once. Then use mouse to hover element. (Try entering from multiple sides with varying speeds) - hover state triggers multiple times
Here is a snippet of the example:
export default function App() {
const [isHover, setHover] = React.useState(false);
console.log(isHover);
//After first drag, isHover state is triggered multiple times on enter exit element
return (
<div>
<motion.div
drag
// drag={isHover}
//if this is set, then hover no longer triggered multiple times.
//However, sometimes drag will disconnect when moving mouse too fast
style={{ backgroundColor: "red", width: 150, height: 150 }}
onHoverStart={() => setHover(true)}
onHoverEnd={() => setHover(false)}
// onHoverStart={() => {
// console.log("hoverStart");
// setHover(true);
// }}
// onHoverEnd={() => {
// console.log("hoverEnd");
// setHover(false);
// }}
/>
</div>
);
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Gestures | Framer for Developers
Children can stop pointer events propagating to parent motion components using the Capture React props. For instance, a child can stop drag and...
Read more >'While' Animations and Initial - The Framer book
While Hover; While Tap; While Drag; While Focus. whileFocus with validation example. Initial; Bug when using borderRadius in a 'while' animation ...
Read more >Drag 'n Drop Events firing multiple times - Stack Overflow
In general the dragging and dropping works. But I don't get why e.g. the drop-event would trigger four times when only dropping a...
Read more >How To Use Framer Motion With React, TypeScript and Styled ...
Learn how To Use Framer Motion With React, TypeScript and StyledComponents to make your website more alive, animated, and attractive to ...
Read more >framer-motion | Yarn - Package Manager
Various StrictMode -related bugs including layoutTransition origin calculation. Only applying drag constraints during a useEffect to allow render-triggered ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You are correct, in a normal use case, drag should just be enabled. I just tried playing around with the drag prop while trying to investigate and to nail down what is causing these extra triggers. (this is why it is commented out)
I’ve determined that in Chrome, when drag is enabled, onHoverStart and onHoverEnd fires multiple times after dragging the element once. Setting drag to the hover seems to fix these extra triggers.
The reason I’m using onHoverStart/onHoverEnd is because other components in the app will react its hover state.
Ideally I’d suspend event listeners simply by setting a flag while we perform the DOM measurements. But it looks like events are flushed asynchronously so I’ll probably need to do a little rewrite that lets us unsubscribe hover events from the DOM temporarily.