@carbon-labs/react-ui-shell 0.7.0 → 0.9.0
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/es/components/SideNav.js +131 -3
- package/es/components/SideNavItems.d.ts +24 -0
- package/es/components/SideNavItems.js +61 -0
- package/es/components/SideNavMenu.d.ts +49 -0
- package/es/components/SideNavMenu.js +274 -0
- package/es/components/SideNavMenuItem.d.ts +38 -0
- package/es/components/SideNavMenuItem.js +84 -0
- package/es/index.d.ts +4 -1
- package/es/index.js +4 -1
- package/es/node_modules/@carbon/icons-react/es/generated/bucket-3.js +3119 -0
- package/lib/components/SideNav.js +129 -1
- package/lib/components/SideNavItems.d.ts +24 -0
- package/lib/components/SideNavItems.js +63 -0
- package/lib/components/SideNavMenu.d.ts +49 -0
- package/lib/components/SideNavMenu.js +295 -0
- package/lib/components/SideNavMenuItem.d.ts +38 -0
- package/lib/components/SideNavMenuItem.js +86 -0
- package/lib/index.d.ts +4 -1
- package/lib/index.js +7 -0
- package/lib/node_modules/@carbon/icons-react/es/generated/bucket-3.js +3245 -0
- package/package.json +2 -2
- package/scss/styles/_side-nav.scss +18 -0
|
@@ -142,6 +142,39 @@ function SideNavRenderFunction(_ref, ref) {
|
|
|
142
142
|
return child;
|
|
143
143
|
});
|
|
144
144
|
const eventHandlers = {};
|
|
145
|
+
const treeWalkerRef = React.useRef(null);
|
|
146
|
+
React.useEffect(() => {
|
|
147
|
+
treeWalkerRef.current = treeWalkerRef.current ?? document.createTreeWalker(sideNavRef?.current, NodeFilter.SHOW_ELEMENT, {
|
|
148
|
+
acceptNode: function (node) {
|
|
149
|
+
if (!(node instanceof Element)) {
|
|
150
|
+
return NodeFilter.FILTER_SKIP;
|
|
151
|
+
}
|
|
152
|
+
if (node.classList.contains(`${prefix}--side-nav__divider`)) {
|
|
153
|
+
return NodeFilter.FILTER_REJECT;
|
|
154
|
+
}
|
|
155
|
+
if (node.matches(`li.${prefix}--side-nav__item`) || node.matches(`li.${prefix}--side-nav__menu-item`)) {
|
|
156
|
+
return NodeFilter.FILTER_ACCEPT;
|
|
157
|
+
}
|
|
158
|
+
return NodeFilter.FILTER_SKIP;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
resetNodeTabIndices();
|
|
162
|
+
const firstElement = sideNavRef?.current?.querySelector('a, button');
|
|
163
|
+
if (firstElement) {
|
|
164
|
+
firstElement.tabIndex = 0;
|
|
165
|
+
}
|
|
166
|
+
}, [prefix]);
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Returns the parent SideNavMenu, if node is actually inside one.
|
|
170
|
+
* @param node
|
|
171
|
+
* @returns parent side nav menu node
|
|
172
|
+
*/
|
|
173
|
+
function parentSideNavMenu(node) {
|
|
174
|
+
const parentNode = node.parentElement?.closest(`.${prefix}--side-nav__item`);
|
|
175
|
+
if (parentNode) return parentNode;
|
|
176
|
+
return node;
|
|
177
|
+
}
|
|
145
178
|
if (addFocusListeners) {
|
|
146
179
|
eventHandlers.onFocus = event => {
|
|
147
180
|
if (!event.currentTarget.contains(event.relatedTarget) && isRail) {
|
|
@@ -162,7 +195,94 @@ function SideNavRenderFunction(_ref, ref) {
|
|
|
162
195
|
}
|
|
163
196
|
};
|
|
164
197
|
eventHandlers.onKeyDown = event => {
|
|
198
|
+
if (!treeWalkerRef.current) return;
|
|
199
|
+
const treeWalker = treeWalkerRef.current;
|
|
200
|
+
event.stopPropagation();
|
|
201
|
+
|
|
202
|
+
// stops page from scrolling
|
|
203
|
+
if (match.matches(event, [keys__namespace.ArrowUp, keys__namespace.ArrowDown, keys__namespace.Home, keys__namespace.End,
|
|
204
|
+
// @ts-ignore - `matches` doesn't like the object syntax without missing properties
|
|
205
|
+
{
|
|
206
|
+
code: 'KeyA'
|
|
207
|
+
}])) {
|
|
208
|
+
event.preventDefault();
|
|
209
|
+
}
|
|
210
|
+
treeWalker.currentNode = event.target.closest(`li`) ?? treeWalker?.currentNode;
|
|
211
|
+
let nextFocusNode = null;
|
|
212
|
+
if (match.match(event, keys__namespace.ArrowUp)) {
|
|
213
|
+
const parentNode = parentSideNavMenu(treeWalker.currentNode);
|
|
214
|
+
let previousSideNavMenu = parentNode?.previousElementSibling;
|
|
215
|
+
|
|
216
|
+
// skip the divider
|
|
217
|
+
if (previousSideNavMenu?.classList.contains(`${prefix}--side-nav__divider`)) {
|
|
218
|
+
previousSideNavMenu = previousSideNavMenu?.previousElementSibling;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// when previous sibling is open, go to its last item
|
|
222
|
+
if (previousSideNavMenu?.getAttribute('aria-expanded') == 'true') {
|
|
223
|
+
nextFocusNode = treeWalker.previousNode();
|
|
224
|
+
} else {
|
|
225
|
+
nextFocusNode = treeWalker.previousSibling();
|
|
226
|
+
|
|
227
|
+
// first item in the menu, go back up to SideNavMenu button
|
|
228
|
+
if (nextFocusNode == null) {
|
|
229
|
+
nextFocusNode = parentNode;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (match.match(event, keys__namespace.ArrowDown)) {
|
|
234
|
+
if (treeWalker.currentNode.getAttribute('aria-expanded') == 'false') {
|
|
235
|
+
nextFocusNode = treeWalker.nextSibling();
|
|
236
|
+
} else {
|
|
237
|
+
nextFocusNode = treeWalker.nextNode();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Home/End functionality
|
|
242
|
+
if (match.matches(event, [keys__namespace.Home, keys__namespace.End])) {
|
|
243
|
+
if (!sideNavRef?.current) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const allItems = Array.from(sideNavRef.current.querySelectorAll('a, button'));
|
|
247
|
+
if (match.match(event, keys__namespace.Home)) {
|
|
248
|
+
const firstElement = allItems[0];
|
|
249
|
+
if (firstElement) {
|
|
250
|
+
firstElement.tabIndex = 0;
|
|
251
|
+
firstElement?.focus();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (match.match(event, keys__namespace.End)) {
|
|
255
|
+
const allItems = Array.from(sideNavRef.current.querySelectorAll('li'));
|
|
256
|
+
const lastVisibleItem = allItems.reverse().find(item => getComputedStyle(item).visibility !== 'hidden');
|
|
257
|
+
if (lastVisibleItem) {
|
|
258
|
+
const node = lastVisibleItem.querySelector('button') ?? lastVisibleItem.querySelector('a');
|
|
259
|
+
if (node) {
|
|
260
|
+
node.tabIndex = 0;
|
|
261
|
+
node?.focus();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// focus on the focusable element within the node
|
|
268
|
+
if (nextFocusNode && nextFocusNode !== event.target) {
|
|
269
|
+
resetNodeTabIndices();
|
|
270
|
+
if (nextFocusNode instanceof HTMLElement) {
|
|
271
|
+
const node = nextFocusNode.querySelector('button') ?? nextFocusNode.querySelector('a');
|
|
272
|
+
if (node) {
|
|
273
|
+
node.tabIndex = 0;
|
|
274
|
+
node?.focus();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// close menu
|
|
165
280
|
if (match.match(event, keys__namespace.Escape)) {
|
|
281
|
+
if (expanded && !isFixedNav) {
|
|
282
|
+
if (onSideNavBlur) {
|
|
283
|
+
onSideNavBlur();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
166
286
|
handleToggle(event, false);
|
|
167
287
|
if (href) {
|
|
168
288
|
window.location.href = href;
|
|
@@ -188,12 +308,19 @@ function SideNavRenderFunction(_ref, ref) {
|
|
|
188
308
|
}
|
|
189
309
|
useEvent.useWindowEvent('keydown', event => {
|
|
190
310
|
const focusedElement = document.activeElement;
|
|
191
|
-
|
|
311
|
+
|
|
312
|
+
// going from header menu to sideNav
|
|
313
|
+
if (match.match(event, keys__namespace.Tab) && expanded && !isFixedNav && sideNavRef?.current && focusedElement?.classList.contains(`${prefix}--header__menu-toggle`) && !focusedElement.closest('nav')) {
|
|
192
314
|
sideNavRef.current.focus();
|
|
193
315
|
}
|
|
194
316
|
});
|
|
195
317
|
const lgMediaQuery = `(min-width: ${index$1.breakpoints.lg.width})`;
|
|
196
318
|
const isLg = useMatchMedia.useMatchMedia(lgMediaQuery);
|
|
319
|
+
function resetNodeTabIndices() {
|
|
320
|
+
Array.prototype.forEach.call(sideNavRef?.current?.querySelectorAll('[tabIndex="0"]') ?? [], item => {
|
|
321
|
+
item.tabIndex = -1;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
197
324
|
return /*#__PURE__*/React.createElement(SideNavContext.Provider, {
|
|
198
325
|
value: {
|
|
199
326
|
isRail
|
|
@@ -205,6 +332,7 @@ function SideNavRenderFunction(_ref, ref) {
|
|
|
205
332
|
className: overlayClassName,
|
|
206
333
|
onClick: onOverlayClick
|
|
207
334
|
}), /*#__PURE__*/React.createElement("nav", _rollupPluginBabelHelpers.extends({
|
|
335
|
+
role: "tree",
|
|
208
336
|
tabIndex: -1,
|
|
209
337
|
ref: navRef,
|
|
210
338
|
className: `${prefix}--side-nav__navigation ${className}`,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright IBM Corp. 2025
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import React from 'react';
|
|
8
|
+
export interface SideNavItemsProps {
|
|
9
|
+
/**
|
|
10
|
+
* Provide a single icon as the child to `SideNavIcon` to render in the
|
|
11
|
+
* container
|
|
12
|
+
*/
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* Provide an optional class to be applied to the containing node
|
|
16
|
+
*/
|
|
17
|
+
className?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Property to indicate if the side nav container is open (or not). Use to
|
|
20
|
+
* keep local state and styling in step with the SideNav expansion state.
|
|
21
|
+
*/
|
|
22
|
+
isSideNavExpanded?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export declare const SideNavItems: React.FC<SideNavItemsProps>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright IBM Corp. 2024
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
var index = require('../_virtual/index.js');
|
|
11
|
+
var PropTypes = require('prop-types');
|
|
12
|
+
var React = require('react');
|
|
13
|
+
var _utils = require('./_utils.js');
|
|
14
|
+
var usePrefix = require('@carbon/react/lib/internal/usePrefix');
|
|
15
|
+
|
|
16
|
+
const SideNavItems = _ref => {
|
|
17
|
+
let {
|
|
18
|
+
className: customClassName,
|
|
19
|
+
children,
|
|
20
|
+
isSideNavExpanded
|
|
21
|
+
} = _ref;
|
|
22
|
+
const prefix = usePrefix.usePrefix();
|
|
23
|
+
const className = index.default([`${prefix}--side-nav__items`], customClassName);
|
|
24
|
+
const childrenWithExpandedState = React.Children.map(children, child => {
|
|
25
|
+
if (/*#__PURE__*/React.isValidElement(child)) {
|
|
26
|
+
// avoid spreading `isSideNavExpanded` to non-Carbon UI Shell children
|
|
27
|
+
const childJsxElement = child;
|
|
28
|
+
const childDisplayName = childJsxElement.type?.displayName ?? childJsxElement.type?.name;
|
|
29
|
+
const isCarbonSideNavItem = _utils.CARBON_SIDENAV_ITEMS.includes(childDisplayName);
|
|
30
|
+
const isSideNavMenu = childDisplayName === 'SideNavMenu';
|
|
31
|
+
return /*#__PURE__*/React.cloneElement(child, {
|
|
32
|
+
...(isCarbonSideNavItem && {
|
|
33
|
+
isSideNavExpanded: isSideNavExpanded
|
|
34
|
+
}),
|
|
35
|
+
...(isSideNavMenu && {
|
|
36
|
+
depth: 0
|
|
37
|
+
})
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return /*#__PURE__*/React.createElement("ul", {
|
|
42
|
+
className: className
|
|
43
|
+
}, childrenWithExpandedState);
|
|
44
|
+
};
|
|
45
|
+
SideNavItems.displayName = 'SideNavItems';
|
|
46
|
+
SideNavItems.propTypes = {
|
|
47
|
+
/**
|
|
48
|
+
* Provide a single icon as the child to `SideNavIcon` to render in the
|
|
49
|
+
* container
|
|
50
|
+
*/
|
|
51
|
+
children: PropTypes.node,
|
|
52
|
+
/**
|
|
53
|
+
* Provide an optional class to be applied to the containing node
|
|
54
|
+
*/
|
|
55
|
+
className: PropTypes.string,
|
|
56
|
+
/**
|
|
57
|
+
* Property to indicate if the side nav container is open (or not). Use to
|
|
58
|
+
* keep local state and styling in step with the SideNav expansion state.
|
|
59
|
+
*/
|
|
60
|
+
isSideNavExpanded: PropTypes.bool
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
exports.SideNavItems = SideNavItems;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright IBM Corp. 2025
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import React from 'react';
|
|
8
|
+
export interface SideNavMenuProps {
|
|
9
|
+
/**
|
|
10
|
+
* An optional CSS class to apply to the component.
|
|
11
|
+
*/
|
|
12
|
+
className?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The content to render within the SideNavMenu component.
|
|
15
|
+
*/
|
|
16
|
+
children?: React.ReactNode;
|
|
17
|
+
/**
|
|
18
|
+
* Specifies whether the menu should be expanded by default.
|
|
19
|
+
*/
|
|
20
|
+
defaultExpanded?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* **Note:** this is controlled by the parent SideNav component, do not set manually.
|
|
23
|
+
* SideNavMenu depth to determine spacing
|
|
24
|
+
*/
|
|
25
|
+
depth?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Indicates whether the SideNavMenu is active.
|
|
28
|
+
*/
|
|
29
|
+
isActive?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Specifies whether the SideNavMenu is in a large variation.
|
|
32
|
+
*/
|
|
33
|
+
large?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* A custom icon to render next to the SideNavMenu title. This can be a function returning JSX or JSX itself.
|
|
36
|
+
*/
|
|
37
|
+
renderIcon?: React.ComponentType;
|
|
38
|
+
/**
|
|
39
|
+
* Indicates if the side navigation container is expanded or collapsed.
|
|
40
|
+
*/
|
|
41
|
+
isSideNavExpanded?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The tabIndex for the button element.
|
|
44
|
+
* If not specified, the default validation will be applied.
|
|
45
|
+
*/
|
|
46
|
+
tabIndex?: number;
|
|
47
|
+
title: string;
|
|
48
|
+
}
|
|
49
|
+
export declare const SideNavMenu: React.ForwardRefExoticComponent<SideNavMenuProps & React.RefAttributes<HTMLElement>>;
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright IBM Corp. 2024
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
var index = require('../_virtual/index.js');
|
|
11
|
+
var PropTypes = require('prop-types');
|
|
12
|
+
var React = require('react');
|
|
13
|
+
var _utils = require('./_utils.js');
|
|
14
|
+
var SideNavIcon = require('@carbon/react/lib/components/UIShell/SideNavIcon');
|
|
15
|
+
var match = require('@carbon/react/lib/internal/keyboard/match');
|
|
16
|
+
var usePrefix = require('@carbon/react/lib/internal/usePrefix');
|
|
17
|
+
var SideNav = require('./SideNav.js');
|
|
18
|
+
var useMergedRefs = require('@carbon/react/lib/internal/useMergedRefs');
|
|
19
|
+
var keys = require('@carbon/react/lib/internal/keyboard/keys');
|
|
20
|
+
var bucket3 = require('../node_modules/@carbon/icons-react/es/generated/bucket-3.js');
|
|
21
|
+
|
|
22
|
+
function _interopNamespaceDefault(e) {
|
|
23
|
+
var n = Object.create(null);
|
|
24
|
+
if (e) {
|
|
25
|
+
Object.keys(e).forEach(function (k) {
|
|
26
|
+
if (k !== 'default') {
|
|
27
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
28
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: function () { return e[k]; }
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
n.default = e;
|
|
36
|
+
return Object.freeze(n);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
var keys__namespace = /*#__PURE__*/_interopNamespaceDefault(keys);
|
|
40
|
+
|
|
41
|
+
var _ChevronDown;
|
|
42
|
+
const SideNavMenu = /*#__PURE__*/React.forwardRef(function SideNavMenu(_ref, ref) {
|
|
43
|
+
let {
|
|
44
|
+
className: customClassName,
|
|
45
|
+
children,
|
|
46
|
+
defaultExpanded = false,
|
|
47
|
+
depth: propDepth,
|
|
48
|
+
isActive = false,
|
|
49
|
+
large = false,
|
|
50
|
+
renderIcon: IconElement,
|
|
51
|
+
isSideNavExpanded,
|
|
52
|
+
tabIndex,
|
|
53
|
+
title
|
|
54
|
+
} = _ref;
|
|
55
|
+
const depth = propDepth;
|
|
56
|
+
const {
|
|
57
|
+
isRail
|
|
58
|
+
} = React.useContext(SideNav.SideNavContext);
|
|
59
|
+
const prefix = usePrefix.usePrefix();
|
|
60
|
+
const [isExpanded, setIsExpanded] = React.useState(defaultExpanded);
|
|
61
|
+
const [active, setActive] = React.useState(isActive);
|
|
62
|
+
const [prevExpanded, setPrevExpanded] = React.useState(defaultExpanded);
|
|
63
|
+
const className = index.default({
|
|
64
|
+
[`${prefix}--side-nav__item`]: true,
|
|
65
|
+
[`${prefix}--side-nav__item--active`]: active || hasActiveDescendant(children) && !isExpanded,
|
|
66
|
+
[`${prefix}--side-nav__item--icon`]: IconElement,
|
|
67
|
+
[`${prefix}--side-nav__item--large`]: large,
|
|
68
|
+
[customClassName]: !!customClassName
|
|
69
|
+
});
|
|
70
|
+
const buttonClassName = index.default({
|
|
71
|
+
[`${prefix}--side-nav__submenu`]: true,
|
|
72
|
+
[`${prefix}--side-nav__submenu--active`]: active || hasActiveDescendant(children) && isExpanded
|
|
73
|
+
});
|
|
74
|
+
const buttonRef = React.useRef(null);
|
|
75
|
+
const listRef = React.useRef(null);
|
|
76
|
+
const menuRef = useMergedRefs.useMergedRefs([buttonRef, ref]);
|
|
77
|
+
if (!isSideNavExpanded && isExpanded && isRail) {
|
|
78
|
+
setIsExpanded(false);
|
|
79
|
+
setPrevExpanded(true);
|
|
80
|
+
} else if (isSideNavExpanded && prevExpanded && isRail) {
|
|
81
|
+
setIsExpanded(true);
|
|
82
|
+
setPrevExpanded(false);
|
|
83
|
+
}
|
|
84
|
+
let childrenToRender = children;
|
|
85
|
+
|
|
86
|
+
// modify nested SideNavMenus
|
|
87
|
+
childrenToRender = React.Children.map(children, child => {
|
|
88
|
+
if (/*#__PURE__*/React.isValidElement(child)) {
|
|
89
|
+
const childJsxElement = child;
|
|
90
|
+
const childDisplayName = childJsxElement.type?.displayName ?? childJsxElement.type?.name;
|
|
91
|
+
const isCarbonSideNavItem = _utils.CARBON_SIDENAV_ITEMS.includes(childDisplayName);
|
|
92
|
+
return /*#__PURE__*/React.cloneElement(child, {
|
|
93
|
+
...(isCarbonSideNavItem && {
|
|
94
|
+
isSideNavExpanded: isSideNavExpanded
|
|
95
|
+
}),
|
|
96
|
+
...{
|
|
97
|
+
depth: depth + 1
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return child;
|
|
102
|
+
});
|
|
103
|
+
React.useEffect(() => {
|
|
104
|
+
if (depth === 0) return;
|
|
105
|
+
const calcButtonOffset = () => {
|
|
106
|
+
// menu with icon
|
|
107
|
+
if (children && IconElement) {
|
|
108
|
+
return depth + 3;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// menu without icon
|
|
112
|
+
if (children) {
|
|
113
|
+
return depth * 4;
|
|
114
|
+
}
|
|
115
|
+
return depth;
|
|
116
|
+
};
|
|
117
|
+
if (buttonRef.current) {
|
|
118
|
+
buttonRef.current.style.paddingLeft = `${calcButtonOffset()}rem`;
|
|
119
|
+
}
|
|
120
|
+
}, []);
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Returns the parent SideNavMenu, if node is actually inside one.
|
|
124
|
+
* @param node
|
|
125
|
+
* @returns parent side nav menu node
|
|
126
|
+
*/
|
|
127
|
+
function parentSideNavMenu(node) {
|
|
128
|
+
const parentNode = node.parentElement?.closest(`.${prefix}--side-nav__item`);
|
|
129
|
+
if (parentNode) return parentNode;
|
|
130
|
+
return node;
|
|
131
|
+
}
|
|
132
|
+
function handleKeyDown(event) {
|
|
133
|
+
if (match.match(event, keys__namespace.Escape)) {
|
|
134
|
+
setIsExpanded(false);
|
|
135
|
+
}
|
|
136
|
+
const node = event.target;
|
|
137
|
+
const isMenu = node.hasAttribute('aria-expanded');
|
|
138
|
+
const isExpanded = node.getAttribute('aria-expanded');
|
|
139
|
+
const parent = parentSideNavMenu(node);
|
|
140
|
+
if (match.match(event, keys__namespace.ArrowLeft)) {
|
|
141
|
+
event.stopPropagation();
|
|
142
|
+
if (isMenu) {
|
|
143
|
+
// collapse menu
|
|
144
|
+
if (isExpanded == 'true') {
|
|
145
|
+
setIsExpanded(false);
|
|
146
|
+
|
|
147
|
+
// go to previous level's side nav menu button
|
|
148
|
+
} else {
|
|
149
|
+
// since we're in a menu, it finds its own <li>, we go up one more
|
|
150
|
+
const previousMenu = parentSideNavMenu(parent);
|
|
151
|
+
const button = previousMenu.querySelector('button');
|
|
152
|
+
button.tabIndex = 0;
|
|
153
|
+
button?.focus();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// go to side nav menu button
|
|
157
|
+
} else if (parent) {
|
|
158
|
+
const button = parent.querySelector('button');
|
|
159
|
+
button.tabIndex = 0;
|
|
160
|
+
button?.focus();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (match.match(event, keys__namespace.ArrowRight)) {
|
|
164
|
+
event.stopPropagation();
|
|
165
|
+
|
|
166
|
+
// expand menu
|
|
167
|
+
if (isMenu) {
|
|
168
|
+
setIsExpanded(true);
|
|
169
|
+
|
|
170
|
+
// if already expanded, focus on first element
|
|
171
|
+
if (isExpanded == 'true') {
|
|
172
|
+
let nextNode = node.nextElementSibling?.querySelector('a, button');
|
|
173
|
+
if (nextNode) {
|
|
174
|
+
nextNode.tabIndex = 0;
|
|
175
|
+
nextNode.focus();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return (
|
|
182
|
+
/*#__PURE__*/
|
|
183
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
|
|
184
|
+
React.createElement("li", {
|
|
185
|
+
role: "treeitem",
|
|
186
|
+
"aria-expanded": isExpanded,
|
|
187
|
+
className: className,
|
|
188
|
+
ref: listRef,
|
|
189
|
+
onKeyDown: handleKeyDown
|
|
190
|
+
}, /*#__PURE__*/React.createElement("button", {
|
|
191
|
+
"aria-expanded": isExpanded,
|
|
192
|
+
className: buttonClassName,
|
|
193
|
+
onClick: () => {
|
|
194
|
+
setIsExpanded(!isExpanded);
|
|
195
|
+
},
|
|
196
|
+
ref: menuRef,
|
|
197
|
+
type: "button",
|
|
198
|
+
tabIndex: -1
|
|
199
|
+
}, IconElement && /*#__PURE__*/React.createElement(SideNavIcon, null, /*#__PURE__*/React.createElement(IconElement, null)), /*#__PURE__*/React.createElement("span", {
|
|
200
|
+
className: `${prefix}--side-nav__submenu-title`
|
|
201
|
+
}, title), /*#__PURE__*/React.createElement(SideNavIcon, {
|
|
202
|
+
className: `${prefix}--side-nav__submenu-chevron`,
|
|
203
|
+
small: true
|
|
204
|
+
}, _ChevronDown || (_ChevronDown = /*#__PURE__*/React.createElement(bucket3.ChevronDown, {
|
|
205
|
+
size: 20
|
|
206
|
+
})))), /*#__PURE__*/React.createElement("ul", {
|
|
207
|
+
className: `${prefix}--side-nav__menu`,
|
|
208
|
+
role: "group"
|
|
209
|
+
}, childrenToRender))
|
|
210
|
+
);
|
|
211
|
+
});
|
|
212
|
+
SideNavMenu.displayName = 'SideNavMenu';
|
|
213
|
+
SideNavMenu.propTypes = {
|
|
214
|
+
/**
|
|
215
|
+
* Provide <SideNavMenuItem>'s inside of the `SideNavMenu`
|
|
216
|
+
*/
|
|
217
|
+
children: PropTypes.node,
|
|
218
|
+
/**
|
|
219
|
+
* Provide an optional class to be applied to the containing node
|
|
220
|
+
*/
|
|
221
|
+
className: PropTypes.string,
|
|
222
|
+
/**
|
|
223
|
+
* Specify whether the menu should default to expanded. By default, it will
|
|
224
|
+
* be closed.
|
|
225
|
+
*/
|
|
226
|
+
defaultExpanded: PropTypes.bool,
|
|
227
|
+
/**
|
|
228
|
+
* **Note:** this is controlled by the parent SideNav component, do not set manually.
|
|
229
|
+
* SideNavMenu depth to determine spacing
|
|
230
|
+
*/
|
|
231
|
+
depth: PropTypes.number,
|
|
232
|
+
/**
|
|
233
|
+
* Specify whether the `SideNavMenu` is "active". `SideNavMenu` should be
|
|
234
|
+
* considered active if one of its menu items are a link for the current
|
|
235
|
+
* page.
|
|
236
|
+
*/
|
|
237
|
+
isActive: PropTypes.bool,
|
|
238
|
+
/**
|
|
239
|
+
* Property to indicate if the side nav container is open (or not). Use to
|
|
240
|
+
* keep local state and styling in step with the SideNav expansion state.
|
|
241
|
+
*/
|
|
242
|
+
isSideNavExpanded: PropTypes.bool,
|
|
243
|
+
/**
|
|
244
|
+
* Specify if this is a large variation of the SideNavMenu
|
|
245
|
+
*/
|
|
246
|
+
large: PropTypes.bool,
|
|
247
|
+
/**
|
|
248
|
+
* Pass in a custom icon to render next to the `SideNavMenu` title
|
|
249
|
+
*/
|
|
250
|
+
// @ts-expect-error - PropTypes are unable to cover this case.
|
|
251
|
+
renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
|
|
252
|
+
/**
|
|
253
|
+
* Optional prop to specify the tabIndex of the button. If undefined, it will be applied default validation
|
|
254
|
+
*/
|
|
255
|
+
tabIndex: PropTypes.number,
|
|
256
|
+
/**
|
|
257
|
+
* Provide the text for the overall menu name
|
|
258
|
+
*/
|
|
259
|
+
title: PropTypes.string.isRequired
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
Defining the children parameter with the type ReactNode | ReactNode[]. This allows for various possibilities:
|
|
264
|
+
a single element, an array of elements, or null or undefined.
|
|
265
|
+
**/
|
|
266
|
+
function hasActiveDescendant(children) {
|
|
267
|
+
if (Array.isArray(children)) {
|
|
268
|
+
return children.some(child => {
|
|
269
|
+
if (! /*#__PURE__*/React.isValidElement(child)) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Explicitly defining the expected prop types (isActive and 'aria-current) for the children to ensure type
|
|
274
|
+
safety when accessing their props.
|
|
275
|
+
**/
|
|
276
|
+
const props = child.props;
|
|
277
|
+
if (props.isActive === true || props['aria-current'] || props.children instanceof Array && hasActiveDescendant(props.children)) {
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
return false;
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// We use React.isValidElement(child) to check if the child is a valid React element before accessing its props
|
|
285
|
+
|
|
286
|
+
if (/*#__PURE__*/React.isValidElement(children)) {
|
|
287
|
+
const props = children.props;
|
|
288
|
+
if (props.isActive === true || props['aria-current']) {
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
exports.SideNavMenu = SideNavMenu;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright IBM Corp. 2025
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the Apache-2.0 license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import React, { ElementType, ComponentProps } from 'react';
|
|
8
|
+
import Link from '@carbon/react/lib/components/UIShell/Link';
|
|
9
|
+
export interface SideNavMenuItemProps extends ComponentProps<typeof Link> {
|
|
10
|
+
/**
|
|
11
|
+
* Specify the children to be rendered inside of the `SideNavMenuItem`
|
|
12
|
+
*/
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* Provide an optional class to be applied to the containing node
|
|
16
|
+
*/
|
|
17
|
+
className?: string;
|
|
18
|
+
/**
|
|
19
|
+
* **Note:** this is controlled by the parent SideNavMenu component, do not set manually.
|
|
20
|
+
* SideNavMenu depth to determine spacing
|
|
21
|
+
*/
|
|
22
|
+
depth?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Optionally specify whether the link is "active". An active link is one that
|
|
25
|
+
* has an href that is the same as the current page. Can also pass in
|
|
26
|
+
* `aria-current="page"`, as well.
|
|
27
|
+
*/
|
|
28
|
+
isActive?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Optionally provide an href for the underlying li`
|
|
31
|
+
*/
|
|
32
|
+
href?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Optional component to render instead of default Link
|
|
35
|
+
*/
|
|
36
|
+
as?: ElementType;
|
|
37
|
+
}
|
|
38
|
+
export declare const SideNavMenuItem: React.ForwardRefExoticComponent<Omit<SideNavMenuItemProps, "ref"> & React.RefAttributes<HTMLElement>>;
|