Files
formbricks-formbricks/packages/lib/useDocumentVisibility.ts
Naitik Kapadia 04e43725d1 feat: New Question Type Address (#2162)
Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com>
Co-authored-by: Dhruwang Jariwala <67850763+Dhruwang@users.noreply.github.com>
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2024-04-11 13:30:11 +00:00

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
}, []);
};