Skip to content

Commit 02fc0fa

Browse files
authoredAug 9, 2022
Rollup merge of #98775 - notriddle:notriddle/mobile-sidebar-scroll-lock, r=jsha
rustdoc: improve scroll locking in the rustdoc mobile sidebars This PR prevents the main content area from scrolling while the mobile sidebar is open on documentation pages (porting the scroll locking behavior from the source sidebar to the regular sidebar), and also fixes some bad behavior where opening a "mobile" sidebar, and growing the viewport so that the "desktop" mode without scroll locking is activated, could potentially leave the page stuck. This does not affect the behavior on larger screens. Only small ones, where the sidebar covers up the main content. Split out from #98772
2 parents 63e4312 + 6b60bc6 commit 02fc0fa

File tree

4 files changed

+101
-7
lines changed

4 files changed

+101
-7
lines changed
 

Diff for: ‎src/librustdoc/html/static/js/main.js

+42-4
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ function loadCss(cssFileName) {
348348

349349
function onHashChange(ev) {
350350
// If we're in mobile mode, we should hide the sidebar in any case.
351-
const sidebar = document.getElementsByClassName("sidebar")[0];
352-
removeClass(sidebar, "shown");
351+
hideSidebar();
353352
handleHashes(ev);
354353
}
355354

@@ -728,11 +727,50 @@ function loadCss(cssFileName) {
728727
});
729728
}());
730729

730+
let oldSidebarScrollPosition = null;
731+
732+
function showSidebar() {
733+
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
734+
// This is to keep the scroll position on mobile.
735+
oldSidebarScrollPosition = window.scrollY;
736+
document.body.style.width = `${document.body.offsetWidth}px`;
737+
document.body.style.position = "fixed";
738+
document.body.style.top = `-${oldSidebarScrollPosition}px`;
739+
document.querySelector(".mobile-topbar").style.top = `${oldSidebarScrollPosition}px`;
740+
document.querySelector(".mobile-topbar").style.position = "relative";
741+
} else {
742+
oldSidebarScrollPosition = null;
743+
}
744+
const sidebar = document.getElementsByClassName("sidebar")[0];
745+
addClass(sidebar, "shown");
746+
}
747+
731748
function hideSidebar() {
749+
if (oldSidebarScrollPosition !== null) {
750+
// This is to keep the scroll position on mobile.
751+
document.body.style.width = "";
752+
document.body.style.position = "";
753+
document.body.style.top = "";
754+
document.querySelector(".mobile-topbar").style.top = "";
755+
document.querySelector(".mobile-topbar").style.position = "";
756+
// The scroll position is lost when resetting the style, hence why we store it in
757+
// `oldSidebarScrollPosition`.
758+
window.scrollTo(0, oldSidebarScrollPosition);
759+
oldSidebarScrollPosition = null;
760+
}
732761
const sidebar = document.getElementsByClassName("sidebar")[0];
733762
removeClass(sidebar, "shown");
734763
}
735764

