Skip to content

Commit 0a19575

Browse files
authored
Rollup merge of #103160 - notriddle:notriddle/js-mobile-scroll, r=GuillaumeGomez
rustdoc: factor JS mobile scroll lock into its own function #98775 (comment)
2 parents 245b12e + 3932b2c commit 0a19575

File tree

2 files changed

+28
-41
lines changed

2 files changed

+28
-41
lines changed

src/librustdoc/html/static/js/main.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -733,37 +733,51 @@ function loadCss(cssFileName) {
733733

734734
let oldSidebarScrollPosition = null;
735735

736-
function showSidebar() {
736+
// Scroll locking used both here and in source-script.js
737+
738+
window.rustdocMobileScrollLock = function() {
737739
const mobile_topbar = document.querySelector(".mobile-topbar");
738-
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT && mobile_topbar) {
740+
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
739741
// This is to keep the scroll position on mobile.
740742
oldSidebarScrollPosition = window.scrollY;
741743
document.body.style.width = `${document.body.offsetWidth}px`;
742744
document.body.style.position = "fixed";
743745
document.body.style.top = `-${oldSidebarScrollPosition}px`;
744-
mobile_topbar.style.top = `${oldSidebarScrollPosition}px`;
745-
mobile_topbar.style.position = "relative";
746+
if (mobile_topbar) {
747+
mobile_topbar.style.top = `${oldSidebarScrollPosition}px`;
748+
mobile_topbar.style.position = "relative";
749+
}
746750
} else {
747751
oldSidebarScrollPosition = null;
748752
}
749-
const sidebar = document.getElementsByClassName("sidebar")[0];
750-
addClass(sidebar, "shown");
751-
}
753+
};
752754

753-
function hideSidebar() {
755+
window.rustdocMobileScrollUnlock = function() {
754756
const mobile_topbar = document.querySelector(".mobile-topbar");
755-
if (oldSidebarScrollPosition !== null && mobile_topbar) {
757+
if (oldSidebarScrollPosition !== null) {
756758
// This is to keep the scroll position on mobile.
757759
document.body.style.width = "";
758760
document.body.style.position = "";
759761
document.body.style.top = "";
760-
mobile_topbar.style.top = "";
761-
mobile_topbar.style.position = "";
762+
if (mobile_topbar) {
763+
mobile_topbar.style.top = "";
764+
mobile_topbar.style.position = "";
765+
}
762766
// The scroll position is lost when resetting the style, hence why we store it in
763767
// `oldSidebarScrollPosition`.
764768
window.scrollTo(0, oldSidebarScrollPosition);
765769
oldSidebarScrollPosition = null;
766770
}
771+
};
772+
773+
function showSidebar() {
774+
window.rustdocMobileScrollLock();
775+
const sidebar = document.getElementsByClassName("sidebar")[0];
776+
addClass(sidebar, "shown");
777+
}
778+
779+
function hideSidebar() {
780+
window.rustdocMobileScrollUnlock();
767781
const sidebar = document.getElementsByClassName("sidebar")[0];
768782
removeClass(sidebar, "shown");
769783
}

src/librustdoc/html/static/js/source-script.js

+3-30
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
(function() {
1111

1212
const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value;
13-
let oldScrollPosition = null;
1413

1514
const NAME_OFFSET = 0;
1615
const DIRS_OFFSET = 1;
@@ -70,44 +69,18 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
7069
function toggleSidebar() {
7170
const child = this.parentNode.children[0];
7271
if (child.innerText === ">") {
73-
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
74-
// This is to keep the scroll position on mobile.
75-
oldScrollPosition = window.scrollY;
76-
document.body.style.position = "fixed";
77-
document.body.style.top = `-${oldScrollPosition}px`;
78-
} else {
79-
oldScrollPosition = null;
80-
}
72+
window.rustdocMobileScrollLock();
8173
addClass(document.documentElement, "source-sidebar-expanded");
8274
child.innerText = "<";
8375
updateLocalStorage("source-sidebar-show", "true");
8476
} else {
85-
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT && oldScrollPosition !== null) {
86-
// This is to keep the scroll position on mobile.
87-
document.body.style.position = "";
88-
document.body.style.top = "";
89-
// The scroll position is lost when resetting the style, hence why we store it in
90-
// `oldScrollPosition`.
91-
window.scrollTo(0, oldScrollPosition);
92-
oldScrollPosition = null;
93-
}
77+
window.rustdocMobileScrollUnlock();
9478
removeClass(document.documentElement, "source-sidebar-expanded");
9579
child.innerText = ">";
9680
updateLocalStorage("source-sidebar-show", "false");
9781
}
9882
}
9983

100-
window.addEventListener("resize", () => {
101-
if (window.innerWidth >= window.RUSTDOC_MOBILE_BREAKPOINT && oldScrollPosition !== null) {
102-
// If the user opens the sidebar in "mobile" mode, and then grows the browser window,
103-
// we need to switch away from mobile mode and make the main content area scrollable.
104-
document.body.style.position = "";
105-
document.body.style.top = "";
106-
window.scrollTo(0, oldScrollPosition);
107-
oldScrollPosition = null;
108-
}
109-
});
110-
11184
function createSidebarToggle() {
11285
const sidebarToggle = document.createElement("div");
11386
sidebarToggle.id = "sidebar-toggle";
@@ -125,7 +98,7 @@ function createSidebarToggle() {
12598
return sidebarToggle;
12699
}
127100

128-
// This function is called from "source-files.js", generated in `html/render/mod.rs`.
101+
// This function is called from "source-files.js", generated in `html/render/write_shared.rs`.
129102
// eslint-disable-next-line no-unused-vars
130103
function createSourceSidebar() {
131104
const container = document.querySelector("nav.sidebar");

0 commit comments

Comments
 (0)