mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-23 14:55:48 +03:00
32 lines
812 B
TypeScript
32 lines
812 B
TypeScript
import { useEffect, RefObject } from 'react';
|
|
|
|
export function useOutsideClick(
|
|
ref: RefObject<HTMLElement | null | undefined>,
|
|
onClick: () => void
|
|
) {
|
|
useEffect(() => {
|
|
function handleClick(event: MouseEvent) {
|
|
const portalRoot = document.querySelector('#portal-root')!;
|
|
if (
|
|
ref.current &&
|
|
!ref.current.contains(event.target as any)
|
|
) {
|
|
onClick();
|
|
}
|
|
}
|
|
|
|
function handleKeyDown(ev) {
|
|
if(ev.key === 'Escape') {
|
|
onClick();
|
|
}
|
|
}
|
|
document.addEventListener('mousedown', handleClick);
|
|
document.addEventListener('keydown', handleKeyDown);
|
|
|
|
return () => {
|
|
document.removeEventListener('mousedown', handleClick);
|
|
document.removeEventListener('keydown', handleKeyDown);
|
|
};
|
|
}, [ref.current, onClick]);
|
|
}
|