Skip to content

Commit 02ca45b

Browse files
authored
Popover: enable auto-updating every animation frame (#43617)
* Popover: enable autoupdating every animation frame * Remove unnecessary __unobserveElement prop * Remove unnecessary scroll listener on iframe * CHANGELOG
1 parent d9e6344 commit 02ca45b

File tree

4 files changed

+21
-38
lines changed

4 files changed

+21
-38
lines changed

packages/block-editor/src/components/block-popover/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ function BlockPopover(
6666
// Render in the old slot if needed for backward compatibility,
6767
// otherwise render in place (not in the default popover slot).
6868
__unstableSlotName={ __unstablePopoverSlot || null }
69-
// Observe movement for block animations (especially horizontal).
70-
__unstableObserveElement={ selectedElement }
7169
__unstableForcePosition
7270
__unstableShift
7371
{ ...props }

packages/components/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Bug Fix
6+
7+
- `Popover`: enable auto-updating every animation frame ([#43617](https://github.com/WordPress/gutenberg/pull/43617)).
8+
59
### Internal
610

711
- Remove unused `normalizeArrowKey` utility function ([#43640](https://github.com/WordPress/gutenberg/pull/43640/)).

packages/components/src/popover/index.js

+17-35
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ const Popover = (
143143
expandOnMobile,
144144
onFocusOutside,
145145
__unstableSlotName = SLOT_NAME,
146-
__unstableObserveElement,
147146
__unstableForcePosition = false,
148147
__unstableShift = false,
149148
...contentProps
@@ -374,59 +373,42 @@ const Popover = (
374373
return autoUpdate(
375374
resultingReferenceRef,
376375
refs.floating.current,
377-
update
376+
update,
377+
{
378+
animationFrame: true,
379+
}
378380
);
379381
// 'reference' and 'refs.floating' are refs and don't need to be listed
380382
// as dependencies (see https://github.com/WordPress/gutenberg/pull/41612)
381383
// eslint-disable-next-line react-hooks/exhaustive-deps
382384
}, [ anchorRef, anchorRect, getAnchorRect, update ] );
383385

384-
// This is only needed for a smooth transition when moving blocks.
385-
useLayoutEffect( () => {
386-
if ( ! __unstableObserveElement ) {
387-
return;
388-
}
389-
const observer = new window.MutationObserver( update );
390-
observer.observe( __unstableObserveElement, { attributes: true } );
391-
392-
return () => {
393-
observer.disconnect();
394-
};
395-
}, [ __unstableObserveElement, update ] );
396-
397386
// If the reference element is in a different ownerDocument (e.g. iFrame),
398387
// we need to manually update the floating's position as the reference's owner
399388
// document scrolls. Also update the frame offset if the view resizes.
400389
useLayoutEffect( () => {
401-
if ( referenceOwnerDocument === document ) {
390+
const referenceAndFloatingHaveSameDocument =
391+
referenceOwnerDocument === document;
392+
const hasFrameElement =
393+
!! referenceOwnerDocument?.defaultView?.frameElement;
394+
395+
if ( referenceAndFloatingHaveSameDocument || ! hasFrameElement ) {
402396
frameOffsetRef.current = undefined;
403397
return;
404398
}
405399

406400
const { defaultView } = referenceOwnerDocument;
407401

408-
referenceOwnerDocument.addEventListener( 'scroll', update );
402+
const updateFrameOffset = () => {
403+
frameOffsetRef.current = getFrameOffset( referenceOwnerDocument );
404+
update();
405+
};
406+
defaultView.addEventListener( 'resize', updateFrameOffset );
409407

410-
let updateFrameOffset;
411-
const hasFrameElement =
412-
!! referenceOwnerDocument?.defaultView?.frameElement;
413-
if ( hasFrameElement ) {
414-
updateFrameOffset = () => {
415-
frameOffsetRef.current = getFrameOffset(
416-
referenceOwnerDocument
417-
);
418-
update();
419-
};
420-
updateFrameOffset();
421-
defaultView.addEventListener( 'resize', updateFrameOffset );
422-
}
408+
updateFrameOffset();
423409

424410
return () => {
425-
referenceOwnerDocument.removeEventListener( 'scroll', update );
426-
427-
if ( updateFrameOffset ) {
428-
defaultView.removeEventListener( 'resize', updateFrameOffset );
429-
}
411+
defaultView.removeEventListener( 'resize', updateFrameOffset );
430412
};
431413
}, [ referenceOwnerDocument, update ] );
432414

packages/components/src/popover/stories/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export default {
7373
options: AVAILABLE_POSITIONS,
7474
},
7575
__unstableSlotName: { control: { type: null } },
76-
__unstableObserveElement: { control: { type: null } },
7776
__unstableForcePosition: { control: { type: 'boolean' } },
7877
__unstableShift: { control: { type: 'boolean' } },
7978
},

0 commit comments

Comments
 (0)