Reorganize frontend and install Craco to alias modules (#190)
This commit is contained in:
21
front/src/modules/ui/hooks/useOutsideAlerter.ts
Normal file
21
front/src/modules/ui/hooks/useOutsideAlerter.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
declare type CallbackType = () => void;
|
||||
|
||||
export function useOutsideAlerter(
|
||||
ref: React.RefObject<HTMLInputElement>,
|
||||
callback: CallbackType,
|
||||
) {
|
||||
useEffect(() => {
|
||||
function handleClickOutside(event: Event) {
|
||||
const target = event.target as HTMLButtonElement;
|
||||
if (ref.current && !ref.current.contains(target)) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => {
|
||||
document.removeEventListener('mousedown', handleClickOutside);
|
||||
};
|
||||
}, [ref, callback]);
|
||||
}
|
||||
Reference in New Issue
Block a user