-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathSiteNavigation.tsx
227 lines (217 loc) · 10 KB
/
SiteNavigation.tsx
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
215
216
217
218
219
220
221
222
223
224
225
226
227
import { useState, useCallback, useEffect } from "react"
import {
faListUl,
faBars,
faXmark,
faEnvelopeOpenText,
} from "@fortawesome/free-solid-svg-icons"
import { NewsletterSubscriptionContext } from "./newsletter.js"
import { NewsletterSubscriptionForm } from "./NewsletterSubscription.js"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { SiteNavigationTopics } from "./SiteNavigationTopics.js"
import { SiteLogos } from "./SiteLogos.js"
import { SiteAbout } from "./SiteAbout.js"
import { SiteResources } from "./SiteResources.js"
import { SiteSearchNavigation } from "./SiteSearchNavigation.js"
import { SiteMobileMenu } from "./SiteMobileMenu.js"
import { SiteNavigationToggle } from "./SiteNavigationToggle.js"
import classnames from "classnames"
import { useTriggerOnEscape } from "./hooks.js"
import { AUTOCOMPLETE_CONTAINER_ID } from "./search/Autocomplete.js"
import { Menu, SiteNavigationStatic } from "./SiteConstants.js"
// Note: tranforming the flag from an env string to a boolean in
// clientSettings.ts is convoluted due to the two-pass SSR/Vite build process.
const HAS_DONATION_FLAG = false
export const SiteNavigation = ({
baseUrl,
hideDonationFlag,
isOnHomepage,
}: {
baseUrl: string
hideDonationFlag?: boolean
isOnHomepage?: boolean
}) => {
const [menu, setActiveMenu] = useState<Menu | null>(null)
const [query, setQuery] = useState<string>("")
const isActiveMobileMenu =
menu !== null &&
[Menu.Topics, Menu.Resources, Menu.About].includes(menu)
// useCallback so as to not trigger a re-render for SiteSearchNavigation, which remounts
// Autocomplete and breaks it
const closeOverlay = useCallback(() => {
setActiveMenu(null)
setQuery("")
}, [])
// Same SiteSearchNavigation re-rendering case as above
const setSearchAsActiveMenu = useCallback(() => {
setActiveMenu(Menu.Search)
// Forced DOM manipulation of the algolia autocomplete panel position 🙃
// Without this, the panel initially renders at the same width as the shrunk search input
// Fortunately we only have to do this when it mounts - it takes care of resizes
setTimeout(() => {
// Only run when screen size is large, .aa-DetachedContainer gets positioned correctly
if (window.innerWidth < 768) return
const [panel, autocompleteContainer] = [
".aa-Panel",
AUTOCOMPLETE_CONTAINER_ID,
].map((className) => document.querySelector<HTMLElement>(className))
if (panel && autocompleteContainer) {
const bounds = autocompleteContainer.getBoundingClientRect()
panel.style.left = `${bounds.left}px`
}
}, 10)
setTimeout(() => {
const input = document.querySelector<HTMLElement>(".aa-Input")
if (input) {
input.focus()
input.setAttribute("required", "true")
}
}, 10)
}, [])
const toggleMenu = (root: Menu) => {
if (menu === root) {
closeOverlay()
} else {
setActiveMenu(root)
}
}
// Open overlay back when query entered after pressing "esc"
useEffect(() => {
if (query) {
setActiveMenu(Menu.Search)
}
}, [query])
useTriggerOnEscape(closeOverlay)
return (
<>
{menu && <div className="overlay" onClick={closeOverlay} />}
<div className="site-navigation">
<div className="wrapper">
<div
className={classnames("site-navigation-bar", {
"search-active": menu === Menu.Search,
})}
>
<SiteNavigationToggle
ariaLabel="Toggle menu"
isActive={isActiveMobileMenu}
onToggle={() => toggleMenu(Menu.Topics)}
className="SiteNavigationToggle--mobile-menu hide-sm-up"
dropdown={
<SiteMobileMenu
menu={menu}
toggleMenu={toggleMenu}
topics={SiteNavigationStatic.categories}
className="hide-sm-up"
/>
}
>
<FontAwesomeIcon
icon={isActiveMobileMenu ? faXmark : faBars}
/>
</SiteNavigationToggle>
<SiteLogos baseUrl={baseUrl} />
<nav className="site-primary-links hide-sm-only">
<ul>
<li>
<SiteNavigationToggle
ariaLabel="Toggle topics menu"
isActive={menu === Menu.Topics}
onToggle={() => toggleMenu(Menu.Topics)}
dropdown={
<SiteNavigationTopics
onClose={closeOverlay}
topics={
SiteNavigationStatic.categories
}
className="hide-sm-only"
/>
}
className="topics"
>
<FontAwesomeIcon
icon={faListUl}
style={{ marginRight: "8px" }}
/>
Browse by topic
</SiteNavigationToggle>
</li>
<li>
<a href="/latest">Latest</a>
</li>
<li className="with-relative-dropdown">
<SiteNavigationToggle
ariaLabel="Toggle resources menu"
isActive={menu === Menu.Resources}
onToggle={() =>
toggleMenu(Menu.Resources)
}
dropdown={<SiteResources />}
withCaret={true}
>
Resources
</SiteNavigationToggle>
</li>
<li className="with-relative-dropdown">
<SiteNavigationToggle
ariaLabel="Toggle about menu"
isActive={menu === Menu.About}
onToggle={() => toggleMenu(Menu.About)}
dropdown={<SiteAbout />}
withCaret={true}
>
About
</SiteNavigationToggle>
</li>
</ul>
</nav>
<div className="site-search-cta">
{!isOnHomepage && (
<SiteSearchNavigation
isActive={menu === Menu.Search}
onClose={closeOverlay}
onActivate={setSearchAsActiveMenu}
/>
)}
<SiteNavigationToggle
ariaLabel="Toggle subscribe menu"
isActive={menu === Menu.Subscribe}
onToggle={() => toggleMenu(Menu.Subscribe)}
dropdown={
<NewsletterSubscriptionForm
context={
NewsletterSubscriptionContext.Floating
}
/>
}
className="newsletter-subscription"
>
<span className="hide-lg-down">Subscribe</span>
<FontAwesomeIcon
className="hide-lg-up"
icon={
menu === Menu.Subscribe
? faXmark
: faEnvelopeOpenText
}
/>
</SiteNavigationToggle>
<a
href="/donate"
className="donate"
data-track-note="header_navigation"
>
Donate
</a>
</div>
</div>
</div>
</div>
{HAS_DONATION_FLAG && !hideDonationFlag && (
<a href="/donate" className="site-navigation__giving">
It’s Giving Season. Help us do more with a donation.
</a>
)}
</>
)
}