Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal reaching clickDistance threshold? #48

Open
Herst opened this issue Feb 2, 2018 · 4 comments
Open

Signal reaching clickDistance threshold? #48

Herst opened this issue Feb 2, 2018 · 4 comments
Labels

Comments

@Herst
Copy link
Contributor

Herst commented Feb 2, 2018

In my application I want to do certain things only after I am certain that the user intents to do a drag, e.g.:

  • Adding the styles for dragged elements. If I would do it on the drag start event then it the style would be added on every mousedown which is not what I want.

  • .raise() the element being dragged. If I would do it right away it might cause the click event to not be triggered (at least in some browsers) if the element got removed from the DOM.

The way I solved it is to simply use the delta x and y inside the drag event handler to calculate whether the click stance threshold has been breached or not.

Now d3 already does the same calculation already, I wondered whether it couldn't just simply tell me about whether the distance has been overstepped in the drag event, e.g. using d3.event.isOutsideClickDistance.


On a side note, an alternative idea would be a .dragDistance() which would cause the start events to only appear after a certain distance. This would cause the elements to appear a bit sticky though and it depends on the specific use case whether that's what you want or not.

@Herst
Copy link
Contributor Author

Herst commented Feb 3, 2018

Demo demonstrating my current solution: https://bl.ocks.org/Herst/3d486648556a3eee60442da977022fd1

@GordonSmith
Copy link

GordonSmith commented Oct 26, 2018

@Herst - to workaround losing your click event, tweak how "raise" works to not move the "drag" node, but rather move all its siblings:

            //  Safe Raise - does not interfere with current click event  ---
            const dragNode = /* the DOM node being dragged during "dragstart" event */
            let nextSibling = dragNode.nextSibling;
            while (nextSibling) {
                target.parentNode.insertBefore(nextSibling, dragNode);
                nextSibling = dragNode.nextSibling;
            }

@mbostock
Copy link
Member

mbostock commented Aug 5, 2019

Would it be cleaner to not call event.preventDefault() on the mousemove event until the click distance threshold is crossed? Then you could check event.isDefaultPrevented to test.

@mbostock mbostock changed the title Flag in drag event to signal reaching clickDistance threshold Signal reaching clickDistance threshold? Aug 6, 2019
@Herst
Copy link
Contributor Author

Herst commented Aug 6, 2019

I guess this could work. I wonder if this would have any (good or bad) side effects concerning #9:

It’s a Chrome bug that calling mousemove.preventDefault prevents text selection, drag-and-drop, and scrolling (see issue 485892). The mousemove event is defined as having no default action.

@Fil Fil added the idea label Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants