Skip to content

Commit

Permalink
Remove custom iframe logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Jan 2, 2023
1 parent d9197bb commit 59856b7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 284 deletions.
71 changes: 11 additions & 60 deletions packages/components/src/popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
useFloating,
flip as flipMiddleware,
shift as shiftMiddleware,
limitShift,
autoUpdate,
arrow,
offset as offsetMiddleware,
size,
Middleware,
MiddlewareArguments,
} from '@floating-ui/react-dom';
// eslint-disable-next-line no-restricted-imports
import {
Expand Down Expand Up @@ -51,7 +51,6 @@ import Button from '../button';
import ScrollLock from '../scroll-lock';
import { Slot, Fill, useSlot } from '../slot-fill';
import {
getFrameOffset,
positionToPlacement,
placementToMotionAnimationProps,
getReferenceOwnerDocument,
Expand All @@ -64,7 +63,6 @@ import type {
PopoverAnchorRefReference,
PopoverAnchorRefTopBottom,
} from './types';
import { limitShift as customLimitShift } from './limit-shift';

/**
* Name of slot in which popover should fill.
Expand Down Expand Up @@ -259,37 +257,7 @@ const UnforwardedPopover = (
? positionToPlacement( position )
: placementProp;

/**
* Offsets the position of the popover when the anchor is inside an iframe.
*
* Store the offset in a ref, due to constraints with floating-ui:
* https://floating-ui.com/docs/react-dom#variables-inside-middleware-functions.
*/
const frameOffsetRef = useRef( getFrameOffset( referenceOwnerDocument ) );

const middleware = [
// Custom middleware which adjusts the popover's position by taking into
// account the offset of the anchor's iframe (if any) compared to the page.
{
name: 'frameOffset',
fn( { x, y }: MiddlewareArguments ) {
if ( ! frameOffsetRef.current ) {
return {
x,
y,
};
}

return {
x: x + frameOffsetRef.current.x,
y: y + frameOffsetRef.current.y,
data: {
// This will be used in the customLimitShift() function.
amount: frameOffsetRef.current,
},
};
},
},
offsetMiddleware( offsetProp ),
computedFlipProp ? flipMiddleware() : undefined,
computedResizeProp
Expand All @@ -313,7 +281,7 @@ const UnforwardedPopover = (
shift
? shiftMiddleware( {
crossAxis: true,
limiter: customLimitShift(),
limiter: limitShift(),
padding: 1, // Necessary to avoid flickering at the edge of the viewport.
} )
: undefined,
Expand Down Expand Up @@ -425,35 +393,24 @@ const UnforwardedPopover = (

// If the reference element is in a different ownerDocument (e.g. iFrame),
// we need to manually update the floating's position as the reference's owner
// document scrolls. Also update the frame offset if the view resizes.
// document scrolls.
useLayoutEffect( () => {
if (
// Reference and root documents are the same.
referenceOwnerDocument === document ||
// Reference and floating are in the same document.
referenceOwnerDocument === refs.floating.current?.ownerDocument ||
// The reference's document has no view (i.e. window)
// or frame element (ie. it's not an iframe).
! referenceOwnerDocument?.defaultView?.frameElement
! referenceOwnerDocument ||
! referenceOwnerDocument.defaultView
) {
frameOffsetRef.current = undefined;
return;
}

const { defaultView } = referenceOwnerDocument;

const updateFrameOffset = () => {
frameOffsetRef.current = getFrameOffset( referenceOwnerDocument );
update();
};
defaultView.addEventListener( 'resize', updateFrameOffset );

updateFrameOffset();
defaultView.addEventListener( 'resize', update );
update();

return () => {
defaultView.removeEventListener( 'resize', updateFrameOffset );
defaultView.removeEventListener( 'resize', update );
};
}, [ referenceOwnerDocument, update, refs.floating ] );
}, [ referenceOwnerDocument, update ] );

const mergedFloatingRef = useMergeRefs( [
floating,
Expand Down Expand Up @@ -527,18 +484,12 @@ const UnforwardedPopover = (
left:
typeof arrowData?.x !== 'undefined' &&
Number.isFinite( arrowData.x )
? `${
arrowData.x +
( frameOffsetRef.current?.x ?? 0 )
}px`
? `${ arrowData.x }px`
: '',
top:
typeof arrowData?.y !== 'undefined' &&
Number.isFinite( arrowData.y )
? `${
arrowData.y +
( frameOffsetRef.current?.y ?? 0 )
}px`
? `${ arrowData.y }px`
: '',
} }
>
Expand Down
205 changes: 0 additions & 205 deletions packages/components/src/popover/limit-shift.ts

This file was deleted.

19 changes: 0 additions & 19 deletions packages/components/src/popover/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,6 @@ export const placementToMotionAnimationProps = (
};
};

/**
* Returns the offset of a document's frame element.
*
* @param document The iframe's owner document.
*
* @return The offset of the document's frame element, or undefined if the
* document has no frame element.
*/
export const getFrameOffset = (
document?: Document
): { x: number; y: number } | undefined => {
const frameElement = document?.defaultView?.frameElement;
if ( ! frameElement ) {
return;
}
const iframeRect = frameElement.getBoundingClientRect();
return { x: iframeRect.left, y: iframeRect.top };
};

export const getReferenceOwnerDocument = ( {
anchor,
anchorRef,
Expand Down

0 comments on commit 59856b7

Please sign in to comment.