The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/client/hooks/useOutsideClick.ts

19 lines
537 B

import { useEffect, RefObject } from 'react';
// TODO: fuselage
export function useOutsideClick<T extends HTMLElement>(ref: RefObject<T>, cb: (e: MouseEvent) => void): void {
useEffect(() => {
function handleClickOutside(event: MouseEvent): void {
if (event && ref.current && !ref.current.contains(event.target as Node)) {
return cb(event);
}
}
document.addEventListener('mousedown', handleClickOutside);
return (): void => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [cb, ref]);
}