You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
boundary/website/lib/hooks/use-on-click-outside.js

23 lines
587 B

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