jonbio/components/Link.tsx

24 lines
698 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) {
2024-07-07 11:29:07 +09:00
return <Link className="break-words" href={href} {...rest} />
2021-01-09 17:50:45 +08:00
}
if (isAnchorLink) {
2024-07-07 11:29:07 +09:00
return <a className="break-words" href={href} {...rest} />
2021-01-09 17:50:45 +08:00
}
2024-07-10 21:34:54 +08:00
return (
<a className="break-words" target="_blank" rel="noopener noreferrer" href={href} {...rest} />
)
2021-01-09 17:50:45 +08:00
}
export default CustomLink