13

I need to close the modal also using the "ESC" key, at the moment it is closing the "CLOSE" and "CONFIRM" button. i'm using reactstrap, react hooks. keyboard {show} and handleClose it didn't work.

Here is the code:


const DeleteUserModal = props => {
    const { user, show } = props;

    const deleteUser = async () => {
        await props.removeUser(user);
        props.onCloseModal();
    };

    const handleKeyPress = target => {
        if (target.charCode === ENTER_KEY) {
            deleteUser();
        }
    };
    


    return (
        <div onKeyPress={handleKeyPress}>
            <Modal isOpen={show} toggle={props.onCloseModal}  >
                
                <ModalHeader toggle={props.onCloseModal}>
                    <IntlMessages id="modal.delete.user.title" />
                </ModalHeader>

                <ModalBody>
                    <IntlMessages id="modal.delete.user.description" />
                </ModalBody>

                <ModalFooter>
                    <Button className="cancel" onClick={props.onCloseModal}>
                        <IntlMessages id="modal.common.cancel" />
                    </Button>
                    <Button className="action" onClick={deleteUser}>
                        <IntlMessages id="modal.common.ok" />
                    </Button>
                </ModalFooter>
            </Modal>
        </div>
    );
};

export default DeleteUserModal;

follows the modal with two actions

Basil
  • 1,664
  • 13
  • 25
Allan Alencar
  • 143
  • 1
  • 1
  • 8
  • What do you mean by closing a button? – ProEvilz Jul 24 '20 at 13:31
  • Kindly refer the following https://getbootstrap.com/docs/4.0/components/modal/#options provided by bootstrap – Basil Jul 24 '20 at 13:42
  • I need to close the modal by pressing the ESC key. At the moment it closes when pressing the confirm button or cancel the action. I'm checking the documentation, but when I put the code, nothing happens. – Allan Alencar Jul 24 '20 at 13:46

3 Answers3

29

You can setup an event listener.

 useEffect(() => {
      const close = (e) => {
        if(e.keyCode === 27){
          props.onCloseModal()
        }
      }
      window.addEventListener('keydown', close)
    return () => window.removeEventListener('keydown', close)
  },[])
K.P
  • 444
  • 3
  • 6
  • 20
    `keyCode` is deprecated: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode. Use `e.key === 'Escape` instead', for better international keyboard support. – MegaCookie May 02 '21 at 13:47
  • what would be the class component equivalent of this? – Ronny Fitzgerald Dec 02 '21 at 06:05
  • Is there a way to do this in react without touching the actual dom? – Solomon Barayev Nov 29 '22 at 17:48
  • Not only will this not work without placing the state variable change in the useEffect() parameter (the brackets at the end), it leaves your listener dangling forver inside your app! – Midiman May 24 '23 at 16:48
2

In vanilla JavaScript, you could do:

document.onkeydown = function (evt) {
    if (evt.keyCode == 27) {
        // Escape key pressed
        props.onCloseModal();
    }
};
Nanoo
  • 836
  • 8
  • 16
1

You can check bootstrap documentation.

If nothing found, then you could add an event listener to the ESC key press and than call onCloseModal

Ajay Gupta
  • 703
  • 5
  • 11