Skip to content

Commit a0a03e1

Browse files
authoredApr 11, 2023
Rollup merge of rust-lang#110165 - notriddle:notriddle/overscroll-behavior, r=GuillaumeGomez
rustdoc: use CSS `overscroll-behavior` instead of JavaScript Fixes the desktop scrolling weirdness mentioned in rust-lang#98775 (comment) Preview: https://notriddle.com/rustdoc-demo-html-3/overscroll-behavior/issue_107918/index.html As described in the [MDN overscroll-behavior] page: * The current Firefox ESR is 102, and the first Firefox version to support this feature is 59. * The current Chrome version 112, and the first version to support this is 63. * Edge is described as having a minor bug in `none` mode, but we use `contain` mode anyway, so it doesn't matter. * Safari 16, released September 2022, is the last browser to add this feature, and is also the oldest version we officially support. [MDN overscroll-behavior]: https://developer.mozilla.org/en-US/docs/Web/CSS/overscroll-behavior
2 parents de6e9cf + 6da8cb1 commit a0a03e1

File tree

5 files changed

+20
-97
lines changed

5 files changed

+20
-97
lines changed
 

‎src/librustdoc/html/static/css/rustdoc.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ img {
384384
font-size: 0.875rem;
385385
flex: 0 0 200px;
386386
overflow-y: scroll;
387+
overscroll-behavior: contain;
387388
position: sticky;
388389
height: 100vh;
389390
top: 0;
@@ -1531,7 +1532,7 @@ However, it's not needed with smaller screen width because the doc/code block is
15311532
/*
15321533
WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
15331534
If you update this line, then you also need to update the line with the same warning
1534-
in main.js
1535+
in source-script.js
15351536
*/
15361537
@media (max-width: 700px) {
15371538
/* When linking to an item with an `id` (for instance, by clicking a link in the sidebar,

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

-52
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
"use strict";
66

7-
// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
8-
// If you update this line, then you also need to update the media query with the same
9-
// warning in rustdoc.css
10-
window.RUSTDOC_MOBILE_BREAKPOINT = 700;
11-
127
// Given a basename (e.g. "storage") and an extension (e.g. ".js"), return a URL
138
// for a resource under the root-path, with the resource-suffix.
149
function resourcePath(basename, extension) {
@@ -730,65 +725,18 @@ function preLoadCss(cssUrl) {
730725
window.rustdoc_add_line_numbers_to_examples();
731726
}
732727

733-
let oldSidebarScrollPosition = null;
734-
735-
// Scroll locking used both here and in source-script.js
736-
737-
window.rustdocMobileScrollLock = function() {
738-
const mobile_topbar = document.querySelector(".mobile-topbar");
739-
if (window.innerWidth <= window.RUSTDOC_MOBILE_BREAKPOINT) {
740-
// This is to keep the scroll position on mobile.
741-
oldSidebarScrollPosition = window.scrollY;
742-
document.body.style.width = `${document.body.offsetWidth}px`;
743-
document.body.style.position = "fixed";
744-
document.body.style.top = `-${oldSidebarScrollPosition}px`;
745-
if (mobile_topbar) {
746-
mobile_topbar.style.top = `${oldSidebarScrollPosition}px`;
747-
mobile_topbar.style.position = "relative";
748-
}
749-
} else {
750-
oldSidebarScrollPosition = null;
751-
}
752-
};
753-
754-
window.rustdocMobileScrollUnlock = function() {
755-
const mobile_topbar = document.querySelector(".mobile-topbar");
756-
if (oldSidebarScrollPosition !== null) {
757-
// This is to keep the scroll position on mobile.
758-
document.body.style.width = "";
759-
document.body.style.position = "";
760-
document.body.style.top = "";
761-
if (mobile_topbar) {
762-
mobile_topbar.style.top = "";
763-
mobile_topbar.style.position = "";
764-
}
765-
// The scroll position is lost when resetting the style, hence why we store it in
766-
// `oldSidebarScrollPosition`.
767-
window.scrollTo(0, oldSidebarScrollPosition);
768-
oldSidebarScrollPosition = null;
769-
}
770-
};
771-
772728
function showSidebar() {
773729
window.hideAllModals(false);
774-
window.rustdocMobileScrollLock();
775730
const sidebar = document.getElementsByClassName("sidebar")[0];
776731
addClass(sidebar, "shown");
777732
}
778733

779734
function hideSidebar() {
780-
window.rustdocMobileScrollUnlock();
781735
const sidebar = document.getElementsByClassName("sidebar")[0];
782736
removeClass(sidebar, "shown");
783737
}
784738

785739
window.addEventListener("resize", () => {
786-
if (window.innerWidth > window.RUSTDOC_MOBILE_BREAKPOINT &&
787-
oldSidebarScrollPosition !== null) {
788-
// If the user opens the sidebar in "mobile" mode, and then grows the browser window,
789-
// we need to switch away from mobile mode and make the main content area scrollable.
790-
hideSidebar();
791-
}
792740
if (window.CURRENT_TOOLTIP_ELEMENT) {
793741
// As a workaround to the behavior of `contains: layout` used in doc togglers,
794742
// tooltip popovers are positioned using javascript.

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ const NAME_OFFSET = 0;
1515
const DIRS_OFFSET = 1;
1616
const FILES_OFFSET = 2;
1717

18+
// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
19+
// If you update this line, then you also need to update the media query with the same
20+
// warning in rustdoc.css
21+
const RUSTDOC_MOBILE_BREAKPOINT = 700;
22+
1823
function closeSidebarIfMobile() {
19-
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
24+
if (window.innerWidth < RUSTDOC_MOBILE_BREAKPOINT) {
2025
updateLocalStorage("source-sidebar-show", "false");
2126
}
2227
}
@@ -69,12 +74,10 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
6974
function toggleSidebar() {
7075
const child = this.parentNode.children[0];
7176
if (child.innerText === ">") {
72-
window.rustdocMobileScrollLock();
7377
addClass(document.documentElement, "source-sidebar-expanded");
7478
child.innerText = "<";
7579
updateLocalStorage("source-sidebar-show", "true");
7680
} else {
77-
window.rustdocMobileScrollUnlock();
7881
removeClass(document.documentElement, "source-sidebar-expanded");
7982
child.innerText = ">";
8083
updateLocalStorage("source-sidebar-show", "false");
+11-30
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
// This test ensures that the mobile sidebar preserves scroll position.
1+
// This test ensures that the mobile disables scrolling the page.
22
goto: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
3-
// Switching to "mobile view" by reducing the width to 600px.
4-
size: (700, 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": "622"}
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": "622"}
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, 600)
31-
assert-window-property: {"pageYOffset": "622"}
3+
size: (1280, 800) // desktop
4+
assert-css: (".sidebar", {"overscroll-behavior": "contain"})
5+
size: (700, 600) // mobile
6+
assert-css: (".sidebar", {"overscroll-behavior": "contain"})
7+
8+
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
9+
size: (1280, 800) // desktop
10+
assert-css: (".sidebar", {"overscroll-behavior": "contain"})
11+
size: (700, 600) // mobile
12+
assert-css: (".sidebar", {"overscroll-behavior": "contain"})

‎tests/rustdoc-gui/sidebar-source-code-display.goml

+1-11
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,12 @@ wait-for-css: (".sidebar", {"left": "-1000px"})
183183
// The "scrollTop" property should be the same.
184184
assert-window-property: {"pageYOffset": "2542"}
185185

186-
// We now check that the scroll position is restored if the window is resized.
187-
size: (500, 700)
188-
click: "#src-sidebar-toggle"
189-
wait-for-css: ("#source-sidebar", {"visibility": "visible"})
190-
assert-window-property: {"pageYOffset": "0"}
191-
size: (900, 900)
192-
assert-window-property: {"pageYOffset": "2542"}
193-
size: (500, 700)
194-
click: "#src-sidebar-toggle"
195-
wait-for-css: ("#source-sidebar", {"visibility": "hidden"})
196-
197186
// We now check that opening the sidebar and clicking a link will close it.
198187
// The behavior here on mobile is different than the behavior on desktop,
199188
// but common sense dictates that if you have a list of files that fills the entire screen, and
200189
// you click one of them, you probably want to actually see the file's contents, and not just
201190
// make it the current selection.
191+
size: (500, 700)
202192
click: "#src-sidebar-toggle"
203193
wait-for-css: ("#source-sidebar", {"visibility": "visible"})
204194
assert-local-storage: {"rustdoc-source-sidebar-show": "true"}

0 commit comments

Comments
 (0)