@@ -19,7 +19,7 @@ import {chain, focusWithoutScrolling, getOwnerDocument, getOwnerWindow, isMac, i
19
19
import { disableTextSelection , restoreTextSelection } from './textSelection' ;
20
20
import { DOMAttributes , FocusableElement , PressEvent as IPressEvent , PointerType , PressEvents } from '@react-types/shared' ;
21
21
import { PressResponderContext } from './context' ;
22
- import { RefObject , useContext , useEffect , useMemo , useRef , useState } from 'react' ;
22
+ import { RefObject , TouchEvent as RTouchEvent , useContext , useEffect , useMemo , useRef , useState } from 'react' ;
23
23
24
24
export interface PressProps extends PressEvents {
25
25
/** Whether the target is in a controlled press state (e.g. an overlay it triggers is open). */
@@ -63,7 +63,10 @@ interface EventBase {
63
63
shiftKey : boolean ,
64
64
ctrlKey : boolean ,
65
65
metaKey : boolean ,
66
- altKey : boolean
66
+ altKey : boolean ,
67
+ clientX ?: number ,
68
+ clientY ?: number ,
69
+ targetTouches ?: Array < { clientX ?: number , clientY ?: number } >
67
70
}
68
71
69
72
export interface PressResult {
@@ -94,16 +97,37 @@ class PressEvent implements IPressEvent {
94
97
ctrlKey : boolean ;
95
98
metaKey : boolean ;
96
99
altKey : boolean ;
100
+ x : number ;
101
+ y : number ;
97
102
#shouldStopPropagation = true ;
98
103
99
- constructor ( type : IPressEvent [ 'type' ] , pointerType : PointerType , originalEvent : EventBase ) {
104
+ constructor ( type : IPressEvent [ 'type' ] , pointerType : PointerType , originalEvent : EventBase , state ?: PressState ) {
105
+ let currentTarget = state ?. target ?? originalEvent . currentTarget ;
106
+ const rect : DOMRect | undefined = ( currentTarget as Element ) ?. getBoundingClientRect ( ) ;
107
+ let x , y = 0 ;
108
+ let clientX , clientY : number | null = null ;
109
+ if ( originalEvent . clientX != null && originalEvent . clientY != null ) {
110
+ clientX = originalEvent . clientX ;
111
+ clientY = originalEvent . clientY ;
112
+ }
113
+ if ( rect ) {
114
+ if ( clientX != null && clientY != null ) {
115
+ x = clientX - rect . left ;
116
+ y = clientY - rect . top ;
117
+ } else {
118
+ x = rect . width / 2 ;
119
+ y = rect . height / 2 ;
120
+ }
121
+ }
100
122
this . type = type ;
101
123
this . pointerType = pointerType ;
102
124
this . target = originalEvent . currentTarget as Element ;
103
125
this . shiftKey = originalEvent . shiftKey ;
104
126
this . metaKey = originalEvent . metaKey ;
105
127
this . ctrlKey = originalEvent . ctrlKey ;
106
128
this . altKey = originalEvent . altKey ;
129
+ this . x = x ;
130
+ this . y = y ;
107
131
}
108
132
109
133
continuePropagation ( ) {
@@ -628,7 +652,7 @@ export function usePress(props: PressHookProps): PressResult {
628
652
disableTextSelection ( state . target ) ;
629
653
}
630
654
631
- let shouldStopPropagation = triggerPressStart ( e , state . pointerType ) ;
655
+ let shouldStopPropagation = triggerPressStart ( createTouchEvent ( state . target , e ) , state . pointerType ) ;
632
656
if ( shouldStopPropagation ) {
633
657
e . stopPropagation ( ) ;
634
658
}
@@ -651,12 +675,12 @@ export function usePress(props: PressHookProps): PressResult {
651
675
if ( touch && isOverTarget ( touch , e . currentTarget ) ) {
652
676
if ( ! state . isOverTarget && state . pointerType != null ) {
653
677
state . isOverTarget = true ;
654
- shouldStopPropagation = triggerPressStart ( e , state . pointerType ) ;
678
+ shouldStopPropagation = triggerPressStart ( createTouchEvent ( state . target ! , e ) , state . pointerType ) ;
655
679
}
656
680
} else if ( state . isOverTarget && state . pointerType != null ) {
657
681
state . isOverTarget = false ;
658
- shouldStopPropagation = triggerPressEnd ( e , state . pointerType , false ) ;
659
- cancelOnPointerExit ( e ) ;
682
+ shouldStopPropagation = triggerPressEnd ( createTouchEvent ( state . target ! , e ) , state . pointerType , false ) ;
683
+ cancelOnPointerExit ( createTouchEvent ( state . target ! , e ) ) ;
660
684
}
661
685
662
686
if ( shouldStopPropagation ) {
@@ -677,10 +701,10 @@ export function usePress(props: PressHookProps): PressResult {
677
701
let touch = getTouchById ( e . nativeEvent , state . activePointerId ) ;
678
702
let shouldStopPropagation = true ;
679
703
if ( touch && isOverTarget ( touch , e . currentTarget ) && state . pointerType != null ) {
680
- triggerPressUp ( e , state . pointerType ) ;
681
- shouldStopPropagation = triggerPressEnd ( e , state . pointerType ) ;
704
+ triggerPressUp ( createTouchEvent ( state . target ! , e ) , state . pointerType ) ;
705
+ shouldStopPropagation = triggerPressEnd ( createTouchEvent ( state . target ! , e ) , state . pointerType ) ;
682
706
} else if ( state . isOverTarget && state . pointerType != null ) {
683
- shouldStopPropagation = triggerPressEnd ( e , state . pointerType , false ) ;
707
+ shouldStopPropagation = triggerPressEnd ( createTouchEvent ( state . target ! , e ) , state . pointerType , false ) ;
684
708
}
685
709
686
710
if ( shouldStopPropagation ) {
@@ -704,7 +728,7 @@ export function usePress(props: PressHookProps): PressResult {
704
728
705
729
e . stopPropagation ( ) ;
706
730
if ( state . isPressed ) {
707
- cancel ( e ) ;
731
+ cancel ( createTouchEvent ( state . target ! , e ) ) ;
708
732
}
709
733
} ;
710
734
@@ -802,13 +826,35 @@ function getTouchById(
802
826
return null ;
803
827
}
804
828
829
+ function createTouchEvent ( target : FocusableElement , e : RTouchEvent < FocusableElement > ) : EventBase {
830
+ let clientX = 0 ;
831
+ let clientY = 0 ;
832
+ if ( e . targetTouches && e . targetTouches . length === 1 ) {
833
+ clientX = e . targetTouches [ 0 ] . clientX ;
834
+ clientY = e . targetTouches [ 0 ] . clientY ;
835
+ }
836
+ return {
837
+ currentTarget : target ,
838
+ shiftKey : e . shiftKey ,
839
+ ctrlKey : e . ctrlKey ,
840
+ metaKey : e . metaKey ,
841
+ altKey : e . altKey ,
842
+ clientX,
843
+ clientY
844
+ } ;
845
+ }
846
+
805
847
function createEvent ( target : FocusableElement , e : EventBase ) : EventBase {
848
+ let clientX = e . clientX ;
849
+ let clientY = e . clientY ;
806
850
return {
807
851
currentTarget : target ,
808
852
shiftKey : e . shiftKey ,
809
853
ctrlKey : e . ctrlKey ,
810
854
metaKey : e . metaKey ,
811
- altKey : e . altKey
855
+ altKey : e . altKey ,
856
+ clientX,
857
+ clientY
812
858
} ;
813
859
}
814
860
0 commit comments