synapsecns/sanguine

View on GitHub
packages/synapse-interface/utils/hooks/useCloseOnOutsideClick.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { useEffect } from 'react'

const useCloseOnOutsideClick = (ref, onClose) => {
  const handleClickOutside = (event) => {
    if (ref.current && !ref.current.contains(event.target)) {
      onClose()
    }
  }

  useEffect(() => {
    document.addEventListener('mousedown', handleClickOutside)
    return () => {
      document.removeEventListener('mousedown', handleClickOutside)
    }
  }, [ref, onClose])
}

export default useCloseOnOutsideClick