File size: 2,931 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Link, useLocation } from '@remix-run/react'
import { Location } from 'react-router'

import {
  NavigationSchema,
  NavigationSchemaItem,
} from '../../../scripts/docs/navigation'
import { WidgetSearch } from '../Widgets/WidgetSearch'
import { BadgeNew } from '../BadgeNew'
import {
  anchorActive,
  anchorHasNoLink,
  anchorStyles,
  anchorTitle,
  docsList,
  scrollArea,
  widgetContainer,
} from './MenuDocs.css'
import clsx from 'clsx'

interface MenuDocsProps {
  submenu?: NavigationSchema
  onNavClick?: () => void
}

export const MenuDocs = ({ submenu, onNavClick }: MenuDocsProps) => {
  const location = useLocation()

  const handleNavClick = () => {
    if (onNavClick) {
      onNavClick()
    }
  }

  const isDocs = location.pathname.includes('docs')

  return (
    <ul className={docsList}>
      <li
        className={widgetContainer}
        style={{ display: !isDocs ? 'none' : 'flex' }}
      >
        <WidgetSearch />
      </li>
      <div className={scrollArea}>
        {Array.isArray(submenu) &&
          submenu.map(item =>
            renderSubMenu({ ...item, location, onClick: handleNavClick }, 0)
          )}
        <li>
          <a
            className={clsx(anchorStyles, anchorTitle)}
            style={{ display: !isDocs ? 'none' : 'flex' }}
            href="https://github.com/pmndrs/react-spring/releases"
            rel="noopener noreferrer"
            target="_blank"
            onClick={handleNavClick}
          >
            {`Changelog`}
          </a>
        </li>
      </div>
    </ul>
  )
}

interface SubMenuSchema extends NavigationSchemaItem {
  location: Location
  onClick?: () => void
}

const renderSubMenu = (
  {
    children,
    id,
    title,
    href,
    location,
    onClick,
    noPage,
    isNew,
  }: SubMenuSchema,
  level: number
) => {
  const handleClick = () => {
    if (onClick) {
      onClick()
    }
  }
  const hasRenderableChildren = children.length > 0

  const isTitle = level === 0

  const doesNotWantPage = Boolean(noPage)

  return (
    <li key={id}>
      {!doesNotWantPage ? (
        <Link
          to={href}
          className={clsx(
            anchorStyles,
            isTitle && anchorTitle,
            location.pathname === href && anchorActive
          )}
          onClick={handleClick}
        >
          <span>{title}</span>
          {isNew ? <BadgeNew /> : null}
        </Link>
      ) : (
        <span
          className={clsx(
            anchorStyles,
            isTitle && anchorTitle,
            anchorHasNoLink
          )}
        >
          <span>{title}</span>
          {isNew ? <BadgeNew /> : null}
        </span>
      )}
      {hasRenderableChildren ? (
        <ul>
          {children.map(item =>
            renderSubMenu(
              { ...item, location, onClick: handleClick },
              level + 1
            )
          )}
        </ul>
      ) : null}
    </li>
  )
}