mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-30 18:30:32 -06:00
20 lines
612 B
TypeScript
20 lines
612 B
TypeScript
import { useEffect } from "react";
|
|
|
|
// This hook will listen to the visibilitychange event and run the provided function whenever the document's visibility state changes to visible
|
|
export const useDocumentVisibility = (onVisible: () => void) => {
|
|
useEffect(() => {
|
|
const listener = () => {
|
|
if (document.visibilityState === "visible") {
|
|
onVisible();
|
|
}
|
|
};
|
|
|
|
document.addEventListener("visibilitychange", listener);
|
|
|
|
return () => {
|
|
document.removeEventListener("visibilitychange", listener);
|
|
};
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
};
|