about summary refs log tree commit diff
path: root/src/librustdoc/html
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2022-11-26 09:52:58 -0700
committerMichael Howell <michael@notriddle.com>2022-11-26 10:19:22 -0700
commit74de78a414043202ed70c239b2aca74dceb4c1a8 (patch)
treeb1ba3cb1418e87942da3bd8cd658c17c9c8a9692 /src/librustdoc/html
parent579c993b35a02dc439b74bba18b0a14f77911c95 (diff)
downloadrust-74de78a414043202ed70c239b2aca74dceb4c1a8.tar.gz
rust-74de78a414043202ed70c239b2aca74dceb4c1a8.zip
rustdoc: improve popover focus handling JS
This commit fixes a few inconsistencies and erratic behavior from the
notable traits, settings, and sidebar popups:

* It makes it so that pressing Escape closes the mobile sidebar.
  This is a bit difficult to do on iPhone, but on other setups like
  desktop tiling window managers, it's easy and makes sense.
* It makes sure that pressing escape while a notable trait popover is
  open focuses the popover's toggle button, instead of leaving nothing
  focused, since that makes more sense with keyboard navigation. Clicking
  the settings, help, or sidebar buttons, however, will not focus the
  notable trait popover toggle button.
* It ensures that notable trait and settings popovers are exclusive
  with the mobile sidebar. Nothing should ever overlap a popover, and
  there should never be more than one popover open at once.
Diffstat (limited to 'src/librustdoc/html')
-rw-r--r--src/librustdoc/html/static/js/main.js24
-rw-r--r--src/librustdoc/html/static/js/settings.js2
2 files changed, 21 insertions, 5 deletions
diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index 2a109ffbc60..ef1ae68c59e 100644
--- a/src/librustdoc/html/static/js/main.js
+++ b/src/librustdoc/html/static/js/main.js
@@ -202,6 +202,7 @@ function loadCss(cssUrl) {
         if (event.ctrlKey || event.altKey || event.metaKey) {
             return;
         }
+        window.hideAllModals();
         addClass(getSettingsButton(), "rotate");
         event.preventDefault();
         // Sending request for the CSS and the JS files at the same time so it will
@@ -377,7 +378,10 @@ function loadCss(cssUrl) {
         }
         ev.preventDefault();
         searchState.defocus();
-        window.hidePopoverMenus();
+        // If the notable traits popover is open, and the user presses Escape,
+        // reset focus back to the link.
+        hideNotable(true);
+        window.hideAllModals();
     }
 
     function handleShortcut(ev) {
@@ -767,6 +771,7 @@ function loadCss(cssUrl) {
     };
 
     function showSidebar() {
+        window.hideAllModals();
         window.rustdocMobileScrollLock();
         const sidebar = document.getElementsByClassName("sidebar")[0];
         addClass(sidebar, "shown");
@@ -843,7 +848,7 @@ function loadCss(cssUrl) {
             // Make this function idempotent.
             return;
         }
-        hideNotable(false);
+        window.hideAllModals();
         const ty = e.getAttribute("data-ty");
         const wrapper = document.createElement("div");
         wrapper.innerHTML = "<div class=\"docblock\">" + window.NOTABLE_TRAITS[ty] + "</div>";
@@ -1050,13 +1055,24 @@ function loadCss(cssUrl) {
     }
 
     /**
+     * Hide popover menus, notable trait tooltips, and the sidebar (if applicable).
+     *
+     * This version does not do anything to tweak the focused element. The caller
+     * should make sure it behaves reasonably.
+     */
+    window.hideAllModals = function() {
+        hideSidebar();
+        window.hidePopoverMenus();
+        hideNotable(false);
+    };
+
+    /**
      * Hide all the popover menus.
      */
     window.hidePopoverMenus = function() {
         onEachLazy(document.querySelectorAll(".search-form .popover"), elem => {
             elem.style.display = "none";
         });
-        hideNotable(false);
     };
 
     /**
@@ -1081,7 +1097,7 @@ function loadCss(cssUrl) {
     function showHelp() {
         const menu = getHelpMenu(true);
         if (menu.style.display === "none") {
-            window.hidePopoverMenus();
+            window.hideAllModals();
             menu.style.display = "";
         }
     }
diff --git a/src/librustdoc/html/static/js/settings.js b/src/librustdoc/html/static/js/settings.js
index 5256ae916a7..589bfc79360 100644
--- a/src/librustdoc/html/static/js/settings.js
+++ b/src/librustdoc/html/static/js/settings.js
@@ -268,7 +268,7 @@
             event.preventDefault();
             const shouldDisplaySettings = settingsMenu.style.display === "none";
 
-            window.hidePopoverMenus();
+            window.hideAllModals();
             if (shouldDisplaySettings) {
                 displaySettings();
             }