Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function useOnClickOutside(
ref: React.RefObject,
handler: Handler | null,
) {
if (!isBrowser) {
return
}
const handlerRef = useLatest(handler)
useEffect(() => {
if (!handler) {
return
}
const listener = (event: PossibleEvent) => {
if (
!ref.current ||
!handlerRef.current ||
ref.current.contains(event.target as Node)
) {
return
}
handlerRef.current(event)