Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block toolbar: use Popover's new anchor prop #43692

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
ciampo marked this conversation as resolved.
Show resolved Hide resolved
const left = Math.min( selectedBCR.left, lastSelectedBCR.left );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is an improved version of the same logic inside the Popover component

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