Skip to content

Commit

Permalink
use currentTarget and save TouchEvent target
Browse files Browse the repository at this point in the history
  • Loading branch information
sus-yoshikane-t committed Sep 17, 2024
1 parent 7643b38 commit 1ae5624
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/hooks/useDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function useDrag(

const mouseMoveEventRef = React.useRef<(event: MouseEvent) => void>(null);
const mouseUpEventRef = React.useRef<(event: MouseEvent) => void>(null);
const touchEventTargetRef = React.useRef<EventTarget>(null);

const { onDragStart, onDragChange } = React.useContext(UnstableContext);

Expand All @@ -54,8 +55,10 @@ function useDrag(
() => () => {
document.removeEventListener('mousemove', mouseMoveEventRef.current);
document.removeEventListener('mouseup', mouseUpEventRef.current);
document.removeEventListener('touchmove', mouseMoveEventRef.current);
document.removeEventListener('touchend', mouseUpEventRef.current);
if (touchEventTargetRef.current) {
touchEventTargetRef.current.removeEventListener('touchmove', mouseMoveEventRef.current);
touchEventTargetRef.current.removeEventListener('touchend', mouseUpEventRef.current);
}
},
[],
);
Expand Down Expand Up @@ -193,10 +196,11 @@ function useDrag(

document.removeEventListener('mouseup', onMouseUp);
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('touchend', onMouseUp);
document.removeEventListener('touchmove', onMouseMove);
event.currentTarget.removeEventListener('touchend', onMouseUp);
event.currentTarget.removeEventListener('touchmove', onMouseMove);
mouseMoveEventRef.current = null;
mouseUpEventRef.current = null;
touchEventTargetRef.current = null;

finishChange(deleteMark);

Expand All @@ -206,10 +210,11 @@ function useDrag(

document.addEventListener('mouseup', onMouseUp);
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('touchend', onMouseUp);
document.addEventListener('touchmove', onMouseMove);
e.currentTarget.addEventListener('touchend', onMouseUp);
e.currentTarget.addEventListener('touchmove', onMouseMove);
mouseMoveEventRef.current = onMouseMove;
mouseUpEventRef.current = onMouseUp;
touchEventTargetRef.current = e.currentTarget;
};

// Only return cache value when it mapping with rawValues
Expand Down

0 comments on commit 1ae5624

Please sign in to comment.