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

Click events following drag are not suppressed in Firefox? #34

Closed
amillette opened this issue Apr 13, 2017 · 3 comments
Closed

Click events following drag are not suppressed in Firefox? #34

amillette opened this issue Apr 13, 2017 · 3 comments

Comments

@amillette
Copy link

amillette commented Apr 13, 2017

Whenever the drag behavior is applied, the click event will not be triggered on the same element in Chrome but is triggered in Mozilla Firefox.

To work around this, I dispatch a click event whenever there is no dragging. This will work in Chrome. The click event will be triggered twice in Mozilla Firefox.

It seems the click event is not prevented in Firefox like it is supposed to be.

The propagation of all consumed events is immediately stopped. If you want to prevent some events from initiating a drag gesture, use drag.filter.

Example :

return d3.drag()
                .on("start", function (d) {
                    dragged = false;

                    if (!d3.event.active) simulation.alphaTarget(0.2).restart();
                    instance.get.getNodeSvg(c.id).raise();
                    d.fx = d.x;
                    d.fy = d.y;
                })
                .on("drag", function (d) {
                    dragged = true;

                    d.fx += d3.event.dx;
                    d.fy += d3.event.dy;
                })
                .on("end", function (d) {
                    if (!d3.event.active) simulation.alphaTarget(0);
                    d.fx = null;
                    d.fy = null;

                    if (!dragged) {
                        this.dispatchEvent(new MouseEvent("click", d3.event.sourceEvent));
                    }

                    dragged = false;
                });
@mbostock
Copy link
Member

It’s possible that Firefox is slightly slower to trigger the click event following a drag gesture, so our zero-millisecond timeout in nodrag.js isn’t working to suppress click following drag.

setTimeout(function() { selection.on("click.drag", null); }, 0);

Could you try increasing the timeout and seeing if that fixes the problem?

@mbostock mbostock changed the title D3 Drag cross-browser compatibility Click events following drag are not suppressed in Firefox? Apr 14, 2017
@amillette
Copy link
Author

amillette commented Apr 17, 2017

@mbostock I have tried to replace the timeout like you said but it doesn't seems to make any difference. The click is still triggered twice in Firefox.

@mbostock
Copy link
Member

mbostock commented May 11, 2017

I’m not able to reproduce this issue, and this issue does not include a live example that demonstrates the problem, so I am afraid I cannot further investigate and am closing this issue. When reporting an issue, please include a link to a live example, preferably on bl.ocks.org or RunKit, or as a pull request in the appropriate repository with new unit tests, that demonstrates that the described behavior is not the expected behavior. Use only the minimum amount of code necessary to reproduce the unexpected behavior.

I tried to reproduce this issue by adding a click handler to this example, and it worked as expected in both Chrome and Firefox (on macOS):

https://bl.ocks.org/mbostock/2990a882e007f8384b04827617752738

I suspect that there may be an issue with your use of what appears to be selection.raise in the code snippet. Since this method may re-insert the selected elements into the DOM, it may prevent a subsequent click event, since browsers only trigger a click event if the mousedown and mouseup occur on the same element, and re-inserting an element may cause the browser to consider the re-inserted element a different element (even though it is technically the same). In some cases I have found it necessary to defer raising the element until mousemove, rather than raising on mousedown. However, it might be desirable to raise on mousedown, in which case you will have to trigger the click event manually. Whichever approach you choose, I would not consider this behavior to be an issue with d3-drag, since it is caused by raising the element.

While not directly related to this issue, I’ll also mention that I just committed a fix to #28.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants