This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathindex.jsx
320 lines (292 loc) · 7.98 KB
/
index.jsx
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
import React from 'react';
import styled from '@emotion/styled';
import { shape, string, node, bool, oneOf } from 'prop-types';
import VisuallyHiddenText from '@bbc/psammead-visually-hidden-text';
import { C_WHITE, C_EBON } from '@bbc/psammead-styles/colours';
import {
GEL_SPACING_HLF,
GEL_SPACING,
GEL_SPACING_SEXT,
} from '@bbc/gel-foundations/spacings';
import {
GEL_GROUP_2_SCREEN_WIDTH_MAX,
GEL_GROUP_3_SCREEN_WIDTH_MIN,
GEL_GROUP_5_SCREEN_WIDTH_MIN,
} from '@bbc/gel-foundations/breakpoints';
import { getPica } from '@bbc/gel-foundations/typography';
import { scriptPropType } from '@bbc/gel-foundations/prop-types';
import { getSansRegular } from '@bbc/psammead-styles/font-styles';
import { NAV_BAR_TOP_BOTTOM_SPACING } from './DropdownNavigation';
const SPACING_AROUND_NAV_ITEMS = `${NAV_BAR_TOP_BOTTOM_SPACING}rem`; // 12px
const CURRENT_ITEM_HOVER_BORDER = '0.3125rem'; // 5px
const NavWrapper = styled.div`
position: relative;
max-width: ${GEL_GROUP_5_SCREEN_WIDTH_MIN};
margin: 0 auto;
`;
const StyledUnorderedList = styled.ul`
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
@media (min-width: ${GEL_GROUP_3_SCREEN_WIDTH_MIN}) {
overflow: hidden;
}
`;
const ListItemBorder = `
content: '';
position: absolute;
left: 0;
right: 0;
bottom: 0;
`;
const StyledLink = styled.a`
${({ script }) => script && getPica(script)};
${({ service }) => getSansRegular(service)};
${({ brandForegroundColour }) => `color: ${brandForegroundColour};`}
cursor: pointer;
text-decoration: none;
display: inline-block;
padding: ${SPACING_AROUND_NAV_ITEMS};
@media (max-width: ${GEL_GROUP_2_SCREEN_WIDTH_MAX}) {
padding: ${SPACING_AROUND_NAV_ITEMS} ${GEL_SPACING};
}
&:hover::after {
${ListItemBorder}
${({ brandHighlightColour }) =>
`border-bottom: ${GEL_SPACING_HLF} solid ${brandHighlightColour};`}
${({ currentLink, brandHighlightColour }) =>
currentLink &&
`
border-bottom: ${CURRENT_ITEM_HOVER_BORDER} solid ${brandHighlightColour};
`}
}
&:focus::after {
${ListItemBorder}
${({ brandHighlightColour }) =>
`border-bottom: ${GEL_SPACING_HLF} solid ${brandHighlightColour};`}
top: 0;
${({ brandHighlightColour }) =>
`border: ${GEL_SPACING_HLF} solid ${brandHighlightColour};`}
}
`;
const StyledListItem = styled.li`
display: inline-block;
position: relative;
z-index: 2;
@media (max-width: ${GEL_GROUP_2_SCREEN_WIDTH_MAX}) {
&:last-child {
${({ dir }) => `
margin-${dir === 'ltr' ? 'right' : 'left'}: ${GEL_SPACING_SEXT};
`}
}
}
@media (min-width: ${GEL_GROUP_3_SCREEN_WIDTH_MIN}) {
/* Trick to display a border between the list items when it breaks into multiple lines, which takes the full width */
&::after {
content: '';
position: absolute;
bottom: -1px;
width: ${GEL_GROUP_5_SCREEN_WIDTH_MIN};
${({ brandBorderColour }) =>
`border-bottom: 0.0625rem solid ${brandBorderColour};`}
z-index: -1;
}
}
`;
const StyledSpan = styled.span`
&::after {
${ListItemBorder}
${({ brandHighlightColour }) =>
`border-bottom: ${GEL_SPACING_HLF} solid ${brandHighlightColour};`}
}
`;
const CurrentLink = ({
linkId,
children: link,
script,
currentPageText,
brandHighlightColour,
}) => (
<>
<StyledSpan
// eslint-disable-next-line jsx-a11y/aria-role
role="text"
script={script}
brandHighlightColour={brandHighlightColour}
// This is a temporary fix for the a11y nested span's bug experienced in TalkBack, refer to the following issue: https://github.com/bbc/simorgh/issues/9652
id={`NavigationLinks-${linkId}`}
>
<VisuallyHiddenText>{`${currentPageText}, `}</VisuallyHiddenText>
{link}
</StyledSpan>
</>
);
CurrentLink.propTypes = {
linkId: string.isRequired,
children: string.isRequired,
script: shape(scriptPropType).isRequired,
currentPageText: string,
brandHighlightColour: string.isRequired,
};
CurrentLink.defaultProps = {
currentPageText: null,
};
export const NavigationUl = ({ children, ...props }) => (
<StyledUnorderedList role="list" {...props}>
{children}
</StyledUnorderedList>
);
NavigationUl.propTypes = {
children: node.isRequired,
};
export const NavigationLi = ({
children: link,
url,
script,
currentPageText,
active,
service,
dir,
brandForegroundColour,
brandHighlightColour,
brandBorderColour,
...props
}) => {
return (
<StyledListItem
dir={dir}
role="listitem"
brandForegroundColour={brandForegroundColour}
brandHighlightColour={brandHighlightColour}
brandBorderColour={brandBorderColour}
>
{active && currentPageText ? (
<StyledLink
href={url}
script={script}
service={service}
currentLink
brandForegroundColour={brandForegroundColour}
brandHighlightColour={brandHighlightColour}
// This is a temporary fix for the a11y nested span's bug experienced in TalkBack, refer to the following issue: https://github.com/bbc/simorgh/issues/9652
aria-labelledby={`NavigationLinks-${link}`}
{...props}
>
<CurrentLink
linkId={link}
script={script}
currentPageText={currentPageText}
brandHighlightColour={brandHighlightColour}
>
{link}
</CurrentLink>
</StyledLink>
) : (
<StyledLink
href={url}
script={script}
service={service}
brandForegroundColour={brandForegroundColour}
brandHighlightColour={brandHighlightColour}
{...props}
>
{link}
</StyledLink>
)}
</StyledListItem>
);
};
NavigationLi.propTypes = {
children: node.isRequired,
url: string.isRequired,
script: shape(scriptPropType).isRequired,
active: bool,
currentPageText: string,
service: string.isRequired,
dir: oneOf(['ltr', 'rtl']),
brandForegroundColour: string.isRequired,
brandHighlightColour: string.isRequired,
brandBorderColour: string.isRequired,
};
NavigationLi.defaultProps = {
active: false,
currentPageText: null,
dir: 'ltr',
};
// ampOpenClass is the class added to the Navigation, and is toggled on tap.
// It indicates whether the menu is open or not. This overrides the background
// color of the Navigation
const StyledNav = styled.nav`
position: relative;
${({ isOpen, brandBackgroundColour }) =>
`background-color: ${isOpen ? C_EBON : brandBackgroundColour};`}
${({ ampOpenClass }) =>
ampOpenClass &&
`
&.${ampOpenClass} {
@media (max-width: ${GEL_GROUP_2_SCREEN_WIDTH_MAX}) {
background-color: ${C_EBON};
}
}
`}
border-top: 0.0625rem solid ${C_WHITE};
&::after {
content: '';
position: absolute;
bottom: 0;
right: 0;
left: 0;
border-bottom: 0.0625rem solid transparent;
}
${StyledListItem} {
${({ dir }) => `
&::after {
${dir === 'ltr' ? 'left' : 'right'}: 0;
}
`}
}
`;
const Navigation = ({
children,
dir,
isOpen,
ampOpenClass,
brandBackgroundColour,
brandForegroundColour,
brandBorderColour,
brandHighlightColour,
...props
}) => {
return (
<StyledNav
role="navigation"
dir={dir}
isOpen={isOpen}
ampOpenClass={ampOpenClass}
brandBackgroundColour={brandBackgroundColour}
brandForegroundColour={brandForegroundColour}
brandBorderColour={brandBorderColour}
brandHighlightColour={brandHighlightColour}
{...props}
>
<NavWrapper>{children}</NavWrapper>
</StyledNav>
);
};
Navigation.propTypes = {
children: node.isRequired,
dir: oneOf(['ltr', 'rtl']),
isOpen: bool,
ampOpenClass: string,
brandBackgroundColour: string.isRequired,
brandForegroundColour: string.isRequired,
brandBorderColour: string.isRequired,
brandHighlightColour: string.isRequired,
};
Navigation.defaultProps = {
dir: 'ltr',
isOpen: false,
ampOpenClass: null,
};
export default Navigation;