diff --git a/packages/format-library/src/link/inline.js b/packages/format-library/src/link/inline.js index 2d5f52406b678..c12ccc0b08f7f 100644 --- a/packages/format-library/src/link/inline.js +++ b/packages/format-library/src/link/inline.js @@ -212,7 +212,17 @@ function InlineLinkUI( { // This caches the last truthy value of the selection anchor reference. // This ensures the Popover is positioned correctly on initial submission of the link. const cachedRect = useCachedTruthy( popoverAnchor.getBoundingClientRect() ); - popoverAnchor.getBoundingClientRect = () => cachedRect; + + // If the link is not active (i.e. it is a new link) then we need to + // override the getBoundingClientRect method on the anchor element + // to return the cached value of the selection represented by the text + // that the user selected to be linked. + // If the link is active (i.e. it is an existing link) then we allow + // the default behaviour of the popover anchor to be used. This will get + // the anchor based on the `` element in the rich text. + if ( ! isActive ) { + popoverAnchor.getBoundingClientRect = () => cachedRect; + } async function handleCreate( pageTitle ) { const page = await createPageEntity( {