Skip to content

Commit ae2e909

Browse files
authored
Fix dnd issues in docs examples (#3588)
1 parent 629bf66 commit ae2e909

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

packages/@react-aria/dnd/docs/useDraggableCollection.mdx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,12 @@ function DropIndicator(props) {
586586
return null;
587587
}
588588

589-
let className = props.target.type === 'item'
590-
? `drop-indicator ${isDropTarget ? 'drop-target' : ''}`
591-
: '';
592-
593589
return (
594590
<li
595591
{...dropIndicatorProps}
596592
role="option"
597593
ref={ref}
598-
className={className} />
594+
className={`drop-indicator ${isDropTarget ? 'drop-target' : ''}`} />
599595
);
600596
}
601597
///- end highlight -///

packages/@react-aria/dnd/docs/useDroppableCollection.mdx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,12 @@ function DropIndicator(props) {
266266
return null;
267267
}
268268

269-
let className = props.target.type === 'item'
270-
? `drop-indicator ${isDropTarget ? 'drop-target' : ''}`
271-
: '';
272-
273269
return (
274270
<li
275271
{...dropIndicatorProps}
276272
role="option"
277273
ref={ref}
278-
className={className} />
274+
className={`drop-indicator ${isDropTarget ? 'drop-target' : ''}`} />
279275
);
280276
}
281277
```

packages/@react-aria/dnd/src/DragManager.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ export function beginDragging(target: DragTarget, stringFormatter: LocalizedStri
7373
requestAnimationFrame(() => {
7474
dragSession.setup();
7575
if (getDragModality() === 'keyboard') {
76-
let target = dragSession.findNearestDropTarget();
77-
dragSession.setCurrentDropTarget(target);
76+
dragSession.next();
7877
}
7978
});
8079

@@ -338,6 +337,16 @@ class DragSession {
338337
}
339338

340339
this.validDropTargets = findValidDropTargets(this.dragTarget);
340+
341+
// Shuffle drop target order based on starting drag target.
342+
if (this.validDropTargets.length > 0) {
343+
let nearestIndex = this.findNearestDropTarget();
344+
this.validDropTargets = [
345+
...this.validDropTargets.slice(nearestIndex),
346+
...this.validDropTargets.slice(0, nearestIndex)
347+
];
348+
}
349+
341350
if (this.currentDropTarget && !this.validDropTargets.includes(this.currentDropTarget)) {
342351
this.setCurrentDropTarget(this.validDropTargets[0]);
343352
}
@@ -419,19 +428,20 @@ class DragSession {
419428
}
420429
}
421430

422-
findNearestDropTarget(): DropTarget {
431+
findNearestDropTarget(): number {
423432
let dragTargetRect = this.dragTarget.element.getBoundingClientRect();
424433

425434
let minDistance = Infinity;
426-
let nearest = null;
427-
for (let dropTarget of this.validDropTargets) {
435+
let nearest = -1;
436+
for (let i = 0; i < this.validDropTargets.length; i++) {
437+
let dropTarget = this.validDropTargets[i];
428438
let rect = dropTarget.element.getBoundingClientRect();
429439
let dx = rect.left - dragTargetRect.left;
430440
let dy = rect.top - dragTargetRect.top;
431441
let dist = (dx * dx) + (dy * dy);
432442
if (dist < minDistance) {
433443
minDistance = dist;
434-
nearest = dropTarget;
444+
nearest = i;
435445
}
436446
}
437447

0 commit comments

Comments
 (0)