From c0bbfcceb2a52f66ee35ee594064b3e5fbb18fd6 Mon Sep 17 00:00:00 2001 From: Adithya M S Date: Tue, 13 Jun 2023 11:34:51 +0530 Subject: [PATCH] Fix getOffset function to render elements in iframe offset from the parent document --- src/util/getOffset.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/util/getOffset.ts b/src/util/getOffset.ts index 1b454f585..c39095a3f 100644 --- a/src/util/getOffset.ts +++ b/src/util/getOffset.ts @@ -28,6 +28,16 @@ export default function getOffset( height: x.height, }; + let boundingFrameClientRect = { + top: 0, + left: 0 + } + + if((element) && (element.ownerDocument !== window.document)) + { + boundingFrameClientRect = element.ownerDocument.defaultView.frameElement.getBoundingClientRect() + } + if ( (relativeEl.tagName.toLowerCase() !== "body" && relativeElPosition === "relative") || @@ -36,19 +46,19 @@ export default function getOffset( // when the container of our target element is _not_ body and has either "relative" or "sticky" position, we should not // consider the scroll position but we need to include the relative x/y of the container element return Object.assign(obj, { - top: x.top - xr.top, - left: x.left - xr.left, + top: x.top - xr.top + boundingFrameClientRect.top, + left: x.left - xr.left + boundingFrameClientRect.left, }); } else { if (isFixed(element)) { return Object.assign(obj, { - top: x.top, - left: x.left, + top: x.top + boundingFrameClientRect.top, + left: x.left + boundingFrameClientRect.left, }); } else { return Object.assign(obj, { - top: x.top + scrollTop, - left: x.left + scrollLeft, + top: x.top + scrollTop + boundingFrameClientRect.top, + left: x.left + scrollLeft + boundingFrameClientRect.left, }); } }