Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Account for positioned elements when positioning Block Toolbar #40625

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 96 additions & 2 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import classnames from 'classnames';
*/
import { Popover } from '@wordpress/components';
import { getScrollContainer } from '@wordpress/dom';
import { useMemo } from '@wordpress/element';
import { useMemo, useLayoutEffect, useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -19,14 +19,108 @@ import usePopoverScroll from './use-popover-scroll';
export default function BlockPopover( {
clientId,
bottomClientId,
rootClientId,
children,
__unstableRefreshSize,
__unstableCoverTarget = false,
__unstablePopoverSlot,
__unstableContentRef,
...props
} ) {
const [ layoutBound, setLayoutBound ] = useState( null );
const selectedElement = useBlockElement( clientId );
const rootElement = useBlockElement( rootClientId );

useLayoutEffect( () => {
if ( ! rootElement || ! selectedElement ) {
return;
}

const parentBoundary = rootElement?.getBoundingClientRect()?.bottom;

const elChildren = selectedElement?.children;

function isVisible( el ) {
const hasHeight = el.offsetHeight > 0;

const compStyle = window.getComputedStyle( el );

return (
hasHeight ||
compStyle.visibility !== 'hidden' ||
compStyle.opacity !== 0 ||
! el.classList.contains( 'components-visually-hidden' )
);
}

function getOutsideBounds( nodes, currCandidate ) {
if ( ! nodes?.length ) {
return null;
}

return Array.from( nodes )?.reduce(
( acc, curr ) => {
if ( ! isVisible( curr ) ) {
return acc;
}

const bottom = curr.getBoundingClientRect().bottom;

// Update
if (
bottom > parentBoundary && // is beyond parent bottom edge
bottom > acc[ 0 ] && // is beyond bototm edge of any of its siblings
bottom > currCandidate[ 0 ] // is beyond the bottom edge of the current largest candidate element
) {
acc = [ bottom, curr ];
}

// Recurse through child elements.
if ( curr?.children?.length ) {
const childResults = getOutsideBounds(
curr.children,
acc
);

if ( childResults ) {
acc.concat( childResults );
}
}

return acc;
},
[ 0 ]
);
}

function checkLayoutBounds() {
if ( ! elChildren?.length ) {
return;
}

const candidate = getOutsideBounds( elChildren, [ 0 ] );

if ( candidate ) {
setLayoutBound( candidate[ 1 ] );
}
}

checkLayoutBounds();

// Watch for DOM changes and check bounds to determine toolbar anchoring.
const observer = new window.MutationObserver( checkLayoutBounds );

observer.observe( selectedElement, {
attributes: true,
childList: true,
subtree: true,
} );

return () => {
observer.disconnect();
};
}, [ rootElement, selectedElement ] );

const lastSelectedElement = useBlockElement( bottomClientId ?? clientId );
const popoverScrollRef = usePopoverScroll( __unstableContentRef );
const style = useMemo( () => {
Expand All @@ -47,7 +141,7 @@ export default function BlockPopover( {

const anchorRef = {
top: selectedElement,
bottom: lastSelectedElement,
bottom: layoutBound ?? lastSelectedElement,
};

const { ownerDocument } = selectedElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function SelectedBlockPopover( {

return (
<BlockPopover
rootClientId={ rootClientId }
clientId={ capturingClientId || clientId }
bottomClientId={ lastClientId }
className={ classnames( 'block-editor-block-list__block-popover', {
Expand Down