carbon-react 152.5.0 → 152.5.1

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.
@@ -36,6 +36,13 @@ export const Link = /*#__PURE__*/React.forwardRef(({
36
36
  batchSelectionDisabled
37
37
  } = useContext(BatchSelectionContext);
38
38
  const isDisabled = disabled || batchSelectionDisabled;
39
+ const setRefs = React.useCallback(reference => {
40
+ if (!ref) return;
41
+ if (typeof ref === "object") ref.current = reference;
42
+ if (typeof ref === "function") {
43
+ ref(reference);
44
+ }
45
+ }, [ref]);
39
46
  const renderLinkIcon = (currentAlignment = "left") => {
40
47
  const hasProperAlignment = icon && iconAlign === currentAlignment;
41
48
  return hasProperAlignment ? /*#__PURE__*/React.createElement(Icon, {
@@ -59,7 +66,7 @@ export const Link = /*#__PURE__*/React.forwardRef(({
59
66
  onClick,
60
67
  disabled: isDisabled,
61
68
  target,
62
- ref,
69
+ ref: setRefs,
63
70
  href,
64
71
  rel,
65
72
  "aria-label": ariaLabel,
@@ -1,5 +1,5 @@
1
1
  function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
2
- import React, { useRef, useEffect, useContext } from "react";
2
+ import React, { useRef, useEffect, useContext, useState, useLayoutEffect } from "react";
3
3
  import invariant from "invariant";
4
4
  import { defaultFocusableSelectors as focusableSelectors } from "../../../__internal__/focus-trap/focus-trap-utils";
5
5
  import { filterStyledSystemPaddingProps } from "../../../style/utils";
@@ -63,8 +63,14 @@ export const MenuItem = ({
63
63
  } = submenuContext;
64
64
  const focusFromMenu = focusId === menuItemId.current;
65
65
  const focusFromSubmenu = submenuFocusId ? submenuFocusId === menuItemId.current : undefined;
66
- const ref = useRef(null);
67
- const firstFocusableChild = ref.current?.querySelector(focusableSelectors) ?? null;
66
+ const [ref, setRef] = useState(null);
67
+ const [firstFocusableChild, setFirstFocusableChild] = useState(null);
68
+ useLayoutEffect(() => {
69
+ const firstFocusable = ref?.querySelector(focusableSelectors) ?? null;
70
+ if (firstFocusable !== firstFocusableChild) {
71
+ setFirstFocusableChild(firstFocusable);
72
+ }
73
+ }, [ref]);
68
74
  useEffect(() => {
69
75
  const id = menuItemId.current;
70
76
 
@@ -85,7 +91,7 @@ export const MenuItem = ({
85
91
  firstFocusableChild.focus();
86
92
  return;
87
93
  }
88
- ref.current?.focus();
94
+ ref?.focus();
89
95
  }
90
96
  }, [firstFocusableChild, focusFromMenu, focusFromSubmenu]);
91
97
  const handleFocus = event => {
@@ -99,7 +105,7 @@ export const MenuItem = ({
99
105
  const handleKeyDown = event => {
100
106
  onKeyDown?.(event);
101
107
  if (Events.isEscKey(event)) {
102
- ref.current?.focus();
108
+ ref?.focus();
103
109
  }
104
110
  handleSubmenuKeyDown?.(event);
105
111
  };
@@ -114,7 +120,7 @@ export const MenuItem = ({
114
120
  selected,
115
121
  onKeyDown: !inFullscreenView ? handleKeyDown : undefined,
116
122
  overrideColor,
117
- ref
123
+ ref: setRef
118
124
  };
119
125
  if (overriddenVariant === "alternate" && isChildOfSegment && variant === "alternate" && ["white", "black"].includes(menuType)) {
120
126
  elementProps.overrideColor = true;
@@ -153,7 +159,7 @@ export const MenuItem = ({
153
159
  }, rest), children));
154
160
  }
155
161
  const paddingProps = filterStyledSystemPaddingProps(rest);
156
- const hasInput = !!ref.current?.querySelector("[data-element='input']");
162
+ const hasInput = !!ref?.querySelector("[data-element='input']");
157
163
  return /*#__PURE__*/React.createElement(StyledMenuItem, _extends({
158
164
  "data-component": "menu-item",
159
165
  "data-element": dataElement,
@@ -45,6 +45,13 @@ const Link = exports.Link = /*#__PURE__*/_react.default.forwardRef(({
45
45
  batchSelectionDisabled
46
46
  } = (0, _react.useContext)(_batchSelection.default);
47
47
  const isDisabled = disabled || batchSelectionDisabled;
48
+ const setRefs = _react.default.useCallback(reference => {
49
+ if (!ref) return;
50
+ if (typeof ref === "object") ref.current = reference;
51
+ if (typeof ref === "function") {
52
+ ref(reference);
53
+ }
54
+ }, [ref]);
48
55
  const renderLinkIcon = (currentAlignment = "left") => {
49
56
  const hasProperAlignment = icon && iconAlign === currentAlignment;
50
57
  return hasProperAlignment ? /*#__PURE__*/_react.default.createElement(_icon.default, {
@@ -68,7 +75,7 @@ const Link = exports.Link = /*#__PURE__*/_react.default.forwardRef(({
68
75
  onClick,
69
76
  disabled: isDisabled,
70
77
  target,
71
- ref,
78
+ ref: setRefs,
72
79
  href,
73
80
  rel,
74
81
  "aria-label": ariaLabel,
@@ -72,8 +72,14 @@ const MenuItem = ({
72
72
  } = submenuContext;
73
73
  const focusFromMenu = focusId === menuItemId.current;
74
74
  const focusFromSubmenu = submenuFocusId ? submenuFocusId === menuItemId.current : undefined;
75
- const ref = (0, _react.useRef)(null);
76
- const firstFocusableChild = ref.current?.querySelector(_focusTrapUtils.defaultFocusableSelectors) ?? null;
75
+ const [ref, setRef] = (0, _react.useState)(null);
76
+ const [firstFocusableChild, setFirstFocusableChild] = (0, _react.useState)(null);
77
+ (0, _react.useLayoutEffect)(() => {
78
+ const firstFocusable = ref?.querySelector(_focusTrapUtils.defaultFocusableSelectors) ?? null;
79
+ if (firstFocusable !== firstFocusableChild) {
80
+ setFirstFocusableChild(firstFocusable);
81
+ }
82
+ }, [ref]);
77
83
  (0, _react.useEffect)(() => {
78
84
  const id = menuItemId.current;
79
85
 
@@ -94,7 +100,7 @@ const MenuItem = ({
94
100
  firstFocusableChild.focus();
95
101
  return;
96
102
  }
97
- ref.current?.focus();
103
+ ref?.focus();
98
104
  }
99
105
  }, [firstFocusableChild, focusFromMenu, focusFromSubmenu]);
100
106
  const handleFocus = event => {
@@ -108,7 +114,7 @@ const MenuItem = ({
108
114
  const handleKeyDown = event => {
109
115
  onKeyDown?.(event);
110
116
  if (_events.default.isEscKey(event)) {
111
- ref.current?.focus();
117
+ ref?.focus();
112
118
  }
113
119
  handleSubmenuKeyDown?.(event);
114
120
  };
@@ -123,7 +129,7 @@ const MenuItem = ({
123
129
  selected,
124
130
  onKeyDown: !inFullscreenView ? handleKeyDown : undefined,
125
131
  overrideColor,
126
- ref
132
+ ref: setRef
127
133
  };
128
134
  if (overriddenVariant === "alternate" && isChildOfSegment && variant === "alternate" && ["white", "black"].includes(menuType)) {
129
135
  elementProps.overrideColor = true;
@@ -162,7 +168,7 @@ const MenuItem = ({
162
168
  }, rest), children));
163
169
  }
164
170
  const paddingProps = (0, _utils.filterStyledSystemPaddingProps)(rest);
165
- const hasInput = !!ref.current?.querySelector("[data-element='input']");
171
+ const hasInput = !!ref?.querySelector("[data-element='input']");
166
172
  return /*#__PURE__*/_react.default.createElement(_menu2.StyledMenuItem, _extends({
167
173
  "data-component": "menu-item",
168
174
  "data-element": dataElement,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-react",
3
- "version": "152.5.0",
3
+ "version": "152.5.1",
4
4
  "description": "A library of reusable React components for easily building user interfaces.",
5
5
  "files": [
6
6
  "lib",