From 41528d1f1b3a30be2d371dd6344565fae1945da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claud=C3=A9ric=20Demers?= Date: Mon, 6 Nov 2023 16:42:48 -0500 Subject: [PATCH] Introduce `bypassActivationConstraints` option --- .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..a6610a87 --- /dev/null +++ b/.changeset/bypass-activation-constraints.md @@ -0,0 +1,17 @@ +--- +'@dnd-kit/core': minor +--- + +Introduce `bypassActivationConstraints()` 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, + }, + bypassActivationConstraints({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..207390ce 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; + bypassActivationConstraints?( + props: Pick + ): boolean; onActivation?({event}: {event: Event}): void; } @@ -100,7 +103,7 @@ export class AbstractPointerSensor implements SensorInstance { const { events, props: { - options: {activationConstraint}, + options: {activationConstraint, bypassActivationConstraints}, }, } = this; @@ -113,6 +116,16 @@ export class AbstractPointerSensor implements SensorInstance { this.documentListeners.add(EventName.Keydown, this.handleKeydown); if (activationConstraint) { + if ( + bypassActivationConstraints?.({ + event: this.props.event, + activeNode: this.props.activeNode, + options: this.props.options, + }) + ) { + return this.handleStart(); + } + if (isDelayConstraint(activationConstraint)) { this.timeoutId = setTimeout( this.handleStart,