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

External link component: Add a check for on page anchor links #42259

Merged
merged 4 commits into from
Aug 2, 2022

Conversation

carolinan
Copy link
Contributor

@carolinan carolinan commented Jul 8, 2022

What?

This is an attempt to solve an issue where clicking an anchor link inside the link control modal opens the editor in a new tab.
Please see #10117.

This PR prevents following the anchor link by checking if the href starts with # and then using event.preventDefault on click. There is also a valid suggestion in the issue, to open the link but with a front view, not editor view. I was unsure of how to make that change, so I went with this smaller change.

While I believe that this PR works, it feels like treating the symptoms rather than the cause.
Does anyone have insight in why the external link component is not only used for, well, external links?

Testing Instructions

  1. Open the editor and create a new post or page.
  2. Next add a block with a link control like a paragraph block or button block. Add an anchor link (#test).
  3. Click the link in the link control modal. Nothing should happen.
  4. Please also test an internal content link (I mean a link to a post or page on the same install) and an external link to see that they still open correctly.

Screenshots or screencast

@carolinan carolinan requested a review from getdave July 8, 2022 08:01
@carolinan carolinan marked this pull request as ready for review July 8, 2022 09:42
@carolinan carolinan requested a review from ajitbohra as a code owner July 8, 2022 09:42
Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

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

Thanks for this PR. It makes sense to catch this situation and will be a good improvement.

Left a few ideas.

packages/components/src/external-link/index.tsx Outdated Show resolved Hide resolved
/* Anchor links are percieved as external links.
This constant helps check for on page anchor links,
to prevent them from being opened in the editor. */
const shouldPreventDefault = href.startsWith( '#' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we guarantee href is always a String? For example is it possible for it to be undefined? If it is then we should guard here and also coerce to Boolean.

Suggested change
const shouldPreventDefault = href.startsWith( '#' );
const shouldPreventDefault = !! href?.startsWith( '#' );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure how to backtrace it to see if it can be undefined for some reason, so I believe it is easier to add the check :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Though the type for href is set to string in types.ts. I think @walbo would be able to answer how to best handle the check.

@carolinan
Copy link
Contributor Author

Do I also need to add an entry to packages/components/CHANGELOG.md?

@getdave
Copy link
Contributor

getdave commented Jul 20, 2022

Do I also need to add an entry to packages/components/CHANGELOG.md?

Based on this I'd say you have to maintain that file manually. So add this PR under the Unreleased section.

@carolinan carolinan added [Type] Bug An existing feature does not function as intended [Feature] Link Editing Link components (LinkControl, URLInput) and integrations (RichText link formatting) [Feature] UI Components Impacts or related to the UI component system labels Jul 28, 2022
Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

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

I think this is ready to 🚢

packages/components/CHANGELOG.md Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Link Editing Link components (LinkControl, URLInput) and integrations (RichText link formatting) [Feature] UI Components Impacts or related to the UI component system [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants