From 300362e0e4e30439bd2f55f3dc94596ab197607e Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 30 Oct 2020 13:03:01 -0700 Subject: [PATCH 1/5] Close the theme picker when Escape is pressed --- src/librustdoc/html/static/main.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 28bd1ba5247d6..69873524bd729 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -4,6 +4,7 @@ // Local js definitions: /* global addClass, getCurrentValue, hasClass */ /* global onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */ +/* global hideThemeButtonState */ if (!String.prototype.startsWith) { String.prototype.startsWith = function(searchString, position) { @@ -137,10 +138,6 @@ function defocusSearchBar() { sidebar.appendChild(div); } } - var themePickers = document.getElementsByClassName("theme-picker"); - if (themePickers && themePickers.length > 0) { - themePickers[0].style.display = "none"; - } } function hideSidebar() { @@ -155,10 +152,6 @@ function defocusSearchBar() { filler.remove(); } document.getElementsByTagName("body")[0].style.marginTop = ""; - var themePickers = document.getElementsByClassName("theme-picker"); - if (themePickers && themePickers.length > 0) { - themePickers[0].style.display = null; - } } function showSearchResults(search) { @@ -376,6 +369,7 @@ function defocusSearchBar() { document.title = titleBeforeSearch; } defocusSearchBar(); + hideThemeButtonState(); } function handleShortcut(ev) { From ac3a434ed947058635fbe1f09b1da8bd6f1ea242 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 30 Oct 2020 13:34:30 -0700 Subject: [PATCH 2/5] Allow the theme picker to work with arrow keys This is mostly motivated by docs.rs. It's really weird when arrow keys work in the top dropdown menu, but don't work in other dropdown menus on the same page. --- src/librustdoc/html/render/mod.rs | 48 +++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 0621eafd91347..8d07f479b6eae 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -798,17 +798,61 @@ function handleThemeButtonsBlur(e) {{ var active = document.activeElement; var related = e.relatedTarget; - if (active.id !== "themePicker" && + if (active.id !== "theme-picker" && (!active.parentNode || active.parentNode.id !== "theme-choices") && (!related || - (related.id !== "themePicker" && + (related.id !== "theme-picker" && (!related.parentNode || related.parentNode.id !== "theme-choices")))) {{ hideThemeButtonState(); }} }} +function handleThemeKeyPress(e) {{ + if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {{ return; }} + if (!themePicker.parentNode.contains(e.target)) {{ return; }} + var active = document.activeElement; + switch (e.key) {{ + case "ArrowUp": + e.preventDefault(); + if (active.previousElementSibling && e.target.id !== "theme-picker") {{ + active.previousElementSibling.focus(); + }} else {{ + showThemeButtonState(); + themes.lastElementChild.focus(); + }} + break; + case "ArrowDown": + e.preventDefault(); + if (active.nextElementSibling && e.target.id !== "theme-picker") {{ + active.nextElementSibling.focus(); + }} else {{ + showThemeButtonState(); + themes.firstElementChild.focus(); + }} + break; + case "Enter": + case "Return": + case "Space": + if (e.target.id === "theme-picker" && themes.style.display === "none") {{ + e.preventDefault(); + showThemeButtonState(); + themes.firstElementChild.focus(); + }} + break; + case "Home": + e.preventDefault(); + themes.firstElementChild.focus(); + break; + case "End": + e.preventDefault(); + themes.lastElementChild.focus(); + break; + }} +}}; + themePicker.onclick = switchThemeButtonState; themePicker.onblur = handleThemeButtonsBlur; +document.addEventListener("keydown", handleThemeKeyPress); {}.forEach(function(item) {{ var but = document.createElement("button"); but.textContent = item; From 6918a17aa4ed5d06c1fce292763ff26605c52b97 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 30 Oct 2020 13:35:41 -0700 Subject: [PATCH 3/5] Add aria roles for theme picker menu --- src/librustdoc/html/layout.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index b089bcb0862a5..db73af7ec1689 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -79,12 +79,12 @@ pub fn render( {sidebar}\ \
\ - \ -
\ +
\
\ \