File size: 968 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { ReactNode } from 'react'
import { Link } from '@remix-run/react'

import { isValidHttpUrl } from '~/helpers/strings'
import clsx from 'clsx'
import { anchor } from './Anchor.css'

export interface AnchorProps {
  href: string
  children: ReactNode
  className?: string
  onClick?: React.MouseEventHandler<HTMLAnchorElement>
}

export const Anchor = ({ href, children, className, onClick }: AnchorProps) => {
  const isExternal = isValidHttpUrl(href)

  const handleClick: React.MouseEventHandler<HTMLAnchorElement> = e => {
    if (onClick) {
      onClick(e)
    }
  }

  if (isExternal) {
    return (
      <a
        className={clsx(anchor, className)}
        href={href}
        rel="noopener noreferrer"
        target="_blank"
        onClick={handleClick}
      >
        {children}
      </a>
    )
  } else {
    return (
      <Link className={clsx(anchor, className)} onClick={handleClick} to={href}>
        {children}
      </Link>
    )
  }
}