Skip to content

Commit 71b70fe

Browse files
Greatly improve sidebar when width < 700px
1 parent 9c49f40 commit 71b70fe

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

src/librustdoc/html/layout.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ r##"<!DOCTYPE html>
6565
{before_content}
6666
6767
<nav class="sidebar">
68+
<div class="sidebar-menu">&#9776;</div>
6869
{logo}
6970
{sidebar}
7071
</nav>

src/librustdoc/html/render.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,6 +3542,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
35423542
let cx = self.cx;
35433543
let it = self.item;
35443544
let parentlen = cx.current.len() - if it.is_mod() {1} else {0};
3545+
let mut should_close = false;
35453546

35463547
if it.is_struct() || it.is_trait() || it.is_primitive() || it.is_union()
35473548
|| it.is_enum() || it.is_mod() || it.is_typedef()
@@ -3575,6 +3576,8 @@ impl<'a> fmt::Display for Sidebar<'a> {
35753576
}
35763577
}
35773578

3579+
write!(fmt, "<div class=\"sidebar-elems\">")?;
3580+
should_close = true;
35783581
match it.inner {
35793582
clean::StructItem(ref s) => sidebar_struct(fmt, it, s)?,
35803583
clean::TraitItem(ref t) => sidebar_trait(fmt, it, t)?,
@@ -3625,6 +3628,10 @@ impl<'a> fmt::Display for Sidebar<'a> {
36253628
write!(fmt, "<script defer src=\"{path}sidebar-items.js\"></script>",
36263629
path = relpath)?;
36273630
}
3631+
if should_close {
3632+
// Closes sidebar-elems div.
3633+
write!(fmt, "</div>")?;
3634+
}
36283635

36293636
Ok(())
36303637
}

src/librustdoc/html/static/main.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@
106106
return (elem.offsetParent === null)
107107
}
108108

109+
function showSidebar() {
110+
document.getElementsByClassName("sidebar-elems")[0].style.display = "block";
111+
var sidebar = document.getElementsByClassName('sidebar')[0];
112+
sidebar.style.position = 'fixed';
113+
sidebar.style.width = '100%';
114+
sidebar.style.marginLeft = '0';
115+
document.getElementsByTagName("body")[0].style.marginTop = '45px';
116+
}
117+
118+
function hideSidebar() {
119+
document.getElementsByClassName("sidebar-elems")[0].style.display = "";
120+
var sidebar = document.getElementsByClassName('sidebar')[0];
121+
sidebar.style.position = '';
122+
sidebar.style.width = '';
123+
sidebar.style.marginLeft = '';
124+
document.getElementsByTagName("body")[0].style.marginTop = '';
125+
}
126+
109127
// used for special search precedence
110128
var TY_PRIMITIVE = itemTypes.indexOf("primitive");
111129

@@ -130,6 +148,8 @@
130148
}
131149

132150
function highlightSourceLines(ev) {
151+
// If we're in mobile mode, we should add the sidebar in any case.
152+
hideSidebar();
133153
var search = document.getElementById("search");
134154
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
135155
if (match) {
@@ -1459,7 +1479,7 @@
14591479

14601480
// delayed sidebar rendering.
14611481
function initSidebarItems(items) {
1462-
var sidebar = document.getElementsByClassName('sidebar')[0];
1482+
var sidebar = document.getElementsByClassName('sidebar-elems')[0];
14631483
var current = window.sidebarCurrent;
14641484

14651485
function block(shortty, longty) {
@@ -1829,6 +1849,22 @@
18291849
removeClass(search, "hidden");
18301850
search.innerHTML = '<h3 style="text-align: center;">Loading search results...</h3>';
18311851
}
1852+
1853+
var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
1854+
if (sidebar_menu) {
1855+
sidebar_menu.onclick = function() {
1856+
var sidebar = document.getElementsByClassName('sidebar')[0];
1857+
if (sidebar.style.position === "fixed") {
1858+
hideSidebar();
1859+
} else {
1860+
showSidebar();
1861+
}
1862+
};
1863+
}
1864+
1865+
window.onresize = function() {
1866+
hideSidebar();
1867+
};
18321868
}());
18331869

18341870
// Sets the focus on the search bar at the top of the page

src/librustdoc/html/static/rustdoc.css

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ nav.sub {
263263
width: 100%;
264264
}
265265

266+
.sidebar-menu {
267+
display: none;
268+
}
269+
266270
.content {
267271
padding: 15px 0;
268272
}
@@ -823,7 +827,7 @@ span.since {
823827
position: static;
824828
}
825829

826-
.sidebar .location {
830+
.sidebar > .location {
827831
float: right;
828832
margin: 0px;
829833
margin-top: 2px;
@@ -843,16 +847,33 @@ span.since {
843847
margin-top: 5px;
844848
margin-bottom: 5px;
845849
float: left;
850+
margin-left: 50px;
846851
}
847852

848-
nav.sub {
849-
margin: 0 auto;
853+
.sidebar-menu {
854+
position: absolute;
855+
font-size: 2rem;
856+
cursor: pointer;
857+
margin-top: 2px;
858+
display: block;
850859
}
851860

852-
.sidebar .block {
861+
.sidebar-elems {
862+
background-color: #F1F1F1;
863+
position: fixed;
864+
z-index: 1;
865+
left: 0;
866+
top: 45px;
867+
bottom: 0;
868+
overflow-y: auto;
869+
border-right: 1px solid #000;
853870
display: none;
854871
}
855872

873+
nav.sub {
874+
margin: 0 auto;
875+
}
876+
856877
.content {
857878
margin-left: 0px;
858879
}

0 commit comments

Comments
 (0)