diff --git a/packages/components/src/tooltip/index.js b/packages/components/src/tooltip/index.js index bef68a9363a211..c2d438f4ad41ca 100644 --- a/packages/components/src/tooltip/index.js +++ b/packages/components/src/tooltip/index.js @@ -9,7 +9,6 @@ import { concatChildren, useEffect, useState, - useRef, } from '@wordpress/element'; import { useDebounce, useMergeRefs } from '@wordpress/compose'; @@ -60,7 +59,7 @@ const getRegularElement = ( { }; const addPopoverToGrandchildren = ( { - anchorRef, + anchor, grandchildren, isOver, offset, @@ -78,7 +77,7 @@ const addPopoverToGrandchildren = ( { aria-hidden="true" animate={ false } offset={ offset } - anchorRef={ anchorRef } + anchor={ anchor } __unstableShift > { text } @@ -124,13 +123,18 @@ function Tooltip( props ) { const [ isMouseDown, setIsMouseDown ] = useState( false ); const [ isOver, setIsOver ] = useState( false ); const delayedSetIsOver = useDebounce( setIsOver, delay ); + // Using internal state (instead of a ref) for the popover anchor to make sure + // that the component re-renders when the anchor updates. + const [ popoverAnchor, setPopoverAnchor ] = useState(); // Create a reference to the Tooltip's child, to be passed to the Popover // so that the Tooltip can be correctly positioned. Also, merge with the // existing ref for the first child, so that its ref is preserved. - const childRef = useRef( null ); const existingChildRef = Children.toArray( children )[ 0 ]?.ref; - const mergedChildRefs = useMergeRefs( [ childRef, existingChildRef ] ); + const mergedChildRefs = useMergeRefs( [ + setPopoverAnchor, + existingChildRef, + ] ); const createMouseDown = ( event ) => { // In firefox, the mouse down event is also fired when the select @@ -253,7 +257,8 @@ function Tooltip( props ) { : getRegularElement; const popoverData = { - anchorRef: childRef, + // `anchor` should never be `null` + anchor: popoverAnchor ?? undefined, isOver, offset: 4, position,