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
|
/* eslint-disable max-statements */
import NextImage from 'next/image';
import { useRouter } from 'next/router';
import Script from 'next/script';
import {
type FC,
type ReactElement,
type ReactNode,
useRef,
type CSSProperties,
type FormEvent,
useCallback,
} from 'react';
import { useIntl } from 'react-intl';
import type { Person, SearchAction, WebSite, WithContext } from 'schema-dts';
import type { NextPageWithLayoutOptions } from '../../../types';
import { CONFIG } from '../../../utils/config';
import { ROUTES } from '../../../utils/constants';
import {
useAutofocus,
useBoolean,
useOnRouteChange,
useScrollPosition,
} from '../../../utils/hooks';
import {
ButtonLink,
Footer,
Header,
Heading,
Icon,
Logo,
Main,
} from '../../atoms';
import {
BackToTop,
Branding,
Colophon,
type ColophonLink,
Copyright,
FlippingLogo,
} from '../../molecules';
import {
type MainNavItem,
Navbar,
MainNav,
SearchForm,
SettingsForm,
type NavbarItems,
type SearchFormSubmit,
} from '../../organisms';
import styles from './layout.module.scss';
export type QueryAction = SearchAction & {
'query-input': string;
};
export type LayoutProps = {
/**
* The layout main content.
*/
children: ReactNode;
/**
* Is it homepage?
*
* @default false
*/
isHome?: boolean;
/**
* Determine if article has a comments section.
*/
withExtraPadding?: boolean;
/**
* Determine if article should use grid. Default: false.
*/
useGrid?: boolean;
};
/**
* Layout component
*
* Render the base layout used by all pages.
*/
export const Layout: FC<LayoutProps> = ({
children,
withExtraPadding = false,
isHome,
useGrid = false,
}) => {
const router = useRouter();
const intl = useIntl();
const { baseline, copyright, locales, name, url } = CONFIG;
const articleGridClass = useGrid ? 'article--grid' : '';
const articleCommentsClass = withExtraPadding ? 'article--padding' : '';
const skipToContent = intl.formatMessage({
defaultMessage: 'Skip to content',
description: 'Layout: Skip to content link',
id: 'K4rYdT',
});
const noScript = intl.formatMessage({
defaultMessage:
'Warning: If you want to benefit from all features (search for example), please activate Javascript.',
description: 'Layout: noscript message',
id: '7jVUT6',
});
const copyrightTitle = intl.formatMessage({
defaultMessage: 'CC BY SA',
description: 'Layout: copyright title',
id: 'yB1SPF',
});
const homeLabel = intl.formatMessage({
defaultMessage: 'Home',
description: 'Layout: main nav - home link',
id: 'bojYF5',
});
const blogLabel = intl.formatMessage({
defaultMessage: 'Blog',
description: 'Layout: main nav - blog link',
id: 'D8vB38',
});
const projectsLabel = intl.formatMessage({
defaultMessage: 'Projects',
description: 'Layout: main nav - projects link',
id: 'qnwsWV',
});
const cvLabel = intl.formatMessage({
defaultMessage: 'CV',
description: 'Layout: main nav - cv link',
id: 'R895yC',
});
const contactLabel = intl.formatMessage({
defaultMessage: 'Contact',
description: 'Layout: main nav - contact link',
id: 'AE4kCD',
});
const photoAltText = intl.formatMessage(
{
defaultMessage: '{website} picture',
description: 'Layout: photo alternative text',
id: '8jjY1X',
},
{ website: name }
);
const logoTitle = intl.formatMessage(
{
defaultMessage: '{website} logo',
description: 'Layout: logo title',
id: '52H2HA',
},
{ website: name }
);
const backToTop = intl.formatMessage({
defaultMessage: 'Back to top',
description: 'Layout: an accessible name for the back to top button',
id: 'Kjj1Zk',
});
const mainNav: MainNavItem[] = [
{
id: 'home',
label: homeLabel,
href: '/',
logo: <Icon aria-hidden={true} shape="home" />,
},
{
id: 'blog',
label: blogLabel,
href: ROUTES.BLOG,
logo: <Icon aria-hidden={true} shape="posts-stack" />,
},
{
id: 'projects',
label: projectsLabel,
href: ROUTES.PROJECTS,
logo: <Icon aria-hidden={true} shape="computer" />,
},
{
id: 'cv',
label: cvLabel,
href: ROUTES.CV,
logo: <Icon aria-hidden={true} shape="career" />,
},
{
id: 'contact',
label: contactLabel,
href: ROUTES.CONTACT,
logo: <Icon aria-hidden={true} shape="envelop" />,
},
];
const {
deactivate: deactivateMainNav,
state: isMainNavOpen,
toggle: toggleMainNav,
} = useBoolean(false);
const {
deactivate: deactivateSearch,
state: isSearchOpen,
toggle: toggleSearch,
} = useBoolean(false);
const {
deactivate: deactivateSettings,
state: isSettingsOpen,
toggle: toggleSettings,
} = useBoolean(false);
const labels = {
mainNavItem: intl.formatMessage({
defaultMessage: 'Open menu',
description: 'Layout: main nav button label in navbar',
id: 'Fgt/RZ',
}),
mainNavModal: intl.formatMessage({
defaultMessage: 'Main navigation',
description: 'Layout: main nav accessible name',
id: 'dfTljv',
}),
searchItem: intl.formatMessage({
defaultMessage: 'Open search',
id: 'XRwEoA',
description: 'Layout: search button label in navbar',
}),
searchModal: intl.formatMessage({
defaultMessage: 'Search',
description: 'Layout: search modal title in navbar',
id: 'Mq+O6q',
}),
settingsItem: intl.formatMessage({
defaultMessage: 'Open settings',
id: 'mDKiaN',
description: 'Layout: settings button label in navbar',
}),
settingsForm: intl.formatMessage({
defaultMessage: 'Settings form',
id: 'h3J0a+',
description: 'Layout: an accessible name for the settings form in navbar',
}),
settingsModal: intl.formatMessage({
defaultMessage: 'Settings',
description: 'Layout: settings modal title in navbar',
id: 'o3WSz5',
}),
};
const settingsSubmitHandler = useCallback((e: FormEvent) => {
e.preventDefault();
}, []);
const searchInputRef = useAutofocus<HTMLInputElement>({
condition: () => isSearchOpen,
delay: 360,
});
const searchSubmitHandler: SearchFormSubmit = useCallback(
({ query }) => {
if (!query)
return {
messages: {
error: intl.formatMessage({
defaultMessage: 'Query must be longer than one character.',
description: 'Layout: invalid query message',
id: 'C2YcUJ',
}),
},
validator: (value) => value.query.length > 1,
};
router.push({ pathname: ROUTES.SEARCH, query: { s: query } });
return undefined;
},
[intl, router]
);
useOnRouteChange(deactivateSearch);
const navbarItems: NavbarItems = [
{
contents: <MainNav aria-label={labels.mainNavModal} items={mainNav} />,
icon: 'hamburger',
id: 'main-nav',
isActive: isMainNavOpen,
label: labels.mainNavItem,
modalVisibleFrom: 'md',
onDeactivate: deactivateMainNav,
onToggle: toggleMainNav,
},
{
contents: (
<SearchForm
className={styles.search}
isLabelHidden
onSubmit={searchSubmitHandler}
ref={searchInputRef}
/>
),
icon: 'magnifying-glass',
id: 'search',
isActive: isSearchOpen,
label: labels.searchItem,
onDeactivate: deactivateSearch,
onToggle: toggleSearch,
modalHeading: labels.searchModal,
},
{
contents: (
<SettingsForm
aria-label={labels.settingsForm}
className={styles.settings}
onSubmit={settingsSubmitHandler}
/>
),
icon: 'cog',
id: 'settings',
isActive: isSettingsOpen,
label: labels.settingsItem,
onDeactivate: deactivateSettings,
onToggle: toggleSettings,
modalHeading: labels.settingsModal,
showIconOnModal: true,
},
];
const legalNoticeLabel = intl.formatMessage({
defaultMessage: 'Legal notice',
description: 'Layout: Legal notice label',
id: 'nwbzKm',
});
const footerNav: ColophonLink[] = [
{ id: 'legal-notice', label: legalNoticeLabel, href: ROUTES.LEGAL_NOTICE },
];
const searchActionSchema: QueryAction = {
'@type': 'SearchAction',
target: {
'@type': 'EntryPoint',
urlTemplate: `${url}${ROUTES.SEARCH}?s={search_term_string}`,
},
query: 'required',
'query-input': 'required name=search_term_string',
};
const schemaJsonLd: WithContext<WebSite> = {
'@context': 'https://schema.org',
'@id': `${url}`,
'@type': 'WebSite',
name,
description: baseline,
url,
author: { '@id': `${url}/#branding` },
copyrightYear: Number(copyright.startYear),
creator: { '@id': `${url}/#branding` },
editor: { '@id': `${url}/#branding` },
inLanguage: locales.defaultLocale,
potentialAction: searchActionSchema,
};
const brandingSchema: WithContext<Person> = {
'@context': 'https://schema.org',
'@type': 'Person',
'@id': `${url}/#branding`,
name,
url,
jobTitle: baseline,
image: '/armand-philippot.jpg',
subjectOf: { '@id': `${url}` },
};
const scrollPos = useScrollPosition();
const backToTopBreakpoint = 300;
const backToTopClassName = [
styles['back-to-top'],
styles[
scrollPos.y > backToTopBreakpoint
? 'back-to-top--visible'
: 'back-to-top--hidden'
],
].join(' ');
const topRef = useRef<HTMLSpanElement>(null);
const giveFocusToTopRef = () => {
if (topRef.current) topRef.current.focus();
};
useOnRouteChange(giveFocusToTopRef);
const brandingTitleStyles = {
'--typing-animation':
'blink 0.7s ease-in-out 0s 2, typing 4.3s linear 0s 1',
} as CSSProperties;
const brandingBaselineStyles = {
'--typing-animation':
'hide-text 4.25s linear 0s 1, blink 0.8s ease-in-out 4.25s 2, typing 3.8s linear 4.25s 1',
} as CSSProperties;
return (
<>
<Script
dangerouslySetInnerHTML={{ __html: JSON.stringify(schemaJsonLd) }}
// eslint-disable-next-line react/jsx-no-literals -- Id allowed
id="schema-layout"
type="application/ld+json"
/>
<Script
dangerouslySetInnerHTML={{ __html: JSON.stringify(brandingSchema) }}
// eslint-disable-next-line react/jsx-no-literals -- Id allowed
id="schema-branding"
type="application/ld+json"
/>
<noscript>
<div className={styles['noscript-spacing']} />
</noscript>
<span ref={topRef} tabIndex={-1} />
<ButtonLink className="screen-reader-text" to="#main">
{skipToContent}
</ButtonLink>
<Header className={styles.header}>
<div className={styles.header__body}>
<Branding
baseline={
<div
className={styles.brand__baseline}
style={brandingBaselineStyles}
>
{baseline}
</div>
}
logo={
<FlippingLogo
back={<Logo heading={logoTitle} />}
className={styles.brand__logo}
front={
<NextImage
alt={photoAltText}
height={120}
src="/armand-philippot.jpg"
width={120}
/>
}
/>
}
name={
<Heading
className={styles.brand__title}
isFake={!isHome}
level={1}
style={brandingTitleStyles}
>
{name}
</Heading>
}
url="/"
/>
<Navbar items={navbarItems} />
</div>
</Header>
<Main id="main" className={styles.main}>
<article
className={`${styles[articleGridClass]} ${styles[articleCommentsClass]}`}
>
{children}
</article>
</Main>
<Footer className={styles.footer}>
<Colophon
copyright={
<Copyright
from={copyright.startYear}
owner={name}
to={copyright.endYear}
/>
}
license={<Icon heading={copyrightTitle} shape="cc-by-sa" size="lg" />}
links={footerNav}
/>
<BackToTop
anchor="#top"
className={backToTopClassName}
label={backToTop}
/>
</Footer>
<noscript>
<div className={styles.noscript}>{noScript}</div>
</noscript>
</>
);
};
/**
* Get the global layout.
*
* @param {ReactElement} page - A page.
* @param {NextPageWithLayoutOptions} props - An object with layout options.
* @returns A page wrapped with the global layout.
*/
export const getLayout = (
page: ReactElement,
props: NextPageWithLayoutOptions
) => <Layout {...props}>{page}</Layout>;
|