From 9e42a8baf70a3f365da5c5b4d798c34ddef39545 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Mon, 24 Apr 2023 15:48:46 +1000 Subject: [PATCH] Ensure resizing the block editor will update the block popover dimensions --- .../src/components/block-popover/index.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/block-editor/src/components/block-popover/index.js b/packages/block-editor/src/components/block-popover/index.js index 5382949684abd9..229b625dc3c948 100644 --- a/packages/block-editor/src/components/block-popover/index.js +++ b/packages/block-editor/src/components/block-popover/index.js @@ -75,6 +75,28 @@ function BlockPopover( }; }, [ selectedElement ] ); + // When cover target is enabled, the popover dimensions need to be recomputed + // when the window is resized. This is to ensure that the calculated dimensions + // of the popover are correct when the block editor is resized. + useLayoutEffect( () => { + if ( ! __unstableCoverTarget || ! selectedElement ) { + return; + } + + const defaultView = selectedElement?.ownerDocument.defaultView; + defaultView.addEventListener( + 'resize', + forceRecomputePopoverDimensions + ); + + return () => { + defaultView.removeEventListener( + 'resize', + forceRecomputePopoverDimensions + ); + }; + }, [ __unstableCoverTarget, selectedElement ] ); + const style = useMemo( () => { if ( // popoverDimensionsRecomputeCounter is by definition always equal or greater