Skip to content

Commit 72f6322

Browse files
Fix scroll when source sidebar is open on mobile
1 parent 277b77a commit 72f6322

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(function() {
1111

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

1415
function createDirEntry(elem, parent, fullPath, hasFoundFile) {
1516
const name = document.createElement("div");
@@ -65,10 +66,24 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
6566
function toggleSidebar() {
6667
const child = this.children[0];
6768
if (child.innerText === ">") {
69+
if (window.innerWidth < 701) {
70+
// This is to keep the scroll position on mobile.
71+
oldScrollPosition = window.scrollY;
72+
document.body.style.position = "fixed";
73+
document.body.style.top = `-${oldScrollPosition}px`;
74+
}
6875
addClass(document.documentElement, "source-sidebar-expanded");
6976
child.innerText = "<";
7077
updateLocalStorage("source-sidebar-show", "true");
7178
} else {
79+
if (window.innerWidth < 701) {
80+
// This is to keep the scroll position on mobile.
81+
document.body.style.position = "";
82+
document.body.style.top = "";
83+
// The scroll position is lost when resetting the style, hence why we store it in
84+
// `oldScroll`.
85+
window.scrollTo(0, oldScrollPosition);
86+
}
7287
removeClass(document.documentElement, "source-sidebar-expanded");
7388
child.innerText = ">";
7489
updateLocalStorage("source-sidebar-show", "false");

0 commit comments

Comments
 (0)