@@ -5,6 +5,8 @@ type Args = {
5
5
scrollYAxis ?: boolean
6
6
enable ?: boolean
7
7
ref : React . MutableRefObject < HTMLDivElement | null >
8
+ onDrag : ( ) => void
9
+ onDragEnd : ( ) => void
8
10
}
9
11
10
12
export type UseDraggable = ( args ?: Args ) => null // eslint-disable-line no-unused-vars
@@ -14,7 +16,9 @@ export const useDraggable: UseDraggable = (args) => {
14
16
buttons = [ 1 , 4 , 5 ] , // See https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
15
17
scrollYAxis,
16
18
enable,
17
- ref
19
+ ref,
20
+ onDrag,
21
+ onDragEnd
18
22
} = args || { } ;
19
23
20
24
// Position of the mouse on the page on mousedown
@@ -37,6 +41,9 @@ export const useDraggable: UseDraggable = (args) => {
37
41
setStartY ( e . pageY - ref . current . offsetTop ) ;
38
42
setStartScrollLeft ( ref . current . scrollLeft ) ;
39
43
setStartScrollTop ( ref . current . scrollTop ) ;
44
+ if ( typeof onDrag === 'function' ) {
45
+ onDrag ( ) ;
46
+ }
40
47
}
41
48
} ;
42
49
@@ -57,15 +64,16 @@ export const useDraggable: UseDraggable = (args) => {
57
64
}
58
65
59
66
e . preventDefault ( ) ;
67
+
60
68
// Position of mouse on the page
61
69
const mouseX = e . pageX - ref . current . offsetLeft ;
62
70
const mouseY = e . pageY - ref . current . offsetTop ;
63
71
// Distance of the mouse from the origin of the last mousedown event
64
- const walkX = mouseX - startX ;
65
- const walkY = mouseY - startY ;
66
- // Set element scroll
67
- ref . current . scrollLeft = startScrollLeft - walkX ;
68
- const newScrollTop = startScrollTop - walkY ;
72
+ const xDisplacement = mouseX - startX ;
73
+ const yDisplacement = mouseY - startY ;
74
+ // Finally, set the element's scroll
75
+ ref . current . scrollLeft = startScrollLeft - xDisplacement ;
76
+ const newScrollTop = startScrollTop - yDisplacement ;
69
77
if ( scrollYAxis !== false ) {
70
78
ref . current . scrollTop = newScrollTop ;
71
79
}
@@ -80,6 +88,9 @@ export const useDraggable: UseDraggable = (args) => {
80
88
const childAsElement = child as HTMLElement ;
81
89
childAsElement . style . removeProperty ( 'pointer-events' ) ;
82
90
} ) ;
91
+ if ( typeof onDragEnd === 'function' ) {
92
+ onDragEnd ( ) ;
93
+ }
83
94
}
84
95
} ;
85
96
@@ -101,7 +112,9 @@ export const useDraggable: UseDraggable = (args) => {
101
112
startY ,
102
113
scrollYAxis ,
103
114
enable ,
104
- ref
115
+ ref ,
116
+ onDragEnd ,
117
+ onDrag
105
118
] ) ;
106
119
107
120
return null ;
0 commit comments