Skip to content

Link UI incorrectly anchored to "last" link within rich text contenteditable #58280

Closed

Description

@getdave

If you have multiple links within a block of rich text then the Link UI will be "anchor" to the final first link you clicked in the block.

So if you click the first link in the block and then click on other links, the popover will continue to anchor to the first link. Similarly if you clicked the last link first, it would continue to anchor to that link.

It should be anchored to the link that triggers it. I suspect this is due to the way in which we recently changed the usage of useAnchor to cache the first position.

// As you change the link by interacting with the Link UI
// the return value of document.getSelection jumps to the field you're editing,
// not the highlighted text. Given that useAnchor uses document.getSelection,
// it will return null, since it can't find the <mark> element within the Link UI.
// 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;

Screen.Capture.on.2024-01-25.at.20-41-55.mp4

Activity

  1. added
    [Type] Bug
    [Feature] Link Editing
    on Jan 25, 2024
  2. getdave commented on Jan 25, 2024

    @getdave
    ContributorAuthor
  3. getdave commented on Jan 25, 2024

    @getdave
    ContributorAuthor

    I confirmed that removing theses lines...

    https://github.com/WordPress/gutenberg/blob/trunk/packages/format-library/src/link/inline.js#L208-L215

    ...will "fix" the problem. But now if you create a new link and submit you'll see the popover is incorrectly positioned.

  4. added
    [Status] In Progress
    on Jan 25, 2024
  5. jeryj commented on Jan 25, 2024

    @jeryj
    Contributor

    The same problem lines also cause an issue where the popover is "fixed" on the page, so it doesn't stay attached to the link, but scrolls away from it.

    But now if you create a new link and submit you'll see the popover is incorrectly positioned.

    I think the incorrectly positioned new link issue has to do with the popover anchor changing from the paragraph to the link, but the popover anchor doesn't update to be anchored to the new element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[Feature] Link Editing[Status] In Progress[Type] Bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions