1
- import { Component , createRef , CSSProperties , MouseEventHandler , ReactNode } from 'react'
1
+ import { Component , createRef , CSSProperties , ReactNode } from 'react'
2
2
import { getParentPosition } from '../utility/dom-helpers'
3
3
4
4
type Props = {
@@ -17,7 +17,7 @@ type State = {
17
17
}
18
18
19
19
class ScrollElement extends Component < Props , State > {
20
- scrollComponentRef = createRef < HTMLDivElement > ( ) ;
20
+ scrollComponentRef = createRef < HTMLDivElement > ( )
21
21
private dragLastPosition : number | null = null
22
22
private lastTouchDistance : number | null = null
23
23
private singleTouchStart : { x : number ; y : number ; screenY : number } | null = null
@@ -30,14 +30,40 @@ class ScrollElement extends Component<Props, State> {
30
30
}
31
31
}
32
32
componentDidMount ( ) {
33
- if ( this . scrollComponentRef . current ) {
34
- this . props . scrollRef ( this . scrollComponentRef . current )
35
- this . scrollComponentRef . current . addEventListener ( 'wheel' , this . handleWheel , { passive : false } )
36
- this . scrollComponentRef . current . addEventListener ( 'itemInteraction' , this . handleItemInteract )
37
- this . scrollComponentRef . current . addEventListener ( 'touchstart' , this . handleTouchStart , { passive : false } )
38
- this . scrollComponentRef . current . addEventListener ( "touchmove" , this . handleTouchMove , { passive : false } )
39
- }
33
+ if ( this . scrollComponentRef . current ) {
34
+ this . props . scrollRef ( this . scrollComponentRef . current )
35
+ this . scrollComponentRef . current . addEventListener ( 'wheel' , this . handleWheel , { passive : false } )
36
+ this . scrollComponentRef . current . addEventListener ( 'itemInteraction' , this . handleItemInteract )
37
+ this . scrollComponentRef . current . addEventListener ( 'pointerdown' , this . handlePointerStart , { passive : false } )
38
+ this . scrollComponentRef . current . addEventListener ( 'pointermove' , this . handlePointerMove , { passive : false } )
39
+ this . scrollComponentRef . current . addEventListener ( 'pointerup' , this . handlePointerEnd )
40
+ this . scrollComponentRef . current . addEventListener ( 'pointerleave' , this . handlePointerLeave )
41
+ }
42
+ }
43
+
44
+ handlePointerStart = ( e : PointerEvent ) => {
45
+ if ( e . pointerType === 'touch' ) {
46
+ this . handleTouchStart ( e )
47
+ } else if ( e . pointerType === 'mouse' ) {
48
+ this . handleMouseDown ( e )
49
+ }
50
+ }
51
+
52
+ handlePointerMove = ( e : PointerEvent ) => {
53
+ if ( e . pointerType === 'touch' ) {
54
+ this . handleTouchMove ( e )
55
+ } else if ( e . pointerType === 'mouse' ) {
56
+ this . handleMouseMove ( e )
40
57
}
58
+ }
59
+
60
+ handlePointerEnd = ( e : PointerEvent ) => {
61
+ if ( e . pointerType === 'touch' ) {
62
+ this . handleTouchEnd ( )
63
+ } else if ( e . pointerType === 'mouse' ) {
64
+ this . handleMouseUp ( )
65
+ }
66
+ }
41
67
42
68
/**
43
69
* needed to handle scrolling with trackpad
@@ -47,8 +73,6 @@ class ScrollElement extends Component<Props, State> {
47
73
this . props . onScroll ( scrollX )
48
74
}
49
75
50
-
51
-
52
76
handleWheel = ( e : WheelEvent ) => {
53
77
//const { traditionalZoom } = this.props
54
78
@@ -70,7 +94,7 @@ class ScrollElement extends Component<Props, State> {
70
94
}
71
95
}
72
96
73
- handleMouseDown : MouseEventHandler < HTMLDivElement > = ( e ) => {
97
+ handleMouseDown = ( e : PointerEvent ) => {
74
98
if ( e . button === 0 ) {
75
99
this . dragLastPosition = e . pageX
76
100
this . setState ( {
@@ -79,8 +103,7 @@ class ScrollElement extends Component<Props, State> {
79
103
}
80
104
}
81
105
82
- handleMouseMove : MouseEventHandler < HTMLDivElement > = ( e ) => {
83
- // this.props.onMouseMove(e)
106
+ handleMouseMove = ( e : PointerEvent ) => {
84
107
//why is interacting with item important?
85
108
if ( this . state . isDragging && ! this . isItemInteraction ) {
86
109
this . props . onScroll ( this . scrollComponentRef . current ! . scrollLeft + this . dragLastPosition ! - e . pageX )
@@ -96,52 +119,48 @@ class ScrollElement extends Component<Props, State> {
96
119
} )
97
120
}
98
121
99
- handleMouseLeave = ( ) => {
100
- // this.props.onMouseLeave(e)
101
- this . dragLastPosition = null
102
- this . setState ( {
103
- isDragging : false ,
104
- } )
122
+ handlePointerLeave = ( e : PointerEvent ) => {
123
+ if ( e . pointerType === 'mouse' ) {
124
+ this . dragLastPosition = null
125
+ this . setState ( {
126
+ isDragging : false ,
127
+ } )
128
+ }
105
129
}
106
130
107
- handleTouchStart = ( e :TouchEvent ) => {
108
- if ( e . touches . length === 2 ) {
131
+ handleTouchStart = ( e : PointerEvent ) => {
132
+ if ( e . isPrimary ) {
109
133
e . preventDefault ( )
110
-
111
- this . lastTouchDistance = Math . abs ( e . touches [ 0 ] . screenX - e . touches [ 1 ] . screenX )
134
+ this . lastTouchDistance = null
135
+ this . singleTouchStart = { x : e . clientX , y : e . clientY , screenY : window . scrollY }
136
+ this . lastSingleTouch = { x : e . clientX , y : e . clientY , screenY : window . scrollY }
137
+ } else {
138
+ e . preventDefault ( )
139
+ this . lastTouchDistance = Math . abs ( e . clientX - this . singleTouchStart ! . x )
112
140
this . singleTouchStart = null
113
141
this . lastSingleTouch = null
114
- } else if ( e . touches . length === 1 ) {
115
- e . preventDefault ( )
116
-
117
- const x = e . touches [ 0 ] . clientX
118
- const y = e . touches [ 0 ] . clientY
119
-
120
- this . lastTouchDistance = null
121
- this . singleTouchStart = { x : x , y : y , screenY : window . pageYOffset }
122
- this . lastSingleTouch = { x : x , y : y , screenY : window . pageYOffset }
123
142
}
124
143
}
125
144
126
- handleTouchMove = ( e :TouchEvent ) => {
145
+ handleTouchMove = ( e : PointerEvent ) => {
127
146
const { width, onZoom } = this . props
128
147
if ( this . isItemInteraction ) {
129
148
e . preventDefault ( )
130
149
return
131
150
}
132
- if ( this . lastTouchDistance && e . touches . length === 2 ) {
151
+ if ( this . lastTouchDistance && ! e . isPrimary ) {
133
152
e . preventDefault ( )
134
- const touchDistance = Math . abs ( e . touches [ 0 ] . screenX - e . touches [ 1 ] . screenX )
153
+ const touchDistance = Math . abs ( e . clientX - this . singleTouchStart ! . x )
135
154
const parentPosition = getParentPosition ( e . currentTarget as HTMLElement )
136
- const xPosition = ( e . touches [ 0 ] . screenX + e . touches [ 1 ] . screenX ) / 2 - parentPosition . x
155
+ const xPosition = ( e . clientX + this . singleTouchStart ! . x ) / 2 - parentPosition . x
137
156
if ( touchDistance !== 0 && this . lastTouchDistance !== 0 ) {
138
157
onZoom ( this . lastTouchDistance / touchDistance , xPosition / width )
139
158
this . lastTouchDistance = touchDistance
140
159
}
141
- } else if ( this . lastSingleTouch && e . touches . length === 1 ) {
160
+ } else if ( this . lastSingleTouch && e . isPrimary ) {
142
161
e . preventDefault ( )
143
- const x = e . touches [ 0 ] . clientX
144
- const y = e . touches [ 0 ] . clientY
162
+ const x = e . clientX
163
+ const y = e . clientY
145
164
const deltaX = x - this . lastSingleTouch . x
146
165
const deltaX0 = x - this . singleTouchStart ! . x
147
166
const deltaY0 = y - this . singleTouchStart ! . y
@@ -174,8 +193,10 @@ class ScrollElement extends Component<Props, State> {
174
193
if ( this . scrollComponentRef . current ) {
175
194
this . scrollComponentRef . current . removeEventListener ( 'wheel' , this . handleWheel )
176
195
this . scrollComponentRef . current . removeEventListener ( 'itemInteraction' , this . handleItemInteract )
177
- this . scrollComponentRef . current . removeEventListener ( 'touchstart' , this . handleTouchStart )
178
- this . scrollComponentRef . current . removeEventListener ( "touchmove" , this . handleTouchMove )
196
+ this . scrollComponentRef . current . removeEventListener ( 'pointerdown' , this . handlePointerStart )
197
+ this . scrollComponentRef . current . removeEventListener ( 'pointermove' , this . handlePointerMove )
198
+ this . scrollComponentRef . current . removeEventListener ( 'pointerup' , this . handlePointerEnd )
199
+ this . scrollComponentRef . current . removeEventListener ( 'pointerleave' , this . handlePointerLeave )
179
200
}
180
201
}
181
202
@@ -196,13 +217,6 @@ class ScrollElement extends Component<Props, State> {
196
217
data-testid = "scroll-element"
197
218
className = "rct-scroll"
198
219
style = { scrollComponentStyle }
199
- onMouseDown = { this . handleMouseDown }
200
- onMouseMove = { this . handleMouseMove }
201
- onMouseUp = { this . handleMouseUp }
202
- onMouseLeave = { this . handleMouseLeave }
203
- // onTouchStart={this.handleTouchStart}
204
- // onTouchMove={this.handleTouchMove}
205
- onTouchEnd = { this . handleTouchEnd }
206
220
onScroll = { this . handleScroll }
207
221
>
208
222
{ children }
0 commit comments