@@ -26,7 +26,6 @@ import { OverflowMenuVertical } from '@carbon/icons-react';
26
26
import classNames from 'classnames' ;
27
27
import invariant from 'invariant' ;
28
28
import PropTypes from 'prop-types' ;
29
- import { ClickListener } from '../../internal/ClickListener' ;
30
29
import {
31
30
DIRECTION_BOTTOM ,
32
31
DIRECTION_TOP ,
@@ -41,6 +40,7 @@ import deprecate from '../../prop-types/deprecate';
41
40
import mergeRefs from '../../tools/mergeRefs' ;
42
41
import setupGetInstanceId from '../../tools/setupGetInstanceId' ;
43
42
import { IconButton } from '../IconButton' ;
43
+ import { useOutsideClick } from '../../internal/useOutsideClick' ;
44
44
45
45
const getInstanceId = setupGetInstanceId ( ) ;
46
46
@@ -275,6 +275,7 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
275
275
const prevOpenState = useRef ( open ) ;
276
276
/** The element ref of the tooltip's trigger button. */
277
277
const triggerRef = useRef < HTMLButtonElement > ( null ) ;
278
+ const wrapperRef = useRef < HTMLSpanElement > ( null ) ;
278
279
279
280
// Sync open prop changes.
280
281
useEffect ( ( ) => {
@@ -299,6 +300,16 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
299
300
prevOpenState . current = open ;
300
301
} , [ open , onClose ] ) ;
301
302
303
+ useOutsideClick ( wrapperRef , ( { target } ) => {
304
+ if (
305
+ open &&
306
+ ( ! menuBodyRef . current ||
307
+ ( target instanceof Node && ! menuBodyRef . current . contains ( target ) ) )
308
+ ) {
309
+ closeMenu ( ) ;
310
+ }
311
+ } ) ;
312
+
302
313
const focusMenuEl = useCallback ( ( ) => {
303
314
if ( triggerRef . current ) {
304
315
triggerRef . current . focus ( ) ;
@@ -369,17 +380,6 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
369
380
}
370
381
} ;
371
382
372
- const handleClickOutside = ( evt : globalThis . MouseEvent ) => {
373
- if (
374
- open &&
375
- ( ! menuBodyRef . current ||
376
- ( evt . target instanceof Node &&
377
- ! menuBodyRef . current . contains ( evt . target ) ) )
378
- ) {
379
- closeMenu ( ) ;
380
- }
381
- } ;
382
-
383
383
/**
384
384
* Focuses the next enabled overflow menu item given the currently focused
385
385
* item index and direction to move.
@@ -545,10 +545,11 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
545
545
) ;
546
546
547
547
return (
548
- < ClickListener onClickOutside = { handleClickOutside } >
548
+ < >
549
549
< span
550
550
className = { `${ prefix } --overflow-menu__wrapper` }
551
- aria-owns = { open ? menuBodyId : undefined } >
551
+ aria-owns = { open ? menuBodyId : undefined }
552
+ ref = { wrapperRef } >
552
553
< IconButton
553
554
{ ...other }
554
555
type = "button"
@@ -569,7 +570,7 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
569
570
</ IconButton >
570
571
{ open && hasMountedTrigger && wrappedMenuBody }
571
572
</ span >
572
- </ ClickListener >
573
+ </ >
573
574
) ;
574
575
}
575
576
) ;
0 commit comments