mirror of
https://github.com/gethomepage/homepage.git
synced 2025-12-07 09:35:54 -08:00
revalidate config changes, check on focus changes
This commit is contained in:
26
src/utils/hooks/window-focus.js
Normal file
26
src/utils/hooks/window-focus.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
const hasFocus = () => typeof document !== "undefined" && document.hasFocus();
|
||||
|
||||
const useWindowFocus = () => {
|
||||
const [focused, setFocused] = useState(hasFocus);
|
||||
|
||||
useEffect(() => {
|
||||
setFocused(hasFocus());
|
||||
|
||||
const onFocus = () => setFocused(true);
|
||||
const onBlur = () => setFocused(false);
|
||||
|
||||
window.addEventListener("focus", onFocus);
|
||||
window.addEventListener("blur", onBlur);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("focus", onFocus);
|
||||
window.removeEventListener("blur", onBlur);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return focused;
|
||||
};
|
||||
|
||||
export default useWindowFocus;
|
||||
Reference in New Issue
Block a user