Skip to content

Commit

Permalink
Revert "Fix block toolbar offset in site editor when toggling sidebars (
Browse files Browse the repository at this point in the history
#43172)"

This reverts commit 9031960.
  • Loading branch information
ntsekouras authored Aug 16, 2022
1 parent 430a9c0 commit 342da41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
1 change: 0 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
- `Popover`: anchor correctly to parent node when no explicit anchor is passed ([#42971](https://github.com/WordPress/gutenberg/pull/42971)).
- `ColorPalette`: forward correctly `popoverProps` in the `CustomColorPickerDropdown` component [#42989](https://github.com/WordPress/gutenberg/pull/42989)).
- `ColorPalette`, `CustomGradientBar`: restore correct color picker popover position [#42989](https://github.com/WordPress/gutenberg/pull/42989)).
- `Popover`: fix iframe offset not updating when iframe resizes ([#42971](https://github.com/WordPress/gutenberg/pull/43172)).

### Enhancements

Expand Down
47 changes: 17 additions & 30 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,23 @@ const Popover = (

/**
* 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 frameOffset = useRef();
const frameOffset = useMemo( () => {
const { defaultView } = ownerDocument;
const { frameElement } = defaultView;

if ( ! frameElement || ownerDocument === document ) {
return undefined;
}

const iframeRect = frameElement.getBoundingClientRect();
return { x: iframeRect.left, y: iframeRect.top };
}, [ ownerDocument ] );

const middleware = [
frameOffset.current || offset
frameOffset || offset
? offsetMiddleware( ( { placement: currentPlacement } ) => {
if ( ! frameOffset.current ) {
if ( ! frameOffset ) {
return offset;
}

Expand All @@ -221,8 +228,8 @@ const Popover = (
return {
mainAxis:
normalizedOffset +
frameOffset.current[ mainAxis ] * mainAxisModifier,
crossAxis: frameOffset.current[ crossAxis ],
frameOffset[ mainAxis ] * mainAxisModifier,
crossAxis: frameOffset[ crossAxis ],
};
} )
: undefined,
Expand Down Expand Up @@ -381,34 +388,14 @@ const Popover = (

// 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 ( ownerDocument === document ) {
return;
}

const { defaultView } = ownerDocument;
const { frameElement } = defaultView;

ownerDocument.addEventListener( 'scroll', update );

let updateFrameOffset;
if ( frameElement ) {
updateFrameOffset = () => {
const iframeRect = frameElement.getBoundingClientRect();
frameOffset.current = { x: iframeRect.left, y: iframeRect.top };
};
updateFrameOffset();
defaultView.addEventListener( 'resize', updateFrameOffset );
}

return () => {
ownerDocument.removeEventListener( 'scroll', update );

if ( updateFrameOffset ) {
defaultView.removeEventListener( 'resize', updateFrameOffset );
}
};
return () => ownerDocument.removeEventListener( 'scroll', update );
}, [ ownerDocument ] );

/** @type {false | string} */
Expand Down

0 comments on commit 342da41

Please sign in to comment.