@@ -7,13 +7,13 @@ import useEvent from 'rc-util/lib/hooks/useEvent';
77import useId from 'rc-util/lib/hooks/useId' ;
88import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect' ;
99import isMobile from 'rc-util/lib/isMobile' ;
10- import warning from 'rc-util/lib/warning' ;
1110import * as React from 'react' ;
1211import type { TriggerContextProps } from './context' ;
1312import TriggerContext from './context' ;
1413import useAction from './hooks/useAction' ;
1514import useAlign from './hooks/useAlign' ;
1615import useWatch from './hooks/useWatch' ;
16+ import useWinClick from './hooks/useWinClick' ;
1717import type {
1818 ActionType ,
1919 AlignType ,
@@ -25,7 +25,7 @@ import type {
2525} from './interface' ;
2626import Popup from './Popup' ;
2727import TriggerWrapper from './TriggerWrapper' ;
28- import { getAlignPopupClassName , getMotion , getWin } from './util' ;
28+ import { getAlignPopupClassName , getMotion } from './util' ;
2929
3030export type {
3131 BuildInPlacements ,
@@ -498,66 +498,16 @@ export function generateTrigger(
498498 }
499499
500500 // 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+ ) ;
561511
562512 // ======================= Action: Hover ========================
563513 const hoverToShow = showActions . has ( 'hover' ) ;
0 commit comments