Files
threshold/website/lib/hooks/use-on-click-outside.js
T

22 lines
587 B
JavaScript
Raw Normal View History

2020-10-09 16:26:32 -04:00
import { useEffect } from 'react'
/**
* Hook that runs a callback on clicks outside the ref
*/
function useOnClickOutside(ref, callbackFn) {
useEffect(() => {
function handleClick(event) {
const isOutside = ref.current && !ref.current.contains(event.target)
if (isOutside) callbackFn(event)
}
// Bind the event listener
document.addEventListener('mousedown', handleClick)
return () => {
// Unbind the event listener on clean up
document.removeEventListener('mousedown', handleClick)
}
}, [ref])
}
export default useOnClickOutside