-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathGridItem.tsx
179 lines (158 loc) · 3.69 KB
/
GridItem.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as React from "react";
import {
StateType,
useGestureResponder,
ResponderEvent
} from "react-gesture-responder";
import { animated, interpolate, useSpring } from "react-spring";
import { GridItemContext } from "./GridItemContext";
interface GridItemProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
}
export function GridItem({
children,
style,
className,
...other
}: GridItemProps) {
const context = React.useContext(GridItemContext);
if (!context) {
throw Error(
"Unable to find GridItem context. Please ensure that GridItem is used as a child of GridDropZone"
);
}
const {
top,
disableDrag,
endTraverse,
onStart,
mountWithTraverseTarget,
left,
i,
onMove,
onEnd,
grid,
dragging: isDragging
} = context;
const { columnWidth, rowHeight } = grid;
const dragging = React.useRef(false);
const startCoords = React.useRef([left, top]);
const [styles, set] = useSpring(() => {
if (mountWithTraverseTarget) {
// this feels really brittle. unsure of a better
// solution for now.
const mountXY = mountWithTraverseTarget;
endTraverse();
return {
xy: mountXY,
immediate: true,
zIndex: "1",
scale: 1.1,
opacity: 0.8
};
}
return {
xy: [left, top],
immediate: true,
zIndex: "0",
scale: 1,
opacity: 1
};
});
// handle move updates imperatively
function handleMove(state: StateType, e: ResponderEvent) {
const x = startCoords.current[0] + state.delta[0];
const y = startCoords.current[1] + state.delta[1];
set({
xy: [x, y],
zIndex: "1",
immediate: true,
opacity: 0.8,
scale: 1.1
});
onMove(state, x, y);
}
// handle end of drag
function handleEnd(state: StateType) {
const x = startCoords.current[0] + state.delta[0];
const y = startCoords.current[1] + state.delta[1];
dragging.current = false;
onEnd(state, x, y);
}
const { bind } = useGestureResponder(
{
onMoveShouldSet: state => {
if (disableDrag) {
return false;
}
onStart();
startCoords.current = [left, top];
dragging.current = true;
return true;
},
onMove: handleMove,
onTerminationRequest: () => {
if (dragging.current) {
return false;
}
return true;
},
onTerminate: handleEnd,
onRelease: handleEnd
},
{
enableMouse: true
}
);
/**
* Update our position when left or top
* values change
*/
React.useEffect(() => {
if (!dragging.current) {
set({
xy: [left, top],
zIndex: "0",
opacity: 1,
scale: 1,
immediate: false
});
}
}, [dragging.current, left, top]);
const props = {
className:
"GridItem" +
(isDragging ? " dragging" : "") +
(!!disableDrag ? " disabled" : "") +
className
? ` ${className}`
: "",
...bind,
style: {
cursor: !!disableDrag ? "grab" : undefined,
zIndex: styles.zIndex,
position: "absolute",
width: columnWidth + "px",
opacity: styles.opacity,
height: rowHeight + "px",
boxSizing: "border-box",
transform: interpolate(
[styles.xy, styles.scale],
(xy: any, s: any) =>
`translate3d(${xy[0]}px, ${xy[1]}px, 0) scale(${s})`
),
...style
},
...other
};
return typeof children === "function" ? (
children(animated.div, props, {
dragging: isDragging,
disabled: !!disableDrag,
i,
grid
})
) : (
<animated.div {...props}>{children}</animated.div>
);
}