Skip to content

Commit

Permalink
Block toolbar: use anchor prop instead of anchorRef.{top,bottom}
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Aug 31, 2022
1 parent fb73356 commit 35ff84c
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 larget 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 (
<Popover
ref={ mergedRefs }
animate={ false }
position="top right left"
focusOnMount={ false }
anchorRef={ anchorRef }
anchor={ popoverAnchor }
// Render in the old slot if needed for backward compatibility,
// otherwise render in place (not in the default popover slot).
__unstableSlotName={ __unstablePopoverSlot || null }
Expand Down

0 comments on commit 35ff84c

Please sign in to comment.