@nex-ui/react
Version:
🎉 A beautiful, modern, and reliable React component library.
42 lines (39 loc) • 1.67 kB
JavaScript
import { ownerWindow } from '@nex-ui/utils';
import { isSVGElement, isRootElement, isElement, isStaticPositioned, isTableElement, isContainingBlock, isHTMLElement } from './dom.mjs';
function _getOffsetParent(element) {
// istanbul ignore if
if (!isHTMLElement(element)) {
return null;
}
return element.offsetParent;
}
/**
* A positioned ancestor might be:
* - a containing block for absolutely-positioned elements
* - an element with a different effective zoom value (that is, the product of all zoom scales of its parents) from this element
* - td, th, table in case the element itself is static positioned.
*
* https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetparent
*/ const getOffsetParent = (element)=>{
const win = ownerWindow(element);
if (isSVGElement(element)) {
// SVG element hasn't offsetParent, find the nearest Element
let svgOffsetParent = element.parentElement;
while(svgOffsetParent && !isRootElement(svgOffsetParent)){
if (isElement(svgOffsetParent) && !isStaticPositioned(svgOffsetParent)) {
return svgOffsetParent;
}
svgOffsetParent = svgOffsetParent.parentElement;
}
return win;
}
let offsetParent = _getOffsetParent(element);
while(offsetParent && isTableElement(offsetParent) && isStaticPositioned(offsetParent)){
offsetParent = _getOffsetParent(offsetParent);
}
if (offsetParent && isRootElement(offsetParent) && isStaticPositioned(offsetParent) && !isContainingBlock(offsetParent)) {
return win;
}
return offsetParent || win;
};
export { getOffsetParent };