-
Notifications
You must be signed in to change notification settings - Fork 99
/
contentTree.js
200 lines (170 loc) · 5.85 KB
/
contentTree.js
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
import React, { useState, useEffect, useMemo } from 'react';
import { css } from '@emotion/core';
import { useTheme } from 'emotion-theming';
import { calculateNavigation } from '../';
import { Link } from "gatsby"
import { Nav } from '@fluentui/react/lib/Nav';
import { initializeIcons } from '@fluentui/react/lib/Icons';
import emoji from '../../utils/emoji';
import { syncFunction } from '../../utils/utils';
import { onMobile } from '../../styles/responsive';
// Maximum Deep level of render Nav menu (Start with 0)
const limitDeepLevel = 3;
// initialize Icons for Fluent UI
// Load Font Icons from locally, not from MS Cdn
// By fix issue: failed load font icon on production
// Ref: https://dev.to/lybekk/how-to-use-fluent-ui-icons-in-gatsby-277e
initializeIcons('/assets/fluenticons/');
// Official Way to customize Fluent UI CSS
const navStyles = props => {
const { isGroup } = props;
return {
chevronButton:[
// Add custom css class for override group element
isGroup && 'is-group'
]
}
}
// Override Fluent UI CSS (Not good way)
const style = theme => css`
width: ${theme.layout.leftWidth};
${onMobile} {
width: 100%;
}
margin-bottom: 30px;
margin-right: 10px;
.ms-Nav-compositeLink {
background: none;
a, a:visited{
color: ${theme.navigationSidebar.font.base};
}
&.is-selected button{
background: none;
}
&.is-selected {
background-color: ${theme.navigationSidebar.backgroundActive};
}
&:hover{
background-color: ${theme.navigationSidebar.backgroundHover};
}
}
.ms-Nav-chevronButton{
font-size: 16px;
color: ${theme.navigationSidebar.font.base};
// Not official css class, overriding from above.
&.is-group{
border-bottom: 1px solid ${theme.navigationSidebar.divider};
}
&:after{
border: none !important;
}
}
.ms-Nav-groupContent{
margin-left: 15px;
}
.ms-Nav-navItem .ms-Nav-link:hover{
color: ${theme.navigationSidebar.font.hover}
}
.ms-Nav-link{
background: none !important;
}
`;
const extractLevelGroupID = pathname => pathname.replace(/(^\/|\/$)/g, '').split('/');
const getDeepNavigation = (navItemGroup, navLinkGroup, pathname, deepLevel) => {
if(deepLevel > limitDeepLevel){
return [];
}
// Exit recursive when children.length = 0
navItemGroup.children.map( childNavItemGroup => {
const isAccessByUrl = childNavItemGroup.url.indexOf(extractLevelGroupID(pathname)[deepLevel]) > 0;
const navLinkGroupTmp = {
name: childNavItemGroup.title,
url: childNavItemGroup.url,
key: childNavItemGroup.url,
isExpanded: isAccessByUrl
}
const deepNavigationLinks = getDeepNavigation(childNavItemGroup, [], pathname, deepLevel + 1);
if(deepNavigationLinks.length > 0) {
navLinkGroupTmp.links = deepNavigationLinks;
}
navLinkGroup.push(navLinkGroupTmp);
});
return navLinkGroup;
}
const calculateNavigationFluentUI = (calculatedNavigation, pathname) => {
const navLinkGroups = [];
calculatedNavigation.children.map(sourceNavGroup => {
const isAccessByUrl =
sourceNavGroup.title === '' ? false :
sourceNavGroup.id !== extractLevelGroupID(pathname)[0];
const navLinkGroup = {
name: emoji.emojify(sourceNavGroup.title),
collapseByDefault: isAccessByUrl,
links: [],
key: sourceNavGroup.id,
}
navLinkGroup.links = getDeepNavigation(sourceNavGroup, [], pathname, 1);
navLinkGroups.push(navLinkGroup);
});
return navLinkGroups;
}
const ContentTree = ({ edges, location, menuOpen }) => {
const memoizedCalculatedNavigation = useMemo(() => calculateNavigation(edges));
const [navClickStatus, setNavClickStatus] = useState(false);
const [treeData, setTreeData] = useState(() => calculateNavigationFluentUI(memoizedCalculatedNavigation, location.pathname));
const theme = useTheme();
const onRenderLink = (link, linkRender) => {
return () => ({ link, linkRender });
};
const linkAs = props => {
// Ref: https://codesandbox.io/s/93olj3k1xr?file=/src/component/ContextMenuNav.js:2431-2435
return (
<Link id={props.link.key} to={props.link.url} {...props} >
{props.link.name}
</Link>
);
}
const handleLinkClick = (ev, link) => {
// Expand nav on clicking link
link.isExpanded = true;
// Debounce click nav state to prevent unnecessarily moving to active nav button
setNavClickStatus(true);
setTimeout(() => setNavClickStatus(false), 200);
};
const goToActiveNav = () => {
const sidebarElements = document.getElementsByClassName('ms-Nav-compositeLink is-selected');
if(sidebarElements.length > 0) {
sidebarElements[0].scrollIntoView({block: "center", inline: "nearest"});
}
}
useEffect(()=> {
goToActiveNav();
},[menuOpen]);
useEffect(()=> {
// Prevent unnecessarily moving to active nav button
if(!navClickStatus) {
// This active should be executed only other event route path changed, such as clicking item on search result.
// Make sure every time that the route path is changed, then go to the active Nav button.
syncFunction(() => setTreeData(calculateNavigationFluentUI(memoizedCalculatedNavigation, location.pathname)))
.then(() => goToActiveNav());
}
// When the route path is changed, scroll the content to top, such as clicking item on search result, clicking on nav menu.
document.getElementById('scroll-to-top-main-content')?.scrollTo(0,0);
},[location.pathname]);
return (
<>
<div css={style(theme)}>
<Nav
styles={navStyles}
ariaLabel="Nav menu"
groups={treeData}
linkAs={linkAs}
onRenderLink={onRenderLink}
onLinkClick={handleLinkClick}
selectedKey={location.pathname.replace(/\/$/, '')}
/>
</div>
</>
);
};
export default ContentTree;