Skip to content

Commit

Permalink
Remove custom iframe logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo authored and jsnajdr committed Aug 21, 2023
1 parent 07ee1ba commit 961758f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 301 deletions.
90 changes: 15 additions & 75 deletions packages/components/src/popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
*/
import type { ForwardedRef, SyntheticEvent, RefCallback } from 'react';
import classnames from 'classnames';
import type { Middleware, MiddlewareArguments } from '@floating-ui/react-dom';
import type { Middleware } from '@floating-ui/react-dom';
import {
useFloating,
flip as flipMiddleware,
shift as shiftMiddleware,
limitShift,
autoUpdate,
arrow,
offset as offsetMiddleware,
Expand Down Expand Up @@ -39,7 +40,6 @@ import {
import { close } from '@wordpress/icons';
import deprecated from '@wordpress/deprecated';
import { Path, SVG } from '@wordpress/primitives';
import { getScrollContainer } from '@wordpress/dom';

/**
* Internal dependencies
Expand All @@ -49,7 +49,6 @@ import ScrollLock from '../scroll-lock';
import { Slot, Fill, useSlot } from '../slot-fill';
import {
computePopoverPosition,
getFrameOffset,
getFrameScale,
positionToPlacement,
placementToMotionAnimationProps,
Expand All @@ -63,7 +62,6 @@ import type {
PopoverAnchorRefReference,
PopoverAnchorRefTopBottom,
} from './types';
import { limitShift as customLimitShift } from './limit-shift';
import { overlayMiddlewares } from './overlay-middlewares';

/**
Expand Down Expand Up @@ -246,38 +244,8 @@ 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 = [
...( placementProp === 'overlay' ? overlayMiddlewares() : [] ),
// 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 @@ -301,7 +269,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 @@ -337,10 +305,6 @@ const UnforwardedPopover = (
// Positioning coordinates
x,
y,
// Callback refs (not regular refs). This allows the position to be updated.
// when either elements change.
reference: referenceCallbackRef,
floating,
// Object with "regular" refs to both "reference" and "floating"
refs,
// Type of CSS position property to use (absolute or fixed)
Expand Down Expand Up @@ -400,7 +364,7 @@ const UnforwardedPopover = (
scale,
} );

referenceCallbackRef( resultingReferenceElement );
refs.setReference( resultingReferenceElement );

setReferenceOwnerDocument( resultingReferenceOwnerDoc );
}, [
Expand All @@ -413,50 +377,32 @@ const UnforwardedPopover = (
anchorRect,
getAnchorRect,
fallbackReferenceElement,
referenceCallbackRef,
refs,
] );

// 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 { frameElement } = defaultView;

const scrollContainer = frameElement
? getScrollContainer( frameElement )
: null;

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

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

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

const mergedFloatingRef = useMergeRefs( [
floating,
refs.floating,
dialogRef,
forwardedRef,
] );
Expand Down Expand Up @@ -527,18 +473,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.

Loading

0 comments on commit 961758f

Please sign in to comment.