asab_webui_shell 27.8.3 → 27.8.4

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.
@@ -12,6 +12,7 @@ var _reactI18next = require("react-i18next");
12
12
  var _reactstrap = require("reactstrap");
13
13
  var _asab_webui_components = require("asab_webui_components");
14
14
  var _SidebarIcon = _interopRequireDefault(require("./SidebarIcon"));
15
+ var _sidebarRouteMatch = require("./utils/sidebarRouteMatch.js");
15
16
  var cache;
16
17
  function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var r, c, a = Object.defineProperty, n = { __proto__: null, default: e }; if (Object(e) !== e) return n; if (cache || "function" != typeof WeakMap || (cache = new WeakMap()), cache) { if (cache.has(e)) return cache.get(e); cache.set(e, n); } for (c in e) "default" !== c && {}.hasOwnProperty.call(e, c) && ((r = a && Object.getOwnPropertyDescriptor(e, c)) && (r.get || r.set) ? a(n, c, r) : n[c] = e[c]); return n; }
17
18
  var SidebarItem = _ref => {
@@ -35,15 +36,19 @@ var SidebarItem = _ref => {
35
36
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
36
37
  isActive = _useState2[0],
37
38
  setActive = _useState2[1];
39
+ // Collapse parent only after a real navigation (not on mount / Strict Mode double-invoke)
40
+ var prevPathname = (0, _react.useRef)(location.pathname);
38
41
  var lowercasedItemName = item === null || item === void 0 || (_item$name = item.name) === null || _item$name === void 0 ? void 0 : _item$name.toLowerCase();
39
42
  // Attention requred flag
40
43
  var itemBeacon = beacon === null || beacon === void 0 ? void 0 : beacon["beacon.".concat(lowercasedItemName)];
41
44
  (0, _react.useEffect)(() => {
42
45
  // TODO: refactor the handling of active and open states, since it does not behave as expected in some cases
43
- if (isOpen && !isActive && !matchesOpenFor(location.pathname, openFor)) {
46
+ var pathnameChanged = prevPathname.current !== location.pathname;
47
+ prevPathname.current = location.pathname;
48
+ if (pathnameChanged && isOpen && !isActive && !(0, _sidebarRouteMatch.matchesOpenFor)(location.pathname, openFor)) {
44
49
  setOpen(false);
45
50
  }
46
- setActive(Boolean(item.url && (location.pathname === item.url || location.pathname.startsWith(item.url + '/'))) || matchesOpenFor(location.pathname, item.openFor));
51
+ setActive(Boolean(item.url && (location.pathname === item.url || location.pathname.startsWith(item.url + '/'))) || (0, _sidebarRouteMatch.matchesOpenFor)(location.pathname, item.openFor));
47
52
  }, [location]);
48
53
  return /*#__PURE__*/_react.default.createElement("div", {
49
54
  className: "nav-item"
@@ -81,8 +86,10 @@ var SidebarCollapsibleItem = _ref2 => {
81
86
  disabled = _ref2.disabled,
82
87
  isSmallResolution = _ref2.isSmallResolution,
83
88
  _ref2$beacon = _ref2.beacon,
84
- beacon = _ref2$beacon === void 0 ? undefined : _ref2$beacon;
85
- var _useState3 = (0, _react.useState)(false),
89
+ beacon = _ref2$beacon === void 0 ? undefined : _ref2$beacon,
90
+ _ref2$defaultOpen = _ref2.defaultOpen,
91
+ defaultOpen = _ref2$defaultOpen === void 0 ? false : _ref2$defaultOpen;
92
+ var _useState3 = (0, _react.useState)(defaultOpen),
86
93
  _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
87
94
  isOpen = _useState4[0],
88
95
  setOpen = _useState4[1];
@@ -104,17 +111,8 @@ var SidebarCollapsibleItem = _ref2 => {
104
111
 
105
112
  // Should collapsed item uncollapse
106
113
  (0, _react.useEffect)(() => {
107
- if (item.children && !isOpen) {
108
- if (matchesOpenFor(location.pathname, item.openFor)) {
109
- setOpen(true);
110
- return;
111
- }
112
- for (var child of item.children) {
113
- if (location.pathname.includes(child.url)) {
114
- setOpen(true);
115
- break;
116
- }
117
- }
114
+ if (item.children && !isOpen && (0, _sidebarRouteMatch.collapsibleMatchesRoute)(location.pathname, item)) {
115
+ setOpen(true);
118
116
  }
119
117
  }, [isOpen]);
120
118
  var onClickOpen = e => {
@@ -193,16 +191,4 @@ var childBeacon = (data, key) => {
193
191
  return data[key] ? {
194
192
  [key]: data[key]
195
193
  } : {};
196
- };
197
-
198
- // Optional item.openFor patterns, e.g. ['/route/*'] - keeps parent open on matching routes
199
- var matchesOpenFor = (pathname, patterns) => {
200
- var _patterns$some;
201
- return (_patterns$some = patterns === null || patterns === void 0 ? void 0 : patterns.some(p => {
202
- if (p.endsWith('*')) {
203
- var prefix = p.slice(0, -1);
204
- return pathname.startsWith(prefix) || pathname === prefix.slice(0, -1);
205
- }
206
- return pathname === p || pathname.startsWith(p + '/');
207
- })) !== null && _patterns$some !== void 0 ? _patterns$some : false;
208
194
  };
@@ -6,7 +6,9 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.SidebarItemRenderer = void 0;
8
8
  var _react = _interopRequireDefault(require("react"));
9
+ var _reactRouter = require("react-router");
9
10
  var _SidebarItem = require("./SidebarItem");
11
+ var _sidebarRouteMatch = require("./utils/sidebarRouteMatch.js");
10
12
  var SidebarItemRenderer = _ref => {
11
13
  var sidebarItems = _ref.sidebarItems,
12
14
  sessionExpired = _ref.sessionExpired,
@@ -14,6 +16,12 @@ var SidebarItemRenderer = _ref => {
14
16
  isSmallResolution = _ref$isSmallResolutio === void 0 ? undefined : _ref$isSmallResolutio,
15
17
  _ref$beacon = _ref.beacon,
16
18
  beacon = _ref$beacon === void 0 ? undefined : _ref$beacon;
19
+ var location = (0, _reactRouter.useLocation)();
20
+ /*
21
+ Default-open the first collapsible unless some other sidebar item already owns the route.
22
+ If the first item itself matches (e.g. its child), start open to avoid a closed-then-open flash.
23
+ */
24
+ var otherItemOwnsRoute = sidebarItems === null || sidebarItems === void 0 ? void 0 : sidebarItems.slice(1).some(item => (0, _sidebarRouteMatch.navItemMatchesRoute)(location.pathname, item));
17
25
  return /*#__PURE__*/_react.default.createElement("nav", {
18
26
  className: "nav flex-column"
19
27
  }, sidebarItems.map((item, idx) => item.children ? /*#__PURE__*/_react.default.createElement(_SidebarItem.SidebarCollapsibleItem, {
@@ -21,7 +29,8 @@ var SidebarItemRenderer = _ref => {
21
29
  item: item,
22
30
  disabled: sessionExpired,
23
31
  isSmallResolution: isSmallResolution,
24
- beacon: beacon
32
+ beacon: beacon,
33
+ defaultOpen: idx === 0 && !otherItemOwnsRoute
25
34
  }) : /*#__PURE__*/_react.default.createElement(_SidebarItem.SidebarItem, {
26
35
  key: idx,
27
36
  item: item,
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.navItemMatchesRoute = exports.matchesOpenFor = exports.collapsibleMatchesRoute = void 0;
7
+ // Optional item.openFor patterns, e.g. ['/route/*'] - keeps parent open on matching routes
8
+ var matchesOpenFor = (pathname, patterns) => {
9
+ var _patterns$some;
10
+ return (_patterns$some = patterns === null || patterns === void 0 ? void 0 : patterns.some(p => {
11
+ if (p.endsWith('*')) {
12
+ var prefix = p.slice(0, -1);
13
+ return pathname.startsWith(prefix) || pathname === prefix.slice(0, -1);
14
+ }
15
+ return pathname === p || pathname.startsWith(p + '/');
16
+ })) !== null && _patterns$some !== void 0 ? _patterns$some : false;
17
+ };
18
+ exports.matchesOpenFor = matchesOpenFor;
19
+ var collapsibleMatchesRoute = (pathname, item) => {
20
+ if (!(item !== null && item !== void 0 && item.children)) {
21
+ return false;
22
+ }
23
+ if (matchesOpenFor(pathname, item.openFor)) {
24
+ return true;
25
+ }
26
+ return item.children.some(child => child.url && pathname.includes(child.url));
27
+ };
28
+ exports.collapsibleMatchesRoute = collapsibleMatchesRoute;
29
+ var navItemMatchesRoute = (pathname, item) => {
30
+ if (item !== null && item !== void 0 && item.children) {
31
+ return collapsibleMatchesRoute(pathname, item);
32
+ }
33
+ return Boolean((item === null || item === void 0 ? void 0 : item.url) && (pathname === item.url || pathname.startsWith(item.url + '/'))) || matchesOpenFor(pathname, item === null || item === void 0 ? void 0 : item.openFor);
34
+ };
35
+ exports.navItemMatchesRoute = navItemMatchesRoute;
@@ -0,0 +1,27 @@
1
+ // Optional item.openFor patterns, e.g. ['/route/*'] - keeps parent open on matching routes
2
+ export const matchesOpenFor = (pathname, patterns) =>
3
+ patterns?.some((p) => {
4
+ if (p.endsWith('*')) {
5
+ const prefix = p.slice(0, -1);
6
+ return pathname.startsWith(prefix) || pathname === prefix.slice(0, -1);
7
+ }
8
+ return pathname === p || pathname.startsWith(p + '/');
9
+ }) ?? false;
10
+
11
+ export const collapsibleMatchesRoute = (pathname, item) => {
12
+ if (!item?.children) {
13
+ return false;
14
+ }
15
+ if (matchesOpenFor(pathname, item.openFor)) {
16
+ return true;
17
+ }
18
+ return item.children.some((child) => child.url && pathname.includes(child.url));
19
+ };
20
+
21
+ export const navItemMatchesRoute = (pathname, item) => {
22
+ if (item?.children) {
23
+ return collapsibleMatchesRoute(pathname, item);
24
+ }
25
+ return Boolean(item?.url && (pathname === item.url || pathname.startsWith(item.url + '/')))
26
+ || matchesOpenFor(pathname, item?.openFor);
27
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asab_webui_shell",
3
- "version": "27.8.3",
3
+ "version": "27.8.4",
4
4
  "license": "BSD-3-Clause",
5
5
  "description": "TeskaLabs ASAB WebUI Shell Application",
6
6
  "contributors": [