import { useCallback , useEffect } from 'react' ; export const useHandleEscape = ( cb ) => { const handleKeyPress = useCallback ( ( e ) => { if ( e . key === 'Escape' || e . key === 'Esc' ) { cb ( ) ; } } , [ cb ] ) ; useEffect ( ( ) => { document . addEventListener ( 'keydown' , handleKeyPress ) ; return ( ) => document . removeEventListener ( 'keydown' , handleKeyPress ) ; } , [ handleKeyPress ] ) ; } ;