@nex-ui/react
Version:
🎉 A beautiful, modern, and reliable React component library.
35 lines (32 loc) • 1.13 kB
JavaScript
import { ownerWindow } from '@nex-ui/utils';
import { getElementName, isHTMLElement, isOverflowElement } from './dom.mjs';
const getNearestOverflowAncestor = (element)=>{
const { parentElement } = element;
if (!parentElement) {
return ownerWindow(element);
}
if (isHTMLElement(parentElement) && isOverflowElement(parentElement)) {
return parentElement;
}
return getNearestOverflowAncestor(parentElement);
};
const getOverflowAncestors = (element)=>{
const scrollableAncestors = [];
const win = ownerWindow(element);
let ancestor = getNearestOverflowAncestor(element);
let body = null;
while(ancestor){
const elementName = getElementName(ancestor);
if (elementName === 'body') {
body = ancestor;
} else if (elementName === 'html' && body) {
scrollableAncestors.push(body);
} else {
scrollableAncestors.push(ancestor);
}
if (win === ancestor) break;
ancestor = getNearestOverflowAncestor(ancestor);
}
return scrollableAncestors;
};
export { getOverflowAncestors };