Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rebind-listeners-on-effect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/dom': patch
---

Rebind PointerSensor event listeners on effect to eliminate some race conditions when the document changes.
82 changes: 56 additions & 26 deletions packages/dom/src/core/sensors/pointer/PointerSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,70 @@ export class PointerSensor extends Sensor<

target.addEventListener('pointerdown', listener);

// Re-bind listeners if dragging, in case document changed during effect
const unbindListeners = this.bindListeners(source, options);

this.cleanup.add(() => {
setTimeout(unbindListeners);
});

return () => {
target.removeEventListener('pointerdown', listener);
unbindListeners();
};
}
});

return unbind;
}

protected bindListeners(
source: Draggable,
options: PointerSensorOptions = {}
): () => void {
const target = source.handle ?? source.element;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was previously event.target


if (!target) {
return () => {};
}

// Only register if dragging
if (!this.initialCoordinates) {
return () => {};
}

const isNativeDraggable =
isHTMLElement(target) &&
target.draggable &&
target.getAttribute('draggable') === 'true';

const ownerDocument = getDocument(target);

const unbindListeners = this.listeners.bind(ownerDocument, [
{
type: 'pointermove',
listener: (event: PointerEvent) =>
this.handlePointerMove(event, source, options),
},
{
type: 'pointerup',
listener: this.handlePointerUp,
options: {
capture: true,
},
},
{
// Cancel activation if there is a competing Drag and Drop interaction
type: 'dragstart',
listener: isNativeDraggable ? this.handleCancel : preventDefault,
},
]);

return () => {
unbindListeners();
};
}

protected handlePointerDown(
event: PointerEvent,
source: Draggable,
Expand All @@ -106,11 +161,6 @@ export class PointerSensor extends Sensor<
) {
return;
}
const {target} = event;
const isNativeDraggable =
isHTMLElement(target) &&
target.draggable &&
target.getAttribute('draggable') === 'true';

const offset = getFrameTransform(source.element);

Expand Down Expand Up @@ -145,27 +195,7 @@ export class PointerSensor extends Sensor<
}
}

const ownerDocument = getDocument(event.target);

const unbindListeners = this.listeners.bind(ownerDocument, [
{
type: 'pointermove',
listener: (event: PointerEvent) =>
this.handlePointerMove(event, source, options),
},
{
type: 'pointerup',
listener: this.handlePointerUp,
options: {
capture: true,
},
},
{
// Cancel activation if there is a competing Drag and Drop interaction
type: 'dragstart',
listener: isNativeDraggable ? this.handleCancel : preventDefault,
},
]);
const unbindListeners = this.bindListeners(source, options);

const cleanup = () => {
setTimeout(unbindListeners);
Expand Down