Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit dbe7943

Browse files
author
Brian Ryu
committed
Improve getScrollingParent utility
1 parent 2287518 commit dbe7943

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/utils.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,24 +174,22 @@ export function getLockPixelOffset({lockOffset, width, height}) {
174174
};
175175
}
176176

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+
177187
export function getScrollingParent(el) {
178-
try {
179-
const {
180-
overflow,
181-
overflowX,
182-
overflowY,
183-
} = window.getComputedStyle(el);
184-
185-
if (
186-
overflow === 'auto' || overflow === 'scroll' ||
187-
overflowX === 'auto' || overflowX === 'scroll' ||
188-
overflowY === 'auto' || overflowY === 'scroll'
189-
) {
190-
return el;
191-
} else {
192-
return getScrollingParent(el.parentNode);
193-
}
194-
} catch (err) {
188+
if (!el) {
195189
return null;
190+
} else if (isScrollable(el)) {
191+
return el;
192+
} else {
193+
return getScrollingParent(el.parentNode);
196194
}
197195
}

0 commit comments

Comments
 (0)