-
Notifications
You must be signed in to change notification settings - Fork 30
/
kibana_collapsible_nav.tsx
269 lines (250 loc) · 7.61 KB
/
kibana_collapsible_nav.tsx
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
import { useState } from 'react';
import {
EuiCollapsibleNav,
EuiCollapsibleNavGroup,
EuiHeaderSectionItemButton,
EuiHeaderLogo,
EuiHeader,
EuiIcon,
EuiPinnableListGroup,
EuiPinnableListGroupItemProps,
EuiFlexItem,
EuiHorizontalRule,
EuiListGroup,
useGeneratedHtmlId,
EuiAvatar,
} from '@elastic/eui';
import find from 'lodash/find';
import findIndex from 'lodash/findIndex';
import { css } from '@emotion/react';
import ThemeSwitcher from '../components/chrome/theme_switcher';
const pathPrefix = process.env.PATH_PREFIX;
const TopLinks: EuiPinnableListGroupItemProps[] = [
{
label: 'Home',
iconType: 'home',
isActive: true,
'aria-current': true,
href: `${pathPrefix}/kibana`,
pinnable: false,
},
];
const KibanaLinks: EuiPinnableListGroupItemProps[] = [
{ label: 'Discover', href: `${pathPrefix}/kibana/discover` },
{ label: 'Dashboard', href: `${pathPrefix}/kibana/dashboards` },
{ label: 'Maps', href: `${pathPrefix}/kibana/maps` },
];
const CollapsibleNav = () => {
const [navIsOpen, setNavIsOpen] = useState(false);
const breadcrumbs = [
{
text: 'Home',
},
];
/**
* Accordion toggling
*/
const [openGroups, setOpenGroups] = useState(['Kibana']);
// Save which groups are open and which are not with state and local store
const toggleAccordion = (isOpen: boolean, title?: string) => {
if (!title) return;
const itExists = openGroups.includes(title);
if (isOpen) {
if (itExists) return;
openGroups.push(title);
} else {
const index = openGroups.indexOf(title);
if (index > -1) {
openGroups.splice(index, 1);
}
}
setOpenGroups([...openGroups]);
localStorage.setItem('openNavGroups', JSON.stringify(openGroups));
};
/**
* Pinning
*/
const [pinnedItems, setPinnedItems] = useState<
EuiPinnableListGroupItemProps[]
>([]);
const addPin = (item: any) => {
if (!item || find(pinnedItems, { label: item.label })) {
return;
}
item.pinned = true;
const newPinnedItems = pinnedItems ? pinnedItems.concat(item) : [item];
setPinnedItems(newPinnedItems);
localStorage.setItem('pinnedItems', JSON.stringify(newPinnedItems));
};
const removePin = (item: any) => {
const pinIndex = findIndex(pinnedItems, { label: item.label });
if (pinIndex > -1) {
item.pinned = false;
const newPinnedItems = pinnedItems;
newPinnedItems.splice(pinIndex, 1);
setPinnedItems([...newPinnedItems]);
localStorage.setItem('pinnedItems', JSON.stringify(newPinnedItems));
}
};
function alterLinksWithCurrentState(
links: EuiPinnableListGroupItemProps[],
showPinned = false
): EuiPinnableListGroupItemProps[] {
return links.map(link => {
const { pinned, ...rest } = link;
return {
pinned: showPinned ? pinned : false,
...rest,
};
});
}
function addLinkNameToPinTitle(listItem: EuiPinnableListGroupItemProps) {
return `Pin ${listItem.label} to top`;
}
function addLinkNameToUnpinTitle(listItem: EuiPinnableListGroupItemProps) {
return `Unpin ${listItem.label}`;
}
const collapsibleNavId = useGeneratedHtmlId({ prefix: 'collapsibleNav' });
const collapsibleNav = (
<EuiCollapsibleNav
ownFocus={false}
css={css`
margin-top: 96px; // two top navs
min-height: calc(100vh - 96px);
display: flex;
`}
id={collapsibleNavId}
aria-label="Main navigation"
isOpen={navIsOpen}
button={
<EuiHeaderSectionItemButton
aria-label="Toggle main navigation"
onClick={() => setNavIsOpen(!navIsOpen)}>
<EuiIcon type={'menu'} size="m" aria-hidden="true" />
</EuiHeaderSectionItemButton>
}
onClose={() => setNavIsOpen(false)}>
{/* Dark deployments section */}
<EuiFlexItem grow={false} style={{ flexShrink: 0 }}>
<EuiCollapsibleNavGroup isCollapsible={false} background="dark">
<EuiListGroup
color="ghost"
maxWidth="none"
gutterSize="none"
size="s"
listItems={[
{
label: 'Manage deployment',
href: '#',
iconType: 'logoCloud',
iconProps: {
color: 'ghost',
},
},
]}
/>
</EuiCollapsibleNavGroup>
</EuiFlexItem>
{/* Shaded pinned section always with a home item */}
<EuiFlexItem grow={false}>
<EuiCollapsibleNavGroup background="light">
<EuiPinnableListGroup
aria-label="Pinned links" // A11y : Since this group doesn't have a visible `title` it should be provided an accessible description
listItems={alterLinksWithCurrentState(TopLinks).concat(
alterLinksWithCurrentState(pinnedItems, true)
)}
unpinTitle={addLinkNameToUnpinTitle}
onPinClick={removePin}
maxWidth="none"
color="text"
gutterSize="none"
size="s"
/>
</EuiCollapsibleNavGroup>
</EuiFlexItem>
<EuiHorizontalRule margin="none" />
{/* Menu items */}
<EuiFlexItem className="eui-yScroll">
<EuiCollapsibleNavGroup
title={
<a
className="eui-textInheritColor"
href="#/navigation/collapsible-nav"
onClick={e => e.stopPropagation()}>
Analytics
</a>
}
buttonElement="div"
iconType="logoKibana"
isCollapsible={true}
initialIsOpen={openGroups.includes('Kibana')}
onToggle={(isOpen: boolean) => toggleAccordion(isOpen, 'Kibana')}>
<EuiPinnableListGroup
aria-label="Kibana" // A11y : EuiCollapsibleNavGroup can't correctly pass the `title` as the `aria-label` to the right HTML element, so it must be added manually
listItems={alterLinksWithCurrentState(KibanaLinks)}
pinTitle={addLinkNameToPinTitle}
onPinClick={addPin}
maxWidth="none"
color="subdued"
gutterSize="none"
size="s"
/>
</EuiCollapsibleNavGroup>
</EuiFlexItem>
</EuiCollapsibleNav>
);
const leftSectionItems = [collapsibleNav];
return (
<>
<EuiHeader
theme="dark"
position="fixed"
sections={[
{
items: [
<EuiHeaderLogo
key="elastic-logo"
iconType="logoElastic"
href={`${pathPrefix}/kibana`}>
Elastic
</EuiHeaderLogo>,
],
borders: 'none',
},
{
items: [
<ThemeSwitcher key={useGeneratedHtmlId()} />,
<EuiHeaderSectionItemButton
key={useGeneratedHtmlId()}
aria-label="Account menu">
<EuiAvatar name="John Username" size="s" />
</EuiHeaderSectionItemButton>,
],
borders: 'none',
},
]}
/>
<EuiHeader
position="fixed"
sections={[
{
items: leftSectionItems,
borders: 'right',
},
{
items: [
<EuiHeaderSectionItemButton
key={useGeneratedHtmlId()}
aria-label="Account menu">
<EuiAvatar type="space" name="Default Space" size="s" />
</EuiHeaderSectionItemButton>,
],
breadcrumbs: breadcrumbs,
borders: 'right',
},
]}
/>
</>
);
};
export default CollapsibleNav;