@qntm-code/utils
Version:
A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.
20 lines (19 loc) • 838 B
JavaScript
const visibilityRegex = new RegExp('^(visible|hidden)');
/**
* Gets the scrollable parent element of a given element
* @param x: Optional. Whether to check if the element can scroll on the x axis. Default: true
* @param y: Optional. Whether to check if the element can scroll on the y axis. Default: true
*/
export function getScrollParent(element, x = true, y = true) {
if (!element) {
return null;
}
const computedStyle = window.getComputedStyle(element);
if (x && !visibilityRegex.test(computedStyle.overflowX) && element.scrollWidth >= element.clientWidth) {
return element;
}
if (y && !visibilityRegex.test(computedStyle.overflowY) && element.scrollHeight >= element.clientHeight) {
return element;
}
return getScrollParent(element.parentElement, x, y) || document.body;
}