-
Notifications
You must be signed in to change notification settings - Fork 48
/
J40Header.tsx
324 lines (296 loc) · 9.49 KB
/
J40Header.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import React, {useEffect, useState} from 'react';
import {Link, useIntl} from 'gatsby-plugin-intl';
import {
// Alert,
Header,
NavMenuButton,
PrimaryNav,
Grid,
NavDropDownButton,
Menu,
} from '@trussworks/react-uswds';
import BetaBanner from '../BetaBanner';
import J40MainGridContainer from '../J40MainGridContainer';
import GovernmentBanner from '../GovernmentBanner';
import Language from '../Language';
import {useWindowSize} from 'react-use';
// @ts-ignore
import siteLogo from '../../images/j40-logo-v2.png';
import * as styles from './J40Header.module.scss';
import * as COMMON_COPY from '../../data/copy/common';
import {PAGES_ENDPOINTS, USWDS_BREAKPOINTS} from '../../data/constants';
interface IJ40Header {
location: Location
}
/**
* The J40Header component will control how the header looks for both mobile and desktop
*
* The Header is defined as
* 1. Two rows of Banners (ie, official gov website and beta site)
* 2. Logo and Nav Links Row
* 3. Any Alerts
*
* @param {Location} location
* @return {JSX.Element}
*/
const J40Header = ({location}:IJ40Header) => {
const intl = useIntl();
// grab last segment of location pathname
const [lastSegmentLocation] = location.pathname.split('/').slice(-1);
// Logo text
const logoLine1 = intl.formatMessage(COMMON_COPY.HEADER.TITLE_LINE_1);
/**
* State variable to control the toggling of mobile menu button
*/
const [mobileNavOpen, setMobileNavOpen] = useState(false);
const toggleMobileNav = (): void => {
setMobileNavOpen((prevOpen) => !prevOpen);
};
/**
* State variable to hold the open/close state of each nav dropdown. This will allow for two
* dropdown that are being used, each corresponding to an index in the state array:
*
* index 0 = Data & Methodology dropdown
* index 1 = About dropdown
*/
const [isOpen, setIsOpen] = useState([false, false]);
/**
* When transitioning between anything larger than desktop and anything less than desktop the nav menu
* changes from the usual row of nav links (dektop) to the MENU button(mobile). On desktop all nav drop
* dropdowns should be closed, while on mobile all nav links should be open.
*
* The useWindowSize provides the device width and the useEffect allows the side effect (opening/closing
* nav links) to occur anytime the device width changes.
*/
const {width} = useWindowSize();
useEffect( () => {
if (width < USWDS_BREAKPOINTS.DESKTOP) {
setIsOpen([true, true]);
} else {
setIsOpen([false, false]);
}
}, [width]);
/**
* This toggle function will handle both navigation toggle links (Meth and About).
*
* @param {number} index
*/
const onToggle = (index: number): void => {
// The setIsOpen is used to toggle the currently selected nav link
setIsOpen((prevIsOpen) => {
const newIsOpen = [...isOpen];
newIsOpen[index] = !prevIsOpen[index];
return newIsOpen;
});
/**
* When on desktop only, the dropdown nav links (Meth and About) should close if any of the other ones
* are still open. This next set of logic handles that.
*/
if (index === 0 && isOpen[1] === true && width > USWDS_BREAKPOINTS.DESKTOP) {
setIsOpen([isOpen[0], false]);
} else if (index === 1 && isOpen[0] === true && width > USWDS_BREAKPOINTS.DESKTOP) {
setIsOpen([false, isOpen[1]]);
}
};
/**
* On mobile, the Methodology & Data page should have two sub-nav links. This defines
* the array that will hold these links
*/
const methPageSubNavLinks = [
<Link
to={PAGES_ENDPOINTS.METHODOLOGY}
key={'methodology'}
activeClassName="usa-current"
data-cy={'nav-link-methodology'}>
{intl.formatMessage(COMMON_COPY.HEADER.METHODOLOGY)}
</Link>,
<Link
to={PAGES_ENDPOINTS.DOWNLOADS}
key={'downloads'}
activeClassName="usa-current"
data-cy={'nav-link-downloads'}>
{intl.formatMessage(COMMON_COPY.HEADER.DOWNLOADS)}
</Link>,
<Link
to={PAGES_ENDPOINTS.PREVIOUS_VERSIONS}
key={'previous-versions'}
activeClassName="usa-current"
data-cy={'nav-link-previous-versions'}>
{intl.formatMessage(COMMON_COPY.HEADER.PREVIOUS_VERSIONS)}
</Link>,
];
/**
* On mobile, the About page should have 3 sub-nav links. This defines
* the array that will hold these links
*/
const aboutPageSubNavLinks = [
<Link
to={PAGES_ENDPOINTS.ABOUT}
key={'about'}
activeClassName="usa-current"
data-cy={'nav-link-about'}>
{intl.formatMessage(COMMON_COPY.HEADER.ABOUT)}
</Link>,
<Link
to={PAGES_ENDPOINTS.PUBLIC_ENG}
key={'publicEng'}
activeClassName="usa-current"
data-cy={'nav-link-public-engagement'}>
{intl.formatMessage(COMMON_COPY.HEADER.PUBLIC_ENG)}
</Link>,
<Link
to={PAGES_ENDPOINTS.FAQS}
key={'faqs'}
activeClassName="usa-current"
data-cy={'nav-link-faqs'}>
{intl.formatMessage(COMMON_COPY.HEADER.FAQS)}
</Link>,
];
// Methodology & Data Nav component
const MethNav = () =>
<>
{/* Add a className of usa-current anytime this component renders when the location of the app is on
the Methodology page or the downloads page. This will style the nav link with a bottom border */}
<NavDropDownButton
className={
lastSegmentLocation === PAGES_ENDPOINTS.METHODOLOGY.slice(1) ||
lastSegmentLocation === PAGES_ENDPOINTS.DOWNLOADS.slice(1) ?
'usa-current' :
''
}
key="methDropDown"
label={intl.formatMessage(COMMON_COPY.HEADER.METHODOLOGY)}
menuId="methMenu"
isOpen={isOpen[0]}
onToggle={(): void => onToggle(0)}
data-cy={'nav-dropdown-methodology'}
>
</NavDropDownButton>
<Menu
id='methMenu'
type='subnav'
items={methPageSubNavLinks}
isOpen={isOpen[0]}
>
</Menu>
</>;
// About Nav component
const AboutNav = () =>
<>
{/* Add a className of usa-current anytime this component renders when the location of the app is on
the About, FAQS or Public Eng page. This will style the nav link with a bottom border */}
<NavDropDownButton
className={
lastSegmentLocation === PAGES_ENDPOINTS.ABOUT.slice(1) ||
lastSegmentLocation === PAGES_ENDPOINTS.FAQS.slice(1) ||
lastSegmentLocation === PAGES_ENDPOINTS.PUBLIC_ENG.slice(1) ?
'usa-current' :
''
}
key="aboutDropDown"
label={intl.formatMessage(COMMON_COPY.HEADER.ABOUT)}
menuId="aboutMenu"
isOpen={isOpen[1]}
onToggle={(): void => onToggle(1)}
data-cy={'nav-dropdown-about'}
>
</NavDropDownButton>
<Menu
id='aboutMenu'
type='subnav'
items={aboutPageSubNavLinks}
isOpen={isOpen[1]}
>
</Menu>
</>;
// Navigation links for app
const navLinks = [
<Link
to={PAGES_ENDPOINTS.EXPLORE}
key={'explore-map'}
activeClassName="usa-current"
data-cy={'nav-link-explore-the-map'}>
{intl.formatMessage(COMMON_COPY.HEADER.EXPLORE)}
</Link>,
<MethNav key="methDropDown"/>,
<AboutNav key="aboutDropDown"/>,
<Link
to={PAGES_ENDPOINTS.CONTACT}
key={'contact'}
activeClassName="usa-current"
data-cy={'nav-link-contact'}>
{intl.formatMessage(COMMON_COPY.HEADER.CONTACT)}
</Link>,
<div key={'language'}>
<Language isDesktop={false}/>
</div>,
];
return (
<Header basic={true} role={'banner'}>
{/* Banners */}
<GovernmentBanner />
<BetaBanner/>
{/* Logo and Navigation */}
<J40MainGridContainer>
<Grid className={styles.logoNavRow} row>
{/* Logo */}
<Grid col={1}>
<Link
to={PAGES_ENDPOINTS.EXPLORE}
key={'explore-map'}
data-cy={'nav-link-explore-the-map'}
>
<img className={styles.logo} src={siteLogo} alt={`${logoLine1}`} />
</Link>
</Grid>
{/* Logo Title */}
<Grid col={6}>
<Link
to={PAGES_ENDPOINTS.EXPLORE}
key={'explore-map'}
className="remove-link-style"
data-cy={'nav-link-explore-the-map'}
>
<div className={styles.logoTitle}>
{logoLine1}
</div>
</Link>
</Grid>
{/* Nav links */}
<Grid col={'fill'} className={styles.navLinks}>
<NavMenuButton
key={'mobileMenuButton'}
onClick={toggleMobileNav}
label="Menu">
</NavMenuButton>
<PrimaryNav
items={navLinks}
mobileExpanded={mobileNavOpen}
onToggleMobileNav={toggleMobileNav}
>
</PrimaryNav>
</Grid>
</Grid>
</J40MainGridContainer>
{/* Alert */}
{<J40MainGridContainer>
{/* <Alert
className={styles.alert}
type="info"
heading={intl.formatMessage(COMMON_COPY.ALERTS.ALERT_2_TITLE.TITLE)}
headingLevel={'h1'}>
{COMMON_COPY.ALERTS.ALERT_2_DESCRIPTION}
</Alert> */}
{/* <Alert
className={styles.alert}
type="info"
heading={intl.formatMessage(COMMON_COPY.ALERTS.ALERT_1_TITLE.TITLE)}
headingLevel={'h1'}>
{COMMON_COPY.ALERTS.ALERT_1_DESCRIPTION}
</Alert> */}
</J40MainGridContainer>
}
</Header>
);
};
export default J40Header;