Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard handling to the theme picker menu #78584

Merged
merged 5 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ pub fn render<T: Print, S: Print>(
{sidebar}\
</nav>\
<div class=\"theme-picker\">\
<button id=\"theme-picker\" aria-label=\"Pick another theme!\">\
<button id=\"theme-picker\" aria-label=\"Pick another theme!\" aria-haspopup=\"menu\">\
<img src=\"{static_root_path}brush{suffix}.svg\" \
width=\"18\" \
alt=\"Pick another theme!\">\
</button>\
<div id=\"theme-choices\"></div>\
<div id=\"theme-choices\" role=\"menu\"></div>\
</div>\
<script src=\"{static_root_path}theme{suffix}.js\"></script>\
<nav class=\"sub\">\
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,10 @@ 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();
}}
Expand Down
68 changes: 60 additions & 8 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Local js definitions:
/* global addClass, getCurrentValue, hasClass */
/* global onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
/* global hideThemeButtonState, showThemeButtonState */

if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
Expand Down Expand Up @@ -47,6 +48,14 @@ function getSearchElement() {
return document.getElementById("search");
}

function getThemesElement() {
return document.getElementById("theme-choices");
}

function getThemePickerElement() {
return document.getElementById("theme-picker");
}

// Sets the focus on the search bar at the top of the page
function focusSearchBar() {
getSearchInput().focus();
Expand Down Expand Up @@ -137,10 +146,6 @@ function defocusSearchBar() {
sidebar.appendChild(div);
}
}
var themePickers = document.getElementsByClassName("theme-picker");
notriddle marked this conversation as resolved.
Show resolved Hide resolved
if (themePickers && themePickers.length > 0) {
themePickers[0].style.display = "none";
}
}

function hideSidebar() {
Expand All @@ -155,10 +160,6 @@ function defocusSearchBar() {
filler.remove();
}
document.getElementsByTagName("body")[0].style.marginTop = "";
var themePickers = document.getElementsByClassName("theme-picker");
notriddle marked this conversation as resolved.
Show resolved Hide resolved
if (themePickers && themePickers.length > 0) {
themePickers[0].style.display = null;
}
}

function showSearchResults(search) {
Expand Down Expand Up @@ -376,6 +377,7 @@ function defocusSearchBar() {
document.title = titleBeforeSearch;
}
defocusSearchBar();
hideThemeButtonState();
}

function handleShortcut(ev) {
Expand Down Expand Up @@ -412,7 +414,57 @@ function defocusSearchBar() {
case "?":
displayHelp(true, ev);
break;

default:
var themePicker = getThemePickerElement();
if (themePicker.parentNode.contains(ev.target)) {
handleThemeKeyDown(ev);
}
}
}
}

function handleThemeKeyDown(ev) {
var active = document.activeElement;
var themes = getThemesElement();
switch (getVirtualKey(ev)) {
case "ArrowUp":
ev.preventDefault();
if (active.previousElementSibling && ev.target.id !== "theme-picker") {
active.previousElementSibling.focus();
} else {
showThemeButtonState();
themes.lastElementChild.focus();
}
break;
case "ArrowDown":
ev.preventDefault();
if (active.nextElementSibling && ev.target.id !== "theme-picker") {
active.nextElementSibling.focus();
} else {
showThemeButtonState();
themes.firstElementChild.focus();
}
break;
case "Enter":
case "Return":
case "Space":
if (ev.target.id === "theme-picker" && themes.style.display === "none") {
ev.preventDefault();
showThemeButtonState();
themes.firstElementChild.focus();
}
break;
case "Home":
ev.preventDefault();
themes.firstElementChild.focus();
break;
case "End":
ev.preventDefault();
themes.lastElementChild.focus();
break;
// The escape key is handled in handleEscape, not here,
// so that pressing escape will close the menu even if it isn't focused
}
}

Expand Down