Skip to content

Commit 622c6b6

Browse files
fix: When touch the slider while touching another area, the slider will move with the first touch (#1037)
1 parent 0b1f05b commit 622c6b6

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/hooks/useDrag.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { OffsetValues } from './useOffset';
88
const REMOVE_DIST = 130;
99

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

1313
return { pageX: obj.pageX, pageY: obj.pageY };
1414
}
@@ -40,6 +40,7 @@ function useDrag(
4040

4141
const mouseMoveEventRef = React.useRef<(event: MouseEvent) => void>(null);
4242
const mouseUpEventRef = React.useRef<(event: MouseEvent) => void>(null);
43+
const touchEventTargetRef = React.useRef<EventTarget>(null);
4344

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

@@ -54,8 +55,10 @@ function useDrag(
5455
() => () => {
5556
document.removeEventListener('mousemove', mouseMoveEventRef.current);
5657
document.removeEventListener('mouseup', mouseUpEventRef.current);
57-
document.removeEventListener('touchmove', mouseMoveEventRef.current);
58-
document.removeEventListener('touchend', mouseUpEventRef.current);
58+
if (touchEventTargetRef.current) {
59+
touchEventTargetRef.current.removeEventListener('touchmove', mouseMoveEventRef.current);
60+
touchEventTargetRef.current.removeEventListener('touchend', mouseUpEventRef.current);
61+
}
5962
},
6063
[],
6164
);
@@ -193,10 +196,13 @@ function useDrag(
193196

194197
document.removeEventListener('mouseup', onMouseUp);
195198
document.removeEventListener('mousemove', onMouseMove);
196-
document.removeEventListener('touchend', onMouseUp);
197-
document.removeEventListener('touchmove', onMouseMove);
199+
if (touchEventTargetRef.current) {
200+
touchEventTargetRef.current.removeEventListener('touchmove', mouseMoveEventRef.current);
201+
touchEventTargetRef.current.removeEventListener('touchend', mouseUpEventRef.current);
202+
}
198203
mouseMoveEventRef.current = null;
199204
mouseUpEventRef.current = null;
205+
touchEventTargetRef.current = null;
200206

201207
finishChange(deleteMark);
202208

@@ -206,10 +212,11 @@ function useDrag(
206212

207213
document.addEventListener('mouseup', onMouseUp);
208214
document.addEventListener('mousemove', onMouseMove);
209-
document.addEventListener('touchend', onMouseUp);
210-
document.addEventListener('touchmove', onMouseMove);
215+
e.currentTarget.addEventListener('touchend', onMouseUp);
216+
e.currentTarget.addEventListener('touchmove', onMouseMove);
211217
mouseMoveEventRef.current = onMouseMove;
212218
mouseUpEventRef.current = onMouseUp;
219+
touchEventTargetRef.current = e.currentTarget;
213220
};
214221

215222
// Only return cache value when it mapping with rawValues

tests/Range.test.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,18 @@ describe('Range', () => {
8080
) {
8181
const touchStart = createEvent.touchStart(container.getElementsByClassName(element)[0], {
8282
touches: [{}],
83+
targetTouches: [{}],
8384
});
84-
(touchStart as any).touches[0].pageX = start;
85+
(touchStart as any).targetTouches[0].pageX = start;
8586
fireEvent(container.getElementsByClassName(element)[0], touchStart);
8687

8788
// Drag
88-
const touchMove = createEvent.touchMove(document, {
89+
const touchMove = createEvent.touchMove(container.getElementsByClassName(element)[0], {
8990
touches: [{}],
91+
targetTouches: [{}],
9092
});
91-
(touchMove as any).touches[0].pageX = end;
92-
fireEvent(document, touchMove);
93+
(touchMove as any).targetTouches[0].pageX = end;
94+
fireEvent(container.getElementsByClassName(element)[0], touchMove);
9395
}
9496

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

0 commit comments

Comments
 (0)