forked from newrelic/docs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavigation.js
More file actions
95 lines (88 loc) · 2.54 KB
/
Copy pathNavigation.js
File metadata and controls
95 lines (88 loc) · 2.54 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
import React, { useMemo, useRef, useState } from 'react';
import { css } from '@emotion/react';
import { NavItem } from '@newrelic/gatsby-theme-newrelic';
import { Flipped, Flipper } from 'react-flip-toolkit';
const slugify = require('../../scripts/utils/slugify');
const Navigation = ({ nav, className }) => {
const subNav = nav.url === '/docs/style-guide';
const [flipKey, setFlipKey] = useState();
const lastClickedId = useRef(null);
const keyedNav = useMemo(
() => ({
...nav,
pages: addFlipIds(nav.pages),
}),
[nav]
);
const updateFlipKey = (clickedItemId) => {
lastClickedId.current = clickedItemId;
// using `Symbol` here ensures the `flipKey` changes on every update
// eslint-disable-next-line symbol-description
setFlipKey(Symbol());
};
return (
<nav
role="navigation"
aria-label="Navigation"
css={css`
height: 100%;
overflow: auto;
margin: 0 0 16px;
-ms-overflow-style: none;
scrollbar-width: none;
&::-webkit-scrollbar {
display: none;
}
span,
svg {
color: var(--system-text-primary-dark);
opacity: 0.8;
}
`}
id="nav"
className={className}
>
<Flipper decisionData={lastClickedId.current} flipKey={flipKey}>
{keyedNav.pages.map((page) => {
if (page.title === 'section-break') {
return <hr />;
}
if (page.title && !page.url && !subNav) {
return (
<Flipped flipId={page.flipId} translate>
<p
css={css`
color: var(--system-text-primary-dark);
opacity: 0.8;
margin: 0;
font-size: 14px;
font-weight: 500;
margin-top: 1rem;
`}
>
{page.title.toUpperCase()}
</p>
</Flipped>
);
}
return (
<Flipped flipId={page.flipId} key={page.title} opacity translate>
<NavItem
onExpand={updateFlipKey}
name={`${page.url}/`}
page={page}
/>
</Flipped>
);
})}
</Flipper>
</nav>
);
};
const addFlipIds = (pages, parentKey = []) =>
pages?.map((page) => ({
...page,
flipId: [...parentKey, slugify(page.title)],
pages: addFlipIds(page.pages, [...parentKey, slugify(page.title)]),
}));
export default Navigation;