-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathDrag.tsx
68 lines (64 loc) · 1.5 KB
/
Drag.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* eslint-disable react/jsx-handler-names */
import React from 'react';
import useDrag, { UseDrag, UseDragOptions, HandlerArgs as HandlerArgsType } from './useDrag';
export type HandlerArgs = HandlerArgsType;
export type DragProps = UseDragOptions & {
/** Children render function which is passed the state of dragging and callbacks for drag start/end/move. */
children: (args: UseDrag) => React.ReactNode;
/** Width of the drag container. */
width: number;
/** Height of the drag container. */
height: number;
/** Whether to render an invisible rect below children to capture the drag area as defined by width and height. */
captureDragArea?: boolean;
/** If defined, parent controls dragging state. */
isDragging?: boolean;
};
export default function Drag({
captureDragArea = true,
snapToPointer = true,
children,
dx,
dy,
height,
onDragEnd,
onDragMove,
onDragStart,
resetOnStart,
width,
x,
y,
isDragging,
restrict,
restrictToPath,
}: DragProps) {
const drag = useDrag({
resetOnStart,
snapToPointer,
onDragEnd,
onDragMove,
onDragStart,
x,
y,
dx,
dy,
isDragging,
restrict,
restrictToPath,
});
return (
<>
{drag.isDragging && captureDragArea && (
<rect
width={width}
height={height}
onPointerDown={drag.dragStart}
onPointerMove={drag.dragMove}
onPointerUp={drag.dragEnd}
fill="transparent"
/>
)}
{children(drag)}
</>
);
}