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