1
0
mirror of https://github.com/tabler/tabler.git synced 2026-08-09 21:02:22 +04:00
Files
tabler/site/hooks/use-on-click-outside.ts
T
2023-07-27 01:16:23 +02:00

20 lines
575 B
TypeScript

import { RefObject } from 'react';
import useEventListener from './use-event-listener';
type Handler = (event: MouseEvent) => void;
const useOnClickOutside = <T extends HTMLElement = HTMLElement>(ref: RefObject<T>, handler: Handler, mouseEvent: 'mousedown' | 'mouseup' = 'mousedown'): void => {
useEventListener(mouseEvent, (event) => {
const el = ref?.current;
// Do nothing if clicking ref's element or descendent elements
if (!el || el.contains(event.target as Node)) {
return;
}
handler(event);
});
};
export default useOnClickOutside;