about summary refs log tree commit diff
path: root/src/librustdoc/html/static/js/source-script.js
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-10-17 23:09:07 +0200
committerGitHub <noreply@github.com>2022-10-17 23:09:07 +0200
commit0a19575591bb8d996a36dce15ff8961f2e90d5e6 (patch)
tree524e1d3e37fed48e74c3cb8c04e8fc3f59f76e97 /src/librustdoc/html/static/js/source-script.js
parent245b12e26ebf37575c9e56a61045ce14bfa98d76 (diff)
parent3932b2c21d5e2a8184739eb41df7501929f70194 (diff)
downloadrust-0a19575591bb8d996a36dce15ff8961f2e90d5e6.tar.gz
rust-0a19575591bb8d996a36dce15ff8961f2e90d5e6.zip
Rollup merge of #103160 - notriddle:notriddle/js-mobile-scroll, r=GuillaumeGomez
rustdoc: factor JS mobile scroll lock into its own function

https://github.com/rust-lang/rust/pull/98775#issuecomment-1172728308
Diffstat (limited to 'src/librustdoc/html/static/js/source-script.js')
-rw-r--r--src/librustdoc/html/static/js/source-script.js33
1 files changed, 3 insertions, 30 deletions
diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js
index 8286e9201e6..0b9368dd899 100644
--- a/src/librustdoc/html/static/js/source-script.js
+++ b/src/librustdoc/html/static/js/source-script.js
@@ -10,7 +10,6 @@
 (function() {
 
 const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value;
-let oldScrollPosition = null;
 
 const NAME_OFFSET = 0;
 const DIRS_OFFSET = 1;
@@ -70,44 +69,18 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
 function toggleSidebar() {
     const child = this.parentNode.children[0];
     if (child.innerText === ">") {
-        if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
-            // This is to keep the scroll position on mobile.
-            oldScrollPosition = window.scrollY;
-            document.body.style.position = "fixed";
-            document.body.style.top = `-${oldScrollPosition}px`;
-        } else {
-            oldScrollPosition = null;
-        }
+        window.rustdocMobileScrollLock();
         addClass(document.documentElement, "source-sidebar-expanded");
         child.innerText = "<";
         updateLocalStorage("source-sidebar-show", "true");
     } else {
-        if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT && oldScrollPosition !== null) {
-            // This is to keep the scroll position on mobile.
-            document.body.style.position = "";
-            document.body.style.top = "";
-            // The scroll position is lost when resetting the style, hence why we store it in
-            // `oldScrollPosition`.
-            window.scrollTo(0, oldScrollPosition);
-            oldScrollPosition = null;
-        }
+        window.rustdocMobileScrollUnlock();
         removeClass(document.documentElement, "source-sidebar-expanded");
         child.innerText = ">";
         updateLocalStorage("source-sidebar-show", "false");
     }
 }
 
-window.addEventListener("resize", () => {
-    if (window.innerWidth >= window.RUSTDOC_MOBILE_BREAKPOINT && oldScrollPosition !== null) {
-        // If the user opens the sidebar in "mobile" mode, and then grows the browser window,
-        // we need to switch away from mobile mode and make the main content area scrollable.
-        document.body.style.position = "";
-        document.body.style.top = "";
-        window.scrollTo(0, oldScrollPosition);
-        oldScrollPosition = null;
-    }
-});
-
 function createSidebarToggle() {
     const sidebarToggle = document.createElement("div");
     sidebarToggle.id = "sidebar-toggle";
@@ -125,7 +98,7 @@ function createSidebarToggle() {
     return sidebarToggle;
 }
 
-// This function is called from "source-files.js", generated in `html/render/mod.rs`.
+// This function is called from "source-files.js", generated in `html/render/write_shared.rs`.
 // eslint-disable-next-line no-unused-vars
 function createSourceSidebar() {
     const container = document.querySelector("nav.sidebar");