Skip to content

Commit

Permalink
Popover: enable auto-updating every animation frame (#43617)
Browse files Browse the repository at this point in the history
* Popover: enable autoupdating every animation frame

* Remove unnecessary __unobserveElement prop

* Remove unnecessary scroll listener on iframe

* CHANGELOG
  • Loading branch information
ciampo authored Aug 29, 2022
1 parent d9e6344 commit 02ca45b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 38 deletions.
2 changes: 0 additions & 2 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ function BlockPopover(
// Render in the old slot if needed for backward compatibility,
// otherwise render in place (not in the default popover slot).
__unstableSlotName={ __unstablePopoverSlot || null }
// Observe movement for block animations (especially horizontal).
__unstableObserveElement={ selectedElement }
__unstableForcePosition
__unstableShift
{ ...props }
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- `Popover`: enable auto-updating every animation frame ([#43617](https://github.com/WordPress/gutenberg/pull/43617)).

### Internal

- Remove unused `normalizeArrowKey` utility function ([#43640](https://github.com/WordPress/gutenberg/pull/43640/)).
Expand Down
52 changes: 17 additions & 35 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ const Popover = (
expandOnMobile,
onFocusOutside,
__unstableSlotName = SLOT_NAME,
__unstableObserveElement,
__unstableForcePosition = false,
__unstableShift = false,
...contentProps
Expand Down Expand Up @@ -374,59 +373,42 @@ const Popover = (
return autoUpdate(
resultingReferenceRef,
refs.floating.current,
update
update,
{
animationFrame: true,
}
);
// '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( () => {
if ( ! __unstableObserveElement ) {
return;
}
const observer = new window.MutationObserver( update );
observer.observe( __unstableObserveElement, { attributes: true } );

return () => {
observer.disconnect();
};
}, [ __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
// document scrolls. Also update the frame offset if the view resizes.
useLayoutEffect( () => {
if ( referenceOwnerDocument === document ) {
const referenceAndFloatingHaveSameDocument =
referenceOwnerDocument === document;
const hasFrameElement =
!! referenceOwnerDocument?.defaultView?.frameElement;

if ( referenceAndFloatingHaveSameDocument || ! hasFrameElement ) {
frameOffsetRef.current = undefined;
return;
}

const { defaultView } = referenceOwnerDocument;

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

let updateFrameOffset;
const hasFrameElement =
!! referenceOwnerDocument?.defaultView?.frameElement;
if ( hasFrameElement ) {
updateFrameOffset = () => {
frameOffsetRef.current = getFrameOffset(
referenceOwnerDocument
);
update();
};
updateFrameOffset();
defaultView.addEventListener( 'resize', updateFrameOffset );
}
updateFrameOffset();

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

if ( updateFrameOffset ) {
defaultView.removeEventListener( 'resize', updateFrameOffset );
}
defaultView.removeEventListener( 'resize', updateFrameOffset );
};
}, [ referenceOwnerDocument, update ] );

Expand Down
1 change: 0 additions & 1 deletion packages/components/src/popover/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export default {
options: AVAILABLE_POSITIONS,
},
__unstableSlotName: { control: { type: null } },
__unstableObserveElement: { control: { type: null } },
__unstableForcePosition: { control: { type: 'boolean' } },
__unstableShift: { control: { type: 'boolean' } },
},
Expand Down

0 comments on commit 02ca45b

Please sign in to comment.