Files
formbricks/packages/lib/useDocumentVisibility.ts
Dhruwang Jariwala 8b5328aa74 feat: Multi language Surveys (#1630)
Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
2024-03-18 19:02:18 +00:00

22 lines
651 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
}, []);
};
export default useDocumentVisibility;