mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 20:01:59 +00:00
Add table and menu components
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
export const useOutsideClick = (
|
||||
handler: () => void,
|
||||
listeningCapturing = true,
|
||||
): React.MutableRefObject<HTMLUListElement | null> => {
|
||||
const ref = useRef<HTMLUListElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const handleClick = (e: Event) => {
|
||||
if (ref.current && !ref.current.contains(e.target as Node)) {
|
||||
handler();
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('click', handleClick, listeningCapturing);
|
||||
|
||||
return () =>
|
||||
document.removeEventListener('click', handleClick, listeningCapturing);
|
||||
}, [handler, listeningCapturing]);
|
||||
|
||||
return ref;
|
||||
};
|
||||
Reference in New Issue
Block a user