tailwind-nextjs-blog/components/Link.tsx

25 lines
630 B
TypeScript
Raw Permalink Normal View History

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