fixes 8 of nick's list

This commit is contained in:
ajaythapliyal
2023-02-27 22:41:47 +05:30
parent b5cb3f994b
commit 828927d167
3 changed files with 42 additions and 7 deletions

19
frontend/src/hooks.ts Normal file
View File

@@ -0,0 +1,19 @@
import { useEffect, RefObject } from 'react';
export function useOutsideAlerter<T extends HTMLElement>(
ref: RefObject<T>,
handler: () => void,
deps: any[],
) {
useEffect(() => {
function handleClickOutside(this: Document, event: MouseEvent) {
if (ref.current && !ref.current.contains(event.target as Node)) {
handler();
}
}
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [ref, ...deps]);
}