765+
window.addEventListener("resize", () => {
766+
if (window.innerWidth >= window.RUSTDOC_MOBILE_BREAKPOINT &&
767+
oldSidebarScrollPosition !== null) {
768+
// If the user opens the sidebar in "mobile" mode, and then grows the browser window,
769+
// we need to switch away from mobile mode and make the main content area scrollable.
770+
hideSidebar();
771+
}
772+
});
773+
736774
function handleClick(id, f) {
737775
const elem = document.getElementById(id);
738776
if (elem) {
@@ -775,9 +813,9 @@ function loadCss(cssFileName) {
775813
sidebar_menu_toggle.addEventListener("click", () => {
776814
const sidebar = document.getElementsByClassName("sidebar")[0];
777815
if (!hasClass(sidebar, "shown")) {
778-
addClass(sidebar, "shown");
816+
showSidebar();
779817
} else {
780-
removeClass(sidebar, "shown");
818+
hideSidebar();
781819
}
782820
});
783821
}

Diff for: ‎src/librustdoc/html/static/js/source-script.js

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

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

1515
const NAME_OFFSET = 0;
1616
const DIRS_OFFSET = 1;
@@ -75,25 +75,39 @@ function toggleSidebar() {
7575
oldScrollPosition = window.scrollY;
7676
document.body.style.position = "fixed";
7777
document.body.style.top = `-${oldScrollPosition}px`;
78+
} else {
79+
oldScrollPosition = null;
7880
}
7981
addClass(document.documentElement, "source-sidebar-expanded");
8082
child.innerText = "<";
8183
updateLocalStorage("source-sidebar-show", "true");
8284
} else {
83-
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
85+
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT && oldScrollPosition !== null) {
8486
// This is to keep the scroll position on mobile.
8587
document.body.style.position = "";
8688
document.body.style.top = "";
8789
// The scroll position is lost when resetting the style, hence why we store it in
88-
// `oldScroll`.
90+
// `oldScrollPosition`.
8991
window.scrollTo(0, oldScrollPosition);
92+
oldScrollPosition = null;
9093
}
9194
removeClass(document.documentElement, "source-sidebar-expanded");
9295
child.innerText = ">";
9396
updateLocalStorage("source-sidebar-show", "false");
9497
}
9598
}
9699

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+
97111
function createSidebarToggle() {
98112
const sidebarToggle = document.createElement("div");
99113
sidebarToggle.id = "sidebar-toggle";

Diff for: ‎src/test/rustdoc-gui/sidebar-mobile-scroll.goml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This test ensures that the mobile sidebar preserves scroll position.
2+
goto: file://|DOC_PATH|/test_docs/struct.Foo.html
3+
// Switching to "mobile view" by reducing the width to 600px.
4+
size: (600, 600)
5+
assert-css: (".sidebar", {"display": "block", "left": "-1000px"})
6+
7+
// Scroll down.
8+
scroll-to: "//h2[@id='blanket-implementations']"
9+
assert-window-property: {"pageYOffset": "702"}
10+
11+
// Open the sidebar menu.
12+
click: ".sidebar-menu-toggle"
13+
wait-for-css: (".sidebar", {"left": "0px"})
14+
15+
// We are no longer "scrolled". It's important that the user can't
16+
// scroll the body at all, but these test scripts are run only in Chrome,
17+
// and we need to use a more complicated solution to this problem because
18+
// of Mobile Safari...
19+
assert-window-property: {"pageYOffset": "0"}
20+
21+
// Close the sidebar menu. Make sure the scroll position gets restored.
22+
click: ".sidebar-menu-toggle"
23+
wait-for-css: (".sidebar", {"left": "-1000px"})
24+
assert-window-property: {"pageYOffset": "702"}
25+
26+
// Now test that scrollability returns when the browser window is just resized.
27+
click: ".sidebar-menu-toggle"
28+
wait-for-css: (".sidebar", {"left": "0px"})
29+
assert-window-property: {"pageYOffset": "0"}
30+
size: (900, 900)
31+
assert-window-property: {"pageYOffset": "702"}

Diff for: ‎src/test/rustdoc-gui/sidebar-source-code-display.goml

+11
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ wait-for-css: (".sidebar", {"width": "0px"})
233233
// The "scrollTop" property should be the same.
234234
assert-window-property: {"pageYOffset": "2519"}
235235

236+
// We now check that the scroll position is restored if the window is resized.
237+
size: (500, 700)
238+
click: "#sidebar-toggle"
239+
wait-for-css: ("#source-sidebar", {"visibility": "visible"})
240+
assert-window-property: {"pageYOffset": "0"}
241+
size: (900, 900)
242+
assert-window-property: {"pageYOffset": "2519"}
243+
size: (500, 700)
244+
click: "#sidebar-toggle"
245+
wait-for-css: ("#source-sidebar", {"visibility": "hidden"})
246+
236247
// We now check that opening the sidebar and clicking a link will close it.
237248
// The behavior here on mobile is different than the behavior on desktop,
238249
// but common sense dictates that if you have a list of files that fills the entire screen, and

0 commit comments

Comments
 (0)