-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
useVisibilityChange.ts
54 lines (46 loc) · 1.26 KB
/
useVisibilityChange.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useEffect } from "react";
import { useRefCache, UserInteractionMode } from "@react-md/utils";
import { TooltipInitiated } from "./useHandlers";
export interface VisibilityChangeOptions {
/**
* An optional function to call after the tooltip becomes visible. You will be provided
* the user interaction mode that caused the tooltip to become visible.
*/
onShow?: (mode: UserInteractionMode) => void;
/**
* An optional function to call after the tooltip becomes visible.
*/
onHide?: () => void;
mode: TooltipInitiated;
visible: boolean;
}
/**
* This hook will call the `onShow` and `onHide` functions as needed based on the current interaction
* mode and the visibility of the tooltip.
*
* @private
*/
export default function useVisiblityChange({
onShow,
onHide,
visible,
mode,
}: VisibilityChangeOptions): void {
const handlers = useRefCache({ onShow, onHide });
useEffect(() => {
if (!visible || mode === null) {
return;
}
const { onShow, onHide } = handlers.current;
if (onShow) {
onShow(mode);
}
return () => {
if (onHide) {
onHide();
}
};
// only want to trigger on visibility changes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [visible]);
}