jonbio/components/Link.tsx

25 lines
646 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-07 11:17:22 +08:00
import { AnchorHTMLAttributes, DetailedHTMLProps } from 'react'
2021-01-09 17:50:45 +08:00
2023-07-08 15:23:25 +08:00
const CustomLink = async ({
2023-07-07 11:17:22 +08:00
href,
...rest
}: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, 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
// @ts-ignore
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