1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-09-18 13:45:34 -04:00

test new next/link behavior

This commit is contained in:
2022-05-17 18:31:58 -04:00
parent a2f4112e06
commit d1f0030d3c
8 changed files with 37 additions and 60 deletions

View File

@@ -4,7 +4,7 @@ import { styled } from "../../lib/styles/stitches.config";
import type { ComponentProps } from "react";
import type { LinkProps as NextLinkProps } from "next/link";
const FancyLink = styled("a", {
const StyledLink = styled(NextLink, {
color: "$link",
textDecoration: "none",
@@ -34,29 +34,20 @@ const FancyLink = styled("a", {
},
});
export type LinkProps = Omit<ComponentProps<typeof FancyLink>, "href"> &
export type LinkProps = Omit<ComponentProps<typeof StyledLink>, "href"> &
NextLinkProps & {
underline?: boolean;
forceNewWindow?: boolean;
};
const Link = ({
href,
prefetch = false,
passHref = true,
target,
rel,
underline = true,
forceNewWindow,
...rest
}: LinkProps) => {
const Link = ({ href, prefetch = false, target, rel, underline = true, forceNewWindow, ...rest }: LinkProps) => {
// this component auto-detects whether or not we should use a normal HTML anchor externally or next/link internally,
// can be overridden with `forceNewWindow={true}`.
const isExternal = isAbsoluteUrl(href.toString());
if (forceNewWindow || isExternal) {
return (
<FancyLink
<StyledLink
href={href.toString()}
target={target ?? "_blank"}
rel={[rel, "noopener", isExternal ? "noreferrer" : ""].join(" ").trim()}
@@ -64,13 +55,9 @@ const Link = ({
{...rest}
/>
);
} else {
return (
<NextLink href={href} prefetch={prefetch} passHref={passHref}>
<FancyLink target={target} rel={rel} underline={underline} {...rest} />
</NextLink>
);
}
return <StyledLink {...{ href, prefetch, target, rel, underline, ...rest }} />;
};
export default Link;