Skip to content

Canceling drag operation when using the closest center/corners collision detection #210

Answered by clauderic
dpavicic-ts asked this question in Q&A
Discussion options

You must be logged in to vote

You can always "cancel" drag operations by not handling the onDragEnd event for a given operation.

As for collision detection, you can fork the closestCenter collision detection and create your own custom collision detection algorithm that does not match droppables if they are further than a certain distance:

import {min} from 'lodash';
import type {Coordinates, CollisionDetection, LayoutRect} from '@dnd-kit/core';

/**
 * Returns the distance between two points
 */
function distanceBetween(p1: Coordinates, p2: Coordinates) {
  return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}

/**
 * Returns the coordinates of the center of a given ClientRect
 */
function centerOfR…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by clauderic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants