Skip to content

Commit

Permalink
Introduce bypassActivationConstraints option (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic authored Nov 6, 2023
1 parent b417f0f commit bc588c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .changeset/bypass-activation-constraints.md
Original file line number Diff line number Diff line change
@@ -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);
},
});
```
15 changes: 14 additions & 1 deletion packages/core/src/sensors/pointer/AbstractPointerSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ function isDelayConstraint(

export interface AbstractPointerSensorOptions extends SensorOptions {
activationConstraint?: PointerActivationConstraint;
bypassActivationConstraint?(
props: Pick<AbstractPointerSensorProps, 'activeNode' | 'event' | 'options'>
): boolean;
onActivation?({event}: {event: Event}): void;
}

Expand Down Expand Up @@ -100,7 +103,7 @@ export class AbstractPointerSensor implements SensorInstance {
const {
events,
props: {
options: {activationConstraint},
options: {activationConstraint, bypassActivationConstraint},
},
} = this;

Expand All @@ -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,
Expand Down

0 comments on commit bc588c7

Please sign in to comment.