tailwind-nextjs-blog/components/Link.tsx

22 lines
616 B
TypeScript
Raw Normal View History

2022-07-17 21:40:41 +08:00
/* eslint-disable jsx-a11y/anchor-has-content */
import Link from 'next/link'
import type { LinkProps } from 'next/link'
import { AnchorHTMLAttributes } from 'react'
2022-07-17 21:40:41 +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) {
return <Link href={href} {...rest} />
2022-07-17 21:40:41 +08:00
}
if (isAnchorLink) {
return <a href={href} {...rest} />
2022-07-17 21:40:41 +08:00
}
return <a target="_blank" rel="noopener noreferrer" href={href} {...rest} />
}
2022-07-17 21:40:41 +08:00
export default CustomLink