@@ -507,7 +507,12 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
507
507
) ;
508
508
509
509
const getMouseArgsForPosition = React . useCallback (
510
- ( canvas : HTMLCanvasElement , posX : number , posY : number , ev ?: MouseEvent | TouchEvent ) : GridMouseEventArgs => {
510
+ (
511
+ canvas : HTMLCanvasElement ,
512
+ posX : number ,
513
+ posY : number ,
514
+ ev ?: PointerEvent | MouseEvent | TouchEvent
515
+ ) : GridMouseEventArgs => {
511
516
const rect = canvas . getBoundingClientRect ( ) ;
512
517
const scale = rect . width / width ;
513
518
const x = ( posX - rect . left ) / scale ;
@@ -518,7 +523,16 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
518
523
519
524
let button = 0 ;
520
525
let buttons = 0 ;
521
- if ( ev instanceof MouseEvent ) {
526
+
527
+ const isMouse =
528
+ ( typeof PointerEvent !== "undefined" && ev instanceof PointerEvent && ev . pointerType === "mouse" ) ||
529
+ ( typeof MouseEvent !== "undefined" && ev instanceof MouseEvent ) ;
530
+
531
+ const isTouch =
532
+ ( typeof PointerEvent !== "undefined" && ev instanceof PointerEvent && ev . pointerType === "touch" ) ||
533
+ ( typeof TouchEvent !== "undefined" && ev instanceof TouchEvent ) ;
534
+
535
+ if ( isMouse ) {
522
536
button = ev . button ;
523
537
buttons = ev . buttons ;
524
538
}
@@ -544,7 +558,6 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
544
558
const shiftKey = ev ?. shiftKey === true ;
545
559
const ctrlKey = ev ?. ctrlKey === true ;
546
560
const metaKey = ev ?. metaKey === true ;
547
- const isTouch = ( ev !== undefined && ! ( ev instanceof MouseEvent ) ) || ( ev as any ) ?. pointerType === "touch" ;
548
561
549
562
const scrollEdge : GridMouseEventArgs [ "scrollEdge" ] = [
550
563
x < 0 ? - 1 : width < x ? 1 : 0 ,
@@ -1037,22 +1050,16 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
1037
1050
const downTime = React . useRef ( 0 ) ;
1038
1051
const downPosition = React . useRef < Item > ( ) ;
1039
1052
const mouseDown = React . useRef ( false ) ;
1040
- const onMouseDownImpl = React . useCallback (
1041
- ( ev : MouseEvent | TouchEvent ) => {
1053
+ const onPointerDown = React . useCallback (
1054
+ ( ev : PointerEvent ) => {
1042
1055
const canvas = ref . current ;
1043
1056
const eventTarget = eventTargetRef ?. current ;
1044
1057
if ( canvas === null || ( ev . target !== canvas && ev . target !== eventTarget ) ) return ;
1045
1058
mouseDown . current = true ;
1046
1059
1047
- let clientX : number ;
1048
- let clientY : number ;
1049
- if ( ev instanceof MouseEvent ) {
1050
- clientX = ev . clientX ;
1051
- clientY = ev . clientY ;
1052
- } else {
1053
- clientX = ev . touches [ 0 ] . clientX ;
1054
- clientY = ev . touches [ 0 ] . clientY ;
1055
- }
1060
+ const clientX = ev . clientX ;
1061
+ const clientY = ev . clientY ;
1062
+
1056
1063
if ( ev . target === eventTarget && eventTarget !== null ) {
1057
1064
const bounds = eventTarget . getBoundingClientRect ( ) ;
1058
1065
if ( clientX > bounds . right || clientY > bounds . bottom ) return ;
@@ -1101,13 +1108,12 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
1101
1108
onMouseDown ,
1102
1109
]
1103
1110
) ;
1104
- useEventListener ( "touchstart" , onMouseDownImpl , windowEventTarget , false ) ;
1105
- useEventListener ( "mousedown" , onMouseDownImpl , windowEventTarget , false ) ;
1111
+ useEventListener ( "pointerdown" , onPointerDown , windowEventTarget , false ) ;
1106
1112
1107
1113
const lastUpTime = React . useRef ( 0 ) ;
1108
1114
1109
- const onMouseUpImpl = React . useCallback (
1110
- ( ev : MouseEvent | TouchEvent ) => {
1115
+ const onPointerUp = React . useCallback (
1116
+ ( ev : PointerEvent ) => {
1111
1117
const lastUpTimeValue = lastUpTime . current ;
1112
1118
lastUpTime . current = Date . now ( ) ;
1113
1119
const canvas = ref . current ;
@@ -1116,21 +1122,9 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
1116
1122
const eventTarget = eventTargetRef ?. current ;
1117
1123
1118
1124
const isOutside = ev . target !== canvas && ev . target !== eventTarget ;
1119
-
1120
- let clientX : number ;
1121
- let clientY : number ;
1122
- let canCancel = true ;
1123
- if ( ev instanceof MouseEvent ) {
1124
- clientX = ev . clientX ;
1125
- clientY = ev . clientY ;
1126
- canCancel = ev . button < 3 ;
1127
- if ( ( ev as any ) . pointerType === "touch" ) {
1128
- return ;
1129
- }
1130
- } else {
1131
- clientX = ev . changedTouches [ 0 ] . clientX ;
1132
- clientY = ev . changedTouches [ 0 ] . clientY ;
1133
- }
1125
+ const clientX = ev . clientX ;
1126
+ const clientY = ev . clientY ;
1127
+ const canCancel = ev . pointerType === "mouse" ? ev . button < 3 : true ;
1134
1128
1135
1129
let args = getMouseArgsForPosition ( canvas , clientX , clientY , ev ) ;
1136
1130
@@ -1178,8 +1172,7 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
1178
1172
} ,
1179
1173
[ onMouseUp , eventTargetRef , getMouseArgsForPosition , isOverHeaderElement , groupHeaderActionForEvent ]
1180
1174
) ;
1181
- useEventListener ( "mouseup" , onMouseUpImpl , windowEventTarget , false ) ;
1182
- useEventListener ( "touchend" , onMouseUpImpl , windowEventTarget , false ) ;
1175
+ useEventListener ( "pointerup" , onPointerUp , windowEventTarget , false ) ;
1183
1176
1184
1177
const onClickImpl = React . useCallback (
1185
1178
( ev : MouseEvent | TouchEvent ) => {
@@ -1284,7 +1277,7 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
1284
1277
} , [ getCellContent , getCellRenderer , hoveredItem ] ) ;
1285
1278
1286
1279
const hoveredRef = React . useRef < GridMouseEventArgs > ( ) ;
1287
- const onMouseMoveImpl = React . useCallback (
1280
+ const onPointerMove = React . useCallback (
1288
1281
( ev : MouseEvent ) => {
1289
1282
const canvas = ref . current ;
1290
1283
if ( canvas === null ) return ;
@@ -1367,7 +1360,7 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
1367
1360
damageInternal ,
1368
1361
]
1369
1362
) ;
1370
- useEventListener ( "mousemove " , onMouseMoveImpl , windowEventTarget , true ) ;
1363
+ useEventListener ( "pointermove " , onPointerMove , windowEventTarget , true ) ;
1371
1364
1372
1365
const onKeyDownImpl = React . useCallback (
1373
1366
( event : React . KeyboardEvent < HTMLCanvasElement > ) => {
0 commit comments