refactor SiteLink to not repeat

This commit is contained in:
Sinclair Chen 2022-05-03 16:28:28 -04:00
parent abf23a1462
commit cf62711c24

View File

@ -1,5 +1,6 @@
import clsx from 'clsx' import clsx from 'clsx'
import Link from 'next/link' import Link from 'next/link'
import { Children } from 'react'
export const SiteLink = (props: { export const SiteLink = (props: {
href: string href: string
@ -9,24 +10,8 @@ export const SiteLink = (props: {
}) => { }) => {
const { href, children, onClick, className } = props const { href, children, onClick, className } = props
return href.startsWith('http') ? ( return (
<a <MaybeLink href={href}>
href={href}
className={clsx(
'z-10 break-words hover:underline hover:decoration-indigo-400 hover:decoration-2',
className
)}
style={{ /* For iOS safari */ wordBreak: 'break-word' }}
target="_blank"
onClick={(e) => {
e.stopPropagation()
if (onClick) onClick()
}}
>
{children}
</a>
) : (
<Link href={href}>
<a <a
className={clsx( className={clsx(
'z-10 break-words hover:underline hover:decoration-indigo-400 hover:decoration-2', 'z-10 break-words hover:underline hover:decoration-indigo-400 hover:decoration-2',
@ -40,6 +25,15 @@ export const SiteLink = (props: {
> >
{children} {children}
</a> </a>
</Link> </MaybeLink>
)
}
function MaybeLink(props: { href: string; children: any }) {
const { href, children } = props
return href.startsWith('http') ? (
children
) : (
<Link href={href}>{children}</Link>
) )
} }