@carbon/react 1.80.0-rc.0 → 1.80.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.
Files changed (40) hide show
  1. package/.playwright/INTERNAL_AVT_REPORT_DO_NOT_USE.json +874 -874
  2. package/es/components/InlineLoading/InlineLoading.js +10 -2
  3. package/es/components/Menu/MenuItem.d.ts +3 -3
  4. package/es/components/Menu/MenuItem.js +1 -1
  5. package/es/components/OverflowMenu/OverflowMenu.js +2 -2
  6. package/es/components/Tag/DismissibleTag.d.ts +1 -55
  7. package/es/components/Tag/DismissibleTag.js +6 -4
  8. package/es/components/Tag/OperationalTag.d.ts +1 -1
  9. package/es/components/Tag/SelectableTag.d.ts +1 -43
  10. package/es/components/Tag/SelectableTag.js +7 -5
  11. package/es/components/TreeView/TreeNode.d.ts +1 -1
  12. package/es/components/TreeView/TreeNode.js +11 -4
  13. package/es/components/TreeView/TreeView.d.ts +1 -1
  14. package/es/components/TreeView/TreeView.js +6 -6
  15. package/es/internal/ClickListener.d.ts +13 -0
  16. package/es/internal/ClickListener.js +33 -60
  17. package/es/internal/useControllableState.d.ts +34 -0
  18. package/es/internal/useControllableState.js +15 -30
  19. package/es/internal/useOutsideClick.d.ts +8 -0
  20. package/es/internal/useOutsideClick.js +9 -6
  21. package/lib/components/InlineLoading/InlineLoading.js +10 -2
  22. package/lib/components/Menu/MenuItem.d.ts +3 -3
  23. package/lib/components/Menu/MenuItem.js +1 -1
  24. package/lib/components/OverflowMenu/OverflowMenu.js +2 -2
  25. package/lib/components/Tag/DismissibleTag.d.ts +1 -55
  26. package/lib/components/Tag/DismissibleTag.js +5 -3
  27. package/lib/components/Tag/OperationalTag.d.ts +1 -1
  28. package/lib/components/Tag/SelectableTag.d.ts +1 -43
  29. package/lib/components/Tag/SelectableTag.js +6 -4
  30. package/lib/components/TreeView/TreeNode.d.ts +1 -1
  31. package/lib/components/TreeView/TreeNode.js +11 -4
  32. package/lib/components/TreeView/TreeView.d.ts +1 -1
  33. package/lib/components/TreeView/TreeView.js +6 -6
  34. package/lib/internal/ClickListener.d.ts +13 -0
  35. package/lib/internal/ClickListener.js +32 -64
  36. package/lib/internal/useControllableState.d.ts +34 -0
  37. package/lib/internal/useControllableState.js +15 -30
  38. package/lib/internal/useOutsideClick.d.ts +8 -0
  39. package/lib/internal/useOutsideClick.js +9 -6
  40. package/package.json +8 -7
@@ -6,26 +6,29 @@
6
6
  */
7
7
 
8
8
  import { useRef, useEffect } from 'react';
9
- import { useEvent } from './useEvent.js';
10
9
  import { canUseDOM } from './environment.js';
10
+ import { useWindowEvent } from './useEvent.js';
11
11
 
12
- function useOutsideClick(ref, callback) {
12
+ const useOutsideClick = (ref, callback) => {
13
13
  const savedCallback = useRef(callback);
14
14
  useEffect(() => {
15
15
  savedCallback.current = callback;
16
- });
16
+ }, [callback]);
17
17
 
18
18
  // We conditionally guard the `useEvent` hook for SSR. `canUseDOM` can be
19
19
  // treated as a constant as it will be false when executed in a Node.js
20
20
  // environment and true when executed in the browser
