mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-09 14:58:47 +00:00
revalidate config changes, check on focus changes
This commit is contained in:
parent
5f0c1ec70a
commit
c980c70798
3 changed files with 96 additions and 1 deletions
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;
|
Loading…
Add table
Add a link
Reference in a new issue