Skip to content

Commit

Permalink
fix: Draggable shouldn't trigger dragEnd without first dragging (#921)
Browse files Browse the repository at this point in the history
- the `onDragEnd` was called every time a cell was clicked even when user was not even dragging, we should make sure to only trigger `onDragEnd` if a drag actually started to avoid triggering too many events for no reasons
  • Loading branch information
ghiscoding authored Nov 23, 2023
1 parent 1a93a64 commit 012b74c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/slick.interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,19 @@ export function Draggable(options: DraggableOption) {
}

function userReleased(event: MouseEvent | TouchEvent) {
const { target } = event;
originaldd = Object.assign(originaldd, { target });
executeDragCallbackWhenDefined(onDragEnd, event, originaldd as DragItem);
document.body.removeEventListener('mousemove', userMoved);
document.body.removeEventListener('touchmove', userMoved);
document.body.removeEventListener('mouseup', userReleased);
document.body.removeEventListener('touchend', userReleased);
document.body.removeEventListener('touchcancel', userReleased);
dragStarted = false;

// trigger a dragEnd event only after dragging started and stopped
if (dragStarted) {
const { target } = event;
originaldd = Object.assign(originaldd, { target });
executeDragCallbackWhenDefined(onDragEnd, event, originaldd as DragItem);
dragStarted = false;
}
}

// initialize Slick.MouseWheel by attaching mousewheel event
Expand Down

0 comments on commit 012b74c

Please sign in to comment.