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

Fix Link UI popover anchor in rich text #58282

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<a>` element in the rich text.
if ( ! isActive ) {
popoverAnchor.getBoundingClientRect = () => cachedRect;
}
Comment on lines +223 to +225
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If isActive is true then there is an existing link and thus we should allow popoverAnchor (derived from useAnchor) to use the default implementation which will seek the <a> in the DOM and anchor to that.

Otherwise if we're creating a link then there is no <a> until the link is submitted. Therefore we need to cache the anchor value of the original text selection that the user selected to be linked.

Previously we just cached the selection value always, which wasn't correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes it for existing links, but for new links, the popover will have the same scroll issue since its position is cached.


async function handleCreate( pageTitle ) {
const page = await createPageEntity( {
Expand Down
Loading