-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
drag-handle.jsx
554 lines (452 loc) · 14.9 KB
/
drag-handle.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// @flow
import { Component } from 'react';
import invariant from 'invariant';
import memoizeOne from 'memoize-one';
import rafSchedule from 'raf-schd';
// Using keyCode's for consistent event pattern matching between
// React synthetic events as well as raw browser events.
import * as keyCodes from '../key-codes';
import type { Position } from '../../types';
import type {
Props,
DragTypes,
Provided,
MouseForceChangedEvent,
} from './drag-handle-types';
const noop = (): void => { };
const getFalse: () => boolean = () => false;
// If we are making an action based on an event often we want to
// prevent the default browser action and stop any other elements
// from receiving the event.
const stop = (event: Event) => {
event.preventDefault();
event.stopPropagation();
};
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
const primaryButton = 0;
// The amount of pixels that need to move before we consider the movement
// a drag rather than a click.
export const sloppyClickThreshold: number = 5;
type State = {
draggingWith: ?DragTypes,
pending: ?Position,
};
type ExecuteBasedOnDirection = {|
vertical: () => void,
horizontal: () => void,
|}
export default class DragHandle extends Component {
/* eslint-disable react/sort-comp */
props: Props
state: State
state: State = {
draggingWith: null,
pending: null,
};
preventClick: boolean
ifDragging = (fn: Function) => {
if (this.state.draggingWith) {
fn();
}
}
// There is a case where if this is fired between
// two different drags with the same x,y then the second
// drag will not fire a move. This will only effect the
// first frame. It was decided that this is better than
// needing to clear the memoization cache between drags
// given that it is a huge edge case.
memoizedMove = memoizeOne((x: number, y: number) => {
const point: Position = { x, y };
this.props.callbacks.onMove(point);
});
// scheduled functions
scheduleMove = rafSchedule((point: Position) => {
this.ifDragging(() => this.memoizedMove(point.x, point.y));
});
scheduleMoveForward = rafSchedule(() => {
this.ifDragging(this.props.callbacks.onMoveForward);
})
scheduleMoveBackward = rafSchedule(() => {
this.ifDragging(this.props.callbacks.onMoveBackward);
});
scheduleCrossAxisMoveForward = rafSchedule(() => {
this.ifDragging(this.props.callbacks.onCrossAxisMoveForward);
})
scheduleCrossAxisMoveBackward = rafSchedule(() => {
this.ifDragging(this.props.callbacks.onCrossAxisMoveBackward);
});
scheduleWindowScrollMove = rafSchedule(() => {
this.ifDragging(this.props.callbacks.onWindowScroll);
});
/* eslint-enable react/sort-comp */
componentWillUnmount() {
if (!this.state.draggingWith) {
return;
}
this.preventClick = false;
this.unbindWindowEvents();
this.props.callbacks.onCancel();
}
// TODO: there is a scenario where events will not be unbound:
// - listeners are bound
// - drag is cancelled during the COLLECTING_DIMENSION phase
// FIX: need to know when a drag is cancelled / cleaned during the
// COLLECTING_DIMENSIONS phase.
componentWillReceiveProps(nextProps: Props) {
// if the application cancels a drag we need to unbind the handlers
const isDragStopping: boolean = (this.props.isDragging && !nextProps.isDragging);
if (isDragStopping && this.state.draggingWith) {
this.stopDragging();
return;
}
if (nextProps.isEnabled) {
return;
}
// dragging is *not* enabled
// if a drag is pending - clear it
if (this.state.pending) {
this.stopPendingMouseDrag();
return;
}
// need to cancel a current drag
if (this.state.draggingWith) {
this.stopDragging(() => this.props.callbacks.onCancel());
}
}
onWindowResize = () => {
if (this.state.pending) {
this.stopPendingMouseDrag();
return;
}
if (!this.state.draggingWith) {
return;
}
this.stopDragging(() => this.props.callbacks.onCancel());
}
onWindowScroll = () => {
const { draggingWith } = this.state;
if (!draggingWith) {
return;
}
if (draggingWith === 'MOUSE') {
this.scheduleWindowScrollMove();
return;
}
if (draggingWith === 'KEYBOARD') {
// currently not supporting window scrolling with a keyboard
this.stopDragging(() => this.props.callbacks.onCancel());
}
}
onWindowMouseMove = (event: MouseEvent) => {
const { draggingWith, pending } = this.state;
if (draggingWith === 'KEYBOARD') {
return;
}
// Mouse dragging
const { button, clientX, clientY } = event;
if (button !== primaryButton) {
return;
}
const point: Position = {
x: clientX,
y: clientY,
};
if (!pending) {
this.scheduleMove(point);
return;
}
// not yet dragging
const shouldStartDrag = Math.abs(pending.x - point.x) >= sloppyClickThreshold ||
Math.abs(pending.y - point.y) >= sloppyClickThreshold;
if (shouldStartDrag) {
this.startDragging('MOUSE', () => this.props.callbacks.onLift(point));
}
};
onWindowMouseUp = () => {
// Did not move far enough for it to actually be a drag
if (this.state.pending) {
// not blocking the default event - letting it pass through
this.stopPendingMouseDrag();
return;
}
if (!this.state.draggingWith) {
console.error('should not be listening to mouse up events when nothing is dragging');
return;
}
if (this.state.draggingWith !== 'MOUSE') {
return;
}
// Allowing any event.button type to drop. Otherwise you
// might not get a corresponding mouseup with a mousedown.
// We could do a`cancel` if the button is not the primary.
this.stopDragging(() => this.props.callbacks.onDrop());
};
onWindowMouseDown = () => {
// this can happen during a drag when the user clicks a button
// other than the primary mouse button
this.stopDragging(() => this.props.callbacks.onCancel());
}
onMouseDown = (event: MouseEvent) => {
if (this.state.draggingWith === 'KEYBOARD') {
// allowing any type of mouse down to cancel
this.stopDragging(() => this.props.callbacks.onCancel());
return;
}
if (!this.props.canLift) {
return;
}
const { button, clientX, clientY } = event;
if (button !== primaryButton) {
return;
}
stop(event);
const point: Position = {
x: clientX,
y: clientY,
};
this.startPendingMouseDrag(point);
};
executeBasedOnDirection = (fns: ExecuteBasedOnDirection) => {
if (!this.props.direction) {
console.error('cannot move based on direction when none is provided');
this.stopDragging(() => this.props.callbacks.onCancel());
return;
}
// eslint-disable-next-line no-unused-expressions
this.props.direction === 'vertical' ? fns.vertical() : fns.horizontal();
}
// window keyboard events are bound during a keyboard drag
// or after the user presses the mouse down
onWindowKeyDown = (event: KeyboardEvent): void => {
const isMouseDragPending: boolean = Boolean(this.state.pending);
if (isMouseDragPending) {
if (event.keyCode === keyCodes.escape) {
stop(event);
this.stopPendingMouseDrag();
}
return;
}
if (!this.state.draggingWith) {
console.error('should not be listening to window mouse up if nothing is dragging');
this.stopDragging(() => this.props.callbacks.onCancel());
return;
}
// Dragging with either a keyboard or mouse
// Blocking standard submission action
if (event.keyCode === keyCodes.enter) {
event.preventDefault();
return;
}
// Preventing tabbing or submitting
if (event.keyCode === keyCodes.tab) {
event.preventDefault();
return;
}
if (event.keyCode === keyCodes.escape) {
event.preventDefault();
this.stopDragging(() => this.props.callbacks.onCancel());
}
}
// When dragging with a mouse - the element may not have focus
// When dragging with a keyboard - the element will have focus
onKeyDown = (event: KeyboardEvent): void => {
if (!this.props.isEnabled) {
return;
}
// Handled in the window key down function as it may not have focus
if (this.state.pending || this.state.draggingWith === 'MOUSE') {
return;
}
const canStartKeyboardDrag: boolean = this.props.canLift && !this.state.draggingWith;
const isKeyboardDragging: boolean = this.state.draggingWith === 'KEYBOARD';
if (!canStartKeyboardDrag && !isKeyboardDragging) {
return;
}
if (canStartKeyboardDrag) {
// Lifting with a keyboard
if (event.keyCode === keyCodes.space) {
stop(event);
this.startDragging('KEYBOARD', () => this.props.callbacks.onKeyLift());
}
return;
}
if (!this.props.direction) {
console.error('cannot handle keyboard event if direction is not provided');
stop(event);
this.stopDragging(() => this.props.callbacks.onCancel());
return;
}
// Dropping
if (event.keyCode === keyCodes.space) {
// need to stop parent Draggable's thinking this is a lift
stop(event);
this.stopDragging(() => this.props.callbacks.onDrop());
}
// Movement
if (event.keyCode === keyCodes.arrowDown) {
stop(event);
this.executeBasedOnDirection({
vertical: this.scheduleMoveForward,
horizontal: this.scheduleCrossAxisMoveForward,
});
return;
}
if (event.keyCode === keyCodes.arrowUp) {
stop(event);
this.executeBasedOnDirection({
vertical: this.scheduleMoveBackward,
horizontal: this.scheduleCrossAxisMoveBackward,
});
return;
}
if (event.keyCode === keyCodes.arrowRight) {
stop(event);
this.executeBasedOnDirection({
vertical: this.scheduleCrossAxisMoveForward,
horizontal: this.scheduleMoveForward,
});
return;
}
if (event.keyCode === keyCodes.arrowLeft) {
stop(event);
this.executeBasedOnDirection({
vertical: this.scheduleCrossAxisMoveBackward,
horizontal: this.scheduleMoveBackward,
});
}
}
onClick = (event: MouseEvent): void => {
if (!this.preventClick) {
return;
}
this.preventClick = false;
event.preventDefault();
}
startPendingMouseDrag = (point: Position) => {
if (this.state.draggingWith) {
console.error('cannot start a pending mouse drag when already dragging');
return;
}
if (this.state.pending) {
console.error('cannot start a pending mouse drag when there is already a pending position');
return;
}
// need to bind the window events
this.bindWindowEvents();
const state: State = {
draggingWith: null,
pending: point,
};
this.setState(state);
}
startDragging = (type: DragTypes, done?: () => void = noop) => {
if (this.state.draggingWith) {
console.error('cannot start dragging when already dragging');
return;
}
if (type === 'MOUSE' && !this.state.pending) {
console.error('cannot start mouse drag when there is not a pending position');
return;
}
// keyboard events already bound for mouse dragging
if (type === 'KEYBOARD') {
this.bindWindowEvents();
}
const state: State = {
draggingWith: type,
pending: null,
};
this.setState(state, done);
}
stopPendingMouseDrag = (done?: () => void = noop) => {
invariant(this.state.pending, 'cannot stop pending drag when there is none');
// we need to allow the click event to get through
this.preventClick = false;
this.unbindWindowEvents();
this.setState({
draggingWith: null,
pending: null,
}, done);
}
stopDragging = (done?: () => void = noop) => {
if (!this.state.draggingWith) {
console.error('cannot stop dragging when not dragging');
return;
}
this.unbindWindowEvents();
if (this.state.draggingWith === 'MOUSE') {
// Need to block any click actions
this.preventClick = true;
}
const state: State = {
draggingWith: null,
pending: null,
};
this.setState(state, done);
}
// Need to opt out of dragging if the user is a force press
// Only for safari which has decided to introduce its own custom way of doing things
// https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/SafariJSProgTopics/RespondingtoForceTouchEventsfromJavaScript.html
mouseForceChanged = (event: MouseForceChangedEvent) => {
if (event.webkitForce == null || MouseEvent.WEBKIT_FORCE_AT_FORCE_MOUSE_DOWN == null) {
console.error('handling a mouse force changed event when it is not supported');
return;
}
const forcePressThreshold: number = (MouseEvent.WEBKIT_FORCE_AT_FORCE_MOUSE_DOWN : any);
// not a force press
if (event.webkitForce < forcePressThreshold) {
return;
}
// is a force press
// if we are dragging - kill the drag
if (this.state.pending) {
this.stopPendingMouseDrag();
return;
}
// This case should not happen as it looks like force press is not
// possible while moving the mouse. However, this is being super defensive.
if (this.state.draggingWith) {
this.stopDragging(() => this.props.callbacks.onCancel());
}
}
unbindWindowEvents = () => {
window.removeEventListener('mousemove', this.onWindowMouseMove);
window.removeEventListener('mouseup', this.onWindowMouseUp);
window.removeEventListener('mousedown', this.onWindowMouseDown);
window.removeEventListener('keydown', this.onWindowKeyDown);
window.removeEventListener('resize', this.onWindowResize);
window.removeEventListener('scroll', this.onWindowScroll);
window.removeEventListener('webkitmouseforcechanged', this.mouseForceChanged);
}
bindWindowEvents = () => {
window.addEventListener('mousemove', this.onWindowMouseMove);
window.addEventListener('mouseup', this.onWindowMouseUp);
window.addEventListener('mousedown', this.onWindowMouseDown);
window.addEventListener('keydown', this.onWindowKeyDown);
window.addEventListener('resize', this.onWindowResize);
window.addEventListener('scroll', this.onWindowScroll, { passive: true });
window.addEventListener('webkitmouseforcechanged', this.mouseForceChanged);
}
getProvided = memoizeOne((isEnabled: boolean, isDragging: boolean): ?Provided => {
if (!isEnabled) {
return null;
}
const provided: Provided = {
onMouseDown: this.onMouseDown,
onKeyDown: this.onKeyDown,
onClick: this.onClick,
tabIndex: 0,
'aria-grabbed': isDragging,
draggable: false,
onDragStart: getFalse,
onDrop: getFalse,
};
return provided;
})
render() {
const { children, isEnabled } = this.props;
const { draggingWith } = this.state;
const isDragging: boolean = Boolean(draggingWith);
return children(this.getProvided(isEnabled, isDragging));
}
}