Skip to content

Commit

Permalink
Stop propagation of click events on the document instead of draggable…
Browse files Browse the repository at this point in the history
… node
  • Loading branch information
Clauderic Demers committed Jul 22, 2021
1 parent 8d70540 commit 4b344df
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .changeset/prevent-click-propagation.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"@dnd-kit/core": minor
'@dnd-kit/core': minor
---

Pointer, Mouse and Touch sensors now stop propagation of click events on the draggable node once activation constraints are met
Pointer, Mouse and Touch sensors now stop propagation of click events once activation constraints are met.
15 changes: 5 additions & 10 deletions packages/core/src/sensors/pointer/AbstractPointerSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export class AbstractPointerSensor implements SensorInstance {
private initialCoordinates: Coordinates;
private timeoutId: NodeJS.Timeout | null = null;
private listeners: Listeners;
private nodeListeners: Listeners;
private documentListeners: Listeners;
private windowListeners: Listeners;

Expand All @@ -70,17 +69,13 @@ export class AbstractPointerSensor implements SensorInstance {
private events: PointerEventHandlers,
listenerTarget = getEventListenerTarget(props.event.target)
) {
const {
event,
activeNode: {node},
} = props;
const {event} = props;
const {target} = event;

this.props = props;
this.events = events;
this.documentListeners = new Listeners(getOwnerDocument(target));
this.listeners = new Listeners(listenerTarget);
this.nodeListeners = new Listeners(node.current);
this.windowListeners = new Listeners(getWindow(target));
this.initialCoordinates = getEventCoordinates(event);
this.handleStart = this.handleStart.bind(this);
Expand Down Expand Up @@ -126,10 +121,10 @@ export class AbstractPointerSensor implements SensorInstance {
private detach() {
this.listeners.removeAll();
this.windowListeners.removeAll();
this.documentListeners.removeAll();

// Wait until the next event loop before removing click listeners
setTimeout(this.nodeListeners.removeAll);
// Wait until the next event loop before removing document listeners
// This is necessary because we listen for `click` events on the document
setTimeout(this.documentListeners.removeAll);

if (this.timeoutId !== null) {
clearTimeout(this.timeoutId);
Expand All @@ -144,7 +139,7 @@ export class AbstractPointerSensor implements SensorInstance {
if (initialCoordinates) {
this.activated = true;
// Stop propagation of click events once activation constraints are met
this.nodeListeners.add(EventName.Click, stopPropagation, {
this.documentListeners.add(EventName.Click, stopPropagation, {
capture: true,
});

Expand Down

0 comments on commit 4b344df

Please sign in to comment.