From 5a66e094e3e2a45df532543edbd05eb7151727e6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 30 Sep 2019 08:30:08 +0530 Subject: [PATCH] Allow using Esc key to close modal dialogs --- src/pyj/modals.pyj | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pyj/modals.pyj b/src/pyj/modals.pyj index b7b42e7847..8753345083 100644 --- a/src/pyj/modals.pyj +++ b/src/pyj/modals.pyj @@ -41,12 +41,13 @@ class Modal: class ModalContainer: def __init__(self): - div = E.div(id='modal-container', + div = E.div(id='modal-container', tabindex='0', E.div( # popup E.div(), # content area E.a(svgicon('close'), title=_('Close')) ) ) + div.addEventListener('keydown', self.onkeydown.bind(self), {'passive': False}) document.body.appendChild(div) # Container style @@ -89,6 +90,7 @@ class ModalContainer: self.modals.push(Modal(create_func, on_close, show_close)) modal_id = self.modals[-1].id self.update() + self.modal_container.focus() return modal_id def hide_modal(self, modal_id): @@ -135,6 +137,10 @@ class ModalContainer: while self.current_modal is not None: self.close_current_modal() + def onkeydown(self, event): + if (event.key is 'Escape' or event.key is 'Esc') and not event.altKey and not event.ctrlKey and not event.metaKey and not event.shiftKey: + self.close_current_modal(event) + def create_simple_dialog_markup(title, msg, details, icon, prefix, parent): details = details or ''