Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Modal Popup using Esc key on keyboard

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

like image 873
Allan Alencar Avatar asked Jul 24 '20 13:07

Allan Alencar


People also ask

How do I use the Esc key on my keyboard?

True to its name, the Esc key acts like a “Cancel” button on most computers. On Windows, for example, the key can be used to close a dialog box without having to click “Cancel” with a mouse button. You can also use the Esc key to cancel loading a website in a browser.

How do you close a modal dialog?

There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal.

Should escape close a modal?

When keyboard users tabs into modals (like a popup or dialog), focus should remain in that window. This helps prevent users from accidentally moving focus outside the modal without closing it. Users should be able to use the ESC key to close the modal.

How do you close a modal by clicking on a button?

Answer: Use the modal('hide') Method You can simply use the modal('hide') method to hide or close the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('show') and modal('toggle') .


3 Answers

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)
  },[])
like image 95
K.P Avatar answered Oct 25 '22 18:10

K.P


In vanilla JavaScript, you could do:

document.onkeydown = function (evt) {
    if (evt.keyCode == 27) {
        // Escape key pressed
        props.onCloseModal();
    }
};
like image 30
Nanoo Avatar answered Oct 25 '22 18:10

Nanoo


You can check bootstrap documentation.

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

like image 1
Ajay Gupta Avatar answered Oct 25 '22 18:10

Ajay Gupta