1
- import * as React from 'react' ;
2
- import { useRef } from 'react' ;
3
1
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect' ;
2
+ import type * as React from 'react' ;
3
+ import { useRef } from 'react' ;
4
4
5
5
const SMOOTH_PTG = 14 / 15 ;
6
6
7
7
export default function useMobileTouchMove (
8
8
inVirtual : boolean ,
9
9
listRef : React . RefObject < HTMLDivElement > ,
10
- callback : ( offsetY : number , smoothOffset ?: boolean ) => boolean ,
10
+ callback : ( isHorizontal : boolean , offset : number , smoothOffset ?: boolean ) => boolean ,
11
11
) {
12
12
const touchedRef = useRef ( false ) ;
13
+ const touchXRef = useRef ( 0 ) ;
13
14
const touchYRef = useRef ( 0 ) ;
14
15
15
16
const elementRef = useRef < HTMLElement > ( null ) ;
@@ -22,20 +23,30 @@ export default function useMobileTouchMove(
22
23
23
24
const onTouchMove = ( e : TouchEvent ) => {
24
25
if ( touchedRef . current ) {
26
+ const currentX = Math . ceil ( e . touches [ 0 ] . pageX ) ;
25
27
const currentY = Math . ceil ( e . touches [ 0 ] . pageY ) ;
28
+ let offsetX = touchXRef . current - currentX ;
26
29
let offsetY = touchYRef . current - currentY ;
27
- touchYRef . current = currentY ;
30
+ const isHorizontal = Math . abs ( offsetX ) > Math . abs ( offsetY ) ;
31
+ if ( isHorizontal ) {
32
+ touchXRef . current = currentX ;
33
+ } else {
34
+ touchYRef . current = currentY ;
35
+ }
28
36
29
- if ( callback ( offsetY ) ) {
37
+ if ( callback ( isHorizontal , isHorizontal ? offsetX : offsetY ) ) {
30
38
e . preventDefault ( ) ;
31
39
}
32
-
33
40
// Smooth interval
34
41
clearInterval ( intervalRef . current ) ;
35
42
intervalRef . current = setInterval ( ( ) => {
36
- offsetY *= SMOOTH_PTG ;
37
-
38
- if ( ! callback ( offsetY , true ) || Math . abs ( offsetY ) <= 0.1 ) {
43
+ if ( isHorizontal ) {
44
+ offsetX *= SMOOTH_PTG ;
45
+ } else {
46
+ offsetY *= SMOOTH_PTG ;
47
+ }
48
+ const offset = isHorizontal ? offsetX : offsetY ;
49
+ if ( ! callback ( isHorizontal , offset , true ) || Math . abs ( offset ) <= 0.1 ) {
39
50
clearInterval ( intervalRef . current ) ;
40
51
}
41
52
} , 16 ) ;
@@ -53,6 +64,7 @@ export default function useMobileTouchMove(
53
64
54
65
if ( e . touches . length === 1 && ! touchedRef . current ) {
55
66
touchedRef . current = true ;
67
+ touchXRef . current = Math . ceil ( e . touches [ 0 ] . pageX ) ;
56
68
touchYRef . current = Math . ceil ( e . touches [ 0 ] . pageY ) ;
57
69
58
70
elementRef . current = e . target as HTMLElement ;
0 commit comments