-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmenu.tsx
More file actions
214 lines (205 loc) · 5.71 KB
/
menu.tsx
File metadata and controls
214 lines (205 loc) · 5.71 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import React from "react"
import { MdArrowDropDown, MdMenu } from "react-icons/md/index"
import {
Dialog,
DialogBackdrop,
DialogDisclosure,
useDialogState,
} from "reakit"
import { Menu, MenuButton, MenuItem, useMenuState } from "reakit"
import Link from "src/components/link"
import * as Dailp from "src/graphql/dailp"
import { MenuItem as MenuItemType } from "src/graphql/dailp"
//import * as Wordpress from "src/graphql/wordpress"
import { Location, useLocation, usePageContext } from "src/renderer/PageShell"
import {
desktopNav,
drawerBg,
drawerItem,
drawerList,
navButton,
navDrawer,
navLink,
navMenu,
subMenuItems,
} from "./menu.css"
type MenuItemNode = Dailp.MenuBySlugQuery["menuBySlug"]["items"][number]
type ChildMenuItemNode = NonNullable<MenuItemNode["items"]>[number]
const isTopLevel = (a: MenuItemType, menuItems: MenuItemType[]) =>
!menuItems?.some((b) => b?.items?.some((b) => b?.path === a?.path))
export const NavMenu = (p: { menuID: number }) => {
const location = useLocation()
const [{ data }] = Dailp.useMenuBySlugQuery({
variables: { slug: "default-nav" },
})
//OLD: Wordpress menu
//const [{ data }] = Wordpress.useMenuByIdQuery({
//variables: { id: p.menuID },
//})
//const menus = data?.menus?.nodes
//const menu = menus[0]
const menu = data?.menuBySlug
if (!menu) {
return null
}
const menuItems = menu.items
if (!menuItems) {
return null
}
return (
<nav className={desktopNav}>
{menuItems
.filter((item) =>
isTopLevel(item as MenuItemType, menuItems as MenuItemType[])
)
.map((item: MenuItemNode) => {
if (!item) {
return null
} else if (item.items?.length) {
return <SubMenu key={item.label} item={item} location={location} />
} else {
let url = { pathname: item.path }
if (item.path && item.path.startsWith("http")) {
url = new URL(item.path)
}
return (
<Link
key={item.path}
href={url.pathname?.valueOf()}
className={navLink}
aria-current={
location.pathname === url.pathname ? "page" : undefined
}
>
{item.label}
</Link>
)
}
})}
</nav>
)
}
const SubMenu = ({
item,
location,
}: {
location: Location
item: MenuItemNode
}) => {
const menu = useMenuState()
return (
<>
<MenuButton {...menu} className={navLink}>
{item.label}
<MdArrowDropDown aria-label="Menu" />
</MenuButton>
<Menu {...menu} aria-label={item.label} className={navMenu}>
{item.items?.map((item: ChildMenuItemNode) => {
let url = { pathname: item.path }
if (item.path.startsWith("http")) {
url = new URL(item.path)
}
return (
<MenuItem
{...menu}
as="a"
key={item.path}
href={url.pathname}
aria-current={
location.pathname === url.pathname ? "page" : undefined
}
className={subMenuItems}
onClick={() => menu.hide()}
>
{item.label}
</MenuItem>
)
})}
</Menu>
</>
)
}
export const MobileNav = (p: { menuID: number }) => {
const router = usePageContext()
const dialog = useDialogState({ animated: true })
//old WP implementation
//const [{ data }] = Wordpress.useMenuByIdQuery({
//variables: { id: p.menuID },
//})
//const menus = data?.menus?.nodes
//if (!menus) {
//return null
//}
//const menu = menus[0]
//const menuItems = menu?.menuItems?.nodes
//if (!menuItems) {
//return null
//}
//const isTopLevel = (a: typeof menuItems[0]) =>
//!menuItems?.some((b) =>
//b?.childItems!.nodes!.some((b) => b?.path === a?.path)
//)
const [{ data }] = Dailp.useMenuBySlugQuery({
variables: { slug: "default-nav" },
})
const menu = data?.menuBySlug
if (!menu) {
return null
}
const menuItems = menu?.items
if (!menuItems) {
return null
}
const isTopLevel = (a: typeof menuItems[0]) =>
!menuItems?.some((b) => b?.items?.some((b) => b?.path === a?.path))
return (
<>
<DialogDisclosure
{...dialog}
className={navButton}
aria-label="Open Mobile Navigation Drawer"
>
<MdMenu size={32} />
</DialogDisclosure>
<DialogBackdrop {...dialog} className={drawerBg}>
<Dialog
{...dialog}
as="nav"
className={navDrawer}
aria-label="Navigation Drawer"
>
<ul className={drawerList}>
{menuItems?.filter(isTopLevel).map((item) => {
let items = item?.items
if (items && !items.length) {
items = [item]
}
return items?.map((item) => {
if (!item) {
return null
}
let url = { pathname: item.path }
if (item.path && item.path.startsWith("http")) {
url = new URL(item.path)
}
return (
<li key={item.path}>
<Link
className={drawerItem}
href={url.pathname?.valueOf()}
aria-current={
router.urlPathname === url.pathname ? "page" : undefined
}
>
{item.label}
</Link>
</li>
)
})
})}
</ul>
</Dialog>
</DialogBackdrop>
</>
)
}