Skip to content

Commit

Permalink
Fix multi select popover position
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Mar 26, 2020
1 parent 7d71564 commit 3dab5d7
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ const FocusManaged = withConstrainedTabbing(
*/
const SLOT_NAME = 'Popover';

function offsetIframe( rect, ownerDocument ) {
if ( ownerDocument !== document ) {
const iframe = Array.from( document.querySelectorAll( 'iframe' ) ).find(
( element ) => {
return element.contentDocument === ownerDocument;
}
);
const iframeRect = iframe.getBoundingClientRect();

rect = new window.DOMRect(
rect.left + iframeRect.left,
rect.top + iframeRect.top,
rect.width,
rect.height
);
}

return rect;
}

function computeAnchorRect(
anchorRefFallback,
anchorRect,
Expand All @@ -63,47 +83,19 @@ function computeAnchorRect(

if ( anchorRef.endContainer ) {
const { ownerDocument } = anchorRef.endContainer;
let rect = getRectangleFromRange( anchorRef );

if ( ownerDocument !== document ) {
const iframe = Array.from(
document.querySelectorAll( 'iframe' )
).find( ( element ) => {
return element.contentDocument === ownerDocument;
} );
const iframeRect = iframe.getBoundingClientRect();

rect = new window.DOMRect(
rect.left + iframeRect.left,
rect.top + iframeRect.top,
rect.width,
rect.height
);
}

return rect;
return offsetIframe(
getRectangleFromRange( anchorRef ),
ownerDocument
);
}

const { ownerDocument } = anchorRef;

if ( ownerDocument ) {
let rect = anchorRef.getBoundingClientRect();

if ( ownerDocument !== document ) {
const iframe = Array.from(
document.querySelectorAll( 'iframe' )
).find( ( element ) => {
return element.contentDocument === ownerDocument;
} );
const iframeRect = iframe.getBoundingClientRect();

rect = new window.DOMRect(
rect.left + iframeRect.left,
rect.top + iframeRect.top,
rect.width,
rect.height
);
}
const rect = offsetIframe(
anchorRef.getBoundingClientRect(),
ownerDocument
);

if ( shouldAnchorIncludePadding ) {
return rect;
Expand All @@ -115,13 +107,15 @@ function computeAnchorRect(
const { top, bottom } = anchorRef;
const topRect = top.getBoundingClientRect();
const bottomRect = bottom.getBoundingClientRect();
const rect = new window.DOMRect(
let rect = new window.DOMRect(
topRect.left,
topRect.top,
topRect.width,
bottomRect.bottom - topRect.top
);

rect = offsetIframe( rect, top.ownerDocument );

if ( shouldAnchorIncludePadding ) {
return rect;
}
Expand Down

0 comments on commit 3dab5d7

Please sign in to comment.