manifold/web/components/site-link.tsx

42 lines
989 B
TypeScript
Raw Permalink Normal View History

2021-12-30 20:03:32 +00:00
import clsx from 'clsx'
import { ReactNode } from 'react'
2021-12-30 20:03:32 +00:00
import Link from 'next/link'
export const SiteLink = (props: {
href: string
children?: ReactNode
2022-04-19 05:16:08 +00:00
onClick?: () => void
2021-12-30 20:03:32 +00:00
className?: string
}) => {
2022-04-19 05:16:08 +00:00
const { href, children, onClick, className } = props
2021-12-30 20:03:32 +00:00
2022-05-17 17:36:36 +00:00
return (
<MaybeLink href={href}>
2021-12-30 20:03:32 +00:00
<a
className={clsx(
'z-10 break-words hover:underline hover:decoration-indigo-400 hover:decoration-2',
2021-12-30 20:03:32 +00:00
className
)}
2022-05-17 21:11:15 +00:00
href={href}
target={href.startsWith('http') ? '_blank' : undefined}
2022-02-15 02:27:43 +00:00
style={{ /* For iOS safari */ wordBreak: 'break-word' }}
2022-04-19 05:16:08 +00:00
onClick={(e) => {
e.stopPropagation()
if (onClick) onClick()
}}
2021-12-30 20:03:32 +00:00
>
{children}
</a>
2022-05-17 17:36:36 +00:00
</MaybeLink>
)
}
function MaybeLink(props: { href: string; children: ReactNode }) {
2022-05-17 17:36:36 +00:00
const { href, children } = props
return href.startsWith('http') ? (
<>{children}</>
) : (
<Link href={href}>{children}</Link>
2021-12-30 20:03:32 +00:00
)
}