Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use dynamic toolbar height in hook
  • Loading branch information
talldan committed Aug 22, 2022
commit fda157e994227e2c8f7652e8c1c2bd7611a8d32a
35 changes: 22 additions & 13 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,35 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useMergeRefs } from '@wordpress/compose';
import { Popover } from '@wordpress/components';
import { useMemo } from '@wordpress/element';
import { forwardRef, useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs';
import usePopoverScroll from './use-popover-scroll';

export default function BlockPopover( {
clientId,
bottomClientId,
children,
__unstableRefreshSize,
__unstableCoverTarget = false,
__unstablePopoverSlot,
__unstableContentRef,
...props
} ) {
function BlockPopover(
{
clientId,
bottomClientId,
children,
__unstableRefreshSize,
__unstableCoverTarget = false,
__unstablePopoverSlot,
__unstableContentRef,
...props
},
ref
) {
const selectedElement = useBlockElement( clientId );
const lastSelectedElement = useBlockElement( bottomClientId ?? clientId );
const popoverScrollRef = usePopoverScroll( __unstableContentRef );
const mergedRefs = useMergeRefs( [
ref,
usePopoverScroll( __unstableContentRef ),
] );
const style = useMemo( () => {
if ( ! selectedElement || lastSelectedElement !== selectedElement ) {
return {};
Expand All @@ -51,7 +58,7 @@ export default function BlockPopover( {

return (
<Popover
ref={ popoverScrollRef }
ref={ mergedRefs }
animate={ false }
position="top right left"
focusOnMount={ false }
Expand All @@ -74,3 +81,5 @@ export default function BlockPopover( {
</Popover>
);
}

export default forwardRef( BlockPopover );
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import { useRefEffect } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';

Expand All @@ -10,22 +11,21 @@ import { useEffect, useState } from '@wordpress/element';
import { store as blockEditorStore } from '../../store';
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs';

const TOOLBAR_HEIGHT = 72;
const DEFAULT_PROPS = { __unstableForcePosition: true, __unstableShift: true };
const RESTRICTED_HEIGHT_PROPS = {
__unstableForcePosition: false,
__unstableShift: false,
};

function getProps( contentElement, selectedBlockElement ) {
function getProps( contentElement, selectedBlockElement, toolbarHeight ) {
if ( ! contentElement || ! selectedBlockElement ) {
return DEFAULT_PROPS;
}

const blockRect = selectedBlockElement.getBoundingClientRect();
const contentRect = contentElement.getBoundingClientRect();

if ( blockRect.top - contentRect.top > TOOLBAR_HEIGHT ) {
if ( blockRect.top - contentRect.top > toolbarHeight ) {
return DEFAULT_PROPS;
}

Expand All @@ -48,18 +48,18 @@ export default function useBlockToolbarPopoverProps( {
clientId,
} ) {
const selectedBlockElement = useBlockElement( clientId );
const [ toolbarHeight, setToolbarHeight ] = useState( 0 );
const [ props, setProps ] = useState(
getProps( contentElement, selectedBlockElement )
getProps( contentElement, selectedBlockElement, toolbarHeight )
);
const blockIndex = useSelect(
( select ) => select( blockEditorStore ).getBlockIndex( clientId ),
[ clientId ]
);

// Update the toolbar position if the block moves.
useEffect( () => {
setProps( getProps( contentElement, selectedBlockElement ) );
}, [ blockIndex ] );
const popoverRef = useRefEffect( ( popoverNode ) => {
setToolbarHeight( popoverNode.offsetHeight );
}, [] );

// Update the toolbar position if the window resizes, or the
// selected element changes.
Expand All @@ -69,7 +69,9 @@ export default function useBlockToolbarPopoverProps( {
}

const updateProps = () =>
setProps( getProps( contentElement, selectedBlockElement ) );
setProps(
getProps( contentElement, selectedBlockElement, toolbarHeight )
);

updateProps();
const view = contentElement?.ownerDocument?.defaultView;
Expand All @@ -78,7 +80,10 @@ export default function useBlockToolbarPopoverProps( {
return () => {
view?.removeEventHandler?.( 'resize', updateProps );
};
}, [ contentElement, selectedBlockElement ] );
}, [ contentElement, selectedBlockElement, blockIndex, toolbarHeight ] );

return props;
return {
...props,
ref: popoverRef,
};
}