Skip to content

Commit b1403d6

Browse files
authored
Rollup merge of #98671 - GuillaumeGomez:source-sidebar-fixes, r=notriddle
Fix source sidebar bugs This PR fixes the following two bugs: ![Screenshot from 2022-06-29 14-39-58](https://user-images.githubusercontent.com/3050060/176449070-3e3762da-2bfe-4acf-8eb0-34f6eb4c94ed.png) ![Screenshot from 2022-06-29 15-05-09](https://user-images.githubusercontent.com/3050060/176449073-b164820b-bd71-4b1a-990c-bba4e5fce196.png) I added regression tests to prevent them to happen again. I think we should backport it to beta as well. You can test it [here](https://rustdoc.crud.net/imperio/source-sidebar-fixes/src/std/lib.rs.html). cc ```@jsha``` r? ```@notriddle```
2 parents 5cd41d7 + 9a1f52d commit b1403d6

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.6
1+
0.9.7

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,9 +1772,11 @@ details.rustdoc-toggle[open] > summary.hideme::after {
17721772
/* The source view uses a different design for the sidebar toggle, and doesn't have a topbar,
17731773
so don't bump down the main content or the sidebar. */
17741774
.source main,
1775-
.source .sidebar {
1775+
.rustdoc.source .sidebar {
17761776
top: 0;
17771777
padding: 0;
1778+
height: 100vh;
1779+
border: 0;
17781780
}
17791781

17801782
.sidebar.shown,
@@ -1924,6 +1926,9 @@ details.rustdoc-toggle[open] > summary.hideme::after {
19241926
width: unset;
19251927
border-top-right-radius: unset;
19261928
border-bottom-right-radius: unset;
1929+
position: sticky;
1930+
border: 0;
1931+
border-bottom: 1px solid;
19271932
}
19281933

19291934
#source-sidebar {

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");

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,39 @@ assert-css: (
116116
"#source-sidebar .expand + .children .folders .name",
117117
{"color": "rgb(255, 180, 76)", "background-color": "rgb(20, 25, 31)"},
118118
)
119+
120+
// Now checking on mobile devices.
121+
size: (500, 700)
122+
reload:
123+
// Waiting for the sidebar to be displayed...
124+
wait-for-css: ("#sidebar-toggle", {"visibility": "visible", "opacity": 1})
125+
126+
// We now check it takes the full size of the display.
127+
assert-property: ("body", {"clientWidth": "500", "clientHeight": "700"})
128+
assert-property: (".sidebar", {"clientWidth": "500", "clientHeight": "700"})
129+
130+
// We now check the display of the toggle once the sidebar is expanded.
131+
assert-property: ("#sidebar-toggle", {"clientWidth": "500", "clientHeight": "39"})
132+
assert-css: (
133+
"#sidebar-toggle",
134+
{
135+
"border-top-width": "0px",
136+
"border-right-width": "0px",
137+
"border-left-width": "0px",
138+
"border-bottom-width": "1px",
139+
},
140+
)
141+
142+
// We now check that the scroll position is kept when opening the sidebar.
143+
click: "#sidebar-toggle"
144+
wait-for-css: (".sidebar", {"width": "0px"})
145+
// We scroll to line 117 to change the scroll position.
146+
scroll-to: '//*[@id="117"]'
147+
assert-window-property: {"pageYOffset": "2519"}
148+
// Expanding the sidebar...
149+
click: "#sidebar-toggle"
150+
wait-for-css: (".sidebar", {"width": "500px"})
151+
click: "#sidebar-toggle"
152+
wait-for-css: (".sidebar", {"width": "0px"})
153+
// The "scrollTop" property should be the same.
154+
assert-window-property: {"pageYOffset": "2519"}

src/test/rustdoc-gui/sidebar-source-code.goml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ assert: "nav.sidebar"
1818

1919
// We now switch to mobile mode.
2020
size: (600, 600)
21-
// We check that the sidebar has the expected width (0 and 1px for the border).
22-
assert-css: ("nav.sidebar", {"width": "1px"})
21+
// We check that the sidebar has the expected width (0).
22+
assert-css: ("nav.sidebar", {"width": "0px"})
2323
// We expand the sidebar.
2424
click: "#sidebar-toggle"
2525
assert-css: (".source-sidebar-expanded nav.sidebar", {"width": "600px"})

0 commit comments

Comments
 (0)