Skip to content

Commit 83f2288

Browse files
committed
Improve click behavior of the source code mobile full-screen "sidebar"
On desktop, if you open the source code sidebar, it stays open even when you move from page to page. It used to do the same thing on mobile, but I think that's stupid. Since the file list fills the entire screen on mobile, and you can't really do anything with the currently selected file other than dismiss the "sidebar" to look at it, it's safe to assume that anybody who clicks a file in that list probably wants the list to go away so they can see it.
1 parent 5b9775f commit 83f2288

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

Diff for: src/librustdoc/html/static/css/rustdoc.css

+10
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,11 @@ details.rustdoc-toggle[open] > summary.hideme::after {
16831683

16841684
/* Media Queries */
16851685

1686+
/*
1687+
WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY;
1688+
If you update this line, then you also need to update the line with the same warning
1689+
in storage.js plus the media query with (max-width: 700px)
1690+
*/
16861691
@media (min-width: 701px) {
16871692
/* In case there is no documentation before a code block, we need to add some margin at the top
16881693
to prevent an overlay between the "collapse toggle" and the information tooltip.
@@ -1703,6 +1708,11 @@ details.rustdoc-toggle[open] > summary.hideme::after {
17031708
}
17041709
}
17051710

1711+
/*
1712+
WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
1713+
If you update this line, then you also need to update the line with the same warning
1714+
in storage.js plus the media query with (min-width: 701px)
1715+
*/
17061716
@media (max-width: 700px) {
17071717
/* When linking to an item with an `id` (for instance, by clicking a link in the sidebar,
17081718
or visiting a URL with a fragment like `#method.new`, we don't want the item to be obscured

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value;
1313
let oldScrollPosition = 0;
1414

15+
function closeSidebarIfMobile() {
16+
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
17+
updateLocalStorage("source-sidebar-show", "false");
18+
}
19+
}
20+
1521
function createDirEntry(elem, parent, fullPath, hasFoundFile) {
1622
const name = document.createElement("div");
1723
name.className = "name";
@@ -48,6 +54,7 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
4854
const file = document.createElement("a");
4955
file.innerText = file_text;
5056
file.href = rootPath + "src/" + fullPath + file_text + ".html";
57+
file.addEventListener("click", closeSidebarIfMobile);
5158
const w = window.location.href.split("#")[0];
5259
if (!hasFoundFile && w === file.href) {
5360
file.className = "selected";
@@ -66,7 +73,7 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
6673
function toggleSidebar() {
6774
const child = this.children[0];
6875
if (child.innerText === ">") {
69-
if (window.innerWidth < 701) {
76+
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
7077
// This is to keep the scroll position on mobile.
7178
oldScrollPosition = window.scrollY;
7279
document.body.style.position = "fixed";
@@ -76,7 +83,7 @@ function toggleSidebar() {
7683
child.innerText = "<";
7784
updateLocalStorage("source-sidebar-show", "true");
7885
} else {
79-
if (window.innerWidth < 701) {
86+
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
8087
// This is to keep the scroll position on mobile.
8188
document.body.style.position = "";
8289
document.body.style.top = "";

Diff for: src/librustdoc/html/static/js/storage.js

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const darkThemes = ["dark", "ayu"];
99
window.currentTheme = document.getElementById("themeStyle");
1010
window.mainTheme = document.getElementById("mainThemeStyle");
1111

12+
// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
13+
// If you update this line, then you also need to update the two media queries with the same
14+
// warning in rustdoc.css
15+
window.RUSTDOC_MOBILE_BREAKPOINT = 701;
16+
1217
const settingsDataset = (function() {
1318
const settingsElement = document.getElementById("default-settings");
1419
if (settingsElement === null) {

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

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ click: "#sidebar-toggle"
1818
// Because of the transition CSS, we check by using `wait-for-css` instead of `assert-css`.
1919
wait-for-css: ("#sidebar-toggle", {"visibility": "visible", "opacity": 1})
2020

21+
// We now check that opening the sidebar and clicking a link will leave it open.
22+
// The behavior here on desktop is different than the behavior on mobile,
23+
// but since the sidebar doesn't fill the entire screen here, it makes sense to have the
24+
// sidebar stay resident.
25+
wait-for-css: (".sidebar", {"width": "300px"})
26+
assert-local-storage: {"rustdoc-source-sidebar-show": "true"}
27+
click: ".sidebar a.selected"
28+
goto: file://|DOC_PATH|/src/test_docs/lib.rs.html
29+
assert-local-storage: {"rustdoc-source-sidebar-show": "true"}
30+
2131
// Now we check the display of the sidebar items.
2232
show-text: true
2333

@@ -152,3 +162,15 @@ click: "#sidebar-toggle"
152162
wait-for-css: (".sidebar", {"width": "0px"})
153163
// The "scrollTop" property should be the same.
154164
assert-window-property: {"pageYOffset": "2519"}
165+
166+
// We now check that opening the sidebar and clicking a link will close it.
167+
// The behavior here on mobile is different than the behavior on desktop,
168+
// but common sense dictates that if you have a list of files that fills the entire screen, and
169+
// you click one of them, you probably want to actually see the file's contents, and not just
170+
// make it the current selection.
171+
click: "#sidebar-toggle"
172+
wait-for-css: (".sidebar", {"width": "500px"})
173+
assert-local-storage: {"rustdoc-source-sidebar-show": "true"}
174+
click: ".sidebar a.selected"
175+
goto: file://|DOC_PATH|/src/test_docs/lib.rs.html
176+
assert-local-storage: {"rustdoc-source-sidebar-show": "false"}

0 commit comments

Comments
 (0)