-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathuseDroppableCollection.ts
769 lines (697 loc) · 28.9 KB
/
useDroppableCollection.ts
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {
clearGlobalDnDState,
DIRECTORY_DRAG_TYPE,
droppableCollectionMap,
getTypes,
globalDndState,
isInternalDropOperation,
setDropCollectionRef
} from './utils';
import {
Collection,
DOMProps,
DropEvent,
DropOperation,
DroppableCollectionDropEvent,
DroppableCollectionProps,
DropPosition,
DropTarget,
DropTargetDelegate,
Key,
KeyboardDelegate,
Node,
RefObject
} from '@react-types/shared';
import * as DragManager from './DragManager';
import {DroppableCollectionState} from '@react-stately/dnd';
import {HTMLAttributes, useCallback, useEffect, useRef} from 'react';
import {mergeProps, useId, useLayoutEffect} from '@react-aria/utils';
import {setInteractionModality} from '@react-aria/interactions';
import {useAutoScroll} from './useAutoScroll';
import {useDrop} from './useDrop';
import {useLocale} from '@react-aria/i18n';
export interface DroppableCollectionOptions extends DOMProps, DroppableCollectionProps {
/** A delegate object that implements behavior for keyboard focus movement. */
keyboardDelegate: KeyboardDelegate,
/** A delegate object that provides drop targets for pointer coordinates within the collection. */
dropTargetDelegate: DropTargetDelegate
}
export interface DroppableCollectionResult {
/** Props for the collection element. */
collectionProps: HTMLAttributes<HTMLElement>
}
interface DroppingState {
collection: Collection<Node<unknown>>,
focusedKey: Key | null,
selectedKeys: Set<Key>,
target: DropTarget,
draggingKeys: Set<Key | null | undefined>,
isInternal: boolean,
timeout: ReturnType<typeof setTimeout> | undefined
}
const DROP_POSITIONS: DropPosition[] = ['before', 'on', 'after'];
const DROP_POSITIONS_RTL: DropPosition[] = ['after', 'on', 'before'];
/**
* Handles drop interactions for a collection component, with support for traditional mouse and touch
* based drag and drop, in addition to full parity for keyboard and screen reader users.
*/
export function useDroppableCollection(props: DroppableCollectionOptions, state: DroppableCollectionState, ref: RefObject<HTMLElement | null>): DroppableCollectionResult {
let localState = useRef<{
props: DroppableCollectionOptions,
state: DroppableCollectionState,
nextTarget: DropTarget | null,
dropOperation: DropOperation | null
}>({
props,
state,
nextTarget: null,
dropOperation: null
}).current;
localState.props = props;
localState.state = state;
let defaultOnDrop = useCallback(async (e: DroppableCollectionDropEvent) => {
let {
onInsert,
onRootDrop,
onItemDrop,
onReorder,
acceptedDragTypes = 'all',
shouldAcceptItemDrop
} = localState.props;
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
let {
target,
dropOperation,
items
} = e;
let filteredItems = items;
if (acceptedDragTypes !== 'all' || shouldAcceptItemDrop) {
filteredItems = items.filter(item => {
let itemTypes: Set<string | symbol>;
if (item.kind === 'directory') {
itemTypes = new Set([DIRECTORY_DRAG_TYPE]);
} else {
itemTypes = item.kind === 'file' ? new Set([item.type]) : item.types;
}
if (acceptedDragTypes === 'all' || acceptedDragTypes.some(type => itemTypes.has(type))) {
// If we are performing a on item drop, check if the item in question accepts the dropped item since the item may have heavier restrictions
// than the droppable collection itself
if (target.type === 'item' && target.dropPosition === 'on' && shouldAcceptItemDrop) {
return shouldAcceptItemDrop(target, itemTypes);
}
return true;
}
return false;
});
}
if (filteredItems.length > 0) {
if (target.type === 'root' && onRootDrop) {
await onRootDrop({items: filteredItems, dropOperation});
}
if (target.type === 'item') {
if (target.dropPosition === 'on' && onItemDrop) {
await onItemDrop({items: filteredItems, dropOperation, isInternal, target});
}
if (target.dropPosition !== 'on') {
if (!isInternal && onInsert) {
await onInsert({items: filteredItems, dropOperation, target});
}
if (isInternal && onReorder) {
await onReorder({keys: draggingKeys, dropOperation, target});
}
}
}
}
}, [localState, ref]);
let autoScroll = useAutoScroll(ref);
let {dropProps} = useDrop({
ref,
onDropEnter() {
if (localState.nextTarget != null) {
state.setTarget(localState.nextTarget);
}
},
onDropMove(e) {
if (localState.nextTarget != null) {
state.setTarget(localState.nextTarget);
}
autoScroll.move(e.x, e.y);
},
getDropOperationForPoint(types, allowedOperations, x, y) {
let {draggingKeys, dropCollectionRef} = globalDndState;
let isInternal = isInternalDropOperation(ref);
let isValidDropTarget = (target) => state.getDropOperation({target, types, allowedOperations, isInternal, draggingKeys}) !== 'cancel';
let target = props.dropTargetDelegate.getDropTargetFromPoint(x, y, isValidDropTarget);
if (!target) {
localState.dropOperation = 'cancel';
localState.nextTarget = null;
return 'cancel';
}
localState.dropOperation = state.getDropOperation({target, types, allowedOperations, isInternal, draggingKeys});
// If the target doesn't accept the drop, see if the root accepts it instead.
if (localState.dropOperation === 'cancel') {
let rootTarget: DropTarget = {type: 'root'};
let dropOperation = state.getDropOperation({target: rootTarget, types, allowedOperations, isInternal, draggingKeys});
if (dropOperation !== 'cancel') {
target = rootTarget;
localState.dropOperation = dropOperation;
}
}
// Only set dropCollectionRef if there is a valid drop target since we cleanup dropCollectionRef in onDropExit
// which only runs when leaving a valid drop target or if the dropEffect become none (mouse dnd only).
if (target && localState.dropOperation !== 'cancel' && ref?.current !== dropCollectionRef?.current) {
setDropCollectionRef(ref);
}
localState.nextTarget = localState.dropOperation === 'cancel' ? null : target;
return localState.dropOperation;
},
onDropExit() {
setDropCollectionRef(undefined);
state.setTarget(null);
autoScroll.stop();
},
onDropActivate(e) {
if (state.target?.type === 'item' && state.target?.dropPosition === 'on' && typeof props.onDropActivate === 'function') {
props.onDropActivate({
type: 'dropactivate',
x: e.x, // todo
y: e.y,
target: state.target
});
}
},
onDrop(e) {
setDropCollectionRef(ref);
if (state.target) {
onDrop(e, state.target);
}
// If there wasn't a collection being tracked as a dragged collection, then we are in a case where a non RSP drag is dropped on a
// RSP collection and thus we don't need to preserve the global DnD state for onDragEnd
let {draggingCollectionRef} = globalDndState;
if (draggingCollectionRef == null) {
clearGlobalDnDState();
}
}
});
let droppingState = useRef<DroppingState>(null);
let updateFocusAfterDrop = useCallback(() => {
let {state} = localState;
if (droppingState.current) {
let {
target,
collection: prevCollection,
selectedKeys: prevSelectedKeys,
focusedKey: prevFocusedKey,
isInternal,
draggingKeys
} = droppingState.current;
// If an insert occurs during a drop, we want to immediately select these items to give
// feedback to the user that a drop occurred. Only do this if the selection didn't change
// since the drop started so we don't override if the user or application did something.
if (
state.collection.size > prevCollection.size &&
state.selectionManager.isSelectionEqual(prevSelectedKeys)
) {
let newKeys = new Set<Key>();
for (let key of state.collection.getKeys()) {
if (!prevCollection.getItem(key)) {
newKeys.add(key);
}
}
state.selectionManager.setSelectedKeys(newKeys);
// If the focused item didn't change since the drop occurred, also focus the first
// inserted item. If selection is disabled, then also show the focus ring so there
// is some indication that items were added.
if (state.selectionManager.focusedKey === prevFocusedKey) {
let first = newKeys.keys().next().value;
let item = state.collection.getItem(first);
// If this is a cell, focus the parent row.
if (item?.type === 'cell') {
first = item.parentKey;
}
state.selectionManager.setFocusedKey(first);
if (state.selectionManager.selectionMode === 'none') {
setInteractionModality('keyboard');
}
}
} else if (
prevFocusedKey != null &&
state.selectionManager.focusedKey === prevFocusedKey &&
isInternal &&
target.type === 'item' &&
target.dropPosition !== 'on' &&
draggingKeys.has(state.collection.getItem(prevFocusedKey)?.parentKey)
) {
// Focus row instead of cell when reordering.
state.selectionManager.setFocusedKey(state.collection.getItem(prevFocusedKey)?.parentKey ?? null);
setInteractionModality('keyboard');
} else if (
state.selectionManager.focusedKey === prevFocusedKey &&
target.type === 'item' &&
target.dropPosition === 'on' &&
state.collection.getItem(target.key) != null
) {
// If focus didn't move already (e.g. due to an insert), and the user dropped on an item,
// focus that item and show the focus ring to give the user feedback that the drop occurred.
// Also show the focus ring if the focused key is not selected, e.g. in case of a reorder.
state.selectionManager.setFocusedKey(target.key);
setInteractionModality('keyboard');
} else if (state.selectionManager.focusedKey != null && !state.selectionManager.isSelected(state.selectionManager.focusedKey)) {
setInteractionModality('keyboard');
}
state.selectionManager.setFocused(true);
}
}, [localState]);
let onDrop = useCallback((e: DropEvent, target: DropTarget) => {
let {state} = localState;
// Save some state of the collection/selection before the drop occurs so we can compare later.
droppingState.current = {
timeout: undefined,
focusedKey: state.selectionManager.focusedKey,
collection: state.collection,
selectedKeys: state.selectionManager.selectedKeys,
draggingKeys: globalDndState.draggingKeys,
isInternal: isInternalDropOperation(ref),
target
};
let onDropFn = localState.props.onDrop || defaultOnDrop;
onDropFn({
type: 'drop',
x: e.x, // todo
y: e.y,
target,
items: e.items,
dropOperation: e.dropOperation
});
// Wait for a short time period after the onDrop is called to allow the data to be read asynchronously
// and for React to re-render. If the collection didn't already change during this time (handled below),
// update the focused key here.
droppingState.current.timeout = setTimeout(() => {
updateFocusAfterDrop();
droppingState.current = null;
}, 50);
}, [localState, defaultOnDrop, ref, updateFocusAfterDrop]);
useEffect(() => {
return () => {
if (droppingState.current) {
clearTimeout(droppingState.current.timeout);
}
};
}, []);
useLayoutEffect(() => {
// If the collection changed after a drop, update the focused key.
if (droppingState.current && state.collection !== droppingState.current.collection) {
updateFocusAfterDrop();
}
});
let {direction} = useLocale();
useEffect(() => {
if (!ref.current) {
return;
}
let getNextTarget = (target: DropTarget | null | undefined, wrap = true, horizontal = false): DropTarget | null => {
if (!target) {
return {
type: 'root'
};
}
let {keyboardDelegate} = localState.props;
let nextKey: Key | null | undefined;
if (target?.type === 'item') {
nextKey = horizontal ? keyboardDelegate.getKeyRightOf?.(target.key) : keyboardDelegate.getKeyBelow?.(target.key);
} else {
nextKey = horizontal && direction === 'rtl' ? keyboardDelegate.getLastKey?.() : keyboardDelegate.getFirstKey?.();
}
let dropPositions = horizontal && direction === 'rtl' ? DROP_POSITIONS_RTL : DROP_POSITIONS;
let dropPosition: DropPosition = dropPositions[0];
if (target.type === 'item') {
// If the the keyboard delegate returned the next key in the collection,
// first try the other positions in the current key. Otherwise (e.g. in a grid layout),
// jump to the same drop position in the new key.
let nextCollectionKey = horizontal && direction === 'rtl' ? localState.state.collection.getKeyBefore(target.key) : localState.state.collection.getKeyAfter(target.key);
let isNextDisabled = nextCollectionKey && localState.state.selectionManager.isDisabled(nextCollectionKey);
if (nextKey == null || nextKey === nextCollectionKey || isNextDisabled) {
let positionIndex = dropPositions.indexOf(target.dropPosition);
let nextDropPosition = dropPositions[positionIndex + 1];
if (positionIndex < dropPositions.length - 1 && (!(nextDropPosition === dropPositions[2] && nextKey != null) || isNextDisabled)) {
return {
type: 'item',
key: target.key,
dropPosition: nextDropPosition
};
}
if (isNextDisabled) {
nextKey = nextCollectionKey;
}
// If the last drop position was 'after', then 'before' on the next key is equivalent.
// Switch to 'on' instead.
if (target.dropPosition === dropPositions[2]) {
dropPosition = 'on';
}
} else {
dropPosition = target.dropPosition;
}
}
if (nextKey == null) {
if (wrap) {
return {
type: 'root'
};
}
return null;
}
return {
type: 'item',
key: nextKey,
dropPosition
};
};
let getPreviousTarget = (target: DropTarget | null | undefined, wrap = true, horizontal = false): DropTarget | null => {
let {keyboardDelegate} = localState.props;
let nextKey: Key | null | undefined;
if (target?.type === 'item') {
nextKey = horizontal ? keyboardDelegate.getKeyLeftOf?.(target.key) : keyboardDelegate.getKeyAbove?.(target.key);
} else {
nextKey = horizontal && direction === 'rtl' ? keyboardDelegate.getFirstKey?.() : keyboardDelegate.getLastKey?.();
}
let dropPositions = horizontal && direction === 'rtl' ? DROP_POSITIONS_RTL : DROP_POSITIONS;
let dropPosition: DropPosition = !target || target.type === 'root' ? dropPositions[2] : 'on';
if (target?.type === 'item') {
// If the the keyboard delegate returned the previous key in the collection,
// first try the other positions in the current key. Otherwise (e.g. in a grid layout),
// jump to the same drop position in the new key.
let prevCollectionKey = horizontal && direction === 'rtl' ? localState.state.collection.getKeyAfter(target.key) : localState.state.collection.getKeyBefore(target.key);
let isPrevDisabled = prevCollectionKey && localState.state.selectionManager.isDisabled(prevCollectionKey);
if (nextKey == null || nextKey === prevCollectionKey || isPrevDisabled) {
let positionIndex = dropPositions.indexOf(target.dropPosition);
let nextDropPosition = dropPositions[positionIndex - 1];
if (positionIndex > 0 && (nextDropPosition !== dropPositions[2] || isPrevDisabled)) {
return {
type: 'item',
key: target.key,
dropPosition: nextDropPosition
};
}
if (isPrevDisabled) {
nextKey = prevCollectionKey;
}
// If the last drop position was 'before', then 'after' on the previous key is equivalent.
// Switch to 'on' instead.
if (target.dropPosition === dropPositions[0]) {
dropPosition = 'on';
}
} else {
dropPosition = target.dropPosition;
}
}
if (nextKey == null) {
if (wrap) {
return {
type: 'root'
};
}
return null;
}
return {
type: 'item',
key: nextKey,
dropPosition
};
};
let nextValidTarget = (
target: DropTarget | null | undefined,
types: Set<string>,
allowedDropOperations: DropOperation[],
getNextTarget: (target: DropTarget | null | undefined, wrap: boolean) => DropTarget | null,
wrap = true
): DropTarget | null => {
let seenRoot = 0;
let operation: DropOperation;
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
do {
let nextTarget = getNextTarget(target, wrap);
if (!nextTarget) {
return null;
}
target = nextTarget;
operation = localState.state.getDropOperation({target: nextTarget, types, allowedOperations: allowedDropOperations, isInternal, draggingKeys});
if (target.type === 'root') {
seenRoot++;
}
} while (
operation === 'cancel' &&
!localState.state.isDropTarget(target) &&
seenRoot < 2
);
if (operation === 'cancel') {
return null;
}
return target;
};
return DragManager.registerDropTarget({
element: ref.current,
preventFocusOnDrop: true,
getDropOperation(types, allowedOperations) {
if (localState.state.target) {
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
return localState.state.getDropOperation({target: localState.state.target, types, allowedOperations, isInternal, draggingKeys});
}
// Check if any of the targets accept the drop.
// TODO: should we have a faster way of doing this or e.g. for pagination?
let target = nextValidTarget(null, types, allowedOperations, getNextTarget);
return target ? 'move' : 'cancel';
},
onDropEnter(e, drag) {
let types = getTypes(drag.items);
let selectionManager = localState.state.selectionManager;
let target: DropTarget | null = null;
// Update the drop collection ref tracker for useDroppableItem's getDropOperation isInternal check
setDropCollectionRef(ref);
// When entering the droppable collection for the first time, the default drop target
// is after the focused key.
let key: Key | null | undefined = selectionManager.focusedKey;
let dropPosition: DropPosition = 'after';
// If the focused key is a cell, get the parent item instead.
// For now, we assume that individual cells cannot be dropped on.
let item = key != null ? localState.state.collection.getItem(key) : null;
if (item?.type === 'cell') {
key = item.parentKey;
}
// If the focused item is also selected, the default drop target is after the last selected item.
// But if the focused key is the first selected item, then default to before the first selected item.
// This is to make reordering lists slightly easier. If you select top down, we assume you want to
// move the items down. If you select bottom up, we assume you want to move the items up.
if (key != null && selectionManager.isSelected(key)) {
if (selectionManager.selectedKeys.size > 1 && selectionManager.firstSelectedKey === key) {
dropPosition = 'before';
} else {
key = selectionManager.lastSelectedKey;
}
}
if (key != null) {
target = {
type: 'item',
key,
dropPosition
};
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
// If the default target is not valid, find the next one that is.
if (localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternal, draggingKeys}) === 'cancel') {
target = nextValidTarget(target, types, drag.allowedDropOperations, getNextTarget, false)
?? nextValidTarget(target, types, drag.allowedDropOperations, getPreviousTarget, false);
}
}
// If no focused key, then start from the root.
if (!target) {
target = nextValidTarget(null, types, drag.allowedDropOperations, getNextTarget);
}
localState.state.setTarget(target);
},
onDropExit() {
setDropCollectionRef(undefined);
localState.state.setTarget(null);
},
onDropTargetEnter(target) {
localState.state.setTarget(target);
},
onDropActivate(e) {
if (
localState.state.target?.type === 'item' &&
localState.state.target?.dropPosition === 'on' &&
typeof localState.props.onDropActivate === 'function'
) {
localState.props.onDropActivate({
type: 'dropactivate',
x: e.x, // todo
y: e.y,
target: localState.state.target
});
}
},
onDrop(e, target) {
setDropCollectionRef(ref);
if (localState.state.target) {
onDrop(e, target || localState.state.target);
}
},
onKeyDown(e, drag) {
let {keyboardDelegate} = localState.props;
let types = getTypes(drag.items);
switch (e.key) {
case 'ArrowDown': {
if (keyboardDelegate.getKeyBelow) {
let target = nextValidTarget(localState.state.target, types, drag.allowedDropOperations, getNextTarget);
localState.state.setTarget(target);
}
break;
}
case 'ArrowUp': {
if (keyboardDelegate.getKeyAbove) {
let target = nextValidTarget(localState.state.target, types, drag.allowedDropOperations, getPreviousTarget);
localState.state.setTarget(target);
}
break;
}
case 'ArrowLeft': {
if (keyboardDelegate.getKeyLeftOf) {
let target = nextValidTarget(localState.state.target, types, drag.allowedDropOperations, (target, wrap) => getPreviousTarget(target, wrap, true));
localState.state.setTarget(target);
}
break;
}
case 'ArrowRight': {
if (keyboardDelegate.getKeyRightOf) {
let target = nextValidTarget(localState.state.target, types, drag.allowedDropOperations, (target, wrap) => getNextTarget(target, wrap, true));
localState.state.setTarget(target);
}
break;
}
case 'Home': {
if (keyboardDelegate.getFirstKey) {
let target = nextValidTarget(null, types, drag.allowedDropOperations, getNextTarget);
localState.state.setTarget(target);
}
break;
}
case 'End': {
if (keyboardDelegate.getLastKey) {
let target = nextValidTarget(null, types, drag.allowedDropOperations, getPreviousTarget);
localState.state.setTarget(target);
}
break;
}
case 'PageDown': {
if (keyboardDelegate.getKeyPageBelow) {
let target = localState.state.target;
if (!target) {
target = nextValidTarget(null, types, drag.allowedDropOperations, getNextTarget);
} else {
// If on the root, go to the item a page below the top. Otherwise a page below the current item.
let targetKey = keyboardDelegate.getFirstKey?.();
if (target.type === 'item') {
targetKey = target.key;
}
let nextKey: Key | null = null;
if (targetKey != null) {
nextKey = keyboardDelegate.getKeyPageBelow(targetKey);
}
let dropPosition = target.type === 'item' ? target.dropPosition : 'after';
// If there is no next key, or we are starting on the last key, jump to the last possible position.
if (nextKey == null || (target.type === 'item' && target.key === keyboardDelegate.getLastKey?.())) {
nextKey = keyboardDelegate.getLastKey?.() ?? null;
dropPosition = 'after';
}
if (nextKey == null) {
break;
}
target = {
type: 'item',
key: nextKey,
dropPosition
};
// If the target does not accept the drop, find the next valid target.
// If no next valid target, find the previous valid target.
let {draggingCollectionRef, draggingKeys} = globalDndState;
let isInternal = draggingCollectionRef?.current === ref?.current;
let operation = localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternal, draggingKeys});
if (operation === 'cancel') {
target = nextValidTarget(target, types, drag.allowedDropOperations, getNextTarget, false)
?? nextValidTarget(target, types, drag.allowedDropOperations, getPreviousTarget, false);
}
}
localState.state.setTarget(target ?? localState.state.target);
}
break;
}
case 'PageUp': {
if (!keyboardDelegate.getKeyPageAbove) {
break;
}
let target = localState.state.target;
if (!target) {
target = nextValidTarget(null, types, drag.allowedDropOperations, getPreviousTarget);
} else if (target.type === 'item') {
// If at the top already, switch to the root. Otherwise navigate a page up.
if (target.key === keyboardDelegate.getFirstKey?.()) {
target = {
type: 'root'
};
} else {
let nextKey: Key | null | undefined = keyboardDelegate.getKeyPageAbove(target.key);
let dropPosition = target.dropPosition;
if (nextKey == null) {
nextKey = keyboardDelegate.getFirstKey?.();
dropPosition = 'before';
}
if (nextKey == null) {
break;
}
target = {
type: 'item',
key: nextKey,
dropPosition
};
}
// If the target does not accept the drop, find the previous valid target.
// If no next valid target, find the next valid target.
let {draggingKeys} = globalDndState;
let isInternal = isInternalDropOperation(ref);
let operation = localState.state.getDropOperation({target, types, allowedOperations: drag.allowedDropOperations, isInternal, draggingKeys});
if (operation === 'cancel') {
target = nextValidTarget(target, types, drag.allowedDropOperations, getPreviousTarget, false)
?? nextValidTarget(target, types, drag.allowedDropOperations, getNextTarget, false);
}
}
localState.state.setTarget(target ?? localState.state.target);
break;
}
}
}
});
}, [localState, ref, onDrop, direction]);
let id = useId(props.id);
droppableCollectionMap.set(state, {id, ref});
return {
collectionProps: mergeProps(dropProps, {
id,
// Remove description from collection element. If dropping on the entire collection,
// there should be a drop indicator that has this description, so no need to double announce.
'aria-describedby': null
})
};
}