Skip to content

Commit 8cd193c

Browse files
committed
Improve Tooltipable
1 parent 1360ad3 commit 8cd193c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/TooltipButton/TooltipButton.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default function TooltipButton<C extends React.ElementType = 'button', D
1818
data,
1919
tooltip,
2020
tooltipDelay,
21+
hideTooltipOnUnmount,
2122
onShowTooltip,
2223
onHideTooltip,
2324
...restProps
@@ -36,6 +37,7 @@ export default function TooltipButton<C extends React.ElementType = 'button', D
3637
data={data as never}
3738
tooltip={tooltip}
3839
tooltipDelay={tooltipDelay}
40+
hideTooltipOnUnmount={hideTooltipOnUnmount}
3941
onShowTooltip={onShowTooltip}
4042
onHideTooltip={onHideTooltip}
4143
component={TweakableElementWrapper}

src/Tooltipable/Tooltipable.tsx

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React from 'react';
22
import { Flex, type DefaultComponentType, type FlexAllProps } from 'reflexy/styled';
33
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';
55
import useRefs from '@js-toolkit/react-hooks/useRefs';
66
import useRefCallback from '@js-toolkit/react-hooks/useRefCallback';
77
import useHoverCallbacks from '@js-toolkit/react-hooks/useHoverCallbacks';
@@ -31,6 +31,7 @@ export type TooltipableTooltipProps<D = undefined, T extends Element = Element>
3131
readonly tooltipDelay?: number | undefined;
3232
readonly onShowTooltip?: ((tooltip: TooltipData<D, T>) => void) | undefined;
3333
readonly onHideTooltip?: ((target: TooltipData<D, T>['target']) => void) | undefined;
34+
readonly hideTooltipOnUnmount?: boolean | undefined;
3435
} & WithData<D>;
3536

3637
export type TooltipableProps<
@@ -45,6 +46,7 @@ export default function Tooltipable<
4546
tooltip,
4647
tooltipDelay = 0,
4748
data,
49+
hideTooltipOnUnmount,
4850
onShowTooltip,
4951
onHideTooltip,
5052
...restProps
@@ -60,9 +62,18 @@ export default function Tooltipable<
6062
// const isHover = getHover();
6163
// const element = getHoverData();
6264

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);
6677

6778
const showTooltip = useRefCallback(() => {
6879
if (rootRef.current && tooltip && onShowTooltip) {
@@ -72,6 +83,8 @@ export default function Tooltipable<
7283
});
7384

7485
const hideTooltip = useRefCallback(() => {
86+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
87+
(hideTooltipDelayed as DebouncedFunc<VoidFunction>).cancel?.();
7588
if (rootRef.current && onHideTooltip) {
7689
isShowingTooltipRef.current = false;
7790
onHideTooltip(rootRef.current);
@@ -86,12 +99,17 @@ export default function Tooltipable<
8699
return [hideTooltip, noop];
87100
}, [hideTooltip, onHideTooltip, tooltipDelay]);
88101

89-
useEffect(() => {
102+
React.useEffect(() => {
90103
if (!isShowingTooltipRef.current) return;
91104
if (tooltip) showTooltip();
92105
else hideTooltip();
93106
}, [hideTooltip, showTooltip, tooltip, data]);
94107

108+
// React.useEffect(() => {
109+
// if (hideTooltipOnUnmount) return hideTooltip;
110+
// return undefined;
111+
// }, [hideTooltip, hideTooltipOnUnmount]);
112+
95113
// useEffect(() => {
96114
// if (isHover && onShowTooltip && tooltip && element) {
97115
// onShowTooltip({ target: element, title: tooltip, data });

0 commit comments

Comments
 (0)