Skip to content

Commit abe0952

Browse files
aminlandClaudéric Demers
authored and
Claudéric Demers
committed
Fix for using window as scrolling container (#306)
* Fix for using window as scrolling container * fix issue with touches on mobile
1 parent 6229993 commit abe0952

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

src/SortableContainer/index.js

+40-26
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,20 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
109109
* snapshots will serialize window, causing a RangeError
110110
* https://github.com/clauderic/react-sortable-hoc/issues/249
111111
*/
112-
const contentWindow = this.props.contentWindow || window;
113112

114113
this.container = typeof getContainer === 'function'
115114
? getContainer(this.getWrappedInstance())
116115
: findDOMNode(this);
117116
this.document = this.container.ownerDocument || document;
118-
this.scrollContainer = useWindowAsScrollContainer
119-
? this.document.body
120-
: this.container;
117+
118+
const contentWindow = this.props.contentWindow || this.document.defaultView || window;
119+
121120
this.contentWindow = typeof contentWindow === 'function'
122121
? contentWindow()
123122
: contentWindow;
123+
this.scrollContainer = useWindowAsScrollContainer
124+
? this.document.scrollingElement || this.document.documentElement
125+
: this.container;
124126

125127
for (const key in this.events) {
126128
if (this.events.hasOwnProperty(key)) {
@@ -273,8 +275,8 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
273275
this.offsetEdge = this.getEdgeOffset(node);
274276
this.initialOffset = this.getOffset(e);
275277
this.initialScroll = {
276-
top: this.scrollContainer.scrollTop,
277-
left: this.scrollContainer.scrollLeft,
278+
top: this.container.scrollTop,
279+
left: this.container.scrollLeft,
278280
};
279281

280282
this.initialWindowScroll = {
@@ -453,10 +455,22 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
453455
}
454456

455457
getOffset(e) {
456-
return {
457-
x: e.touches ? e.touches[0].pageX : e.pageX,
458-
y: e.touches ? e.touches[0].pageY : e.pageY,
459-
};
458+
if (e.touches && e.touches.length) {
459+
return {
460+
x: e.touches[0].pageX,
461+
y: e.touches[0].pageY,
462+
};
463+
} else if (e.changedTouches && e.changedTouches.length) {
464+
return {
465+
x: e.changedTouches[0].pageX,
466+
y: e.changedTouches[0].pageY,
467+
};
468+
} else {
469+
return {
470+
x: e.pageX,
471+
y: e.pageY,
472+
};
473+
}
460474
}
461475

462476
getLockPixelOffsets() {
@@ -568,15 +582,15 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
568582
animateNodes() {
569583
const {transitionDuration, hideSortableGhost, onSortOver} = this.props;
570584
const nodes = this.manager.getOrderedRefs();
571-
const deltaScroll = {
572-
left: this.scrollContainer.scrollLeft - this.initialScroll.left,
573-
top: this.scrollContainer.scrollTop - this.initialScroll.top,
585+
const containerScrollDelta = {
586+
left: this.container.scrollLeft - this.initialScroll.left,
587+
top: this.container.scrollTop - this.initialScroll.top,
574588
};
575589
const sortingOffset = {
576-
left: this.offsetEdge.left + this.translate.x + deltaScroll.left,
577-
top: this.offsetEdge.top + this.translate.y + deltaScroll.top,
590+
left: this.offsetEdge.left + this.translate.x + containerScrollDelta.left,
591+
top: this.offsetEdge.top + this.translate.y + containerScrollDelta.top,
578592
};
579-
const scrollDifference = {
593+
const windowScrollDelta = {
580594
top: (window.pageYOffset - this.initialWindowScroll.top),
581595
left: (window.pageXOffset - this.initialWindowScroll.left),
582596
};
@@ -641,9 +655,9 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
641655
if (
642656
index < this.index &&
643657
(
644-
((sortingOffset.left + scrollDifference.left) - offset.width <= edgeOffset.left &&
645-
(sortingOffset.top + scrollDifference.top) <= edgeOffset.top + offset.height) ||
646-
(sortingOffset.top + scrollDifference.top) + offset.height <= edgeOffset.top
658+
((sortingOffset.left + windowScrollDelta.left) - offset.width <= edgeOffset.left &&
659+
(sortingOffset.top + windowScrollDelta.top) <= edgeOffset.top + offset.height) ||
660+
(sortingOffset.top + windowScrollDelta.top) + offset.height <= edgeOffset.top
647661
)
648662
) {
649663
// If the current node is to the left on the same row, or above the node that's being dragged
@@ -665,9 +679,9 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
665679
} else if (
666680
index > this.index &&
667681
(
668-
((sortingOffset.left + scrollDifference.left) + offset.width >= edgeOffset.left &&
669-
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top) ||
670-
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top + height
682+
((sortingOffset.left + windowScrollDelta.left) + offset.width >= edgeOffset.left &&
683+
(sortingOffset.top + windowScrollDelta.top) + offset.height >= edgeOffset.top) ||
684+
(sortingOffset.top + windowScrollDelta.top) + offset.height >= edgeOffset.top + height
671685
)
672686
) {
673687
// If the current node is to the right on the same row, or below the node that's being dragged
@@ -688,13 +702,13 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
688702
} else {
689703
if (
690704
index > this.index &&
691-
(sortingOffset.left + scrollDifference.left) + offset.width >= edgeOffset.left
705+
(sortingOffset.left + windowScrollDelta.left) + offset.width >= edgeOffset.left
692706
) {
693707
translate.x = -(this.width + this.marginOffset.x);
694708
this.newIndex = index;
695709
} else if (
696710
index < this.index &&
697-
(sortingOffset.left + scrollDifference.left) <= edgeOffset.left + offset.width
711+
(sortingOffset.left + windowScrollDelta.left) <= edgeOffset.left + offset.width
698712
) {
699713
translate.x = this.width + this.marginOffset.x;
700714
if (this.newIndex == null) {
@@ -705,13 +719,13 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
705719
} else if (this.axis.y) {
706720
if (
707721
index > this.index &&
708-
(sortingOffset.top + scrollDifference.top) + offset.height >= edgeOffset.top
722+
(sortingOffset.top + windowScrollDelta.top) + offset.height >= edgeOffset.top
709723
) {
710724
translate.y = -(this.height + this.marginOffset.y);
711725
this.newIndex = index;
712726
} else if (
713727
index < this.index &&
714-
(sortingOffset.top + scrollDifference.top) <= edgeOffset.top + offset.height
728+
(sortingOffset.top + windowScrollDelta.top) <= edgeOffset.top + offset.height
715729
) {
716730
translate.y = this.height + this.marginOffset.y;
717731
if (this.newIndex == null) {

0 commit comments

Comments
 (0)