react-simple-hook-modal icon indicating copy to clipboard operation
react-simple-hook-modal copied to clipboard

Add ability to close modal with ESC key

Open mbrookson opened this issue 5 years ago • 2 comments

This would be a useful addition to help move around the UI with just the keyboard.

mbrookson avatar May 03 '20 09:05 mbrookson

Maybe something like this would work?

const keyboardShortcuts = useCallback((event) => {
  if (event.keyCode === 27) {
    closeModal();
  }
}, []);

useEffect(() => {
  document.addEventListener('keydown', keyboardShortcuts);
  return () => document.removeEventListener('keydown', keyboardShortcuts);
});

manfromanotherland avatar Jul 02 '21 08:07 manfromanotherland

@edmundojr Thanks for the suggestion! I've not tried it but it looks reasonable. Feel free to PR a change, otherwise I will try to get round to implementing this in the near future.

mbrookson avatar Jul 04 '21 15:07 mbrookson