Skip to content

Commit 4c08a60

Browse files
author
Claudéric Demers
authored
Merge pull request clauderic#213 from AlastairTaft/fix-scroll-and-drag
Fix drag when scrolling
2 parents 27b086d + 9f5179c commit 4c08a60

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/SortableContainer/index.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
144144

145145
this._touched = true;
146146
this._pos = {
147-
x: e.clientX,
148-
y: e.clientY,
147+
x: e.pageX,
148+
y: e.pageY,
149149
};
150150

151151
const node = closest(e.target, el => el.sortableInfo != null);
@@ -197,8 +197,8 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
197197

198198
if (!this.state.sorting && this._touched) {
199199
this._delta = {
200-
x: this._pos.x - e.clientX,
201-
y: this._pos.y - e.clientY,
200+
x: this._pos.x - e.pageX,
201+
y: this._pos.y - e.pageY,
202202
};
203203
const delta = Math.abs(this._delta.x) + Math.abs(this._delta.y);
204204

@@ -271,6 +271,11 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
271271
left: this.scrollContainer.scrollLeft,
272272
};
273273

274+
this.initialWindowScroll = {
275+
top: window.scrollY,
276+
left: window.scrollX,
277+
};
278+
274279
const fields = node.querySelectorAll('input, textarea, select');
275280
const clonedNode = node.cloneNode(true);
276281
const clonedFields = [
@@ -443,8 +448,8 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
443448

444449
getOffset(e) {
445450
return {
446-
x: e.touches ? e.touches[0].clientX : e.clientX,
447-
y: e.touches ? e.touches[0].clientY : e.clientY,
451+
x: e.touches ? e.touches[0].pageX : e.pageX,
452+
y: e.touches ? e.touches[0].pageY : e.pageY,
448453
};
449454
}
450455

@@ -508,11 +513,16 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
508513

509514
updatePosition(e) {
510515
const {lockAxis, lockToContainerEdges} = this.props;
516+
511517
const offset = this.getOffset(e);
512518
const translate = {
513519
x: offset.x - this.initialOffset.x,
514520
y: offset.y - this.initialOffset.y,
515521
};
522+
// Adjust for window scroll
523+
translate.y -= (window.scrollY - this.initialWindowScroll.top);
524+
translate.x -= (window.scrollX - this.initialWindowScroll.left);
525+
516526
this.translate = translate;
517527

518528
if (lockToContainerEdges) {
@@ -560,6 +570,10 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
560570
left: this.offsetEdge.left + this.translate.x + deltaScroll.left,
561571
top: this.offsetEdge.top + this.translate.y + deltaScroll.top,
562572
};
573+
const scrollDifference = {
574+
top: (window.scrollY - this.initialWindowScroll.top),
575+
left: (window.scrollX - this.initialWindowScroll.left),
576+
};
563577
this.newIndex = null;
564578

565579
for (let i = 0, len = nodes.length; i < len; i++) {
@@ -571,6 +585,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
571585
width: this.width > width ? width / 2 : this.width / 2,
572586
height: this.height > height ? height / 2 : this.height / 2,
573587
};
588+
574589
const translate = {
575590
x: 0,
576591
y: 0,
@@ -619,9 +634,9 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
619634
if (
620635
index < this.index &&
621636
(
622-
(sortingOffset.left - offset.width <= edgeOffset.left &&
623-
sortingOffset.top <= edgeOffset.top + offset.height) ||
624-
sortingOffset.top + offset.height <= edgeOffset.top
637+
((sortingOffset.left + scrollDifference.left) - offset.width <= edgeOffset.left &&
638+
(sortingOffset.top + scrollDifference.top) <= edgeOffset.top + offset.height) ||
639+
(sortingOffset.top + scrollDifference.top) + offset.height <= edgeOffset.top
625640
)
626641
) {
627642
// If the current node is to the left on the same row, or above the node that's being dragged
@@ -643,9 +658,9 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
643658
} else if (
644659
index > this.index &&
645660
(
646-
(sortingOffset.left + offset.width >= edgeOffset.left &&
647-
sortingOffset.top + offset.height >= edgeOffset.top) ||
648-
sortingOffset.top + offset.height >= edgeOffset.top + height
661+
((sortingOffset.left + scrollDifference.left) + offset.width >= edgeOffset.left &&
662+
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top) ||
663+
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top + height
649664
)
650665
) {
651666
// If the current node is to the right on the same row, or below the node that's being dragged
@@ -666,13 +681,13 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
666681
} else {
667682
if (
668683
index > this.index &&
669-
sortingOffset.left + offset.width >= edgeOffset.left
684+
(sortingOffset.left + scrollDifference.left) + offset.width >= edgeOffset.left
670685
) {
671686
translate.x = -(this.width + this.marginOffset.x);
672687
this.newIndex = index;
673688
} else if (
674689
index < this.index &&
675-
sortingOffset.left <= edgeOffset.left + offset.width
690+
(sortingOffset.left + scrollDifference.left) <= edgeOffset.left + offset.width
676691
) {
677692
translate.x = this.width + this.marginOffset.x;
678693
if (this.newIndex == null) {
@@ -683,13 +698,13 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
683698
} else if (this.axis.y) {
684699
if (
685700
index > this.index &&
686-
sortingOffset.top + offset.height >= edgeOffset.top
701+
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top
687702
) {
688703
translate.y = -(this.height + this.marginOffset.y);
689704
this.newIndex = index;
690705
} else if (
691706
index < this.index &&
692-
sortingOffset.top <= edgeOffset.top + offset.height
707+
(sortingOffset.top + scrollDifference.top) <= edgeOffset.top + offset.height
693708
) {
694709
translate.y = this.height + this.marginOffset.y;
695710
if (this.newIndex == null) {

0 commit comments

Comments
 (0)