@resourge/react-fetch
Version:
[](LICENSE)
43 lines (40 loc) • 1.05 kB
JavaScript
/**
* react-fetch v1.41.3
*
* Copyright (c) resourge.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
import { useEffect } from 'react';
const useOnFocus = (cb, onWindowFocus) => {
useEffect(() => {
if (onWindowFocus) {
const {
focus,
blur,
clear
} = cb();
const visibilitychange = () => {
if (document.visibilityState === 'visible') {
focus();
} else {
blur();
}
};
window.addEventListener('focus', focus);
window.addEventListener('blur', blur);
window.addEventListener('visibilitychange', visibilitychange);
return () => {
clear();
window.removeEventListener('focus', focus);
window.removeEventListener('blur', blur);
window.removeEventListener('visibilitychange', visibilitychange);
};
}
}, [onWindowFocus]);
};
export { useOnFocus };
//# sourceMappingURL=useOnFocus.js.map