From 913a7d7b757e10c28ef69b7a4036c21911ed6e16 Mon Sep 17 00:00:00 2001 From: Aleksandr Gumroian Date: Fri, 18 Sep 2020 11:26:04 +0200 Subject: [PATCH] Add closing bootstrap modal using ESC --- src/bootstrap/Modal.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/Modal.js b/src/bootstrap/Modal.js index 5c080c9..96f1fef 100644 --- a/src/bootstrap/Modal.js +++ b/src/bootstrap/Modal.js @@ -1,11 +1,11 @@ /* - * Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/) + * Copyright (C) 2020 CZ.NIC z.s.p.o. (http://www.nic.cz/) * * This is free software, licensed under the GNU General Public License v3. * See /LICENSE for more information. */ -import React, { useRef } from "react"; +import React, { useRef, useEffect } from "react"; import PropTypes from "prop-types"; import { Portal } from "../utils/Portal"; @@ -31,6 +31,19 @@ export function Modal({ shown, setShown, scrollable, children }) { useClickOutside(dialogRef, () => setShown(false)); + useEffect(() => { + const handleEsc = (event) => { + if (event.keyCode === 27) { + setShown(false); + } + }; + window.addEventListener("keydown", handleEsc); + + return () => { + window.removeEventListener("keydown", handleEsc); + }; + }, [setShown]); + return (