-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
with-controls.jsx
254 lines (225 loc) · 5.71 KB
/
with-controls.jsx
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// @flow
/* eslint-disable no-console */
import React, { useRef, createRef, useState, useCallback } from 'react';
import styled from '@emotion/styled';
import type { Quote } from '../types';
import type {
DropResult,
PreDragActions,
SnapDragActions,
SensorAPI,
} from '../../../src/types';
import { DragDropContext } from '../../../src';
import QuoteList from '../primatives/quote-list';
import reorder from '../reorder';
import { grid, borderRadius } from '../constants';
type ControlProps = {|
quotes: Quote[],
canLift: boolean,
isDragging: boolean,
lift: (quoteId: string) => ?SnapDragActions,
|};
function noop() {}
const ControlBox = styled.div`
display: flex;
flex-direction: column;
`;
const ArrowBox = styled.div`
margin-top: ${grid * 4}px;
display: flex;
flex-direction: column;
align-items: center;
`;
const Button = styled.button`
--off-white: hsla(60, 100%, 98%, 1);
--dark-off-white: #efefe3;
--darker-off-white: #d6d6cb;
--border-width: 4px;
background: var(--off-white);
border-radius: ${borderRadius}px;
cursor: ${props => (props.disabled ? 'not-allowed' : 'pointer')};
font-size: 16px;
position: relative;
box-sizing: border-box;
border: var(--border-width) solid var(--dark-off-white);
box-shadow: 0 0 0 1px var(--darker-off-white);
margin: 2px;
::before {
position: absolute;
content: ' ';
top: 0;
right: 0;
bottom: 0;
left: 0;
border: 1px solid var(--dark-off-white);
}
:active {
border-width: 3px;
}
`;
const ArrowButton = styled(Button)`
width: 40px;
height: 40px;
`;
// locking the height so that the border width change
// does not change the size of the button
const ActionButton = styled(Button)`
height: 40px;
`;
function Controls(props: ControlProps) {
const { quotes, canLift, isDragging, lift } = props;
const actionsRef = useRef<?SnapDragActions>(null);
const selectRef = createRef();
function maybe(fn: (callbacks: SnapDragActions) => void) {
if (actionsRef.current) {
fn(actionsRef.current);
}
}
return (
<ControlBox>
<select disabled={!canLift} ref={selectRef}>
{quotes.map((quote: Quote) => (
<option key={quote.id} value={quote.id}>
id: {quote.id}
</option>
))}
</select>
<ActionButton
type="button"
disabled={!canLift}
onClick={() => {
const select: ?HTMLSelectElement = selectRef.current;
if (!select) {
return;
}
actionsRef.current = lift(select.value);
}}
>
Lift{' '}
<span role="img" aria-label="lift">
🏋️♀️
</span>
</ActionButton>
<ActionButton
type="button"
onClick={() =>
maybe((callbacks: SnapDragActions) => {
actionsRef.current = null;
callbacks.drop();
})
}
disabled={!isDragging}
>
Drop{' '}
<span role="img" aria-label="drop">
🤾♂️
</span>
</ActionButton>
<ArrowBox>
<ArrowButton
type="button"
onClick={() =>
maybe((callbacks: SnapDragActions) => callbacks.moveUp())
}
disabled={!isDragging}
label="up"
>
↑
</ArrowButton>
<div>
<ArrowButton type="button" disabled={!isDragging} label="left">
←
</ArrowButton>
<ArrowButton
type="button"
onClick={() =>
maybe((callbacks: SnapDragActions) => callbacks.moveDown())
}
disabled={!isDragging}
label="down"
>
↓
</ArrowButton>
<ArrowButton type="button" disabled={!isDragging} label="right">
→
</ArrowButton>
</div>
</ArrowBox>
</ControlBox>
);
}
const Layout = styled.div`
display: flex;
justify-content: center;
margin-top: ${grid * 4}px;
> * {
margin: ${grid}px;
}
`;
type Props = {|
initial: Quote[],
|};
export default function QuoteApp(props: Props) {
const [quotes, setQuotes] = useState(props.initial);
const [isDragging, setIsDragging] = useState(false);
const [isControlDragging, setIsControlDragging] = useState(false);
const sensorAPIRef = useRef<?SensorAPI>(null);
const onDragEnd = useCallback(
function onDragEnd(result: DropResult) {
setIsDragging(false);
setIsControlDragging(false);
// dropped outside the list
if (!result.destination) {
return;
}
if (result.destination.index === result.source.index) {
return;
}
const newQuotes = reorder(
quotes,
result.source.index,
result.destination.index,
);
setQuotes(newQuotes);
},
[quotes],
);
function lift(quoteId: string): ?SnapDragActions {
if (isDragging) {
return null;
}
const api: ?SensorAPI = sensorAPIRef.current;
if (!api) {
console.warn('unable to find sensor api');
return null;
}
const preDrag: ?PreDragActions = api.tryGetLock(quoteId, noop);
if (!preDrag) {
console.log('unable to start capturing');
return null;
}
setIsControlDragging(true);
return preDrag.snapLift();
}
return (
<DragDropContext
onDragStart={() => setIsDragging(true)}
onDragEnd={onDragEnd}
sensors={[
api => {
sensorAPIRef.current = api;
},
]}
>
<Layout>
<QuoteList listId="list" quotes={quotes} />
<Controls
quotes={quotes}
canLift={!isDragging}
isDragging={isControlDragging}
lift={lift}
/>
</Layout>
</DragDropContext>
);
}