@carbon-labs/react-ui-shell 0.8.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.
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import { extends as _extends } from '../_virtual/_rollupPluginBabelHelpers.js';
9
- import React, { useRef, isValidElement, useEffect, createContext } from 'react';
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';
@@ -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,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,84 @@
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 { extends as _extends } from '../_virtual/_rollupPluginBabelHelpers.js';
9
+ import cx from '../_virtual/index.js';
10
+ import PropTypes from 'prop-types';
11
+ import React, { useRef, useEffect } from 'react';
12
+ import SideNavLinkText from '@carbon/react/lib/components/UIShell/SideNavLinkText';
13
+ import Link from '@carbon/react/lib/components/UIShell/Link';
14
+ import { usePrefix } from '@carbon/react/lib/internal/usePrefix';
15
+ import { useMergedRefs } from '@carbon/react/lib/internal/useMergedRefs';
16
+
17
+ const SideNavMenuItem = /*#__PURE__*/React.forwardRef(function SideNavMenuItem(props, ref) {
18
+ const prefix = usePrefix();
19
+ const {
20
+ children,
21
+ className: customClassName,
22
+ depth: propDepth,
23
+ as: Component = Link,
24
+ isActive,
25
+ ...rest
26
+ } = props;
27
+ const className = cx(`${prefix}--side-nav__menu-item`, customClassName);
28
+ const depth = propDepth;
29
+ const linkClassName = cx({
30
+ [`${prefix}--side-nav__link`]: true,
31
+ [`${prefix}--side-nav__link--current`]: isActive
32
+ });
33
+ const linkRef = useRef(null);
34
+ const itemRef = useMergedRefs([linkRef, ref]);
35
+ useEffect(() => {
36
+ const calcLinkOffset = () => {
37
+ return 4 + Math.max(0, depth - 1) * 1;
38
+ };
39
+ if (linkRef.current) {
40
+ linkRef.current.style.paddingLeft = `${calcLinkOffset()}rem`;
41
+ }
42
+ }, []);
43
+ return /*#__PURE__*/React.createElement("li", {
44
+ role: "treeitem",
45
+ "aria-selected": isActive ? 'true' : 'false',
46
+ className: className
47
+ }, /*#__PURE__*/React.createElement(Component, _extends({}, rest, {
48
+ className: linkClassName,
49
+ tabIndex: -1,
50
+ ref: itemRef
51
+ }), /*#__PURE__*/React.createElement(SideNavLinkText, null, children)));
52
+ });
53
+ SideNavMenuItem.displayName = 'SideNavMenuItem';
54
+ SideNavMenuItem.propTypes = {
55
+ /**
56
+ * Specify the children to be rendered inside of the `SideNavMenuItem`
57
+ */
58
+ children: PropTypes.node,
59
+ /**
60
+ * Provide an optional class to be applied to the containing node
61
+ */
62
+ className: PropTypes.string,
63
+ /**
64
+ * **Note:** this is controlled by the parent SideNavMenu component, do not set manually.
65
+ * SideNavMenu depth to determine spacing
66
+ */
67
+ depth: PropTypes.number,
68
+ /**
69
+ * Optionally provide an href for the underlying li`
70
+ */
71
+ href: PropTypes.string,
72
+ /**
73
+ * Optionally specify whether the link is "active". An active link is one that
74
+ * has an href that is the same as the current page. Can also pass in
75
+ * `aria-current="page"`, as well.
76
+ */
77
+ isActive: PropTypes.bool,
78
+ /**
79
+ * Optional component to render instead of default Link
80
+ */
81
+ as: PropTypes.elementType
82
+ };
83
+
84
+ export { SideNavMenuItem };
package/es/index.d.ts CHANGED
@@ -6,5 +6,8 @@
6
6
  * This source code is licensed under the Apache-2.0 license found in the
7
7
  * LICENSE file in the root directory of this source tree.
8
8
  */
9
- export { SideNav } from './components/SideNav.js';
9
+ export { SideNav, SIDE_NAV_TYPE } from './components/SideNav.js';
10
+ export { SideNavItems } from './components/SideNavItems.js';
11
+ export { SideNavMenu } from './components/SideNavMenu.js';
12
+ export { SideNavMenuItem } from './components/SideNavMenuItem.js';
10
13
  export { HeaderPanel } from './components/HeaderPanel';
package/es/index.js CHANGED
@@ -5,5 +5,8 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- export { SideNav } from './components/SideNav.js';
8
+ export { SIDE_NAV_TYPE, SideNav } from './components/SideNav.js';
9
+ export { SideNavItems } from './components/SideNavItems.js';
10
+ export { SideNavMenu } from './components/SideNavMenu.js';
11
+ export { SideNavMenuItem } from './components/SideNavMenuItem.js';
9
12
  export { HeaderPanel } from './components/HeaderPanel.js';