diff --git a/packages/block-editor/src/components/block-popover/index.js b/packages/block-editor/src/components/block-popover/index.js index 2a66623e2fbd6..aff95b62cfcff 100644 --- a/packages/block-editor/src/components/block-popover/index.js +++ b/packages/block-editor/src/components/block-popover/index.js @@ -47,22 +47,48 @@ function BlockPopover( }; }, [ selectedElement, lastSelectedElement, __unstableRefreshSize ] ); + const popoverAnchor = useMemo( + () => ( { + getBoundingClientRect() { + const selectedBCR = selectedElement.getBoundingClientRect(); + const lastSelectedBCR = + lastSelectedElement.getBoundingClientRect(); + + // Get the biggest rectangle that encompasses completely the currently + // selected element and the last selected element: + // - for top/left coordinates, use the smaller numbers + // - for the bottom/right coordinates, use the largest numbers + const left = Math.min( selectedBCR.left, lastSelectedBCR.left ); + const top = Math.min( selectedBCR.top, lastSelectedBCR.top ); + const right = Math.max( + selectedBCR.right, + lastSelectedBCR.right + ); + const bottom = Math.max( + selectedBCR.bottom, + lastSelectedBCR.bottom + ); + const width = right - left; + const height = bottom - top; + + return new window.DOMRect( left, top, width, height ); + }, + ownerDocument: selectedElement.ownerDocument, + } ), + [ selectedElement, lastSelectedElement ] + ); + if ( ! selectedElement || ( bottomClientId && ! lastSelectedElement ) ) { return null; } - const anchorRef = { - top: selectedElement, - bottom: lastSelectedElement, - }; - return (