Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,15 @@ function BlockPopover(
ref,
usePopoverScroll( __unstableContentRef ),
] );
const style = useMemo( () => {
if ( ! selectedElement || lastSelectedElement !== selectedElement ) {
return {};
}

return {
position: 'absolute',
width: selectedElement.offsetWidth,
height: selectedElement.offsetHeight,
};
}, [ selectedElement, lastSelectedElement, __unstableRefreshSize ] );

const [ popoverAnchorRecomputeCounter, forceRecomputePopoverAnchor ] =
useReducer(
// Module is there to make sure that the counter doesn't overflow.
( s ) => ( s + 1 ) % MAX_POPOVER_RECOMPUTE_COUNTER,
0
);
const [
popoverDimensionsRecomputeCounter,
forceRecomputePopoverDimensions,
] = useReducer(
// Module is there to make sure that the counter doesn't overflow.
( s ) => ( s + 1 ) % MAX_POPOVER_RECOMPUTE_COUNTER,
0
);

// When blocks are moved up/down, they are animated to their new position by
// updating the `transform` property manually (i.e. without using CSS
Expand All @@ -74,7 +65,7 @@ function BlockPopover(
}

const observer = new window.MutationObserver(
forceRecomputePopoverAnchor
forceRecomputePopoverDimensions
);
observer.observe( selectedElement, { attributes: true } );

Expand All @@ -83,12 +74,36 @@ function BlockPopover(
};
}, [ selectedElement ] );

const style = useMemo( () => {
if (
// popoverDimensionsRecomputeCounter is by definition always equal or greater
// than 0. This check is only there to satisfy the correctness of the
// exhaustive-deps rule for the `useMemo` hook.
popoverDimensionsRecomputeCounter < 0 ||
! selectedElement ||
lastSelectedElement !== selectedElement
) {
return {};
}

return {
position: 'absolute',
width: selectedElement.offsetWidth,
height: selectedElement.offsetHeight,
};
}, [
selectedElement,
lastSelectedElement,
__unstableRefreshSize,
popoverDimensionsRecomputeCounter,
] );

const popoverAnchor = useMemo( () => {
if (
// popoverAnchorRecomputeCounter is by definition always equal or greater
// popoverDimensionsRecomputeCounter is by definition always equal or greater
// than 0. This check is only there to satisfy the correctness of the
// exhaustive-deps rule for the `useMemo` hook.
popoverAnchorRecomputeCounter < 0 ||
popoverDimensionsRecomputeCounter < 0 ||
! selectedElement ||
( bottomClientId && ! lastSelectedElement )
) {
Expand Down Expand Up @@ -132,7 +147,7 @@ function BlockPopover(
bottomClientId,
lastSelectedElement,
selectedElement,
popoverAnchorRecomputeCounter,
popoverDimensionsRecomputeCounter,
] );

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