Skip to content

Commit

Permalink
Fix padding/margin visualizer accuracy (#44485)
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan authored Sep 27, 2022
1 parent aae942f commit 77960a0
Showing 1 changed file with 36 additions and 21 deletions.
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

0 comments on commit 77960a0

Please sign in to comment.