From bc588c7f7b1124514b2834e14f9dad29ce49c8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claud=C3=A9ric=20Demers?= Date: Mon, 6 Nov 2023 16:50:50 -0500 Subject: [PATCH] Introduce `bypassActivationConstraints` option (#1271) --- .changeset/bypass-activation-constraints.md | 17 +++++++++++++++++ .../sensors/pointer/AbstractPointerSensor.ts | 15 ++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 .changeset/bypass-activation-constraints.md diff --git a/.changeset/bypass-activation-constraints.md b/.changeset/bypass-activation-constraints.md new file mode 100644 index 00000000..1214c541 --- /dev/null +++ b/.changeset/bypass-activation-constraints.md @@ -0,0 +1,17 @@ +--- +'@dnd-kit/core': minor +--- + +Introduce `bypassActivationConstraint()` option for `PointerSensor`, `MouseSensor` and `TouchSensor`. This optional argument can be used to conditionally bypass activation constraints. An example use-case would be to bypass activation constraints when the activator event target is the `activatorNode` of a draggable source. + +```tsx +useSensor(PointerSensor, { + activationConstraint: { + delay: 250, + tolerance: 5, + }, + bypassActivationConstraint({event, activeNode}) { + return activeNode.activatorNode.current?.contains(event.target); + }, +}); +``` diff --git a/packages/core/src/sensors/pointer/AbstractPointerSensor.ts b/packages/core/src/sensors/pointer/AbstractPointerSensor.ts index 3f87cd55..5c39f6de 100644 --- a/packages/core/src/sensors/pointer/AbstractPointerSensor.ts +++ b/packages/core/src/sensors/pointer/AbstractPointerSensor.ts @@ -55,6 +55,9 @@ function isDelayConstraint( export interface AbstractPointerSensorOptions extends SensorOptions { activationConstraint?: PointerActivationConstraint; + bypassActivationConstraint?( + props: Pick + ): boolean; onActivation?({event}: {event: Event}): void; } @@ -100,7 +103,7 @@ export class AbstractPointerSensor implements SensorInstance { const { events, props: { - options: {activationConstraint}, + options: {activationConstraint, bypassActivationConstraint}, }, } = this; @@ -113,6 +116,16 @@ export class AbstractPointerSensor implements SensorInstance { this.documentListeners.add(EventName.Keydown, this.handleKeydown); if (activationConstraint) { + if ( + bypassActivationConstraint?.({ + event: this.props.event, + activeNode: this.props.activeNode, + options: this.props.options, + }) + ) { + return this.handleStart(); + } + if (isDelayConstraint(activationConstraint)) { this.timeoutId = setTimeout( this.handleStart,