@@ -59,20 +59,37 @@ const Tooltip = ({
59
59
60
60
const handleShow = ( value : boolean ) => {
61
61
setRendered ( true )
62
-
63
62
/**
64
63
* wait for the component to render and calculate position
65
64
* before actually showing
66
65
*/
67
66
setTimeout ( ( ) => {
68
- if ( setIsOpen ) {
69
- setIsOpen ( value )
70
- } else if ( isOpen === undefined ) {
67
+ setIsOpen ?.( value )
68
+ if ( isOpen === undefined ) {
71
69
setShow ( value )
72
70
}
73
71
} , 10 )
74
72
}
75
73
74
+ /**
75
+ * this replicates the effect from `handleShow()`
76
+ * when `isOpen` is changed from outside
77
+ */
78
+ useEffect ( ( ) => {
79
+ if ( isOpen === undefined ) {
80
+ return ( ) => null
81
+ }
82
+ if ( isOpen ) {
83
+ setRendered ( true )
84
+ }
85
+ const timeout = setTimeout ( ( ) => {
86
+ setShow ( isOpen )
87
+ } , 10 )
88
+ return ( ) => {
89
+ clearTimeout ( timeout )
90
+ }
91
+ } , [ isOpen ] )
92
+
76
93
useEffect ( ( ) => {
77
94
if ( show === wasShowing . current ) {
78
95
return
@@ -318,7 +335,11 @@ const Tooltip = ({
318
335
} )
319
336
parentObserver . disconnect ( )
320
337
}
321
- } , [ anchorRefs , activeAnchor , closeOnEsc , anchorId , events , delayHide , delayShow ] )
338
+ /**
339
+ * rendered is also a dependency to ensure anchor observers are re-registered
340
+ * since `tooltipRef` becomes stale after removing/adding the tooltip to the DOM
341
+ */
342
+ } , [ rendered , anchorRefs , activeAnchor , closeOnEsc , anchorId , events , delayHide , delayShow ] )
322
343
323
344
useEffect ( ( ) => {
324
345
if ( position ) {
@@ -371,18 +392,7 @@ const Tooltip = ({
371
392
return ( ) => {
372
393
mounted = false
373
394
}
374
- } , [
375
- show ,
376
- isOpen ,
377
- anchorId ,
378
- activeAnchor ,
379
- content ,
380
- html ,
381
- place ,
382
- offset ,
383
- positionStrategy ,
384
- position ,
385
- ] )
395
+ } , [ show , anchorId , activeAnchor , content , html , place , offset , positionStrategy , position ] )
386
396
387
397
useEffect ( ( ) => {
388
398
return ( ) => {
@@ -396,9 +406,7 @@ const Tooltip = ({
396
406
} , [ ] )
397
407
398
408
const hasContentOrChildren = Boolean ( html || content || children )
399
- const canShow = Boolean (
400
- hasContentOrChildren && ( isOpen || show ) && Object . keys ( inlineStyles ) . length > 0 ,
401
- )
409
+ const canShow = Boolean ( hasContentOrChildren && show && Object . keys ( inlineStyles ) . length > 0 )
402
410
403
411
return rendered ? (
404
412
< WrapperElement
0 commit comments