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
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bug Fix

- `ExternalLink`: Check if the link is an internal anchor link and prevent anchor links from being opened. ([#42259](https://github.com/WordPress/gutenberg/pull/42259)).
- `BorderControl`: Ensure box-sizing is reset for the control ([#42754](https://github.com/WordPress/gutenberg/pull/42754)).
- `InputControl`: Fix acceptance of falsy values in controlled updates ([#42484](https://github.com/WordPress/gutenberg/pull/42484/)).

Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/external-link/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,22 @@ function UnforwardedExternalLink(
),
].join( ' ' );
const classes = classnames( 'components-external-link', className );
/* 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 isInternalAnchor = !! href?.startsWith( '#' );

return (
/* eslint-disable react/jsx-no-target-blank */
<a
{ ...additionalProps }
className={ classes }
href={ href }
onClick={
isInternalAnchor
? ( event ) => event.preventDefault()
: undefined
}
target="_blank"
rel={ optimizedRel }
ref={ ref }
Expand Down