From 98d4805114544cb70cf01102a70d6cf58ce11e9c Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Wed, 9 Jun 2021 08:00:56 -0700 Subject: [PATCH] fix #78; non-passive event listeners --- src/drag.js | 8 +++++--- src/nodrag.js | 8 ++++---- src/noevent.js | 5 +++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/drag.js b/src/drag.js index 380c11d..790d9c7 100644 --- a/src/drag.js +++ b/src/drag.js @@ -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"; @@ -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)"); @@ -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; diff --git a/src/nodrag.js b/src/nodrag.js index eab81d3..6076694 100644 --- a/src/nodrag.js +++ b/src/nodrag.js @@ -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"; @@ -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) { diff --git a/src/noevent.js b/src/noevent.js index b32552d..173c250 100644 --- a/src/noevent.js +++ b/src/noevent.js @@ -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(); }