1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2026-06-05 20:15:31 -04:00
Files
jarv.is/components/link.tsx
T
2026-01-27 22:53:59 -05:00

24 lines
899 B
TypeScript

import NextLink from "next/link";
const Link = ({ href, rel, target, ...rest }: React.ComponentProps<typeof NextLink>) => {
// This component auto-detects whether or not this link should open in the same window (the default for internal
// links) or a new tab (the default for external links). Defaults can be overridden with `target="_blank"`.
const isExternal = typeof href === "string" && !["/", "#"].includes(href[0]);
const linkProps = {
href,
target: target || (isExternal ? "_blank" : undefined),
rel: `${rel ? `${rel} ` : ""}${target === "_blank" || isExternal ? "noopener noreferrer" : ""}`.trim() || undefined,
...rest,
};
// don't waste time with next's component if it's just an external link
if (isExternal) {
return <a {...(linkProps as unknown as React.ComponentProps<"a">)} />;
}
return <NextLink {...linkProps} />;
};
export default Link;