@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
51 lines • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOffsetParent = void 0;
const getParentNode_1 = require("./getParentNode");
const is_utils_1 = require("./is.utils");
const node_1 = require("./node");
const getTrueOffsetParent = (element) => {
if (!(0, is_utils_1.isHTMLElement)(element) || window.getComputedStyle(element).position === 'fixed') {
return null;
}
return element.offsetParent;
};
/**
* Manual search off the containing block
*/
const getContainingBlock = (element) => {
let currentNode = (0, getParentNode_1.getParentNode)(element);
while ((0, is_utils_1.isHTMLElement)(currentNode) && !(0, is_utils_1.isLastTraversableNode)(currentNode)) {
if ((0, is_utils_1.isContainingBlock)(currentNode)) {
return currentNode;
}
currentNode = (0, getParentNode_1.getParentNode)(currentNode);
}
return null;
};
/**
* Get the closest ancestor positioned element
*/
const getOffsetParent = (element) => {
if (!(0, is_utils_1.isHTMLElement)(element)) {
return window;
}
let offsetParent = getTrueOffsetParent(element);
// Do not take into account tables or static positions
while (offsetParent &&
(0, is_utils_1.isTableElement)(offsetParent) &&
window.getComputedStyle(offsetParent).position === 'static') {
offsetParent = getTrueOffsetParent(offsetParent);
}
const oPIsHtml = offsetParent && (0, node_1.getNodeName)(offsetParent) === 'html';
const oPIsStaticNoContainingBlockBody = offsetParent &&
(0, node_1.getNodeName)(offsetParent) === 'body' &&
window.getComputedStyle(offsetParent).position === 'static' &&
!(0, is_utils_1.isContainingBlock)(offsetParent);
if (oPIsHtml && oPIsStaticNoContainingBlockBody) {
return window;
}
return (offsetParent || getContainingBlock(element) || window);
};
exports.getOffsetParent = getOffsetParent;
//# sourceMappingURL=getOffsetParent.js.map