Skip to content

Commit

Permalink
Move getFrameOffset to utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Aug 17, 2022
1 parent 351e66c commit 433c550
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
15 changes: 5 additions & 10 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ import { Path, SVG } from '@wordpress/primitives';
import Button from '../button';
import ScrollLock from '../scroll-lock';
import { Slot, Fill, useSlot } from '../slot-fill';
import { positionToPlacement, placementToMotionAnimationProps } from './utils';
import {
getFrameOffset,
positionToPlacement,
placementToMotionAnimationProps,
} from './utils';

/**
* Name of slot in which popover should fill.
Expand Down Expand Up @@ -118,15 +122,6 @@ const MaybeAnimatedWrapper = forwardRef(

const slotNameContext = createContext();

const getFrameOffset = ( document ) => {
const frameElement = document?.defaultView?.frameElement;
if ( ! frameElement ) {
return;
}
const iframeRect = frameElement.getBoundingClientRect();
return { x: iframeRect.left, y: iframeRect.top };
};

const Popover = (
{
range,
Expand Down
24 changes: 24 additions & 0 deletions packages/components/src/popover/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,27 @@ export const placementToMotionAnimationProps = ( placement ) => {
transition: { duration: 0.1, ease: [ 0, 0, 0.2, 1 ] },
};
};

/**
* @typedef FrameOffset
* @type {Object}
* @property {number} x A numerical value representing the horizontal offset of the frame.
* @property {number} y A numerical value representing the vertical offset of the frame.
*/

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

0 comments on commit 433c550

Please sign in to comment.