d3 / d3-drag

Drag and drop SVG, HTML or Canvas using mouse or touch input.

Home Page:https://d3js.org/d3-drag

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Click events following drag are not suppressed in Firefox?

amillette opened this issue · comments

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;
                });

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 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.

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!