question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support e.preventDefault() and e.stopPropagation() in tap/drag events

See original GitHub issue
  • In my app I have <Link> elements inside a draggable <motion.div> element. When the user clicks those, I don’t want the drag/whileTap styles to activate.
  • I tried to achieve this by blocking propagation of the onMouseDown event, but this doesn’t seem to have any effect.
  • In fact, the onMouseDown event is never fired at all on any of the childs of the draggable element.

See: https://codesandbox.io/s/framer-motion-block-mousedown-inside-draggable-item-3ecgd

Expected/Desired behaviour: when clicking the white box, the colored <motion.div> element should not scale up and/or follow the cursor when dragging.

Is there a way to do this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:22

github_iconTop GitHub Comments

12reactions
brentjettcommented, Apr 28, 2020

Scratch that, found a way using useDragControls().

onDragStart={ ( e, info ) => {

    // Check if event doesn't originate in the "handle" element
    if ( ! e.target.classList.contains( 'drag-handle' ) ) {

        // Stop the drag
        controls.componentControls.forEach( entry => {
            // be sure to pass along the event & info or it gets angry
            entry.stop( e, info )
        })
    }
} }

Works great.

4reactions
mattgperrycommented, Sep 24, 2020

I think with these slightly more advanced use-cases you’re better off handling gesture animation states yourself with useState. I’m going to leave the ticket open though as I think the request itself is a fair one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event.stopPropagation() - Web APIs | MDN
The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases.
Read more >
How to correctly use preventDefault(), stopPropagation(), or ...
event.preventDefault(). Prevents the browsers default behaviour (such as opening a link), but does not stop the event from bubbling up the DOM.
Read more >
What's the difference between event.stopPropagation and ...
preventDefault() – It stops the browsers default behaviour. event.stopPropagation() – It prevents the event from propagating (or “bubbling up”) the DOM.
Read more >
PreventDefault vs StopPropagation | by Gapur Kassym - ITNEXT
stopPropagation() method stops the propagation of an event from occurring in the bubbling or capturing phase. The event on a href element id=” ......
Read more >
Difference between preventDefault() and stopPropagation ...
preventDefault() Method: It is a method present in the event interface. ... for applying both methods with the help of the examples.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found