From 5efcdd7b4c8a461a6c3d22ae163dc83775a2ead3 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 27 Sep 2022 14:26:12 +0800 Subject: [PATCH] Fix padding/margin visualizer accuracy --- .../src/components/block-popover/index.js | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/packages/block-editor/src/components/block-popover/index.js b/packages/block-editor/src/components/block-popover/index.js index 3d41db03d72a3e..ed6ca0a159f70e 100644 --- a/packages/block-editor/src/components/block-popover/index.js +++ b/packages/block-editor/src/components/block-popover/index.js @@ -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 @@ -74,7 +65,7 @@ function BlockPopover( } const observer = new window.MutationObserver( - forceRecomputePopoverAnchor + forceRecomputePopoverDimensions ); observer.observe( selectedElement, { attributes: true } ); @@ -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 ) ) { @@ -132,7 +147,7 @@ function BlockPopover( bottomClientId, lastSelectedElement, selectedElement, - popoverAnchorRecomputeCounter, + popoverDimensionsRecomputeCounter, ] ); if ( ! selectedElement || ( bottomClientId && ! lastSelectedElement ) ) {