packageDocumentation module import * as React from "react" ; public export interface WithOnOutsideClickProps { /** Outside click callback function */ onOutsideClick ? : ( event : MouseEvent ) => any ; /** If false outside click processing and closing are skipped if click occurs in another Popup component, default to false. */ closeOnNestedPopupOutsideClick ? : boolean ; } public export const withOnOutsideClick = < ComponentProps extends { } > ( Component , defaultOnOutsideClick ? , useCapture = true , usePointerEvents = true ) => { return class WithOnOutsideClick extends React . PureComponent < ComponentProps & WithOnOutsideClickProps > { internal public outsideClickContainerDiv ? : HTMLDivElement | null = null ; internal public isDownOutside = false ; internal public isInCorePopup ( element ) { if ( element . nodeName === "DIV" ) { if ( element . classList && element . classList . contains ( "core-popup" ) ) return true ; if ( element . parentElement && this . isInCorePopup ( element . parentElement ) ) return true ; } else { if ( element . parentElement && this . isInCorePopup ( element . parentElement ) ) return true ; } return false ; } internal public onOutsideClick ( e ) { if ( e . target instanceof Node && e . target . nodeType === Node . ELEMENT_NODE ) { if ( ! this . props . closeOnNestedPopupOutsideClick && this . isInCorePopup ( e . target as HTMLElement ) ) return ; } if ( this . props . onOutsideClick ) return this . props . onOutsideClick ( e ) ; else if ( defaultOnOutsideClick ) return defaultOnOutsideClick ( e ) ; } internal public handleDocumentClick = ( e ) => { if ( ! this . outsideClickContainerDiv || ! ( e . target instanceof Node ) || this . outsideClickContainerDiv . contains ( e . target ) ) return ; return this . onOutsideClick ( e ) ; } ; internal public handleDocumentPointerDown = ( e ) => { this . isDownOutside = true ; if ( this . outsideClickContainerDiv ) { this . isDownOutside = ! ! e . target && ! this . outsideClickContainerDiv . contains ( e . target as any ) ; } } ; internal public handleDocumentPointerUp = ( e ) => { let isUpOutside = true ; if ( this . outsideClickContainerDiv ) { isUpOutside = ! ! e . target && ! this . outsideClickContainerDiv . contains ( e . target as any ) ; } const isOutsideClick = isUpOutside && this . isDownOutside ; this . isDownOutside = false ; isOutsideClick && this . onOutsideClick ( e ) ; } ; public handleOutsideClickContainerDivSet = ( outsideClickContainerDiv ) => { this . outsideClickContainerDiv = outsideClickContainerDiv ; } ; public getParentDocument ( ) { return this . outsideClickContainerDiv ?. ownerDocument ?? document ; } public override componentDidMount ( ) { const outsideClickParentDocument = this . getParentDocument ( ) ; if ( usePointerEvents ) { outsideClickParentDocument . addEventListener ( "pointerdown" , this . handleDocumentPointerDown , useCapture ) ; outsideClickParentDocument . addEventListener ( "pointerup" , this . handleDocumentPointerUp , useCapture ) ; } else outsideClickParentDocument . addEventListener ( "click" , this . handleDocumentClick , useCapture ) ; } internal public override componentWillUnmount ( ) { const outsideClickParentDocument = this . getParentDocument ( ) ; if ( usePointerEvents ) { outsideClickParentDocument . removeEventListener ( "pointerdown" , this . handleDocumentPointerDown , useCapture ) ; outsideClickParentDocument . removeEventListener ( "pointerup" , this . handleDocumentPointerUp , useCapture ) ; } else outsideClickParentDocument . removeEventListener ( "click" , this . handleDocumentClick , useCapture ) ; } public override render ( ) { const { onOutsideClick , closeOnNestedPopupOutsideClick , ... props } = this . props ; return ( < div ref = { this . handleOutsideClickContainerDivSet } > < Component { ... props as ComponentProps } / > < /div>); } } ; } ;