Skip to content

Commit c2613a5

Browse files
authored
Rollup merge of #98776 - notriddle:notriddle/mobile-sidebar-auto-close, r=GuillaumeGomez
rustdoc: 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. Split out separately from #98772
2 parents 291df97 + 6e2c49f commit c2613a5

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

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

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

17401740
/* Media Queries */
17411741

1742+
/*
1743+
WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY;
1744+
If you update this line, then you also need to update the line with the same warning
1745+
in storage.js plus the media query with (max-width: 700px)
1746+
*/
17421747
@media (min-width: 701px) {
17431748
/* In case there is no documentation before a code block, we need to add some margin at the top
17441749
to prevent an overlay between the "collapse toggle" and the information tooltip.
@@ -1759,6 +1764,11 @@ details.rustdoc-toggle[open] > summary.hideme::after {
17591764
}
17601765
}
17611766

1767+
/*
1768+
WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
1769+
If you update this line, then you also need to update the line with the same warning
1770+
in storage.js plus the media query with (min-width: 701px)
1771+
*/
17621772
@media (max-width: 700px) {
17631773
/* When linking to an item with an `id` (for instance, by clicking a link in the sidebar,
17641774
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 dirEntry = document.createElement("details");
1723
const summary = document.createElement("summary");
@@ -42,6 +48,7 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
4248
const file = document.createElement("a");
4349
file.innerText = file_text;
4450
file.href = rootPath + "src/" + fullPath + file_text + ".html";
51+
file.addEventListener("click", closeSidebarIfMobile);
4552
const w = window.location.href.split("#")[0];
4653
if (!hasFoundFile && w === file.href) {
4754
file.className = "selected";
@@ -59,7 +66,7 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
5966
function toggleSidebar() {
6067
const child = this.parentNode.children[0];
6168
if (child.innerText === ">") {
62-
if (window.innerWidth < 701) {
69+
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
6370
// This is to keep the scroll position on mobile.
6471
oldScrollPosition = window.scrollY;
6572
document.body.style.position = "fixed";
@@ -69,7 +76,7 @@ function toggleSidebar() {
6976
child.innerText = "<";
7077
updateLocalStorage("source-sidebar-show", "true");
7178
} else {
72-
if (window.innerWidth < 701) {
79+
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
7380
// This is to keep the scroll position on mobile.
7481
document.body.style.position = "";
7582
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

+31
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ 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+
wait-for-css: (".sidebar", {"width": "300px"})
30+
assert-local-storage: {"rustdoc-source-sidebar-show": "true"}
31+
2132
// Now we check the display of the sidebar items.
2233
show-text: true
2334

@@ -221,3 +232,23 @@ click: "#sidebar-toggle"
221232
wait-for-css: (".sidebar", {"width": "0px"})
222233
// The "scrollTop" property should be the same.
223234
assert-window-property: {"pageYOffset": "2519"}
235+
236+
// We now check that opening the sidebar and clicking a link will close it.
237+
// The behavior here on mobile is different than the behavior on desktop,
238+
// but common sense dictates that if you have a list of files that fills the entire screen, and
239+
// you click one of them, you probably want to actually see the file's contents, and not just
240+
// make it the current selection.
241+
click: "#sidebar-toggle"
242+
wait-for-css: ("#source-sidebar", {"visibility": "visible"})
243+
assert-local-storage: {"rustdoc-source-sidebar-show": "true"}
244+
click: ".sidebar a.selected"
245+
goto: file://|DOC_PATH|/src/test_docs/lib.rs.html
246+
wait-for-css: ("#source-sidebar", {"visibility": "hidden"})
247+
assert-local-storage: {"rustdoc-source-sidebar-show": "false"}
248+
// Resize back to desktop size, to check that the sidebar doesn't spontaneously open.
249+
size: (1000, 1000)
250+
wait-for-css: ("#source-sidebar", {"visibility": "hidden"})
251+
assert-local-storage: {"rustdoc-source-sidebar-show": "false"}
252+
click: "#sidebar-toggle"
253+
wait-for-css: ("#source-sidebar", {"visibility": "visible"})
254+
assert-local-storage: {"rustdoc-source-sidebar-show": "true"}

0 commit comments

Comments
 (0)