Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce bypassActivationConstraint option #1271

Merged
merged 1 commit into from
Nov 6, 2023
Merged
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
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
Loading