tailwind-nextjs-blog/components/Link.tsx

31 lines
677 B
TypeScript
Raw Normal View History

2022-07-17 21:40:41 +08:00
/* eslint-disable jsx-a11y/anchor-has-content */
2022-10-17 23:37:01 +08:00
import Link from 'next/link';
import { AnchorHTMLAttributes, DetailedHTMLProps } from 'react';
2022-07-17 21:40:41 +08:00
const CustomLink = ({
href,
...rest
2022-10-17 23:37:01 +08:00
}: DetailedHTMLProps<
AnchorHTMLAttributes<HTMLAnchorElement>,
HTMLAnchorElement
>) => {
const isInternalLink = href && href.startsWith('/');
const isAnchorLink = href && href.startsWith('#');
2022-07-17 21:40:41 +08:00
if (isInternalLink) {
return (
<Link href={href}>
<a {...rest} />
</Link>
2022-10-17 23:37:01 +08:00
);
2022-07-17 21:40:41 +08:00
}
if (isAnchorLink) {
2022-10-17 23:37:01 +08:00
return <a href={href} {...rest} />;
2022-07-17 21:40:41 +08:00
}
2022-10-17 23:37:01 +08:00
return <a target="_blank" rel="noopener noreferrer" href={href} {...rest} />;
};
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
export default CustomLink;