synapsecns/sanguine

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

Summary

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

export const useCloseOnEscape = (onClose) => {
  useEffect(() => {
    const handleEscapeKey = (event) => {
      if (event.key === 'Escape') {
        onClose()
      }
    }

    document.addEventListener('keydown', handleEscapeKey)

    return () => {
      document.removeEventListener('keydown', handleEscapeKey)
    }
  }, [onClose])
}