Skip to content

Commit f895704

Browse files
committed
Refactor login in link
1 parent 7672bed commit f895704

File tree

1 file changed

+10
-7
lines changed
  • packages/gitbook/src/components/primitives

1 file changed

+10
-7
lines changed

packages/gitbook/src/components/primitives/Link.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ export const Link = React.forwardRef(function Link(
5252
const { externalLinksTarget } = React.useContext(LinkSettingsContext);
5353
const trackEvent = useTrackEvent();
5454
const forwardedClassNames = useClassnames(classNames || []);
55+
// Use specified target or fallback to the default target based on the external links setting.
56+
const target =
57+
props.target ??
58+
(externalLinksTarget === SiteExternalLinksTarget.Blank ? '_blank' : undefined);
59+
// Automatically set rel if target is _blank, or use the specified rel.
60+
const rel = props.rel ?? (target === '_blank' ? 'noopener noreferrer' : undefined);
5561

5662
// Use a real anchor tag for external links,s and a Next.js Link for internal links.
5763
// If we use a NextLink for external links, Nextjs won't rerender the top-level layouts.
@@ -68,12 +74,10 @@ export const Link = React.forwardRef(function Link(
6874
isExternalLink(href, window.location.origin) &&
6975
// When the page is embedded in an iframe, for security reasons other urls cannot be opened.
7076
// In this case, we open the link in a new tab.
71-
(window.self !== window.top ||
72-
// If the site is configured to open links in a new tab
73-
externalLinksTarget === SiteExternalLinksTarget.Blank)
77+
(window.self !== window.top || target === '_blank')
7478
) {
7579
event.preventDefault();
76-
window.open(href, '_blank');
80+
window.open(href, '_blank', rel);
7781
}
7882

7983
domProps.onClick?.(event);
@@ -89,9 +93,8 @@ export const Link = React.forwardRef(function Link(
8993
{...domProps}
9094
href={href}
9195
onClick={onClick}
92-
{...(externalLinksTarget === SiteExternalLinksTarget.Blank && !domProps.target
93-
? { target: '_blank', rel: 'noopener noreferrer' }
94-
: {})}
96+
target={target}
97+
rel={rel}
9598
>
9699
{children}
97100
</a>

0 commit comments

Comments
 (0)