Skip to content

Commit

Permalink
BlockPopover: Remove __unstableCoverTarget and __unstableRefreshSize …
Browse files Browse the repository at this point in the history
…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 <noisysocks@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
  • Loading branch information
3 people authored Feb 22, 2024
1 parent 27dbbe6 commit b8cc4b8
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 61 deletions.
63 changes: 63 additions & 0 deletions packages/block-editor/src/components/block-popover/cover.js
Original file line number Diff line number Diff line change
@@ -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 (
<BlockPopover
ref={ ref }
clientId={ clientId }
bottomClientId={ bottomClientId }
shift={ shift }
{ ...props }
>
{ selectedElement && clientId === bottomClientId ? (
<CoverContainer selectedElement={ selectedElement }>
{ children }
</CoverContainer>
) : (
children
) }
</BlockPopover>
);
}

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 <div style={ style }>{ children }</div>;
}

export default forwardRef( BlockPopoverCover );
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -38,9 +38,8 @@ function BlockDropZonePopover( {
const reducedMotion = useReducedMotion();

return (
<BlockPopover
<BlockPopoverCover
clientId={ clientId }
__unstableCoverTarget
__unstablePopoverSlot={ __unstablePopoverSlot }
__unstableContentRef={ __unstableContentRef }
className="block-editor-block-popover__drop-zone"
Expand All @@ -56,7 +55,7 @@ function BlockDropZonePopover( {
}
className="block-editor-block-popover__drop-zone-foreground"
/>
</BlockPopover>
</BlockPopoverCover>
);
}

Expand Down
29 changes: 1 addition & 28 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ function BlockPopover(
clientId,
bottomClientId,
children,
__unstableRefreshSize,
__unstableCoverTarget = false,
__unstablePopoverSlot,
__unstableContentRef,
shift = true,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -176,8 +150,7 @@ function BlockPopover(
) }
variant="unstyled"
>
{ __unstableCoverTarget && <div style={ style }>{ children }</div> }
{ ! __unstableCoverTarget && children }
{ children }
</Popover>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,9 +28,8 @@ export default function EmptyBlockInserter( {
} );

return (
<BlockPopover
<BlockPopoverCover
clientId={ capturingClientId || clientId }
__unstableCoverTarget
bottomClientId={ lastClientId }
className={ classnames(
'block-editor-block-list__block-side-inserter-popover',
Expand All @@ -39,8 +38,6 @@ export default function EmptyBlockInserter( {
}
) }
__unstableContentRef={ __unstableContentRef }
resize={ false }
shift={ false }
{ ...popoverProps }
>
<div className="block-editor-block-list__empty-block-inserter">
Expand All @@ -51,6 +48,6 @@ export default function EmptyBlockInserter( {
__experimentalIsQuick
/>
</div>
</BlockPopover>
</BlockPopoverCover>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import { ResizableBox } from '@wordpress/components';
/**
* Internal dependencies
*/
import BlockPopover from '../block-popover';
import BlockPopoverCover from '../block-popover/cover';

export default function ResizableBoxPopover( {
clientId,
resizableBoxProps,
...props
} ) {
return (
<BlockPopover
<BlockPopoverCover
clientId={ clientId }
__unstableCoverTarget
__unstablePopoverSlot="__unstable-block-tools-after"
shift={ false }
{ ...props }
>
<ResizableBox { ...resizableBoxProps } />
</BlockPopover>
</BlockPopoverCover>
);
}
9 changes: 3 additions & 6 deletions packages/block-editor/src/hooks/margin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -78,14 +78,11 @@ export function MarginVisualizer( { clientId, attributes, forceShow } ) {
}

return (
<BlockPopover
<BlockPopoverCover
clientId={ clientId }
__unstableCoverTarget
__unstableRefreshSize={ margin }
__unstablePopoverSlot="block-toolbar"
shift={ false }
>
<div className="block-editor__padding-visualizer" style={ style } />
</BlockPopover>
</BlockPopoverCover>
);
}
9 changes: 3 additions & 6 deletions packages/block-editor/src/hooks/padding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -69,14 +69,11 @@ export function PaddingVisualizer( { clientId, value, forceShow } ) {
}

return (
<BlockPopover
<BlockPopoverCover
clientId={ clientId }
__unstableCoverTarget
__unstableRefreshSize={ padding }
__unstablePopoverSlot="block-toolbar"
shift={ false }
>
<div className="block-editor__padding-visualizer" style={ style } />
</BlockPopover>
</BlockPopoverCover>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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 } ),
Expand Down Expand Up @@ -75,7 +71,6 @@ export default function ResizableCoverPopover( {
return (
<ResizableBoxPopover
className="block-library-cover__resizable-box-popover"
__unstableRefreshSize={ dimensions }
resizableBoxProps={ resizableBoxProps }
{ ...props }
/>
Expand Down

1 comment on commit b8cc4b8

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in b8cc4b8.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7999467658
📝 Reported issues:

Please sign in to comment.