From a34ff789d22ea33ed97b40d98a1c3d140321c33d Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Mon, 5 Sep 2022 13:30:01 +0200 Subject: [PATCH] Edit site Actions: use new anchor prop for Popover (#43810) --- .../header/document-actions/index.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/edit-site/src/components/header/document-actions/index.js b/packages/edit-site/src/components/header/document-actions/index.js index 53ae4c395c5a81..1258b759ff6abc 100644 --- a/packages/edit-site/src/components/header/document-actions/index.js +++ b/packages/edit-site/src/components/header/document-actions/index.js @@ -19,7 +19,7 @@ import { __experimentalText as Text, } from '@wordpress/components'; import { chevronDown } from '@wordpress/icons'; -import { useRef } from '@wordpress/element'; +import { useCallback, useState } from '@wordpress/element'; import { store as blockEditorStore } from '@wordpress/block-editor'; function getBlockDisplayText( block ) { @@ -73,10 +73,15 @@ export default function DocumentActions( { } ) { const { label } = useSecondaryText(); - // The title ref is passed to the popover as the anchorRef so that the - // dropdown is centered over the whole title area rather than just one - // part of it. - const titleRef = useRef(); + // Use internal state instead of a ref to make sure that the component + // re-renders when then anchor's ref updates. + const [ popoverAnchor, setPopoverAnchor ] = useState(); + const titleWrapperCallbackRef = useCallback( ( node ) => { + // Use the title wrapper as the popover anchor so that the dropdown is + // centered over the whole title area rather than just one part of it. + // Fall back to `undefined` in case the ref is `null`. + setPopoverAnchor( node ?? undefined ); + }, [] ); // Return a simple loading indicator until we have information to show. if ( ! isLoaded ) { @@ -103,7 +108,7 @@ export default function DocumentActions( { } ) } >
(