Skip to content

Commit 92b76b6

Browse files
committed
Updated animation logic to account for window scroll position.
1 parent bc623fc commit 92b76b6

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/SortableContainer/index.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
270270
top: this.scrollContainer.scrollTop,
271271
left: this.scrollContainer.scrollLeft,
272272
};
273+
274+
this.initialWindowScroll = {
275+
top: window.scrollY,
276+
left: window.scrollX,
277+
};
273278

274279
const fields = node.querySelectorAll('input, textarea, select');
275280
const clonedNode = node.cloneNode(true);
@@ -440,7 +445,6 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
440445
}
441446

442447
getOffset(e) {
443-
debugger
444448
return {
445449
x: e.touches ? e.touches[0].pageX : e.pageX,
446450
y: e.touches ? e.touches[0].pageY : e.pageY,
@@ -507,11 +511,16 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
507511

508512
updatePosition(e) {
509513
const {lockAxis, lockToContainerEdges} = this.props;
514+
510515
const offset = this.getOffset(e);
511516
const translate = {
512517
x: offset.x - this.initialOffset.x,
513518
y: offset.y - this.initialOffset.y,
514519
};
520+
// Adjust for window scroll
521+
translate.y -= (window.scrollY - this.initialWindowScroll.top)
522+
translate.x -= (window.scrollX - this.initialWindowScroll.left)
523+
515524
this.translate = translate;
516525

517526
if (lockToContainerEdges) {
@@ -559,6 +568,10 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
559568
left: this.offsetEdge.left + this.translate.x + deltaScroll.left,
560569
top: this.offsetEdge.top + this.translate.y + deltaScroll.top,
561570
};
571+
const scrollDifference = {
572+
top: (window.scrollY - this.initialWindowScroll.top),
573+
left: (window.scrollX - this.initialWindowScroll.left),
574+
}
562575
this.newIndex = null;
563576

564577
for (let i = 0, len = nodes.length; i < len; i++) {
@@ -570,6 +583,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
570583
width: this.width > width ? width / 2 : this.width / 2,
571584
height: this.height > height ? height / 2 : this.height / 2,
572585
};
586+
573587
const translate = {
574588
x: 0,
575589
y: 0,
@@ -618,9 +632,9 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
618632
if (
619633
index < this.index &&
620634
(
621-
(sortingOffset.left - offset.width <= edgeOffset.left &&
622-
sortingOffset.top <= edgeOffset.top + offset.height) ||
623-
sortingOffset.top + offset.height <= edgeOffset.top
635+
((sortingOffset.left + scrollDifference.left) - offset.width <= edgeOffset.left &&
636+
(sortingOffset.top + scrollDifference.top) <= edgeOffset.top + offset.height) ||
637+
(sortingOffset.top + scrollDifference.top) + offset.height <= edgeOffset.top
624638
)
625639
) {
626640
// If the current node is to the left on the same row, or above the node that's being dragged
@@ -642,9 +656,9 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
642656
} else if (
643657
index > this.index &&
644658
(
645-
(sortingOffset.left + offset.width >= edgeOffset.left &&
646-
sortingOffset.top + offset.height >= edgeOffset.top) ||
647-
sortingOffset.top + offset.height >= edgeOffset.top + height
659+
((sortingOffset.left + scrollDifference.left) + offset.width >= edgeOffset.left &&
660+
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top) ||
661+
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top + height
648662
)
649663
) {
650664
// If the current node is to the right on the same row, or below the node that's being dragged
@@ -665,13 +679,13 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
665679
} else {
666680
if (
667681
index > this.index &&
668-
sortingOffset.left + offset.width >= edgeOffset.left
682+
(sortingOffset.left + scrollDifference.left) + offset.width >= edgeOffset.left
669683
) {
670684
translate.x = -(this.width + this.marginOffset.x);
671685
this.newIndex = index;
672686
} else if (
673687
index < this.index &&
674-
sortingOffset.left <= edgeOffset.left + offset.width
688+
(sortingOffset.left + scrollDifference.left) <= edgeOffset.left + offset.width
675689
) {
676690
translate.x = this.width + this.marginOffset.x;
677691
if (this.newIndex == null) {
@@ -682,13 +696,13 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
682696
} else if (this.axis.y) {
683697
if (
684698
index > this.index &&
685-
sortingOffset.top + offset.height >= edgeOffset.top
699+
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top
686700
) {
687701
translate.y = -(this.height + this.marginOffset.y);
688702
this.newIndex = index;
689703
} else if (
690704
index < this.index &&
691-
sortingOffset.top <= edgeOffset.top + offset.height
705+
(sortingOffset.top + scrollDifference.top) <= edgeOffset.top + offset.height
692706
) {
693707
translate.y = this.height + this.marginOffset.y;
694708
if (this.newIndex == null) {

0 commit comments

Comments
 (0)