mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-26 14:01:16 -07:00
18 lines
522 B
JavaScript
18 lines
522 B
JavaScript
import { useSyncExternalStore } from "react";
|
|
|
|
const hasFocus = () => typeof document !== "undefined" && document.hasFocus();
|
|
|
|
const subscribe = (onFocusChange) => {
|
|
window.addEventListener("focus", onFocusChange);
|
|
window.addEventListener("blur", onFocusChange);
|
|
|
|
return () => {
|
|
window.removeEventListener("focus", onFocusChange);
|
|
window.removeEventListener("blur", onFocusChange);
|
|
};
|
|
};
|
|
|
|
const useWindowFocus = () => useSyncExternalStore(subscribe, hasFocus, () => false);
|
|
|
|
export default useWindowFocus;
|