@buerokratt-ria/menu 0.1.14 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +8 -29
- package/buerokratt-ria-menu-0.1.16.tgz +0 -0
- package/package.json +1 -1
- package/src/menu/components/menuTree/index.tsx +3 -1
- package/src/menu/hooks/useMenuItems.tsx +13 -12
- package/src/menu/index.tsx +10 -11
- package/src/menu/main-navigation.scss +7 -17
- package/buerokratt-ria-menu-0.1.14.tgz +0 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -34,35 +34,14 @@ To publish created package:
|
|
|
34
34
|
* `import { MainNavigation } from '@buerokratt-ria/menu/src'` for Header and Menu
|
|
35
35
|
* If you want to use local package, put created package to the root of react app and add dependency like "@buerokratt-ria/header": "file:buerokratt-ria-menu-0.0.5.tgz" (use proper version)
|
|
36
36
|
### Using MainNavigation component
|
|
37
|
-
*
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
setMainMenuItems(res);
|
|
46
|
-
localStorage.setItem(CACHE_NAME, JSON.stringify(res));
|
|
47
|
-
} catch (e) {
|
|
48
|
-
console.log(e);
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
onError: (error: any) => {
|
|
52
|
-
setMainMenuItems(getCache());
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
function getCache(): any {
|
|
58
|
-
const cache = localStorage.getItem(CACHE_NAME) || '{}';
|
|
59
|
-
return JSON.parse(cache);
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
* Then pass this MainMenu items to menu component `<MainNavigation serviceId={import.meta.env.REACT_APP_SERVICE_ID.split(',')} items={MainMenuItems}/>`
|
|
63
|
-
* If you want to use only local file provided by package then pass empty array instead `[]`
|
|
64
|
-
* REACT_APP_SERVICE_ID set of values seperated by comma resembling current module, examples of this value could
|
|
65
|
-
be found in following links
|
|
37
|
+
* MainNavigation uses four env variable:
|
|
38
|
+
* use `REACT_APP_MENU_URL` & `REACT_APP_MENU_PATH` to set the url for json that contains the menu item array
|
|
39
|
+
* use `REACT_APP_MENU_JSON` with json string of the menu item array
|
|
40
|
+
* `REACT_APP_MENU_JSON` will overrides buth the local file provieded by package and the `REACT_APP_MENU_URL` & `REACT_APP_MENU_PATH`
|
|
41
|
+
* If you want to use only local file provided by package then remove `REACT_APP_MENU_JSON`, `REACT_APP_MENU_URL` & `REACT_APP_MENU_PATH` variables
|
|
42
|
+
* `REACT_APP_SERVICE_ID` set of values seperated by comma resembling current module, examples of this value could be found in following links
|
|
43
|
+
|
|
44
|
+
|
|
66
45
|
|
|
67
46
|
### Implemented examples:
|
|
68
47
|
* https://github.com/buerokratt/Training-Module
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -22,7 +22,9 @@ const MenuTree: FC<MenuTreeProps> = ({
|
|
|
22
22
|
}) => {
|
|
23
23
|
const currentlySelectedLanguage = useTranslation().i18n.language;
|
|
24
24
|
|
|
25
|
-
return menuItems
|
|
25
|
+
return menuItems
|
|
26
|
+
.filter(x => !x.hidden)
|
|
27
|
+
.map((menuItem) => (
|
|
26
28
|
<li key={menuItem.label[currentlySelectedLanguage]}>
|
|
27
29
|
{!!menuItem.children ? (
|
|
28
30
|
<>
|
|
@@ -5,27 +5,22 @@ import { MenuItem } from "../types/menuItem";
|
|
|
5
5
|
|
|
6
6
|
const useMenuItems = () => {
|
|
7
7
|
const externalMenuItems = import.meta.env.REACT_APP_MENU_JSON;
|
|
8
|
-
|
|
9
|
-
const [mainMenuItems, setMainMenuItems] = useState<MenuItem[] | null>([]);
|
|
8
|
+
const cachedMenu = getMenuCache();
|
|
9
|
+
const [mainMenuItems, setMainMenuItems] = useState<MenuItem[] | null>(cachedMenu ?? []);
|
|
10
10
|
|
|
11
11
|
useQuery({
|
|
12
|
-
enabled: !externalMenuItems,
|
|
12
|
+
enabled: !externalMenuItems && !cachedMenu,
|
|
13
13
|
queryKey: [import.meta.env.REACT_APP_MENU_URL + import.meta.env.REACT_APP_MENU_PATH],
|
|
14
14
|
onSuccess: (res: any) => {
|
|
15
15
|
try {
|
|
16
16
|
setMainMenuItems(res);
|
|
17
|
+
setCache(res);
|
|
17
18
|
} catch (e) {
|
|
18
|
-
setMainMenuItems(null);
|
|
19
19
|
console.error(e);
|
|
20
20
|
}
|
|
21
|
-
setCache(res);
|
|
22
21
|
},
|
|
23
22
|
onError: () => {
|
|
24
|
-
|
|
25
|
-
if(cached.length > 0)
|
|
26
|
-
setMainMenuItems(cached);
|
|
27
|
-
else
|
|
28
|
-
setMainMenuItems(null);
|
|
23
|
+
setMainMenuItems(cachedMenu);
|
|
29
24
|
},
|
|
30
25
|
});
|
|
31
26
|
|
|
@@ -43,13 +38,19 @@ const useMenuItems = () => {
|
|
|
43
38
|
console.warn(e);
|
|
44
39
|
}
|
|
45
40
|
|
|
46
|
-
|
|
47
|
-
return allItems.filter(x => !x.hidden);
|
|
41
|
+
return externals ?? mainMenuItems ?? menuStructure ?? [];
|
|
48
42
|
}, [externalMenuItems, mainMenuItems, menuStructure]);
|
|
49
43
|
|
|
50
44
|
return items;
|
|
51
45
|
}
|
|
52
46
|
|
|
47
|
+
function getMenuCache(): any {
|
|
48
|
+
const cached = getCache();
|
|
49
|
+
if(Array.isArray(cached) && cached.length > 0)
|
|
50
|
+
return cached;
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
53
54
|
function getCache(): any {
|
|
54
55
|
const cache = localStorage.getItem('mainmenu-cache') ?? '[]';
|
|
55
56
|
return JSON.parse(cache);
|
package/src/menu/index.tsx
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import React, { FC, MouseEvent, useState, useMemo } from 'react';
|
|
2
2
|
import clsx from 'clsx';
|
|
3
|
-
import {
|
|
4
|
-
MdKeyboardArrowRight,
|
|
5
|
-
MdKeyboardArrowLeft,
|
|
6
|
-
} from 'react-icons/md';
|
|
3
|
+
import { MdClose } from 'react-icons/md';
|
|
7
4
|
import Icon from './components/icons/icon/icon';
|
|
8
5
|
import { useTranslation } from "react-i18next";
|
|
9
6
|
import MenuTree from './components/menuTree';
|
|
@@ -23,17 +20,19 @@ const MainNavigation: FC = () => {
|
|
|
23
20
|
event.currentTarget.setAttribute('aria-expanded', isExpanded ? 'false' : 'true');
|
|
24
21
|
};
|
|
25
22
|
|
|
23
|
+
const handleCloseButtonClick = () => {
|
|
24
|
+
const doesMenuHasExpandedItem = !!document.querySelector('button[aria-expanded="true"]');
|
|
25
|
+
if(doesMenuHasExpandedItem)
|
|
26
|
+
setNavCollapsed(!navCollapsed);
|
|
27
|
+
}
|
|
28
|
+
|
|
26
29
|
if (!menuItems) return null;
|
|
27
30
|
|
|
28
31
|
return (
|
|
29
32
|
<nav className={clsx('nav', { 'collapsed': navCollapsed })}>
|
|
30
|
-
<button className='nav__menu-toggle close-button-item' onClick={
|
|
31
|
-
{
|
|
32
|
-
|
|
33
|
-
? <Icon icon={<MdKeyboardArrowRight className='menu-item-icon' />} size='large' />
|
|
34
|
-
: <Icon icon={<MdKeyboardArrowLeft className='menu-item-icon' />} size='large' />
|
|
35
|
-
}
|
|
36
|
-
<span className='menu-item-title'>{t('mainMenu.closeMenu')}</span>
|
|
33
|
+
<button className='nav__menu-toggle close-button-item' onClick={handleCloseButtonClick}>
|
|
34
|
+
<Icon icon={<MdClose />} />
|
|
35
|
+
<span className='menu-item-title'>{t(navCollapsed ? 'mainMenu.openMenu' : 'mainMenu.closeMenu' )}</span>
|
|
37
36
|
</button>
|
|
38
37
|
<ul className='nav__menu'>
|
|
39
38
|
<MenuTree
|
|
@@ -116,24 +116,14 @@
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
.collapsed {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
.menu-item-title {
|
|
123
|
-
display: none !important;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
.menu-item-icon {
|
|
127
|
-
height: 24px;
|
|
128
|
-
width: 24px;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
.close-button-item,
|
|
132
|
-
button.nav__toggle {
|
|
133
|
-
justify-content: center;
|
|
119
|
+
.nav__submenu {
|
|
120
|
+
visibility: hidden;
|
|
121
|
+
height: 0;
|
|
134
122
|
}
|
|
135
123
|
|
|
136
|
-
|
|
137
|
-
|
|
124
|
+
button[aria-expanded=true] {
|
|
125
|
+
.icon {
|
|
126
|
+
transform: rotate(0deg);
|
|
127
|
+
}
|
|
138
128
|
}
|
|
139
129
|
}
|
|
Binary file
|