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