Allow using Esc key to close modal dialogs

This commit is contained in:
Kovid Goyal 2019-09-30 08:30:08 +05:30
parent 47ddd8165c
commit 5a66e094e3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -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 ''