@kubit-ui-web/react-components
Version:
Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience
47 lines • 1.74 kB
JavaScript
import { getParentNode } from './getParentNode';
import { isContainingBlock, isHTMLElement, isLastTraversableNode, isTableElement, } from './is.utils';
import { getNodeName } from './node';
const getTrueOffsetParent = (element) => {
if (!isHTMLElement(element) || window.getComputedStyle(element).position === 'fixed') {
return null;
}
return element.offsetParent;
};
/**
* Manual search off the containing block
*/
const getContainingBlock = (element) => {
let currentNode = getParentNode(element);
while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
if (isContainingBlock(currentNode)) {
return currentNode;
}
currentNode = getParentNode(currentNode);
}
return null;
};
/**
* Get the closest ancestor positioned element
*/
export const getOffsetParent = (element) => {
if (!isHTMLElement(element)) {
return window;
}
let offsetParent = getTrueOffsetParent(element);
// Do not take into account tables or static positions
while (offsetParent &&
isTableElement(offsetParent) &&
window.getComputedStyle(offsetParent).position === 'static') {
offsetParent = getTrueOffsetParent(offsetParent);
}
const oPIsHtml = offsetParent && getNodeName(offsetParent) === 'html';
const oPIsStaticNoContainingBlockBody = offsetParent &&
getNodeName(offsetParent) === 'body' &&
window.getComputedStyle(offsetParent).position === 'static' &&
!isContainingBlock(offsetParent);
if (oPIsHtml && oPIsStaticNoContainingBlockBody) {
return window;
}
return (offsetParent || getContainingBlock(element) || window);
};
//# sourceMappingURL=getOffsetParent.js.map