@@ -38,12 +38,10 @@ export const ContextOverlay = ({
3838 usePlaceholder = false ,
3939 ...otherPopoverProps
4040} : ContextOverlayProps ) => {
41- const placeholderRef = React . useRef ( null ) ;
41+ const placeholderRef = React . useRef < HTMLElement > ( null ) ;
4242 const eventMemory = React . useRef < undefined | "mouseenter" | "focusin" | "click" > ( undefined ) ;
4343 const swapDelay = React . useRef < null | NodeJS . Timeout > ( null ) ;
44- const interactionKind = React . useRef < InteractionKind > (
45- otherPopoverProps [ "interactionKind" ] ?? InteractionKind . CLICK
46- ) ;
44+ const interactionKind = React . useRef < InteractionKind > ( otherPopoverProps . interactionKind ?? InteractionKind . CLICK ) ;
4745 const swapDelayTime = 15 ;
4846 const [ placeholder , setPlaceholder ] = React . useState < boolean > (
4947 // use placeholder only for "simple" overlays without special states
@@ -57,9 +55,7 @@ export const ContextOverlay = ({
5755 const swap = ( ev : MouseEvent | globalThis . FocusEvent ) => {
5856 const waitForClick =
5957 interactionKind . current === InteractionKind . CLICK ||
60- interactionKind . current === InteractionKind . CLICK_TARGET_ONLY
61- ? true
62- : false ;
58+ interactionKind . current === InteractionKind . CLICK_TARGET_ONLY ;
6359
6460 if ( swapDelay . current ) {
6561 clearTimeout ( swapDelay . current ) ;
@@ -76,58 +72,56 @@ export const ContextOverlay = ({
7672 } ;
7773
7874 React . useEffect ( ( ) => {
75+ interactionKind . current = otherPopoverProps . interactionKind ?? InteractionKind . CLICK ;
7976 const waitForClick =
8077 interactionKind . current === InteractionKind . CLICK ||
81- interactionKind . current === InteractionKind . CLICK_TARGET_ONLY
82- ? true
83- : false ;
78+ interactionKind . current === InteractionKind . CLICK_TARGET_ONLY ;
8479 const removeEvents = ( ) => {
8580 if ( placeholderRef . current ) {
86- ( placeholderRef . current as HTMLElement ) . removeEventListener ( "click" , swap ) ;
87- ( placeholderRef . current as HTMLElement ) . removeEventListener ( "mouseenter" , swap ) ;
88- ( placeholderRef . current as HTMLElement ) . removeEventListener ( "focusin" , swap ) ;
81+ placeholderRef . current . removeEventListener ( "click" , swap ) ;
82+ placeholderRef . current . removeEventListener ( "mouseenter" , swap ) ;
83+ placeholderRef . current . removeEventListener ( "focusin" , swap ) ;
8984 }
9085 return ;
9186 } ;
9287 if ( placeholderRef . current ) {
9388 removeEvents ( ) ; // remove events in case of interaction kind changed during existence
9489 if ( waitForClick ) {
95- ( placeholderRef . current as HTMLElement ) . addEventListener ( "click" , swap ) ;
90+ placeholderRef . current . addEventListener ( "click" , swap ) ;
9691 } else {
97- ( placeholderRef . current as HTMLElement ) . addEventListener ( "mouseenter" , swap ) ;
98- ( placeholderRef . current as HTMLElement ) . addEventListener ( "focusin" , swap ) ;
92+ placeholderRef . current . addEventListener ( "mouseenter" , swap ) ;
93+ placeholderRef . current . addEventListener ( "focusin" , swap ) ;
9994 }
10095 return ( ) => {
10196 removeEvents ( ) ;
10297 } ;
10398 }
10499 return ( ) => { } ;
105- } , [ ! ! placeholderRef . current ] ) ;
100+ } , [ ! ! placeholderRef . current , otherPopoverProps . interactionKind ] ) ;
106101
107102 const refocus = React . useCallback ( ( node ) => {
108- if ( eventMemory . current && node ) {
109- const target = node . targetRef . current . children [ 0 ] ;
110- if ( target ) {
111- switch ( eventMemory . current ) {
112- case "focusin" :
113- target . focus ( ) ;
114- break ;
115- case "click" :
116- target . click ( ) ;
117- break ;
118- case "mouseenter" :
119- // re-check if the cursor is still over the element after swapping the placeholder before triggering the event to bubble up
120- ( target as HTMLElement ) . addEventListener (
121- "mouseover" ,
122- ( ) => ( target as HTMLElement ) . dispatchEvent ( new MouseEvent ( "mouseover" , { bubbles : true } ) ) ,
123- {
124- capture : true ,
125- once : true ,
126- }
127- ) ;
128- break ;
129- }
130- }
103+ const target = node ?. targetRef . current . children [ 0 ] ;
104+ if ( ! eventMemory . current || ! target ) {
105+ return ;
106+ }
107+ switch ( eventMemory . current ) {
108+ case "focusin" :
109+ target . focus ( ) ;
110+ break ;
111+ case "click" :
112+ target . click ( ) ;
113+ break ;
114+ case "mouseenter" :
115+ // re-check if the cursor is still over the element after swapping the placeholder before triggering the event to bubble up
116+ ( target as HTMLElement ) . addEventListener (
117+ "mouseover" ,
118+ ( ) => ( target as HTMLElement ) . dispatchEvent ( new MouseEvent ( "mouseover" , { bubbles : true } ) ) ,
119+ {
120+ capture : true ,
121+ once : true ,
122+ }
123+ ) ;
124+ break ;
131125 }
132126 } , [ ] ) ;
133127
0 commit comments