mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
18 lines
480 B
TypeScript
18 lines
480 B
TypeScript
import {RefObject, useEffect} from 'react';
|
|
import {on, off} from './util';
|
|
|
|
const useClickAway = (ref: RefObject<HTMLElement | null>, onClickAway: (event: KeyboardEvent) => void) => {
|
|
useEffect(() => {
|
|
const handler = (event) => {
|
|
const {current: el} = ref;
|
|
el && !el.contains(event.target) && onClickAway(event);
|
|
};
|
|
on(document, 'click', handler);
|
|
return () => {
|
|
off(document, 'click', handler);
|
|
};
|
|
});
|
|
};
|
|
|
|
export default useClickAway;
|