UNPKG

@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

32 lines 1.51 kB
import { getBoundingClientRect } from './getBoundingClientRect'; import { getElementScroll } from './getElementScroll'; import { getWindowScrollBarX } from './getWindowScrollBarX'; import { isHTMLElement, isOverflowElement } from './is.utils'; import { getNodeName } from './node'; export const getRectRelativeToOffsetParent = (element, offsetParent, strategy) => { const isOffsetParentAnElement = isHTMLElement(offsetParent); const documentElement = window.document.documentElement; const rect = getBoundingClientRect(element, strategy === 'fixed', offsetParent); let scroll = { scrollLeft: 0, scrollTop: 0 }; const offsets = { x: 0, y: 0 }; if (isOffsetParentAnElement || (!isOffsetParentAnElement && strategy !== 'fixed')) { if (getNodeName(offsetParent) !== 'body' || isOverflowElement(documentElement)) { scroll = getElementScroll(offsetParent); } if (isHTMLElement(offsetParent)) { const offsetRect = getBoundingClientRect(offsetParent); offsets.x = offsetRect.x + offsetParent.clientLeft; offsets.y = offsetRect.y + offsetParent.clientTop; } else if (documentElement) { offsets.x = getWindowScrollBarX(documentElement); } } return { x: rect.left + scroll.scrollLeft - offsets.x, y: rect.top + scroll.scrollTop - offsets.y, width: rect.width, height: rect.height, }; }; //# sourceMappingURL=getRectRelativeToOffsetParent.js.map