File size: 1,774 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {
  ForwardRefExoticComponent,
  MouseEventHandler,
  RefAttributes,
} from 'react'
import { Link, useLocation } from '@remix-run/react'
import * as Toolbar from '@radix-ui/react-toolbar'
import { IconProps } from 'phosphor-react'

import { useIsDarkTheme } from '~/hooks/useIsDarkTheme'
import { navAnchor, navIconLabel, navIconWrapper } from './NavButton.css'

export interface NavigationButtonProps {
  title: string
  href: string
  Icon: ForwardRefExoticComponent<IconProps & RefAttributes<SVGSVGElement>>
  isExternal?: boolean
  showLabel?: boolean
  onClick?: MouseEventHandler<HTMLAnchorElement>
}

export const NavigationButton = ({
  Icon,
  title,
  href,
  isExternal,
  showLabel = false,
  onClick,
}: NavigationButtonProps) => {
  const { pathname } = useLocation()

  const isRoute = (href !== '/' && pathname.includes(href)) || pathname === href

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

  const externalLinkProps = isExternal
    ? {
        rel: 'noopener noreferrer',
        target: '_blank',
      }
    : {}

  const isDarkMode = useIsDarkTheme()

  /**
   * TODO: refactor to use `Link` component
   */
  return (
    <Toolbar.Link
      onClick={handleClick}
      href={href}
      className={navAnchor({
        active: isRoute,
        variant: showLabel ? 'withLabel' : undefined,
      })}
      asChild
      {...externalLinkProps}
    >
      <Link to={href}>
        <span
          className={navIconWrapper({
            isRoute,
          })}
        >
          <Icon size={20} weight={isDarkMode ? 'light' : 'regular'} />
          {showLabel ? <span className={navIconLabel}>{title}</span> : null}
        </span>
      </Link>
    </Toolbar.Link>
  )
}