21
21
  if (canUseDOM) {
22
22
  // eslint-disable-next-line react-hooks/rules-of-hooks
23
- useEvent(window, 'click', event => {
24
- if (ref.current && !ref.current.contains(event.target)) {
23
+ useWindowEvent('click', event => {
24
+ const {
25
+ target
26
+ } = event;
27
+ if (target instanceof Node && ref.current && !ref.current.contains(target)) {
25
28
  savedCallback.current(event);
26
29
  }
27
30
  });
28
31
  }
29
- }
32
+ };
30
33
 
31
34
  export { useOutsideClick };
@@ -63,9 +63,9 @@ const InlineLoading = _ref => {
63
63
  className: `${prefix}--inline-loading__checkmark-container`
64
64
  }, /*#__PURE__*/React__default["default"].createElement("title", null, iconLabel));
65
65
  }
66
- if (status === 'inactive' || status === 'active') {
66
+ if (status === 'active') {
67
67
  if (!iconDescription) {
68
- iconLabel = status === 'active' ? 'loading' : 'not loading';
68
+ iconLabel = 'loading';
69
69
  }
70
70
  return /*#__PURE__*/React__default["default"].createElement(Loading["default"], {
71
71
  small: true,
@@ -74,6 +74,14 @@ const InlineLoading = _ref => {
74
74
  active: status === 'active'
75
75
  });
76
76
  }
77
+ if (status === 'inactive') {
78
+ if (!iconDescription) {
79
+ iconLabel = 'not loading';
80
+ }
81
+ return /*#__PURE__*/React__default["default"].createElement("title", {
82
+ className: `${prefix}--inline-loading__inactive-status`
83
+ }, iconLabel);
84
+ }
77
85
  return undefined;
78
86
  };
79
87
  const loadingText = description && /*#__PURE__*/React__default["default"].createElement("div", {
@@ -4,7 +4,7 @@
4
4
  * This source code is licensed under the Apache-2.0 license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import React, { ChangeEventHandler, ComponentProps, FC, KeyboardEvent, LiHTMLAttributes, MouseEvent, ReactNode } from 'react';
7
+ import React, { ComponentProps, FC, KeyboardEvent, LiHTMLAttributes, MouseEvent, ReactNode } from 'react';
8
8
  export interface MenuItemProps extends LiHTMLAttributes<HTMLLIElement> {
9
9
  /**
10
10
  * Optionally provide another Menu to create a submenu. props.children can't be used to specify the content of the MenuItem itself. Use props.label instead.
@@ -48,7 +48,7 @@ export interface MenuItemSelectableProps extends Omit<MenuItemProps, 'onChange'>
48
48
  /**
49
49
  * Provide an optional function to be called when the selection state changes.
50
50
  */
51
- onChange?: ChangeEventHandler<HTMLLIElement>;
51
+ onChange?: (checked: boolean) => void;
52
52
  /**
53
53
  * Controls the state of this option.
54
54
  */
@@ -94,7 +94,7 @@ export interface MenuItemRadioGroupProps<Item> extends Omit<ComponentProps<'ul'>
94
94
  /**
95
95
  * Provide an optional function to be called when the selection changes.
96
96
  */
97
- onChange?: ChangeEventHandler<HTMLLIElement>;
97
+ onChange?: (selectedItem: Item) => void;
98
98
  /**
99
99
  * Provide props.selectedItem to control the state of this radio group. Must match the type of props.items.
100
100
  */
@@ -343,7 +343,7 @@ const MenuItemRadioGroup = /*#__PURE__*/React.forwardRef(function MenuItemRadioG
343
343
  const [selection, setSelection] = useControllableState.useControllableState({
344
344
  value: selectedItem,
345
345
  onChange,
346
- defaultValue: defaultSelectedItem
346
+ defaultValue: defaultSelectedItem ?? {}
347
347
  });
348
348
  function handleClick(item, e) {
349
349
  setSelection(item);
@@ -212,7 +212,7 @@ const OverflowMenu = /*#__PURE__*/React.forwardRef((_ref, ref) => {
212
212
  }
213
213
  };
214
214
  const handleClickOutside = evt => {
215
- if (open && (!menuBodyRef.current || !menuBodyRef.current.contains(evt.target))) {
215
+ if (open && (!menuBodyRef.current || evt.target instanceof Node && !menuBodyRef.current.contains(evt.target))) {
216
216
  closeMenu();
217
217
  }
218
218
  };
@@ -325,7 +325,7 @@ const OverflowMenu = /*#__PURE__*/React.forwardRef((_ref, ref) => {
325
325
  }, /*#__PURE__*/React.cloneElement(menuBody, {
326
326
  'data-floating-menu-direction': direction
327
327
  }));
328
- return /*#__PURE__*/React__default["default"].createElement(ClickListener["default"], {
328
+ return /*#__PURE__*/React__default["default"].createElement(ClickListener.ClickListener, {
329
329
  onClickOutside: handleClickOutside
330
330
  }, /*#__PURE__*/React__default["default"].createElement("span", {
331
331
  className: `${prefix}--overflow-menu__wrapper`,
@@ -4,7 +4,6 @@
4
4
  * This source code is licensed under the Apache-2.0 license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import PropTypes from 'prop-types';
8
7
  import React, { ReactNode } from 'react';
9
8
  import { PolymorphicProps } from '../../types/common';
10
9
  import { SIZES, TYPES } from './Tag';
@@ -61,59 +60,6 @@ export interface DismissibleTagBaseProps {
61
60
  type?: keyof typeof TYPES;
62
61
  }
63
62
  export type DismissibleTagProps<T extends React.ElementType> = PolymorphicProps<T, DismissibleTagBaseProps>;
64
- declare const DismissibleTag: {
65
- <T extends React.ElementType>({ className, decorator, disabled, id, renderIcon, title, onClose, slug, size, text, tagTitle, type, ...other }: DismissibleTagProps<T>): import("react/jsx-runtime").JSX.Element;
66
- propTypes: {
67
- /**
68
- * Provide a custom className that is applied to the containing <span>
69
- */
70
- className: PropTypes.Requireable<string>;
71
- /**
72
- * **Experimental:** Provide a `decorator` component to be rendered inside the `DismissibleTag` component
73
- */
74
- decorator: PropTypes.Requireable<PropTypes.ReactNodeLike>;
75
- /**
76
- * Specify if the `DismissibleTag` is disabled
77
- */
78
- disabled: PropTypes.Requireable<boolean>;
79
- /**
80
- * Specify the id for the tag.
81
- */
82
- id: PropTypes.Requireable<string>;
83
- /**
84
- * Click handler for filter tag close button.
85
- */
86
- onClose: PropTypes.Requireable<(...args: any[]) => any>;
87
- /**
88
- * A component used to render an icon.
89
- */
90
- renderIcon: PropTypes.Requireable<object>;
91
- /**
92
- * Specify the size of the Tag. Currently supports either `sm`,
93
- * `md` (default) or `lg` sizes.
94
- */
95
- size: PropTypes.Requireable<string>;
96
- /**
97
- * **Experimental:** Provide a `Slug` component to be rendered inside the `DismissibleTag` component
98
- */
99
- slug: (props: any, propName: any, componentName: any, ...rest: any[]) => any;
100
- /**
101
- * Provide text to be rendered inside of a the tag.
102
- */
103
- text: PropTypes.Requireable<string>;
104
- /**
105
- * Provide a custom `title` to be inserted in the tag.
106
- */
107
- tagTitle: PropTypes.Requireable<string>;
108
- /**
109
- * Text to show on clear filters
110
- */
111
- title: PropTypes.Requireable<string>;
112
- /**
113
- * Specify the type of the `Tag`
114
- */
115
- type: PropTypes.Requireable<string>;
116
- };
117
- };
63
+ declare const DismissibleTag: React.ForwardRefExoticComponent<Omit<DismissibleTagProps<React.ElementType<any, keyof React.JSX.IntrinsicElements>>, "ref"> & React.RefAttributes<HTMLDivElement>>;
118
64
  export declare const types: string[];
119
65
  export default DismissibleTag;
@@ -22,6 +22,7 @@ require('../Tooltip/DefinitionTooltip.js');
22
22
  var Tooltip = require('../Tooltip/Tooltip.js');
23
23
  require('../Text/index.js');
24
24
  var isEllipsisActive = require('./isEllipsisActive.js');
25
+ var mergeRefs = require('../../tools/mergeRefs.js');
25
26
  var Text = require('../Text/Text.js');
26
27
 
27
28
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -31,7 +32,7 @@ var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
31
32
  var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx);
32
33
 
33
34
  var _Close;
34
- const DismissibleTag = _ref => {
35
+ const DismissibleTag = /*#__PURE__*/React.forwardRef((_ref, forwardRef) => {
35
36
  let {
36
37
  className,
37
38
  decorator,
@@ -56,6 +57,7 @@ const DismissibleTag = _ref => {
56
57
  const newElement = tagLabelRef.current?.getElementsByClassName(`${prefix}--tag__label`)[0];
57
58
  setIsEllipsisApplied(isEllipsisActive.isEllipsisActive(newElement));
58
59
  }, [prefix, tagLabelRef]);
60
+ const combinedRef = mergeRefs["default"](tagLabelRef, forwardRef);
59
61
  const handleClose = event => {
60
62
  if (onClose) {
61
63
  event.stopPropagation();
@@ -79,7 +81,7 @@ const DismissibleTag = _ref => {
79
81
  } = other;
80
82
  const dismissLabel = `Dismiss "${text}"`;
81
83
  return /*#__PURE__*/React__default["default"].createElement(Tag["default"], _rollupPluginBabelHelpers["extends"]({
82
- ref: tagLabelRef,
84
+ ref: combinedRef,
83
85
  type: type,
84
86
  size: size,
85
87
  renderIcon: renderIcon,
@@ -106,7 +108,7 @@ const DismissibleTag = _ref => {
106
108
  disabled: disabled,
107
109
  "aria-label": title
108
110
  }, _Close || (_Close = /*#__PURE__*/React__default["default"].createElement(iconsReact.Close, null))))));
109
- };
111
+ });
110
112
  DismissibleTag.propTypes = {
111
113
  /**
112
114
  * Provide a custom className that is applied to the containing <span>
@@ -52,6 +52,6 @@ export interface OperationalTagBaseProps {
52
52
  type?: keyof typeof TYPES;
53
53
  }
54
54
  export type OperationalTagProps<T extends React.ElementType> = PolymorphicProps<T, OperationalTagBaseProps>;
55
- declare const OperationalTag: React.ForwardRefExoticComponent<Omit<OperationalTagProps<React.ElementType<any, keyof React.JSX.IntrinsicElements>>, "ref"> & React.RefAttributes<HTMLLIElement>>;
55
+ declare const OperationalTag: React.ForwardRefExoticComponent<Omit<OperationalTagProps<React.ElementType<any, keyof React.JSX.IntrinsicElements>>, "ref"> & React.RefAttributes<HTMLButtonElement>>;
56
56
  export declare const types: string[];
57
57
  export default OperationalTag;
@@ -4,7 +4,6 @@
4
4
  * This source code is licensed under the Apache-2.0 license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import PropTypes from 'prop-types';
8
7
  import React, { MouseEvent } from 'react';
9
8
  import { PolymorphicProps } from '../../types/common';
10
9
  import { SIZES } from './Tag';
@@ -48,46 +47,5 @@ export interface SelectableTagBaseProps {
48
47
  text?: string;
49
48
  }
50
49
  export type SelectableTagProps<T extends React.ElementType> = PolymorphicProps<T, SelectableTagBaseProps>;
51
- declare const SelectableTag: {
52
- <T extends React.ElementType>({ className, disabled, id, renderIcon, onChange, onClick, selected, size, text, ...other }: SelectableTagProps<T>): import("react/jsx-runtime").JSX.Element;
53
- propTypes: {
54
- /**
55
- * Provide a custom className that is applied to the containing <span>
56
- */
57
- className: PropTypes.Requireable<string>;
58
- /**
59
- * Specify if the `SelectableTag` is disabled
60
- */
61
- disabled: PropTypes.Requireable<boolean>;
62
- /**
63
- * Specify the id for the tag.
64
- */
65
- id: PropTypes.Requireable<string>;
66
- /**
67
- * A component used to render an icon.
68
- */
69
- renderIcon: PropTypes.Requireable<object>;
70
- /**
71
- * Provide an optional hook that is called when selected is changed
72
- */
73
- onChange: PropTypes.Requireable<(...args: any[]) => any>;
74
- /**
75
- * Provide an optional function to be called when the tag is clicked.
76
- */
77
- onClick: PropTypes.Requireable<(...args: any[]) => any>;
78
- /**
79
- * Specify the state of the selectable tag.
80
- */
81
- selected: PropTypes.Requireable<boolean>;
82
- /**
83
- * Specify the size of the Tag. Currently supports either `sm`,
84
- * `md` (default) or `lg` sizes.
85
- */
86
- size: PropTypes.Requireable<string>;
87
- /**
88
- * Provide text to be rendered inside of a the tag.
89
- */
90
- text: PropTypes.Requireable<string>;
91
- };
92
- };
50
+ declare const SelectableTag: React.ForwardRefExoticComponent<Omit<SelectableTagProps<React.ElementType<any, keyof React.JSX.IntrinsicElements>>, "ref"> & React.RefAttributes<HTMLButtonElement>>;
93
51
  export default SelectableTag;
@@ -20,6 +20,7 @@ require('../Tooltip/DefinitionTooltip.js');
20
20
  var Tooltip = require('../Tooltip/Tooltip.js');
21
21
  require('../Text/index.js');
22
22
  var isEllipsisActive = require('./isEllipsisActive.js');
23
+ var mergeRefs = require('../../tools/mergeRefs.js');
23
24
  var Text = require('../Text/Text.js');
24
25
 
25
26
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -28,7 +29,7 @@ var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
28
29
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
29
30
  var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx);
30
31
 
31
- const SelectableTag = _ref => {
32
+ const SelectableTag = /*#__PURE__*/React.forwardRef((_ref, forwardRef) => {
32
33
  let {
33
34
  className,
34
35
  disabled,
@@ -54,6 +55,7 @@ const SelectableTag = _ref => {
54
55
  setIsEllipsisApplied(isEllipsisActive.isEllipsisActive(newElement));
55
56
  }, [prefix, tagRef]);
56
57
  const tooltipClasses = cx__default["default"](`${prefix}--icon-tooltip`, `${prefix}--tag-label-tooltip`);
58
+ const combinedRef = mergeRefs["default"](tagRef, forwardRef);
57
59
  const handleClick = e => {
58
60
  setSelectedTag(!selectedTag);
59
61
  onChange?.(!selectedTag);
@@ -68,7 +70,7 @@ const SelectableTag = _ref => {
68
70
  onMouseEnter: () => false
69
71
  }, /*#__PURE__*/React__default["default"].createElement(Tag["default"], _rollupPluginBabelHelpers["extends"]({
70
72
  "aria-pressed": selectedTag !== false,
71
- ref: tagRef,
73
+ ref: combinedRef,
72
74
  size: size,
73
75
  renderIcon: renderIcon,
74
76
  disabled: disabled,
@@ -82,7 +84,7 @@ const SelectableTag = _ref => {
82
84
  }
83
85
  return /*#__PURE__*/React__default["default"].createElement(Tag["default"], _rollupPluginBabelHelpers["extends"]({
84
86
  "aria-pressed": selectedTag !== false,
85
- ref: tagRef,
87
+ ref: combinedRef,
86
88
  size: size,
87
89
  renderIcon: renderIcon,
88
90
  disabled: disabled,
@@ -93,7 +95,7 @@ const SelectableTag = _ref => {
93
95
  title: text,
94
96
  className: `${prefix}--tag__label`
95
97
  }, text));
96
- };
98
+ });
97
99
  SelectableTag.propTypes = {
98
100
  /**
99
101
  * Provide a custom className that is applied to the containing <span>
@@ -4,7 +4,7 @@
4
4
  * This source code is licensed under the Apache-2.0 license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import React, { ComponentType, FunctionComponent } from 'react';
7
+ import React, { type ComponentType, type FunctionComponent } from 'react';
8
8
  export type TreeNodeProps = {
9
9
  /**
10
10
  * **Note:** this is controlled by the parent TreeView component, do not set manually.
@@ -57,10 +57,17 @@ const TreeNode = /*#__PURE__*/React__default["default"].forwardRef((_ref, forwar
57
57
  } = React.useRef(nodeId || uniqueId["default"]());
58
58
  const controllableExpandedState = useControllableState.useControllableState({
59
59
  value: isExpanded,
60
- onChange: onToggle,
61
- defaultValue: defaultIsExpanded
60
+ onChange: newValue => {
61
+ onToggle?.(undefined, {
62
+ id,
63
+ isExpanded: newValue,
64
+ label,
65
+ value
66
+ });
67
+ },
68
+ defaultValue: defaultIsExpanded ?? false
62
69
  });
63
- const uncontrollableExpandedState = React.useState(isExpanded);
70
+ const uncontrollableExpandedState = React.useState(isExpanded ?? false);
64
71
  const [expanded, setExpanded] = enableTreeviewControllable ? controllableExpandedState : uncontrollableExpandedState;
65
72
  const currentNode = React.useRef(null);
66
73
  const currentNodeLabel = React.useRef(null);
@@ -254,7 +261,7 @@ const TreeNode = /*#__PURE__*/React__default["default"].forwardRef((_ref, forwar
254
261
  }
255
262
  if (!enableTreeviewControllable) {
256
263
  // sync props and state
257
- setExpanded(isExpanded);
264
+ setExpanded(isExpanded ?? false);
258
265
  }
259
266
  }, [children, depth, Icon, isExpanded, enableTreeviewControllable, setExpanded]);
260
267
  const treeNodeProps = {
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright IBM Corp. 2016, 2023
2
+ * Copyright IBM Corp. 2016, 2025
3
3
  *
4
4
  * This source code is licensed under the Apache-2.0 license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -54,7 +54,11 @@ const TreeView = _ref => {
54
54
  const treeWalker = React.useRef(treeRootRef?.current);
55
55
  const controllableSelectionState = useControllableState.useControllableState({
56
56
  value: preselected,
57
- onChange: onSelect,
57
+ onChange: newSelected => {
58
+ onSelect?.(undefined, {
59
+ activeNodeId: newSelected[0]
60
+ });
61
+ },
58
62
  defaultValue: []
59
63
  });
60
64
  const uncontrollableSelectionState = React.useState(preselected ?? []);
@@ -137,11 +141,7 @@ const TreeView = _ref => {
137
141
  });
138
142
  function handleKeyDown(event) {
139
143
  event.stopPropagation();
140
- if (match.matches(event, [keys.ArrowUp, keys.ArrowDown, keys.Home, keys.End,
141
- // @ts-ignore - `matches` doesn't like the object syntax without missing properties
142
- {
143
- code: 'KeyA'
144
- }])) {
144
+ if (match.matches(event, [keys.ArrowUp, keys.ArrowDown, keys.Home, keys.End])) {
145
145
  event.preventDefault();
146
146
  }
147
147
  if (!treeWalker.current) {
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright IBM Corp. 2016, 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, { type ReactElement } from 'react';
8
+ interface ClickListenerProps {
9
+ children: ReactElement;
10
+ onClickOutside: (event: MouseEvent) => void;
11
+ }
12
+ export declare const ClickListener: ({ children, onClickOutside, }: ClickListenerProps) => React.ReactElement<any, string | React.JSXElementConstructor<any>>;
13
+ export {};
@@ -9,75 +9,43 @@
9
9
 
10
10
  Object.defineProperty(exports, '__esModule', { value: true });
11
11
 
12
- var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js');
13
- var PropTypes = require('prop-types');
14
12
  var React = require('react');
15
13
 
16
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
-
18
- var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
19
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
20
-
21
- /**
22
- * Generic component used for reacting to a click event happening outside of a
23
- * given `children` element.
24
- */
25
- class ClickListener extends React__default["default"].Component {
26
- static getEventTarget(evt) {
27
- // support Shadow DOM
28
- if (evt.composed && typeof evt.composedPath === 'function') {
29
- return evt.composedPath()[0];
14
+ const ClickListener = _ref => {
15
+ let {
16
+ children,
17
+ onClickOutside
18
+ } = _ref;
19
+ const elementRef = React.useRef(null);
20
+ const getEventTarget = event => {
21
+ if (event.composed && typeof event.composedPath === 'function') {
22
+ return event.composedPath()[0];
30
23
  }
31
- return evt.target;
32
- }
33
- constructor(props) {
34
- super(props);
35
- // We manually bind handlers in this Component, versus using class
36
- // properties, so that we can properly test the `handleRef` handler.
37
- this.handleRef = this.handleRef.bind(this);
38
- this.handleDocumentClick = this.handleDocumentClick.bind(this);
39
- }
40
- componentDidMount() {
41
- document.addEventListener('click', this.handleDocumentClick);
42
- }
43
- componentWillUnmount() {
44
- document.removeEventListener('click', this.handleDocumentClick);
45
- }
46
- handleDocumentClick(evt) {
47
- if (this.element) {
48
- if (this.element.contains && !this.element.contains(ClickListener.getEventTarget(evt))) {
49
- this.props.onClickOutside(evt);
24
+ return event.target;
25
+ };
26
+ const handleDocumentClick = event => {
27
+ if (elementRef.current?.contains) {
28
+ const eventTarget = getEventTarget(event);
29
+ if (eventTarget instanceof Node && !elementRef.current.contains(eventTarget)) {
30
+ onClickOutside(event);
50
31
  }
51
32
  }
52
- }
53
- handleRef(el) {
54
- const {
55
- children
56
- } = this.props;
57
- this.element = el;
58
-
59
- /**
60
- * One important note, `children.ref` corresponds to a `ref` prop passed in
61
- * directly to the child, not necessarily a `ref` defined in the component.
62
- * This means that here we target the following `ref` location:
63
- *
64
- * <ClickListener onClickOutside={() => {}}>
65
- * <Child ref={targetedRefHere} />
66
- * </ClickListener>
67
- */
68
- if (children.ref && typeof children.ref === 'function') {
33
+ };
34
+ React.useEffect(() => {
35
+ document.addEventListener('click', handleDocumentClick);
36
+ return () => {
37
+ document.removeEventListener('click', handleDocumentClick);
38
+ };
39
+ }, [onClickOutside]);
40
+ const handleRef = el => {
41
+ elementRef.current = el;
42
+ if ('ref' in children && typeof children.ref === 'function') {
69
43
  children.ref(el);
70
44
  }
71
- }
72
- render() {
73
- return /*#__PURE__*/React__default["default"].cloneElement(this.props.children, {
74
- ref: this.handleRef
75
- });
76
- }
77
- }
78
- _rollupPluginBabelHelpers.defineProperty(ClickListener, "propTypes", {
79
- children: PropTypes__default["default"].element.isRequired,
80
- onClickOutside: PropTypes__default["default"].func.isRequired
81
- });
45
+ };
46
+ return /*#__PURE__*/React.cloneElement(children, {
47
+ ref: handleRef
48
+ });
49
+ };
82
50
 
83
- exports["default"] = ClickListener;
51
+ exports.ClickListener = ClickListener;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Copyright IBM Corp. 2016, 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
+ interface UseControllableStateConfig<T> {
8
+ /** The name of the component. */
9
+ name?: string;
10
+ /**
11
+ * The default value for the component. This value is used when the component
12
+ * is uncontrolled (i.e., when `value` is not provided).
13
+ */
14
+ defaultValue: T;
15
+ /**
16
+ * This callback is called whenever the state changes. It's useful for
17
+ * communicating state updates to parent components of controlled components.
18
+ */
19
+ onChange?: (value: T) => void;
20
+ /**
21
+ * Controlled value. If this prop is omitted, the state will be uncontrolled.
22
+ */
23
+ value?: T;
24
+ }
25
+ /**
26
+ * This hook simplifies the behavior of a component that has state which can be
27
+ * both controlled and uncontrolled. It works like `useState`. You can use the
28
+ * `onChange` callback to communicate state updates to parent components.
29
+ *
30
+ * Note: This hook will warn if the component switches between controlled and
31
+ * uncontrolled states.
32
+ */
33
+ export declare const useControllableState: <T>({ defaultValue, name, onChange, value, }: UseControllableStateConfig<T>) => [T, (stateOrUpdater: T | ((prev: T) => T)) => void];
34
+ export {};