-
Notifications
You must be signed in to change notification settings - Fork 23
/
menu.js
123 lines (116 loc) · 3 KB
/
menu.js
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
import React from "react"
import { Link, StaticQuery, graphql } from "gatsby"
import ChevronDownIcon from "./icons/chevron-down"
const MENU_QUERY = graphql`
fragment MenuFields on WPGraphQL_MenuItem {
id
label
url
connectedObject {
__typename
}
}
query GET_MENU_ITEMS {
site {
_xexperimentalThemes {
options {
wordPressUrl
}
}
}
wpgraphql {
menuItems(where: { location: MENU_1 }) {
nodes {
...MenuFields
childItems {
nodes {
...MenuFields
childItems {
nodes {
...MenuFields
childItems {
nodes {
...MenuFields
childItems {
nodes {
...MenuFields
}
}
}
}
}
}
}
}
}
}
}
}
`
const createLocalLink = (url, wpUrl) => {
if (`#` === url) {
return null
}
return url.replace(wpUrl, ``)
}
const renderMenuItem = (menuItem, wpUrl) => {
const link = createLocalLink(menuItem.url, wpUrl)
if (menuItem.childItems && menuItem.childItems.nodes.length) {
return renderSubMenu(menuItem)
} else {
return (
<li
key={menuItem.id}
className="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-100"
>
<Link to={link}>{menuItem.label}</Link>
</li>
)
}
}
const renderSubMenu = (menuItem, wpUrl) => {
return (
<li
key={menuItem.label}
className="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-100 menu-item-has-children"
>
<Link to="/">{menuItem.label}</Link>
<button className="submenu-expand" tabIndex="-1">
<ChevronDownIcon />
</button>
<ul className="sub-menu">
{menuItem.childItems.nodes.map(item => renderMenuItem(item, wpUrl))}
</ul>
</li>
)
}
const Menu = () => (
<StaticQuery
query={MENU_QUERY}
render={({ wpgraphql, site }) => {
const wpUrl = site._xexperimentalThemes[0].options.wordPressUrl
if (wpgraphql.menuItems) {
return (
<nav
id="site-navigation"
className="main-navigation"
aria-label="Top Menu"
>
<div className="menu-main-menu-container">
<ul id="menu-main-menu" className="main-menu">
{wpgraphql.menuItems.nodes.map(menuItem => {
if (menuItem.childItems.nodes.length) {
return renderSubMenu(menuItem, wpUrl)
} else {
return renderMenuItem(menuItem, wpUrl)
}
})}
</ul>
</div>
</nav>
)
}
}}
/>
)
export default Menu