mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-08 13:00:13 +02:00
23 lines
623 B
TypeScript
23 lines
623 B
TypeScript
// From https://learnersbucket.com/examples/interview/usehasfocus-hook-in-react/
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
export const useAppFocus = () => {
|
|
const [focus, setFocus] = useState(document.hasFocus());
|
|
|
|
useEffect(() => {
|
|
const onFocus = () => setFocus(true);
|
|
const onBlur = () => setFocus(false);
|
|
|
|
window.addEventListener('focus', onFocus);
|
|
window.addEventListener('blur', onBlur);
|
|
|
|
return () => {
|
|
window.removeEventListener('focus', onFocus);
|
|
window.removeEventListener('blur', onBlur);
|
|
};
|
|
}, []);
|
|
|
|
return focus;
|
|
};
|