jonbio/components/Link.tsx

22 lines
616 B
TypeScript
Raw Normal View History

2021-01-12 23:35:36 +08:00
/* eslint-disable jsx-a11y/anchor-has-content */
2021-01-09 17:50:45 +08:00
import Link from 'next/link'
2023-07-12 00:45:49 +08:00
import type { LinkProps } from 'next/link'
import { AnchorHTMLAttributes } from 'react'
2021-01-09 17:50:45 +08:00
2023-07-12 00:45:49 +08:00
const CustomLink = ({ href, ...rest }: LinkProps & AnchorHTMLAttributes<HTMLAnchorElement>) => {
2021-01-09 17:50:45 +08:00
const isInternalLink = href && href.startsWith('/')
const isAnchorLink = href && href.startsWith('#')
if (isInternalLink) {
2023-07-07 11:17:22 +08:00
return <Link href={href} {...rest} />
2021-01-09 17:50:45 +08:00
}
if (isAnchorLink) {
2021-01-10 15:19:00 +08:00
return <a href={href} {...rest} />
2021-01-09 17:50:45 +08:00
}
return <a target="_blank" rel="noopener noreferrer" href={href} {...rest} />
}
export default CustomLink