From 2fdeb4eafa2e56c0fd398cd7ac41ff8d2d476363 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 29 Oct 2020 16:57:32 -0700 Subject: [PATCH 1/2] Do not scroll when closing the menu with space Fixes #1082 --- static/menu.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/static/menu.js b/static/menu.js index 87e116a6e..fd8328187 100644 --- a/static/menu.js +++ b/static/menu.js @@ -128,14 +128,25 @@ break; case "enter": case "return": + // enter and return have the default browser behavior, + // but they also close the menu + // this behavior is identical between both the WAI example, and GitHub's + setTimeout(function() { + closeMenu(); + }, 100); + break; case "space": case " ": - // enter, return, and space have the default browser behavior, - // but they also close the menu + // space closes the menu, and activates the current link // this behavior is identical between both the WAI example, and GitHub's + if (document.activeElement instanceof HTMLAnchorElement && !document.activeElement.hasAttribute("aria-haspopup")) { + document.activeElement.click(); + } setTimeout(function() { closeMenu(); }, 100); + e.preventDefault(); + e.stopPropagation(); break; case "home": case "pageup": From 6d51510f73df78a174e665f8b23576707512da06 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 29 Oct 2020 18:12:12 -0700 Subject: [PATCH 2/2] Add descriptive comment for the space behaviour --- static/menu.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/static/menu.js b/static/menu.js index fd8328187..6ad05f9d6 100644 --- a/static/menu.js +++ b/static/menu.js @@ -140,6 +140,28 @@ // space closes the menu, and activates the current link // this behavior is identical between both the WAI example, and GitHub's if (document.activeElement instanceof HTMLAnchorElement && !document.activeElement.hasAttribute("aria-haspopup")) { + // It's supposed to copy the behaviour of the WAI Menu Bar + // page, and of GitHub's menus. I've been using these two + // sources to judge what is basically "industry standard" + // behaviour for menu keyboard activity on the web. + // + // On GitHub, here's what I notice: + // + // 1 If you click open a menu, the menu button remains + // focused. If, in this stage, I press space, the menu will + // close. + // + // 2 If I use the arrow keys to focus a menu item, and then + // press space, the menu item will be activated. For + // example, clicking "+", then pressing down, then pressing + // space will open the New Repository page. + // + // Behaviour 1 is why the + // `!document.activeElement.hasAttribute("aria-haspopup")` + // condition is there. It's to make sure the menu-link on + // things like the About dropdown don't get activated. + // Behaviour 2 is why this code is required at all; I want to + // activate the currently highlighted menu item. document.activeElement.click(); } setTimeout(function() {