From d63ee9b6fb4defd31e4b9285a22a57c999dce047 Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Mon, 25 Nov 2019 18:54:54 +0000 Subject: [PATCH] Esc key close rightmenu --- src/scripts/index.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/scripts/index.js b/src/scripts/index.js index a3412a6..a49eb8b 100644 --- a/src/scripts/index.js +++ b/src/scripts/index.js @@ -42,6 +42,12 @@ function recursiveAdaptWidgets(prefix, element, user) { }); } +// auxiliary function closes the user profile menu +function closeUserControls() { + let userControls = document.querySelector('#user-controls'); + if (userControls) userControls.removeAttribute("open"); +} + document.addEventListener('DOMContentLoaded', function (event) { const menuWrappers = Array.from(document.querySelectorAll('.menu-wrapper')); const rightMenus = Array.from(document.querySelectorAll('nav.jsRightMenu')); @@ -83,10 +89,13 @@ document.addEventListener('DOMContentLoaded', function (event) { // Document -> close menu document.addEventListener("click", event => { - if (!event.target.closest('#user-controls')) { - let userControls = this.querySelector('#user-controls'); - if (userControls) userControls.removeAttribute("open"); - } + if (!event.target.closest('#user-controls')) closeUserControls(); }); + // listen for keypress + document.onkeydown = (e) => { + e = e || window.event; + if (e.key === "Escape" || e.key === "Esc") closeUserControls(); + }; + }); \ No newline at end of file