Skip to content

Commit 6572921

Browse files
b-ryuClaudéric Demers
authored and
Claudéric Demers
committed
feat: Detect scroll container automatically (#507)
1 parent 4b7bb35 commit 6572921

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/SortableContainer/index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
isTouchEvent,
1717
provideDisplayName,
1818
omit,
19+
getScrollingParent,
1920
} from '../utils';
2021

2122
export default function sortableContainer(
@@ -145,9 +146,10 @@ export default function sortableContainer(
145146

146147
this.contentWindow =
147148
typeof contentWindow === 'function' ? contentWindow() : contentWindow;
149+
148150
this.scrollContainer = useWindowAsScrollContainer
149151
? this.document.scrollingElement || this.document.documentElement
150-
: this.container;
152+
: getScrollingParent(this.container) || this.container;
151153

152154
for (const key in this.events) {
153155
if (this.events.hasOwnProperty(key)) {
@@ -318,7 +320,7 @@ export default function sortableContainer(
318320

319321
const margin = getElementMargin(node);
320322

321-
const containerBoundingRect = this.container.getBoundingClientRect();
323+
const containerBoundingRect = this.scrollContainer.getBoundingClientRect();
322324
const dimensions = getHelperDimensions({index, node, collection});
323325

324326
this.node = node;
@@ -341,8 +343,8 @@ export default function sortableContainer(
341343
this.offsetEdge = getEdgeOffset(node, this.container);
342344
this.initialOffset = getPosition(event);
343345
this.initialScroll = {
344-
top: this.container.scrollTop,
345-
left: this.container.scrollLeft,
346+
top: this.scrollContainer.scrollTop,
347+
left: this.scrollContainer.scrollLeft,
346348
};
347349

348350
this.initialWindowScroll = {
@@ -587,8 +589,8 @@ export default function sortableContainer(
587589
const {transitionDuration, hideSortableGhost, onSortOver} = this.props;
588590
const nodes = this.manager.getOrderedRefs();
589591
const containerScrollDelta = {
590-
left: this.container.scrollLeft - this.initialScroll.left,
591-
top: this.container.scrollTop - this.initialScroll.top,
592+
left: this.scrollContainer.scrollLeft - this.initialScroll.left,
593+
top: this.scrollContainer.scrollTop - this.initialScroll.top,
592594
};
593595
const sortingOffset = {
594596
left:

src/utils.js

+20
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,23 @@ export function getLockPixelOffset({lockOffset, width, height}) {
173173
y: offsetY,
174174
};
175175
}
176+
177+
function isScrollable(el) {
178+
const computedStyle = window.getComputedStyle(el);
179+
const overflowRegex = /(auto|scroll)/;
180+
const properties = ['overflow', 'overflowX', 'overflowY'];
181+
182+
return properties.find((property) =>
183+
overflowRegex.test(computedStyle[property]),
184+
);
185+
}
186+
187+
export function getScrollingParent(el) {
188+
if (!el) {
189+
return null;
190+
} else if (isScrollable(el)) {
191+
return el;
192+
} else {
193+
return getScrollingParent(el.parentNode);
194+
}
195+
}

0 commit comments

Comments
 (0)