Skip to content

Commit

Permalink
BlockPopover: prevent error when selectedElement is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Sep 1, 2022
1 parent 0556874 commit 67e35de
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,48 @@ function BlockPopover(
};
}, [ selectedElement, lastSelectedElement, __unstableRefreshSize ] );

const popoverAnchor = useMemo(
() => ( {
const popoverAnchor = useMemo( () => {
if (
! selectedElement ||
( bottomClientId && ! lastSelectedElement )
) {
return undefined;
}

return {
getBoundingClientRect() {
const selectedBCR = selectedElement.getBoundingClientRect();
const lastSelectedBCR =
lastSelectedElement.getBoundingClientRect();
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 left = Math.min(
selectedBCR.left,
lastSelectedBCR?.left ?? Infinity
);
const top = Math.min(
selectedBCR.top,
lastSelectedBCR?.top ?? Infinity
);
const right = Math.max(
selectedBCR.right,
lastSelectedBCR.right
lastSelectedBCR.right ?? -Infinity
);
const bottom = Math.max(
selectedBCR.bottom,
lastSelectedBCR.bottom
lastSelectedBCR.bottom ?? -Infinity
);
const width = right - left;
const height = bottom - top;

return new window.DOMRect( left, top, width, height );
},
ownerDocument: selectedElement.ownerDocument,
} ),
[ selectedElement, lastSelectedElement ]
);
};
}, [ bottomClientId, lastSelectedElement, selectedElement ] );

if ( ! selectedElement || ( bottomClientId && ! lastSelectedElement ) ) {
return null;
Expand Down

0 comments on commit 67e35de

Please sign in to comment.