useOnClickOutside
A custom hook that calls a callback function when an element outside the target element assigned ref
is clicked.
Interface
const useOnClickOutside: <T extends HTMLElement>(
action: (targetElement: T) => void
) => {
ref: React.RefObject<T>;
};
Usage
import { useMemo } from 'react';
import { useOnClickOutside } from '@devgrace/react';
const Example = () => {
const { ref } = useOnClickOutside<HTMLDivElement>(() => {
window.alert('outside click')
});
const boxStyle = useMemo(() => {
return {
width: '400px',
height: '400px',
background: '#439966',
fontSize: '1.5rem',
color: '#fff',
};
}, []);
return (
<div ref={ref} style={boxStyle}>
Target Box
</div>
);
};