1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/pdf-viewer/hooks/useIsFocused.ts

27 lines
622 B
TypeScript
Raw Normal View History

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;