UNPKG

@qntm-code/utils

Version:

A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.

24 lines (23 loc) 985 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getScrollParent = void 0; 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 */ 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; } exports.getScrollParent = getScrollParent;