Skip to content

Commit ac3a434

Browse files
committed
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.
1 parent 300362e commit ac3a434

File tree

1 file changed

+46
-2
lines changed
  • src/librustdoc/html/render

1 file changed

+46
-2
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,17 +798,61 @@ function handleThemeButtonsBlur(e) {{
798798
var active = document.activeElement;
799799
var related = e.relatedTarget;
800800
801-
if (active.id !== "themePicker" &&
801+
if (active.id !== "theme-picker" &&
802802
(!active.parentNode || active.parentNode.id !== "theme-choices") &&
803803
(!related ||
804-
(related.id !== "themePicker" &&
804+
(related.id !== "theme-picker" &&
805805
(!related.parentNode || related.parentNode.id !== "theme-choices")))) {{
806806
hideThemeButtonState();
807807
}}
808808
}}
809809
810+
function handleThemeKeyPress(e) {{
811+
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {{ return; }}
812+
if (!themePicker.parentNode.contains(e.target)) {{ return; }}
813+
var active = document.activeElement;
814+
switch (e.key) {{
815+
case "ArrowUp":
816+
e.preventDefault();
817+
if (active.previousElementSibling && e.target.id !== "theme-picker") {{
818+
active.previousElementSibling.focus();
819+
}} else {{
820+
showThemeButtonState();
821+
themes.lastElementChild.focus();
822+
}}
823+
break;
824+
case "ArrowDown":
825+
e.preventDefault();
826+
if (active.nextElementSibling && e.target.id !== "theme-picker") {{
827+
active.nextElementSibling.focus();
828+
}} else {{
829+
showThemeButtonState();
830+
themes.firstElementChild.focus();
831+
}}
832+
break;
833+
case "Enter":
834+
case "Return":
835+
case "Space":
836+
if (e.target.id === "theme-picker" && themes.style.display === "none") {{
837+
e.preventDefault();
838+
showThemeButtonState();
839+
themes.firstElementChild.focus();
840+
}}
841+
break;
842+
case "Home":
843+
e.preventDefault();
844+
themes.firstElementChild.focus();
845+
break;
846+
case "End":
847+
e.preventDefault();
848+
themes.lastElementChild.focus();
849+
break;
850+
}}
851+
}};
852+
810853
themePicker.onclick = switchThemeButtonState;
811854
themePicker.onblur = handleThemeButtonsBlur;
855+
document.addEventListener("keydown", handleThemeKeyPress);
812856
{}.forEach(function(item) {{
813857
var but = document.createElement("button");
814858
but.textContent = item;

0 commit comments

Comments
 (0)