Skip to content

Commit

Permalink
Defer some setup until activation constraints are met
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Aug 29, 2024
1 parent f219549 commit bfc8ab2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/pointer-sensor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/dom': patch
---

**PointerSensor**: Defer invoking `setPointerCapture` until activation constraints are met as it can interfere with `click` and other event handlers. Also deferred adding `touchmove`, `click` and `keydown` event listeners until the activation constraints are met.
48 changes: 27 additions & 21 deletions packages/dom/src/core/sensors/pointer/PointerSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export class PointerSensor extends Sensor<
}

const ownerDocument = getDocument(event.target);
ownerDocument.body.setPointerCapture(event.pointerId);

const unbindListeners = this.listeners.bind(ownerDocument, [
{
Expand All @@ -152,37 +151,17 @@ export class PointerSensor extends Sensor<
capture: true,
},
},
{
// Prevent scrolling on touch devices
type: 'touchmove',
listener: preventDefault,
options: {
passive: false,
},
},
{
// Prevent click events
type: 'click',
listener: preventDefault,
},
{
// Cancel activation if there is a competing Drag and Drop interaction
type: 'dragstart',
listener: isNativeDraggable ? this.handleCancel : preventDefault,
},
{
type: 'keydown',
listener: this.handleKeyDown,
},
]);

const cleanup = () => {
console.log('cleanup');
setTimeout(unbindListeners);

this.#clearTimeout?.();
this.initialCoordinates = undefined;
this.cleanup.delete(cleanup);
};

this.cleanup.add(cleanup);
Expand Down Expand Up @@ -255,6 +234,7 @@ export class PointerSensor extends Sensor<

// Remove the pointer move and up event listeners
this.cleanup.forEach((cleanup) => cleanup());
this.cleanup.clear();
}

protected handleKeyDown(event: KeyboardEvent) {
Expand Down Expand Up @@ -283,6 +263,31 @@ export class PointerSensor extends Sensor<
manager.actions.setDragSource(source.id);
manager.actions.start({coordinates: initialCoordinates, event});
});

const ownerDocument = getDocument(event.target);
const unbind = this.listeners.bind(ownerDocument, [
{
// Prevent scrolling on touch devices
type: 'touchmove',
listener: preventDefault,
options: {
passive: false,
},
},
{
// Prevent click events
type: 'click',
listener: preventDefault,
},
{
type: 'keydown',
listener: this.handleKeyDown,
},
]);

ownerDocument.body.setPointerCapture(event.pointerId);

this.cleanup.add(unbind);
}

protected handleCancel() {
Expand All @@ -294,6 +299,7 @@ export class PointerSensor extends Sensor<

// Remove the pointer move and up event listeners
this.cleanup.forEach((cleanup) => cleanup());
this.cleanup.clear();
}

public destroy() {
Expand Down

0 comments on commit bfc8ab2

Please sign in to comment.