@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
@@ -53,9 +53,9 @@ const InlineLoading = _ref => {
53
53
  className: `${prefix}--inline-loading__checkmark-container`
54
54
  }, /*#__PURE__*/React__default.createElement("title", null, iconLabel));
55
55
  }
56
- if (status === 'inactive' || status === 'active') {
56
+ if (status === 'active') {
57
57
  if (!iconDescription) {
58
- iconLabel = status === 'active' ? 'loading' : 'not loading';
58
+ iconLabel = 'loading';
59
59
  }
60
60
  return /*#__PURE__*/React__default.createElement(Loading, {
61
61
  small: true,
@@ -64,6 +64,14 @@ const InlineLoading = _ref => {
64
64
  active: status === 'active'
65
65
  });
66
66
  }
67
+ if (status === 'inactive') {
68
+ if (!iconDescription) {
69
+ iconLabel = 'not loading';
70
+ }
71
+ return /*#__PURE__*/React__default.createElement("title", {
72
+ className: `${prefix}--inline-loading__inactive-status`
73
+ }, iconLabel);
74
+ }
67
75
  return undefined;
68
76
  };
69
77
  const loadingText = description && /*#__PURE__*/React__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
  */
@@ -333,7 +333,7 @@ const MenuItemRadioGroup = /*#__PURE__*/forwardRef(function MenuItemRadioGroup(_
333
333
  const [selection, setSelection] = useControllableState({
334
334
  value: selectedItem,
335
335
  onChange,
336
- defaultValue: defaultSelectedItem
336
+ defaultValue: defaultSelectedItem ?? {}
337
337
  });
338
338
  function handleClick(item, e) {
339
339
  setSelection(item);
@@ -11,7 +11,7 @@ import { OverflowMenuVertical } from '@carbon/icons-react';
11
11
  import cx from 'classnames';
12
12
  import invariant from 'invariant';
13
13
  import PropTypes from 'prop-types';
14
- import ClickListener from '../../internal/ClickListener.js';
14
+ import { ClickListener } from '../../internal/ClickListener.js';
15
15
  import { DIRECTION_TOP, DIRECTION_BOTTOM, FloatingMenu } from '../../internal/FloatingMenu.js';
16
16
  import { ArrowUp, ArrowRight, ArrowDown, ArrowLeft, Escape } from '../../internal/keyboard/keys.js';
17
17
  import { matches } from '../../internal/keyboard/match.js';
@@ -201,7 +201,7 @@ const OverflowMenu = /*#__PURE__*/forwardRef((_ref, ref) => {
201
201
  }
202
202
  };
203
203
  const handleClickOutside = evt => {
204
- if (open && (!menuBodyRef.current || !menuBodyRef.current.contains(evt.target))) {
204
+ if (open && (!menuBodyRef.current || evt.target instanceof Node && !menuBodyRef.current.contains(evt.target))) {
205
205
  closeMenu();
206
206
  }
207
207
  };
@@ -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;
@@ -7,7 +7,7 @@
7
7
 
8
8
  import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
9
9
  import PropTypes from 'prop-types';
10
- import React__default, { useRef, useState, useLayoutEffect } from 'react';
10
+ import React__default, { forwardRef, useRef, useState, useLayoutEffect } from 'react';
11
11
  import cx from 'classnames';
12
12
  import { useId } from '../../internal/useId.js';
13
13
  import { usePrefix } from '../../internal/usePrefix.js';
@@ -18,10 +18,11 @@ import '../Tooltip/DefinitionTooltip.js';
18
18
  import { Tooltip } from '../Tooltip/Tooltip.js';
19
19
  import '../Text/index.js';
20
20
  import { isEllipsisActive } from './isEllipsisActive.js';
21
+ import mergeRefs from '../../tools/mergeRefs.js';
21
22
  import { Text } from '../Text/Text.js';
22
23
 
23
24
  var _Close;
24
- const DismissibleTag = _ref => {
25
+ const DismissibleTag = /*#__PURE__*/forwardRef((_ref, forwardRef) => {
25
26
  let {
26
27
  className,
27
28
  decorator,
@@ -46,6 +47,7 @@ const DismissibleTag = _ref => {
46
47
  const newElement = tagLabelRef.current?.getElementsByClassName(`${prefix}--tag__label`)[0];
47
48
  setIsEllipsisApplied(isEllipsisActive(newElement));
48
49
  }, [prefix, tagLabelRef]);
50
+ const combinedRef = mergeRefs(tagLabelRef, forwardRef);
49
51
  const handleClose = event => {
50
52
  if (onClose) {
51
53
  event.stopPropagation();
@@ -69,7 +71,7 @@ const DismissibleTag = _ref => {
69
71
  } = other;
70
72
  const dismissLabel = `Dismiss "${text}"`;
71
73
  return /*#__PURE__*/React__default.createElement(Tag, _extends({
72
- ref: tagLabelRef,
74
+ ref: combinedRef,
73
75
  type: type,
74
76
  size: size,
75
77
  renderIcon: renderIcon,
@@ -96,7 +98,7 @@ const DismissibleTag = _ref => {
96
98
  disabled: disabled,
97
99
  "aria-label": title
98
100
  }, _Close || (_Close = /*#__PURE__*/React__default.createElement(Close, null))))));
99
- };
101
+ });
100
102
  DismissibleTag.propTypes = {
101
103
  /**
102
104
  * 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;
@@ -7,7 +7,7 @@
7
7
 
8
8
  import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
9
9
  import PropTypes from 'prop-types';
10
- import React__default, { useRef, useState, useLayoutEffect } from 'react';
10
+ import React__default, { forwardRef, useRef, useState, useLayoutEffect } from 'react';
11
11
  import cx from 'classnames';
12
12
  import { useId } from '../../internal/useId.js';
13
13
  import { usePrefix } from '../../internal/usePrefix.js';
@@ -16,9 +16,10 @@ import '../Tooltip/DefinitionTooltip.js';
16
16
  import { Tooltip } from '../Tooltip/Tooltip.js';
17
17
  import '../Text/index.js';
18
18
  import { isEllipsisActive } from './isEllipsisActive.js';
19
+ import mergeRefs from '../../tools/mergeRefs.js';
19
20
  import { Text } from '../Text/Text.js';
20
21
 
21
- const SelectableTag = _ref => {
22
+ const SelectableTag = /*#__PURE__*/forwardRef((_ref, forwardRef) => {
22
23
  let {
23
24
  className,
24
25
  disabled,
@@ -44,6 +45,7 @@ const SelectableTag = _ref => {
44
45
  setIsEllipsisApplied(isEllipsisActive(newElement));
45
46
  }, [prefix, tagRef]);
46
47
  const tooltipClasses = cx(`${prefix}--icon-tooltip`, `${prefix}--tag-label-tooltip`);
48
+ const combinedRef = mergeRefs(tagRef, forwardRef);
47
49
  const handleClick = e => {
48
50
  setSelectedTag(!selectedTag);
49
51
  onChange?.(!selectedTag);
@@ -58,7 +60,7 @@ const SelectableTag = _ref => {
58
60
  onMouseEnter: () => false
59
61
  }, /*#__PURE__*/React__default.createElement(Tag, _extends({
60
62
  "aria-pressed": selectedTag !== false,
61
- ref: tagRef,
63
+ ref: combinedRef,
62
64
  size: size,
63
65
  renderIcon: renderIcon,
64
66
  disabled: disabled,
@@ -72,7 +74,7 @@ const SelectableTag = _ref => {
72
74
  }
73
75
  return /*#__PURE__*/React__default.createElement(Tag, _extends({
74
76
  "aria-pressed": selectedTag !== false,
75
- ref: tagRef,
77
+ ref: combinedRef,
76
78
  size: size,
77
79
  renderIcon: renderIcon,
78
80
  disabled: disabled,
@@ -83,7 +85,7 @@ const SelectableTag = _ref => {
83
85
  title: text,
84
86
  className: `${prefix}--tag__label`
85
87
  }, text));
86
- };
88
+ });
87
89
  SelectableTag.propTypes = {
88
90
  /**
89
91
  * 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.
@@ -47,10 +47,17 @@ const TreeNode = /*#__PURE__*/React__default.forwardRef((_ref, forwardedRef) =>
47
47
  } = useRef(nodeId || uniqueId());
48
48
  const controllableExpandedState = useControllableState({
49
49
  value: isExpanded,
50
- onChange: onToggle,
51
- defaultValue: defaultIsExpanded
50
+ onChange: newValue => {
51
+ onToggle?.(undefined, {
52
+ id,
53
+ isExpanded: newValue,
54
+ label,
55
+ value
56
+ });
57
+ },
58
+ defaultValue: defaultIsExpanded ?? false
52
59
  });
53
- const uncontrollableExpandedState = useState(isExpanded);
60
+ const uncontrollableExpandedState = useState(isExpanded ?? false);
54
61
  const [expanded, setExpanded] = enableTreeviewControllable ? controllableExpandedState : uncontrollableExpandedState;
55
62
  const currentNode = useRef(null);
56
63
  const currentNodeLabel = useRef(null);
@@ -244,7 +251,7 @@ const TreeNode = /*#__PURE__*/React__default.forwardRef((_ref, forwardedRef) =>
244
251
  }
245
252
  if (!enableTreeviewControllable) {
246
253
  // sync props and state
247
- setExpanded(isExpanded);
254
+ setExpanded(isExpanded ?? false);
248
255
  }
249
256
  }, [children, depth, Icon, isExpanded, enableTreeviewControllable, setExpanded]);
250
257
  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.
@@ -44,7 +44,11 @@ const TreeView = _ref => {
44
44
  const treeWalker = useRef(treeRootRef?.current);
45
45
  const controllableSelectionState = useControllableState({
46
46
  value: preselected,
47
- onChange: onSelect,
47
+ onChange: newSelected => {
48
+ onSelect?.(undefined, {
49
+ activeNodeId: newSelected[0]
50
+ });
51
+ },
48
52
  defaultValue: []
49
53
  });
50
54
  const uncontrollableSelectionState = useState(preselected ?? []);
@@ -127,11 +131,7 @@ const TreeView = _ref => {
127
131
  });
128
132
  function handleKeyDown(event) {
129
133
  event.stopPropagation();
130
- if (matches(event, [ArrowUp, ArrowDown, Home, End,
131
- // @ts-ignore - `matches` doesn't like the object syntax without missing properties
132
- {
133
- code: 'KeyA'
134
- }])) {
134
+ if (matches(event, [ArrowUp, ArrowDown, Home, End])) {
135
135
  event.preventDefault();
136
136
  }
137
137
  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 {};
@@ -5,70 +5,43 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import { defineProperty as _defineProperty } from '../_virtual/_rollupPluginBabelHelpers.js';
9
- import PropTypes from 'prop-types';
10
- import React__default from 'react';
8
+ import { useRef, useEffect, cloneElement } from 'react';
11
9
 
12
- /**
13
- * Generic component used for reacting to a click event happening outside of a
14
- * given `children` element.
15
- */
16
- class ClickListener extends React__default.Component {
17
- static getEventTarget(evt) {
18
- // support Shadow DOM
19
- if (evt.composed && typeof evt.composedPath === 'function') {
20
- return evt.composedPath()[0];
10
+ const ClickListener = _ref => {
11
+ let {
12
+ children,
13
+ onClickOutside
14
+ } = _ref;
15
+ const elementRef = useRef(null);
16
+ const getEventTarget = event => {
17
+ if (event.composed && typeof event.composedPath === 'function') {
18
+ return event.composedPath()[0];
21
19
  }
22
- return evt.target;
23
- }
24
- constructor(props) {
25
- super(props);
26
- // We manually bind handlers in this Component, versus using class
27
- // properties, so that we can properly test the `handleRef` handler.
28
- this.handleRef = this.handleRef.bind(this);
29
- this.handleDocumentClick = this.handleDocumentClick.bind(this);
30
- }
31
- componentDidMount() {
32
- document.addEventListener('click', this.handleDocumentClick);
33
- }
34
- componentWillUnmount() {
35
- document.removeEventListener('click', this.handleDocumentClick);
36
- }
37
- handleDocumentClick(evt) {
38
- if (this.element) {
39
- if (this.element.contains && !this.element.contains(ClickListener.getEventTarget(evt))) {
40
- this.props.onClickOutside(evt);
20
+ return event.target;
21
+ };
22
+ const handleDocumentClick = event => {
23
+ if (elementRef.current?.contains) {
24
+ const eventTarget = getEventTarget(event);
25
+ if (eventTarget instanceof Node && !elementRef.current.contains(eventTarget)) {
26
+ onClickOutside(event);
41
27
  }
42
28
  }
43
- }
44
- handleRef(el) {
45
- const {
46
- children
47
- } = this.props;
48
- this.element = el;
49
-
50
- /**
51
- * One important note, `children.ref` corresponds to a `ref` prop passed in
52
- * directly to the child, not necessarily a `ref` defined in the component.
53
- * This means that here we target the following `ref` location:
54
- *
55
- * <ClickListener onClickOutside={() => {}}>
56
- * <Child ref={targetedRefHere} />
57
- * </ClickListener>
58
- */
59
- if (children.ref && typeof children.ref === 'function') {
29
+ };
30
+ useEffect(() => {
31
+ document.addEventListener('click', handleDocumentClick);
32
+ return () => {
33
+ document.removeEventListener('click', handleDocumentClick);
34
+ };
35
+ }, [onClickOutside]);
36
+ const handleRef = el => {
37
+ elementRef.current = el;
38
+ if ('ref' in children && typeof children.ref === 'function') {
60
39
  children.ref(el);
61
40
  }
62
- }
63
- render() {
64
- return /*#__PURE__*/React__default.cloneElement(this.props.children, {
65
- ref: this.handleRef
66
- });
67
- }
68
- }
69
- _defineProperty(ClickListener, "propTypes", {
70
- children: PropTypes.element.isRequired,
71
- onClickOutside: PropTypes.func.isRequired
72
- });
41
+ };
42
+ return /*#__PURE__*/cloneElement(children, {
43
+ ref: handleRef
44
+ });
45
+ };
73
46
 
74
- export { ClickListener as default };
47
+ export { 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 {};
@@ -9,58 +9,43 @@ import { useState, useRef, useEffect } from 'react';
9
9
  import { warning } from './warning.js';
10
10
 
11
11
  /**
12
- * This custom hook simplifies the behavior of a component if it has state that
13
- * can be both controlled and uncontrolled. It functions identical to a
14
- * useState() hook and provides [state, setState] for you to use. You can use
15
- * the `onChange` argument to allow updates to the `state` to be communicated to
16
- * owners of controlled components.
12
+ * This hook simplifies the behavior of a component that has state which can be
13
+ * both controlled and uncontrolled. It works like `useState`. You can use the
14
+ * `onChange` callback to communicate state updates to parent components.
17
15
  *
18
- * Note: this hook will warn if a component is switching from controlled to
19
- * uncontrolled, or vice-versa.
20
- *
21
- * @param {object} config
22
- * @param {string} [config.name] - the name of the custom component
23
- * @param {any} config.defaultValue - the default value used for the state. This will be
24
- * the fallback value used if `value` is not defined.
25
- * @param {Function|undefined} config.onChange - an optional function that is called when
26
- * the value of the state changes. This is useful for communicating to parents of
27
- * controlled components that the value is requesting to be changed.
28
- * @param {any} config.value - a controlled value. Omitting this means that the state is
29
- * uncontrolled
30
- * @returns {[any, (v: any) => void]}
16
+ * Note: This hook will warn if the component switches between controlled and
17
+ * uncontrolled states.
31
18
  */
32
- function useControllableState(_ref) {
19
+ const useControllableState = _ref => {
33
20
  let {
34
21
  defaultValue,
35
22
  name = 'custom',
36
23
  onChange,
37
24
  value
38
25
  } = _ref;
39
- const [state, internalSetState] = useState(value ?? defaultValue);
26
+ const [state, internalSetState] = useState(typeof value !== 'undefined' ? value : defaultValue);
40
27
  const controlled = useRef(null);
41
28
  if (controlled.current === null) {
42
- controlled.current = value !== undefined;
29
+ controlled.current = typeof value !== 'undefined';
43
30
  }
44
- function setState(stateOrUpdater) {
45
- const value = typeof stateOrUpdater === 'function' ? stateOrUpdater(state) : stateOrUpdater;
31
+ const setState = stateOrUpdater => {
32
+ const newValue = typeof stateOrUpdater === 'function' ? stateOrUpdater(state) : stateOrUpdater;
46
33
  if (controlled.current === false) {
47
- internalSetState(value);
34
+ internalSetState(newValue);
48
35
  }
49
36
  if (onChange) {
50
- onChange(value);
37
+ onChange(newValue);
51
38
  }
52
- }
39
+ };
53
40
  useEffect(() => {
54
- const controlledValue = value !== undefined;
41
+ const controlledValue = typeof value !== 'undefined';
55
42
 
56
43
  // Uncontrolled -> Controlled
57
- // If the component prop is uncontrolled, the prop value should be undefined
58
44
  if (controlled.current === false && controlledValue) {
59
45
  process.env.NODE_ENV !== "production" ? warning(false, `A component is changing an uncontrolled ${name} component to be controlled. ` + 'This is likely caused by the value changing to a defined value ' + 'from undefined. Decide between using a controlled or uncontrolled ' + 'value for the lifetime of the component. ' + 'More info: https://reactjs.org/link/controlled-components') : void 0;
60
46
  }
61
47
 
62
48
  // Controlled -> Uncontrolled
63
- // If the component prop is controlled, the prop value should be defined
64
49
  if (controlled.current === true && !controlledValue) {
65
50
  process.env.NODE_ENV !== "production" ? warning(false, `A component is changing a controlled ${name} component to be uncontrolled. ` + 'This is likely caused by the value changing to an undefined value ' + 'from a defined one. Decide between using a controlled or ' + 'uncontrolled value for the lifetime of the component. ' + 'More info: https://reactjs.org/link/controlled-components') : void 0;
66
51
  }
@@ -69,6 +54,6 @@ function useControllableState(_ref) {
69
54
  return [value, setState];
70
55
  }
71
56
  return [state, setState];
72
- }
57
+ };
73
58
 
74
59
  export { useControllableState };
@@ -0,0 +1,8 @@
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 { type RefObject } from 'react';
8
+ export declare const useOutsideClick: <T extends HTMLElement>(ref: RefObject<T>, callback: (event: MouseEvent) => void) => void;