Skip to content

Commit

Permalink
fix #78; non-passive event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 9, 2021
1 parent 3e7e3b0 commit 98d4805
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/drag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {dispatch} from "d3-dispatch";
import {select, pointer} from "d3-selection";
import nodrag, {yesdrag} from "./nodrag.js";
import noevent, {nopropagation} from "./noevent.js";
import noevent, {nonpassive, nonpassivecapture, nopropagation} from "./noevent.js";
import constant from "./constant.js";
import DragEvent from "./event.js";

Expand Down Expand Up @@ -41,7 +41,7 @@ export default function() {
.on("mousedown.drag", mousedowned)
.filter(touchable)
.on("touchstart.drag", touchstarted)
.on("touchmove.drag", touchmoved)
.on("touchmove.drag", touchmoved, nonpassive)
.on("touchend.drag touchcancel.drag", touchended)
.style("touch-action", "none")
.style("-webkit-tap-highlight-color", "rgba(0,0,0,0)");
Expand All @@ -51,7 +51,9 @@ export default function() {
if (touchending || !filter.call(this, event, d)) return;
var gesture = beforestart(this, container.call(this, event, d), event, d, "mouse");
if (!gesture) return;
select(event.view).on("mousemove.drag", mousemoved, true).on("mouseup.drag", mouseupped, true);
select(event.view)
.on("mousemove.drag", mousemoved, nonpassivecapture)
.on("mouseup.drag", mouseupped, nonpassivecapture);
nodrag(event.view);
nopropagation(event);
mousemoving = false;
Expand Down
8 changes: 4 additions & 4 deletions src/nodrag.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {select} from "d3-selection";
import noevent from "./noevent.js";
import noevent, {nonpassivecapture} from "./noevent.js";

export default function(view) {
var root = view.document.documentElement,
selection = select(view).on("dragstart.drag", noevent, true);
selection = select(view).on("dragstart.drag", noevent, nonpassivecapture);
if ("onselectstart" in root) {
selection.on("selectstart.drag", noevent, true);
selection.on("selectstart.drag", noevent, nonpassivecapture);
} else {
root.__noselect = root.style.MozUserSelect;
root.style.MozUserSelect = "none";
Expand All @@ -16,7 +16,7 @@ export function yesdrag(view, noclick) {
var root = view.document.documentElement,
selection = select(view).on("dragstart.drag", null);
if (noclick) {
selection.on("click.drag", noevent, true);
selection.on("click.drag", noevent, nonpassivecapture);
setTimeout(function() { selection.on("click.drag", null); }, 0);
}
if ("onselectstart" in root) {
Expand Down
5 changes: 5 additions & 0 deletions src/noevent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// These are typically used in conjunction with noevent to ensure that we can
// preventDefault on the event.
export const nonpassive = {passive: false};
export const nonpassivecapture = {capture: true, passive: false};

export function nopropagation(event) {
event.stopImmediatePropagation();
}
Expand Down

0 comments on commit 98d4805

Please sign in to comment.