@@ -2,80 +2,80 @@ import { useRef } from 'react';
2
2
import { DragObjectWithType , useDrag , useDrop } from 'react-dnd' ;
3
3
4
4
type DragWithIndex = DragObjectWithType & {
5
- index : number ,
5
+ index : number ;
6
6
} ;
7
- export default function useDraggable ( type : string ,
8
- id : string | number ,
9
- index : number , move : ( from : number , to : number ) => void ) {
10
- const ref = useRef ( null ) ;
11
- const [ , drop ] = useDrop ( {
12
- accept : type ,
13
- hover ( item : DragWithIndex , monitor ) {
14
- if ( ! ref . current ) {
15
- return ;
16
- }
17
- const dragIndex = item . index ;
18
- if ( dragIndex === undefined || dragIndex === null ) return ;
19
- const hoverIndex = index ;
7
+ export default function useDraggable (
8
+ type : string ,
9
+ id : string | number ,
10
+ index : number ,
11
+ move : ( from : number , to : number ) => void ,
12
+ ) {
13
+ const ref = useRef ( null ) ;
14
+ const [ , drop ] = useDrop ( {
15
+ accept : type ,
16
+ hover ( item : DragWithIndex , monitor ) {
17
+ if ( ! ref . current ) {
18
+ return ;
19
+ }
20
+ const dragIndex = item . index ;
21
+ if ( dragIndex === undefined || dragIndex === null ) return ;
22
+ const hoverIndex = index ;
20
23
21
- // Don't replace items with themselves
22
- if ( dragIndex === hoverIndex ) {
23
- return ;
24
- }
24
+ // Don't replace items with themselves
25
+ if ( dragIndex === hoverIndex ) {
26
+ return ;
27
+ }
25
28
26
- // Determine rectangle on screen
27
- const hoverBoundingRect = ref . current . getBoundingClientRect ( ) ;
29
+ // Determine rectangle on screen
30
+ const hoverBoundingRect = ref . current . getBoundingClientRect ( ) ;
28
31
29
- // Get vertical middle
30
- const hoverMiddleY = ( hoverBoundingRect . bottom - hoverBoundingRect . top ) / 2 ;
31
- const hoverMiddleX = ( hoverBoundingRect . right - hoverBoundingRect . left ) / 2 ;
32
+ // Get vertical middle
33
+ const hoverMiddleY = ( hoverBoundingRect . bottom - hoverBoundingRect . top ) / 2 ;
34
+ const hoverMiddleX = ( hoverBoundingRect . right - hoverBoundingRect . left ) / 2 ;
32
35
33
- // Determine mouse position
34
- const clientOffset = monitor . getClientOffset ( ) ;
36
+ // Determine mouse position
37
+ const clientOffset = monitor . getClientOffset ( ) ;
35
38
36
- // Get pixels to the top
37
- const hoverClientY = clientOffset . y - hoverBoundingRect . top ;
38
- const hoverClientX = clientOffset . x - hoverBoundingRect . left ;
39
+ // Get pixels to the top
40
+ const hoverClientY = clientOffset . y - hoverBoundingRect . top ;
41
+ const hoverClientX = clientOffset . x - hoverBoundingRect . left ;
39
42
40
- // console.log(hoverBoundingRect,hoverMiddleY,clientOffset,hoverClientY,
41
- // dragIndex,hoverIndex
42
- // );
43
- // Only perform the move when the mouse has crossed half of the items height
44
- // When dragging downwards, only move when the cursor is below 50%
45
- // When dragging upwards, only move when the cursor is above 50%
43
+ // console.log(hoverBoundingRect,hoverMiddleY,clientOffset,hoverClientY,
44
+ // dragIndex,hoverIndex
45
+ // );
46
+ // Only perform the move when the mouse has crossed half of the items height
47
+ // When dragging downwards, only move when the cursor is below 50%
48
+ // When dragging upwards, only move when the cursor is above 50%
46
49
47
- // Dragging downwards
48
- if ( dragIndex < hoverIndex && hoverClientY < hoverMiddleY
49
- && hoverClientX < hoverMiddleX
50
- ) {
51
- return ;
52
- }
50
+ // Dragging downwards
51
+ if ( dragIndex < hoverIndex && hoverClientY < hoverMiddleY && hoverClientX < hoverMiddleX ) {
52
+ return ;
53
+ }
53
54
54
- // Dragging upwards
55
- if ( dragIndex > hoverIndex && hoverClientY > hoverMiddleY
56
- && hoverClientX > hoverMiddleX
57
- ) {
58
- return ;
59
- }
55
+ // Dragging upwards
56
+ if ( dragIndex > hoverIndex && hoverClientY > hoverMiddleY && hoverClientX > hoverMiddleX ) {
57
+ return ;
58
+ }
60
59
61
- // Time to actually perform the action
62
- move ( dragIndex , hoverIndex ) ;
60
+ // Time to actually perform the action
61
+ move ( dragIndex , hoverIndex ) ;
63
62
64
- // Note: we're mutating the monitor item here!
65
- // Generally it's better to avoid mutations,
66
- // but it's good here for the sake of performance
67
- // to avoid expensive index searches.
68
- item . index = hoverIndex ;
69
- } ,
70
- } ) ;
71
- const [ { isDragging } , drag ] = useDrag ( {
72
- item : { type, id, index } ,
73
- collect : monitor => ( {
74
- isDragging : monitor . isDragging ( ) ,
75
- } ) ,
76
- } ) ;
77
- drag ( drop ( ref ) ) ;
78
- return {
79
- ref, isDragging,
80
- } ;
63
+ // Note: we're mutating the monitor item here!
64
+ // Generally it's better to avoid mutations,
65
+ // but it's good here for the sake of performance
66
+ // to avoid expensive index searches.
67
+ item . index = hoverIndex ;
68
+ } ,
69
+ } ) ;
70
+ const [ { isDragging } , drag ] = useDrag ( {
71
+ item : { type, id, index } ,
72
+ collect : monitor => ( {
73
+ isDragging : monitor . isDragging ( ) ,
74
+ } ) ,
75
+ } ) ;
76
+ drag ( drop ( ref ) ) ;
77
+ return {
78
+ ref,
79
+ isDragging,
80
+ } ;
81
81
}
0 commit comments