Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix popover glitch that results in incorrect toolbar positioning in site editor #43267

Merged
merged 8 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bug Fix

- `Popover`: fix and improve opening animation ([#43186](https://github.com/WordPress/gutenberg/pull/43186)).
- `Popover`: fix incorrect deps in hooks resulting in incorrect positioning after calling `update` ([#43267](https://github.com/WordPress/gutenberg/pull/43267/)).

### Enhancements

Expand Down
26 changes: 17 additions & 9 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 @@ -183,6 +187,10 @@ const Popover = (
}

return documentToReturn ?? document;

// 'reference' and 'refs.floating' are refs and don't need to be listed
// as dependencies (see https://github.com/WordPress/gutenberg/pull/41612)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ anchorRef, anchorRect, getAnchorRect ] );

/**
Expand All @@ -191,7 +199,7 @@ const Popover = (
* 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 = useRef( getFrameOffset( ownerDocument ) );

const middleware = [
frameOffset.current || offset
Expand Down Expand Up @@ -364,7 +372,7 @@ const Popover = (
refs.floating.current,
update
);
}, [ anchorRef, anchorRect, getAnchorRect ] );
}, [ anchorRef, anchorRect, getAnchorRect, update ] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For good measure, I ran npx eslint --rule 'react-hooks/exhaustive-deps: warn' packages/components/src/popover/.

While on trunk it flagged the missing update dependency here (and on the useLayoutEffect below), it also flagged reference and refs.floating as missing dependencies. Since reference is a callback ref, and refs.floating is a "modern" React ref, I suggest that we add a comment disabling the ESLint rule and explaining the reason why:

Suggested change
}, [ anchorRef, anchorRect, getAnchorRect, update ] );
// 'reference' and 'refs.floating' are refs and don't need to be listed
// as dependencies (see https://github.com/WordPress/gutenberg/pull/41612)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ anchorRef, anchorRect, getAnchorRect, update ] );


// This is only needed for a smooth transition when moving blocks.
useLayoutEffect( () => {
Expand All @@ -377,7 +385,7 @@ const Popover = (
return () => {
observer.disconnect();
};
}, [ __unstableObserveElement ] );
}, [ __unstableObserveElement, update ] );

// 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
Expand All @@ -388,15 +396,15 @@ const Popover = (
}

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

ownerDocument.addEventListener( 'scroll', update );

let updateFrameOffset;
if ( frameElement ) {
const hasFrameElement = !! ownerDocument?.defaultView?.frameElement;
if ( hasFrameElement ) {
updateFrameOffset = () => {
const iframeRect = frameElement.getBoundingClientRect();
frameOffset.current = { x: iframeRect.left, y: iframeRect.top };
frameOffset.current = getFrameOffset( ownerDocument );
update();
};
updateFrameOffset();
defaultView.addEventListener( 'resize', updateFrameOffset );
Expand All @@ -409,7 +417,7 @@ const Popover = (
defaultView.removeEventListener( 'resize', updateFrameOffset );
}
};
}, [ ownerDocument ] );
}, [ ownerDocument, update ] );

const mergedFloatingRef = useMergeRefs( [
floating,
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 };
};