Skip to content

Commit

Permalink
fix: When touch the slider while touching another area, the slider wi…
Browse files Browse the repository at this point in the history
…ll move with the first touch (#1037)
  • Loading branch information
sus-yoshikane-t authored Sep 17, 2024
1 parent 0b1f05b commit 622c6b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
21 changes: 14 additions & 7 deletions src/hooks/useDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { OffsetValues } from './useOffset';
const REMOVE_DIST = 130;

function getPosition(e: React.MouseEvent | React.TouchEvent | MouseEvent | TouchEvent) {
const obj = 'touches' in e ? e.touches[0] : e;
const obj = 'targetTouches' in e ? e.targetTouches[0] : e;

return { pageX: obj.pageX, pageY: obj.pageY };
}
Expand Down 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,13 @@ function useDrag(

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

finishChange(deleteMark);

Expand All @@ -206,10 +212,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
10 changes: 6 additions & 4 deletions tests/Range.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,18 @@ describe('Range', () => {
) {
const touchStart = createEvent.touchStart(container.getElementsByClassName(element)[0], {
touches: [{}],
targetTouches: [{}],
});
(touchStart as any).touches[0].pageX = start;
(touchStart as any).targetTouches[0].pageX = start;
fireEvent(container.getElementsByClassName(element)[0], touchStart);

// Drag
const touchMove = createEvent.touchMove(document, {
const touchMove = createEvent.touchMove(container.getElementsByClassName(element)[0], {
touches: [{}],
targetTouches: [{}],
});
(touchMove as any).touches[0].pageX = end;
fireEvent(document, touchMove);
(touchMove as any).targetTouches[0].pageX = end;
fireEvent(container.getElementsByClassName(element)[0], touchMove);
}

it('should render Range with correct DOM structure', () => {
Expand Down

0 comments on commit 622c6b6

Please sign in to comment.