mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-30 17:13:15 +00:00
20 lines
569 B
TypeScript
20 lines
569 B
TypeScript
import { useEffect, RefObject } from 'react';
|
|
|
|
export function useOutsideAlerter<T extends HTMLElement>(
|
|
ref: RefObject<T>,
|
|
handler: () => void,
|
|
additionalDeps: unknown[],
|
|
) {
|
|
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, ...additionalDeps]);
|
|
}
|