@@ -21,6 +21,8 @@ export const openContextMenu = (id: string, data?: any, coords?: Coords) => {
21
21
22
22
var timeout = 0 ;
23
23
24
+ const iOS = ( ) => / ( i P a d | i P h o n e | i P o d ) / g. test ( navigator . userAgent ) || ( navigator . userAgent . includes ( "Mac" ) && "ontouchend" in document ) ;
25
+
24
26
/**
25
27
* Bind context menu events to a Ref.
26
28
* @param ref Ref object
@@ -30,35 +32,40 @@ var timeout = 0;
30
32
* @param touchTimeout Long press duration to show context menu
31
33
*/
32
34
export const useTriggerEvents = ( id : string , data ?: any , disabled ?: boolean , touchTimeout = 610 ) => {
33
- // For most browsers, we can use onContextMenu.
34
- const onContextMenu = ( event : JSX . TargetedMouseEvent < HTMLSpanElement > ) => {
35
- if ( disabled === true ) return ;
36
- openContextMenu ( id , data , { x : event . clientX , y : event . clientY } ) ;
37
- event . stopPropagation ( ) ;
38
- event . preventDefault ( ) ;
39
- } ;
35
+ if ( iOS ( ) ) {
36
+ // On iOS devices, we need to manually handle the touch events.
37
+ const onTouchStart = ( event : JSX . TargetedTouchEvent < HTMLSpanElement > ) => {
38
+ if ( disabled === true ) return ;
39
+ const touch = event . touches [ 0 ] ;
40
+
41
+ clearTimeout ( timeout ) ;
42
+ timeout = setTimeout ( ( ) => {
43
+ openContextMenu ( id , data , { x : touch . clientX , y : touch . clientY } ) ;
44
+ } , touchTimeout ) as unknown as number ;
45
+ } ;
40
46
41
- // On iOS devices, we need to manually handle the touch events.
42
- const onTouchStart = ( event : JSX . TargetedTouchEvent < HTMLSpanElement > ) => {
43
- if ( disabled === true ) return ;
44
- const touch = event . touches [ 0 ] ;
45
-
46
- clearTimeout ( timeout ) ;
47
- timeout = setTimeout ( ( ) => {
48
- openContextMenu ( id , data , { x : touch . clientX , y : touch . clientY } ) ;
49
- } , touchTimeout ) as unknown as number ;
50
- } ;
47
+ // Cancel context menu if we move, end or cancel the touch.
48
+ const onTouchCancel = ( ) => {
49
+ clearTimeout ( timeout ) ;
50
+ } ;
51
51
52
- // Cancel context menu if we move, end or cancel the touch.
53
- const onTouchCancel = ( ) => {
54
- clearTimeout ( timeout ) ;
55
- } ;
52
+ return {
53
+ onTouchStart,
54
+ onTouchCancel,
55
+ onTouchMove : onTouchCancel ,
56
+ onTouchEnd : onTouchCancel
57
+ }
58
+ } else {
59
+ // For most browsers, we can use onContextMenu.
60
+ const onContextMenu = ( event : JSX . TargetedMouseEvent < HTMLSpanElement > ) => {
61
+ if ( disabled === true ) return ;
62
+ openContextMenu ( id , data , { x : event . clientX , y : event . clientY } ) ;
63
+ event . stopPropagation ( ) ;
64
+ event . preventDefault ( ) ;
65
+ } ;
56
66
57
- return {
58
- onContextMenu,
59
- onTouchStart,
60
- onTouchCancel,
61
- onTouchMove : onTouchCancel ,
62
- onTouchEnd : onTouchCancel
67
+ return {
68
+ onContextMenu
69
+ }
63
70
}
64
71
}
0 commit comments