1
- import React , { useEffect , useRef } from 'react' ;
1
+ import React from 'react' ;
2
2
import { Flex , type DefaultComponentType , type FlexAllProps } from 'reflexy/styled' ;
3
3
import { noop } from '@js-toolkit/utils/noop' ;
4
- import { debounce } from '@js-toolkit/utils/debounce' ;
4
+ import { debounce , type DebouncedFunc } from '@js-toolkit/utils/debounce' ;
5
5
import useRefs from '@js-toolkit/react-hooks/useRefs' ;
6
6
import useRefCallback from '@js-toolkit/react-hooks/useRefCallback' ;
7
7
import useHoverCallbacks from '@js-toolkit/react-hooks/useHoverCallbacks' ;
@@ -31,6 +31,7 @@ export type TooltipableTooltipProps<D = undefined, T extends Element = Element>
31
31
readonly tooltipDelay ?: number | undefined ;
32
32
readonly onShowTooltip ?: ( ( tooltip : TooltipData < D , T > ) => void ) | undefined ;
33
33
readonly onHideTooltip ?: ( ( target : TooltipData < D , T > [ 'target' ] ) => void ) | undefined ;
34
+ readonly hideTooltipOnUnmount ?: boolean | undefined ;
34
35
} & WithData < D > ;
35
36
36
37
export type TooltipableProps <
@@ -45,6 +46,7 @@ export default function Tooltipable<
45
46
tooltip,
46
47
tooltipDelay = 0 ,
47
48
data,
49
+ hideTooltipOnUnmount,
48
50
onShowTooltip,
49
51
onHideTooltip,
50
52
...restProps
@@ -60,9 +62,18 @@ export default function Tooltipable<
60
62
// const isHover = getHover();
61
63
// const element = getHoverData();
62
64
63
- const rootRef = useRef < T > ( null ) ;
64
- const rootRefs = useRefs ( rootRef as React . Ref < HTMLDivElement > , ref ) ;
65
- const isShowingTooltipRef = useRef ( false ) ;
65
+ const rootRef = React . useRef < T > ( null ) ;
66
+
67
+ const rootRefs = useRefs ( ( instance ) => {
68
+ rootRef . current = instance as T ;
69
+ return ( ) => {
70
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
71
+ if ( hideTooltipOnUnmount && isShowingTooltipRef . current ) hideTooltip ( ) ;
72
+ rootRef . current = null ;
73
+ } ;
74
+ } , ref ) ;
75
+
76
+ const isShowingTooltipRef = React . useRef ( false ) ;
66
77
67
78
const showTooltip = useRefCallback ( ( ) => {
68
79
if ( rootRef . current && tooltip && onShowTooltip ) {
@@ -72,6 +83,8 @@ export default function Tooltipable<
72
83
} ) ;
73
84
74
85
const hideTooltip = useRefCallback ( ( ) => {
86
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
87
+ ( hideTooltipDelayed as DebouncedFunc < VoidFunction > ) . cancel ?.( ) ;
75
88
if ( rootRef . current && onHideTooltip ) {
76
89
isShowingTooltipRef . current = false ;
77
90
onHideTooltip ( rootRef . current ) ;
@@ -86,12 +99,17 @@ export default function Tooltipable<
86
99
return [ hideTooltip , noop ] ;
87
100
} , [ hideTooltip , onHideTooltip , tooltipDelay ] ) ;
88
101
89
- useEffect ( ( ) => {
102
+ React . useEffect ( ( ) => {
90
103
if ( ! isShowingTooltipRef . current ) return ;
91
104
if ( tooltip ) showTooltip ( ) ;
92
105
else hideTooltip ( ) ;
93
106
} , [ hideTooltip , showTooltip , tooltip , data ] ) ;
94
107
108
+ // React.useEffect(() => {
109
+ // if (hideTooltipOnUnmount) return hideTooltip;
110
+ // return undefined;
111
+ // }, [hideTooltip, hideTooltipOnUnmount]);
112
+
95
113
// useEffect(() => {
96
114
// if (isHover && onShowTooltip && tooltip && element) {
97
115
// onShowTooltip({ target: element, title: tooltip, data });
0 commit comments