@carbon/ibm-products
Version:
Carbon for IBM Products
39 lines (37 loc) • 1.31 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
//#region src/global/js/utils/scrollableAncestor.js
/**
* Copyright IBM Corp. 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const windowExists = typeof window !== `undefined`;
const scrollable = (target) => {
const style = window.getComputedStyle(target);
return /(auto|scroll|hidden)/.test(style.overflow);
};
const scrollableAncestorInner = (target) => {
if (target.parentNode && target.parentNode !== document) if (scrollable(target.parentNode)) return target.parentNode;
else return scrollableAncestorInner(target.parentNode);
else return document.scrollingElement;
};
/**
* Walks up the parent nodes to identify the first scrollable ancestor
*
* @param {HTMLElement} target
* @returns {HTMLElement}
*/
const scrollableAncestor = (target) => {
if (!windowExists || !target) return null;
const style = window.getComputedStyle(target);
if (!target || !style || style.position === "fixed") return document.scrollingElement;
return scrollableAncestorInner(target);
};
//#endregion
exports.scrollableAncestor = scrollableAncestor;