-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathHeader.tsx
More file actions
538 lines (506 loc) · 24.9 KB
/
Copy pathHeader.tsx
File metadata and controls
538 lines (506 loc) · 24.9 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* eslint-disable no-inner-declarations */
import type { JSX } from "../tools/JSX";
import React, {
memo,
forwardRef,
cloneElement,
type ReactNode,
type CSSProperties,
type ComponentProps
} from "react";
import { fr } from "../fr";
import { createComponentI18nApi } from "../i18n";
import { symToStr } from "tsafe/symToStr";
import { cx } from "../tools/cx";
import { getLink } from "../link";
import type { RegisteredLinkProps } from "../link";
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
import type { FrIconClassName, RiIconClassName } from "../fr/generatedFromCss/classNames";
import type { MainNavigationProps } from "../MainNavigation";
import { MainNavigation } from "../MainNavigation";
import { Display } from "../Display/Display";
import { setBrandTopAndHomeLinkProps } from "../zz_internal/brandTopAndHomeLinkProps";
import { typeGuard } from "tsafe/typeGuard";
import { SearchButton } from "../SearchBar/SearchButton";
import { useTranslation as useSearchBarTranslation } from "../SearchBar/SearchBar";
export type HeaderProps = {
className?: string;
id?: string;
brandTop: ReactNode;
homeLinkProps: RegisteredLinkProps & { title: string };
serviceTitle?: ReactNode;
serviceTagline?: ReactNode;
navigation?: MainNavigationProps.Item[] | ReactNode;
/** There should be at most three of them */
quickAccessItems?: (HeaderProps.QuickAccessItem | JSX.Element | null)[];
operatorLogo?: {
orientation: "horizontal" | "vertical";
/**
* Expected ratio:
* If "vertical": 9x16
* If "horizontal": 16x9
*/
imgUrl: string;
/** Textual alternative of the image, it MUST include the text present in the image */
alt: string;
/**
* Custom link props, if not provided, the operator logo will be wrapped in a link that points to the home page
*/
linkProps?: RegisteredLinkProps & { title: string };
};
renderSearchInput?: (
/**
* id and name must be forwarded to the <input /> component
* the others params can, but it's not mandatory.
**/
params: {
id: string;
type: "search";
className: string;
placeholder: string;
}
) => JSX.Element;
/** Called when the search button is clicked */
onSearchButtonClick?: (text: string) => void;
/** Default: false */
clearSearchInputOnSearch?: boolean;
/** Default: false */
allowEmptySearch?: boolean;
classes?: Partial<
Record<
| "root"
| "body"
| "container"
| "bodyRow"
| "brand"
| "brandTop"
| "logo"
| "operator"
| "navbar"
| "service"
| "serviceTitle"
| "serviceTagline"
| "toolsLinks"
| "menu"
| "menuLinks",
string
>
>;
style?: CSSProperties;
/** Default: false */
disableDisplay?: boolean;
};
export namespace HeaderProps {
export type QuickAccessItem = QuickAccessItem.Link | QuickAccessItem.Button;
export namespace QuickAccessItem {
export type Common = {
iconId: FrIconClassName | RiIconClassName;
text: ReactNode;
};
export type Link = Common & {
linkProps: RegisteredLinkProps;
buttonProps?: never;
};
export type Button = Common & {
linkProps?: never;
buttonProps: ComponentProps<"button"> &
Record<`data-${string}`, string | boolean | null | undefined>;
};
}
}
export const headerMenuModalIdPrefix = "header-menu-modal";
/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-header> */
export const Header = memo(
forwardRef<HTMLDivElement, HeaderProps>((props, ref) => {
const {
className,
id: id_props,
brandTop,
serviceTitle,
serviceTagline,
homeLinkProps,
navigation = undefined,
quickAccessItems = [],
operatorLogo,
renderSearchInput,
clearSearchInputOnSearch = false,
allowEmptySearch = false,
onSearchButtonClick,
classes = {},
style,
disableDisplay = false,
...rest
} = props;
assert<Equals<keyof typeof rest, never>>();
const id = id_props ?? "fr-header";
const menuModalId = `${headerMenuModalIdPrefix}-${id}`;
const menuButtonId = `${id}-menu-button`;
const searchModalId = `${id}-search-modal`;
const searchInputId = `${id}-search-input`;
const searchLabelId = `${id}-search-label`;
const isSearchBarEnabled =
renderSearchInput !== undefined || onSearchButtonClick !== undefined;
setBrandTopAndHomeLinkProps({ brandTop, homeLinkProps });
const { t } = useTranslation();
const { t: tSearchBar } = useSearchBarTranslation();
const { Link } = getLink();
const getQuickAccessNode = (usecase: "mobile" | "desktop") => (
<ul className={fr.cx("fr-btns-group")}>
{quickAccessItems.map((quickAccessItem, i) => (
<li key={i}>
{(() => {
const node = !typeGuard<HeaderProps.QuickAccessItem>(
quickAccessItem,
quickAccessItem instanceof Object && "text" in quickAccessItem
) ? (
quickAccessItem
) : (
<HeaderQuickAccessItem quickAccessItem={quickAccessItem} />
);
if (node === null) {
return null;
}
return cloneElement(node, {
"id": `${id}-quick-access-item-${i}${(() => {
switch (usecase) {
case "mobile":
return "-mobile";
case "desktop":
return "";
}
assert<Equals<typeof usecase, never>>();
})()}`
});
})()}
</li>
))}
</ul>
);
const hasOperatorLink = operatorLogo?.linkProps !== undefined;
return (
<>
{!disableDisplay && <Display />}
<header
role="banner"
id={id}
className={cx(fr.cx("fr-header"), classes.root, className)}
ref={ref}
style={style}
{...rest}
>
<div className={cx(fr.cx("fr-header__body" as any), classes.body)}>
<div className={cx(fr.cx("fr-container"), classes.container)}>
<div className={cx(fr.cx("fr-header__body-row"), classes.bodyRow)}>
<div
className={cx(
fr.cx(
"fr-header__brand",
!hasOperatorLink && "fr-enlarge-link"
),
classes.brand
)}
>
<div
className={cx(
fr.cx("fr-header__brand-top"),
classes.brandTop
)}
>
<div className={cx(fr.cx("fr-header__logo"), classes.logo)}>
{(() => {
const children = (
<p className={fr.cx("fr-logo")}>{brandTop}</p>
);
return serviceTitle !== undefined ? (
children
) : (
<Link {...homeLinkProps}>{children}</Link>
);
})()}
</div>
{operatorLogo !== undefined && (
<div
className={cx(
fr.cx(
"fr-header__operator",
hasOperatorLink && "fr-enlarge-link"
),
classes.operator
)}
>
{(() => {
const children = (
<img
className={cx(
fr.cx("fr-responsive-img"),
classes.operator
)}
style={(() => {
switch (operatorLogo.orientation) {
case "vertical":
return {
"width": "3.5rem"
};
case "horizontal":
return {
"maxWidth": "9.0625rem"
};
}
})()}
src={operatorLogo.imgUrl}
alt={operatorLogo.alt}
/>
);
return hasOperatorLink ? (
<Link {...operatorLogo.linkProps}>
{children}
</Link>
) : (
children
);
})()}
</div>
)}
{(quickAccessItems.length > 0 ||
navigation !== undefined ||
isSearchBarEnabled) && (
<div
className={cx(
fr.cx("fr-header__navbar"),
classes.navbar
)}
>
{isSearchBarEnabled && (
<button
id={`${id}-search-button`}
className={fr.cx(
"fr-btn--search",
"fr-btn"
)}
data-fr-opened={false}
aria-controls={searchModalId}
title={tSearchBar("label")}
>
{tSearchBar("label")}
</button>
)}
<button
className={fr.cx("fr-btn--menu", "fr-btn")}
data-fr-opened="false"
aria-controls={menuModalId}
id={menuButtonId}
title={t("menu")}
>
{t("menu")}
</button>
</div>
)}
</div>
{serviceTitle !== undefined && (
<div
className={cx(
fr.cx(
"fr-header__service",
hasOperatorLink && "fr-enlarge-link"
),
classes.service
)}
>
<Link {...homeLinkProps}>
<p
className={cx(
fr.cx("fr-header__service-title"),
classes.serviceTitle
)}
>
{serviceTitle}
</p>
</Link>
{serviceTagline !== undefined && (
<p
className={cx(
fr.cx("fr-header__service-tagline" as any),
classes.serviceTagline
)}
>
{serviceTagline}
</p>
)}
</div>
)}
</div>
{(quickAccessItems.length > 0 || isSearchBarEnabled) && (
<div className={fr.cx("fr-header__tools")}>
{quickAccessItems.length > 0 && (
<div
className={cx(
fr.cx("fr-header__tools-links"),
classes.toolsLinks
)}
>
{getQuickAccessNode("desktop")}
</div>
)}
{isSearchBarEnabled && (
<div
className={fr.cx("fr-header__search", "fr-modal")}
id={searchModalId}
aria-labelledby={`${id}-search-bar-button`}
>
<div
className={fr.cx(
"fr-container",
"fr-container-lg--fluid"
)}
>
<button
id={`${id}-search-close-button`}
className={fr.cx("fr-btn--close", "fr-btn")}
aria-controls={searchModalId}
title={t("close")}
>
{t("close")}
</button>
<div
className={fr.cx("fr-search-bar")}
role="search"
>
<label
className={fr.cx("fr-label")}
htmlFor={searchInputId}
id={searchLabelId}
>
{tSearchBar("label")}
</label>
{(
renderSearchInput ??
(({
className,
id,
placeholder,
type
}) => (
<input
className={className}
id={id}
placeholder={placeholder}
type={type}
/>
))
)({
"className": fr.cx("fr-input"),
"id": searchInputId,
"placeholder": tSearchBar("label"),
"type": "search"
})}
<SearchButton
id={`${id}-search-bar-button`}
searchInputId={searchInputId}
onClick={onSearchButtonClick}
clearInputOnSearch={
clearSearchInputOnSearch
}
allowEmptySearch={allowEmptySearch}
/>
</div>
</div>
</div>
)}
</div>
)}
</div>
</div>
</div>
{(navigation !== undefined || quickAccessItems.length !== 0) && (
<div
className={cx(fr.cx("fr-header__menu", "fr-modal"), classes.menu)}
id={menuModalId}
aria-labelledby={menuButtonId}
>
<div className={fr.cx("fr-container")}>
<button
id={`${id}-mobile-overlay-button-close`}
className={fr.cx("fr-btn--close", "fr-btn")}
aria-controls={menuModalId}
title={t("close")}
>
{t("close")}
</button>
<div
className={cx(
fr.cx("fr-header__menu-links"),
classes.menuLinks
)}
>
{getQuickAccessNode("mobile")}
</div>
{navigation !== undefined &&
(navigation instanceof Array ? (
<MainNavigation
id={`${id}-main-navigation`}
items={navigation}
/>
) : (
navigation
))}
</div>
</div>
)}
</header>
</>
);
})
);
Header.displayName = symToStr({ Header });
export default Header;
export const { useTranslation, addHeaderTranslations } = createComponentI18nApi({
"componentName": symToStr({ Header }),
"frMessages": {
/* spell-checker: disable */
"menu": "Menu",
"close": "Fermer"
/* spell-checker: enable */
}
});
addHeaderTranslations({
"lang": "en",
"messages": {
"close": "Close"
}
});
export type HeaderQuickAccessItemProps = {
className?: string;
quickAccessItem: HeaderProps.QuickAccessItem;
id?: string;
};
/** NOTE: If you wrap this component you should forward the id */
export function HeaderQuickAccessItem(props: HeaderQuickAccessItemProps): JSX.Element {
const { className, quickAccessItem, id } = props;
const { Link } = getLink();
return quickAccessItem.linkProps !== undefined ? (
<Link
{...quickAccessItem.linkProps}
className={cx(
fr.cx("fr-btn", quickAccessItem.iconId),
quickAccessItem.linkProps.className,
className
)}
id={id}
>
{quickAccessItem.text}
</Link>
) : (
<button
{...quickAccessItem.buttonProps}
className={cx(
fr.cx("fr-btn", quickAccessItem.iconId),
quickAccessItem.buttonProps.className,
className
)}
id={id}
>
{quickAccessItem.text}
</button>
);
}