Skip to content

Commit

Permalink
fix: allow link to open in a new tab if internal (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyjablonski authored Dec 2, 2024
1 parent cd2c6da commit 3414236
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/actions/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ const Link = (props: LinkProps) => {
const classNames = ["seeds-link"]

const tailIcon = shouldShowExternalLinkIcon(props) ? (
<Icon><ArrowTopRightOnSquareIcon /></Icon>
<Icon>
<ArrowTopRightOnSquareIcon />
</Icon>
) : (
props.tailIcon
)
Expand All @@ -46,28 +48,29 @@ const Link = (props: LinkProps) => {
if (props.leadIcon) classNames.push("has-lead-icon")
if (tailIcon) classNames.push("has-tail-icon")

const additionalProps = {
let additionalProps: { [key: string]: unknown } = {
id: props.id,
className: classNames.join(" "),
"aria-label": props.ariaLabel,
"aria-hidden": props.ariaHidden,
}

if (props.newWindowTarget) {
additionalProps = { ...additionalProps, target: "_blank", rel: "noreferrer" }
}

const { LinkComponent } = useContext(NavigationContext)

if (props.href && isExternalLink(props.href)) {
return (
<a
href={props.href}
target={props.newWindowTarget ? "_blank" : undefined}
{...additionalProps}
>
<a href={props.href} {...additionalProps}>
{props.leadIcon}
{props.children}
{tailIcon}
{props.newWindowTarget && <ExternalLinkScreenReaderText />}
</a>
)
} else {
const { LinkComponent } = useContext(NavigationContext)
return (
<LinkComponent href={props.href} {...additionalProps}>
{props.leadIcon}
Expand Down
3 changes: 3 additions & 0 deletions src/actions/__stories__/Link.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export const ExternalLink = () => (
<Link href="https://www.exygy.com" newWindowTarget>
External Link (New Window)
</Link>
<Link href="/test" newWindowTarget>
Internal Link (New Window)
</Link>
<Link href="https://www.exygy.com" hideExternalLinkIcon>
External Link (Same Window)
</Link>
Expand Down

0 comments on commit 3414236

Please sign in to comment.