@@ -7,13 +7,13 @@ import useEvent from 'rc-util/lib/hooks/useEvent';
7
7
import useId from 'rc-util/lib/hooks/useId' ;
8
8
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect' ;
9
9
import isMobile from 'rc-util/lib/isMobile' ;
10
- import warning from 'rc-util/lib/warning' ;
11
10
import * as React from 'react' ;
12
11
import type { TriggerContextProps } from './context' ;
13
12
import TriggerContext from './context' ;
14
13
import useAction from './hooks/useAction' ;
15
14
import useAlign from './hooks/useAlign' ;
16
15
import useWatch from './hooks/useWatch' ;
16
+ import useWinClick from './hooks/useWinClick' ;
17
17
import type {
18
18
ActionType ,
19
19
AlignType ,
@@ -25,7 +25,7 @@ import type {
25
25
} from './interface' ;
26
26
import Popup from './Popup' ;
27
27
import TriggerWrapper from './TriggerWrapper' ;
28
- import { getAlignPopupClassName , getMotion , getWin } from './util' ;
28
+ import { getAlignPopupClassName , getMotion } from './util' ;
29
29
30
30
export type {
31
31
BuildInPlacements ,
@@ -498,66 +498,16 @@ export function generateTrigger(
498
498
}
499
499
500
500
// Click to hide is special action since click popup element should not hide
501
- React . useEffect ( ( ) => {
502
- if ( clickToHide && popupEle && ( ! mask || maskClosable ) ) {
503
- let clickInside = false ;
504
-
505
- // User may mouseDown inside and drag out of popup and mouse up
506
- // Record here to prevent close
507
- const onWindowMouseDown = ( { target } : MouseEvent ) => {
508
- clickInside = inPopupOrChild ( target ) ;
509
- } ;
510
-
511
- const onWindowClick = ( { target } : MouseEvent ) => {
512
- if ( openRef . current && ! clickInside && ! inPopupOrChild ( target ) ) {
513
- triggerOpen ( false ) ;
514
- }
515
- } ;
516
-
517
- const win = getWin ( popupEle ) ;
518
-
519
- const targetRoot = targetEle ?. getRootNode ( ) ;
520
-
521
- win . addEventListener ( 'mousedown' , onWindowMouseDown ) ;
522
- win . addEventListener ( 'click' , onWindowClick ) ;
523
-
524
- // shadow root
525
- const inShadow = targetRoot && targetRoot !== targetEle . ownerDocument ;
526
- if ( inShadow ) {
527
- ( targetRoot as ShadowRoot ) . addEventListener (
528
- 'mousedown' ,
529
- onWindowMouseDown ,
530
- ) ;
531
- ( targetRoot as ShadowRoot ) . addEventListener ( 'click' , onWindowClick ) ;
532
- }
533
-
534
- // Warning if target and popup not in same root
535
- if ( process . env . NODE_ENV !== 'production' ) {
536
- const popupRoot = popupEle . getRootNode ( ) ;
537
-
538
- warning (
539
- targetRoot === popupRoot ,
540
- `trigger element and popup element should in same shadow root.` ,
541
- ) ;
542
- }
543
-
544
- return ( ) => {
545
- win . removeEventListener ( 'mousedown' , onWindowMouseDown ) ;
546
- win . removeEventListener ( 'click' , onWindowClick ) ;
547
-
548
- if ( inShadow ) {
549
- ( targetRoot as ShadowRoot ) . removeEventListener (
550
- 'mousedown' ,
551
- onWindowMouseDown ,
552
- ) ;
553
- ( targetRoot as ShadowRoot ) . removeEventListener (
554
- 'click' ,
555
- onWindowClick ,
556
- ) ;
557
- }
558
- } ;
559
- }
560
- } , [ clickToHide , targetEle , popupEle , mask , maskClosable ] ) ;
501
+ useWinClick (
502
+ mergedOpen ,
503
+ clickToHide ,
504
+ targetEle ,
505
+ popupEle ,
506
+ mask ,
507
+ maskClosable ,
508
+ inPopupOrChild ,
509
+ triggerOpen ,
510
+ ) ;
561
511
562
512
// ======================= Action: Hover ========================
563
513
const hoverToShow = showActions . has ( 'hover' ) ;
0 commit comments