1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/packages/pdf-viewer/hooks/useIsFocused.ts
2022-08-04 10:12:22 +01:00

27 lines
622 B
TypeScript

import { useEffect, useState } from 'react';
const useIsFocused = () => {
const [isFocused, setIsFocused] = useState(false);
useEffect(() => {
const onMessage = (event: MessageEvent) => {
if (event.data.type === 'blur') {
setIsFocused(false);
}
};
const onClick = (_event: MouseEvent) => {
setIsFocused(true);
};
window.addEventListener('message', onMessage);
document.addEventListener('click', onClick);
return () => {
window.removeEventListener('message', onMessage);
document.removeEventListener('click', onClick);
};
}, []);
return isFocused;
};
export default useIsFocused;