From b8cc4b8869a34dc66e469153f8e4ce6eb6feb3b3 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 22 Feb 2024 15:28:09 +1100 Subject: [PATCH] BlockPopover: Remove __unstableCoverTarget and __unstableRefreshSize in favour of BlockPopoverCover (#59228) * BlockPopover: Remove __unstableRefreshSize prop and improve how __unstableCoverTarget works * BlockPopover: Remove __unstableCoverTarget in favour of BlockPopoverCover Co-authored-by: noisysocks Co-authored-by: ramonjd --- .../src/components/block-popover/cover.js | 63 +++++++++++++++++++ .../src/components/block-popover/drop-zone.js | 7 +-- .../src/components/block-popover/index.js | 29 +-------- .../block-tools/empty-block-inserter.js | 9 +-- .../components/resizable-box-popover/index.js | 8 +-- packages/block-editor/src/hooks/margin.js | 9 +-- packages/block-editor/src/hooks/padding.js | 9 +-- .../src/cover/edit/resizable-cover-popover.js | 7 +-- 8 files changed, 80 insertions(+), 61 deletions(-) create mode 100644 packages/block-editor/src/components/block-popover/cover.js diff --git a/packages/block-editor/src/components/block-popover/cover.js b/packages/block-editor/src/components/block-popover/cover.js new file mode 100644 index 00000000000000..6d2d5b8ce1ac03 --- /dev/null +++ b/packages/block-editor/src/components/block-popover/cover.js @@ -0,0 +1,63 @@ +/** + * WordPress dependencies + */ +import { useEffect, useState, useMemo, forwardRef } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs'; +import BlockPopover from '.'; + +function BlockPopoverCover( + { clientId, bottomClientId, children, shift = false, ...props }, + ref +) { + bottomClientId ??= clientId; + + const selectedElement = useBlockElement( clientId ); + + return ( + + { selectedElement && clientId === bottomClientId ? ( + + { children } + + ) : ( + children + ) } + + ); +} + +function CoverContainer( { selectedElement, children } ) { + const [ width, setWidth ] = useState( selectedElement.offsetWidth ); + const [ height, setHeight ] = useState( selectedElement.offsetHeight ); + + useEffect( () => { + const observer = new window.ResizeObserver( () => { + setWidth( selectedElement.offsetWidth ); + setHeight( selectedElement.offsetHeight ); + } ); + observer.observe( selectedElement, { box: 'border-box' } ); + return () => observer.disconnect(); + }, [ selectedElement ] ); + + const style = useMemo( () => { + return { + position: 'absolute', + width, + height, + }; + }, [ width, height ] ); + + return
{ children }
; +} + +export default forwardRef( BlockPopoverCover ); diff --git a/packages/block-editor/src/components/block-popover/drop-zone.js b/packages/block-editor/src/components/block-popover/drop-zone.js index c26f28127022d6..1174c619cb3f43 100644 --- a/packages/block-editor/src/components/block-popover/drop-zone.js +++ b/packages/block-editor/src/components/block-popover/drop-zone.js @@ -9,7 +9,7 @@ import { __unstableMotion as motion } from '@wordpress/components'; * Internal dependencies */ import { store as blockEditorStore } from '../../store'; -import BlockPopover from './index'; +import BlockPopoverCover from './cover'; const animateVariants = { hide: { opacity: 0, scaleY: 0.75 }, @@ -38,9 +38,8 @@ function BlockDropZonePopover( { const reducedMotion = useReducedMotion(); return ( - - + ); } diff --git a/packages/block-editor/src/components/block-popover/index.js b/packages/block-editor/src/components/block-popover/index.js index 13e6ba4d9e7f81..d5ac90c68745ec 100644 --- a/packages/block-editor/src/components/block-popover/index.js +++ b/packages/block-editor/src/components/block-popover/index.js @@ -28,8 +28,6 @@ function BlockPopover( clientId, bottomClientId, children, - __unstableRefreshSize, - __unstableCoverTarget = false, __unstablePopoverSlot, __unstableContentRef, shift = true, @@ -75,30 +73,6 @@ 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 ( // popoverDimensionsRecomputeCounter is by definition always equal or greater @@ -176,8 +150,7 @@ function BlockPopover( ) } variant="unstyled" > - { __unstableCoverTarget &&
{ children }
} - { ! __unstableCoverTarget && children } + { children } ); } diff --git a/packages/block-editor/src/components/block-tools/empty-block-inserter.js b/packages/block-editor/src/components/block-tools/empty-block-inserter.js index 1d520ed72b1c69..bf708c686363ff 100644 --- a/packages/block-editor/src/components/block-tools/empty-block-inserter.js +++ b/packages/block-editor/src/components/block-tools/empty-block-inserter.js @@ -6,7 +6,7 @@ import classnames from 'classnames'; /** * Internal dependencies */ -import BlockPopover from '../block-popover'; +import BlockPopoverCover from '../block-popover/cover'; import useBlockToolbarPopoverProps from './use-block-toolbar-popover-props'; import Inserter from '../inserter'; import useSelectedBlockToolProps from './use-selected-block-tool-props'; @@ -28,9 +28,8 @@ export default function EmptyBlockInserter( { } ); return ( -
@@ -51,6 +48,6 @@ export default function EmptyBlockInserter( { __experimentalIsQuick />
-
+ ); } diff --git a/packages/block-editor/src/components/resizable-box-popover/index.js b/packages/block-editor/src/components/resizable-box-popover/index.js index 12a61aceaaf38e..8a49c1631287a1 100644 --- a/packages/block-editor/src/components/resizable-box-popover/index.js +++ b/packages/block-editor/src/components/resizable-box-popover/index.js @@ -6,7 +6,7 @@ import { ResizableBox } from '@wordpress/components'; /** * Internal dependencies */ -import BlockPopover from '../block-popover'; +import BlockPopoverCover from '../block-popover/cover'; export default function ResizableBoxPopover( { clientId, @@ -14,14 +14,12 @@ export default function ResizableBoxPopover( { ...props } ) { return ( - - + ); } diff --git a/packages/block-editor/src/hooks/margin.js b/packages/block-editor/src/hooks/margin.js index 8f723b3f8c97de..7be1179d295100 100644 --- a/packages/block-editor/src/hooks/margin.js +++ b/packages/block-editor/src/hooks/margin.js @@ -7,7 +7,7 @@ import isShallowEqual from '@wordpress/is-shallow-equal'; /** * Internal dependencies */ -import BlockPopover from '../components/block-popover'; +import BlockPopoverCover from '../components/block-popover/cover'; import { __unstableUseBlockElement as useBlockElement } from '../components/block-list/use-block-props/use-block-refs'; function getComputedCSS( element, property ) { @@ -78,14 +78,11 @@ export function MarginVisualizer( { clientId, attributes, forceShow } ) { } return ( -
- + ); } diff --git a/packages/block-editor/src/hooks/padding.js b/packages/block-editor/src/hooks/padding.js index ca4436153d122c..50eed7ac05d5e9 100644 --- a/packages/block-editor/src/hooks/padding.js +++ b/packages/block-editor/src/hooks/padding.js @@ -7,7 +7,7 @@ import isShallowEqual from '@wordpress/is-shallow-equal'; /** * Internal dependencies */ -import BlockPopover from '../components/block-popover'; +import BlockPopoverCover from '../components/block-popover/cover'; import { __unstableUseBlockElement as useBlockElement } from '../components/block-list/use-block-props/use-block-refs'; function getComputedCSS( element, property ) { @@ -69,14 +69,11 @@ export function PaddingVisualizer( { clientId, value, forceShow } ) { } return ( -
- + ); } diff --git a/packages/block-library/src/cover/edit/resizable-cover-popover.js b/packages/block-library/src/cover/edit/resizable-cover-popover.js index 7db8b0cc50b883..64d1bf106c3bc9 100644 --- a/packages/block-library/src/cover/edit/resizable-cover-popover.js +++ b/packages/block-library/src/cover/edit/resizable-cover-popover.js @@ -6,7 +6,7 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { useMemo, useState } from '@wordpress/element'; +import { useState } from '@wordpress/element'; import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; /** @@ -40,10 +40,6 @@ export default function ResizableCoverPopover( { ...props } ) { const [ isResizing, setIsResizing ] = useState( false ); - const dimensions = useMemo( - () => ( { height, minHeight, width } ), - [ minHeight, height, width ] - ); const resizableBoxProps = { className: classnames( className, { 'is-resizing': isResizing } ), @@ -75,7 +71,6 @@ export default function ResizableCoverPopover( { return (