Skip to content

Commit 6338388

Browse files
GuillaumeGomezjyn514
authored andcommitted
Add toggle mechanism for docs.rs source sidebar
1 parent 94f3bba commit 6338388

File tree

5 files changed

+136
-6
lines changed

5 files changed

+136
-6
lines changed

src/web/metrics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ mod tests {
121121
("/-/static/index.js", "static resource"),
122122
("/-/static/menu.js", "static resource"),
123123
("/-/static/keyboard.js", "static resource"),
124+
("/-/static/source.js", "static resource"),
124125
("/-/static/opensearch.xml", "static resource"),
125126
("/releases", "/releases"),
126127
("/releases/feed", "static resource"),

src/web/statics.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,24 @@ mod tests {
228228
});
229229
}
230230

231+
#[test]
232+
fn source_js() {
233+
wrapper(|env| {
234+
let web = env.frontend();
235+
236+
let resp = web.get("/-/static/source.js").send()?;
237+
assert!(resp.status().is_success());
238+
assert_eq!(
239+
resp.headers().get("Content-Type"),
240+
Some(&"application/javascript".parse().unwrap()),
241+
);
242+
assert!(resp.content_length().unwrap() > 10);
243+
assert!(resp.text()?.contains("toggleSource"));
244+
245+
Ok(())
246+
});
247+
}
248+
231249
#[test]
232250
fn static_files() {
233251
wrapper(|env| {

static/source.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
(function() {
2+
var oldLabel;
3+
4+
function showSourceFiles(button, sideMenu, sourceCode) {
5+
button.title = oldLabel;
6+
button.setAttribute("aria-label", button.title);
7+
button.setAttribute("aria-expanded", "true");
8+
9+
sideMenu.classList.remove("collapsed");
10+
sourceCode.classList.remove("expanded");
11+
}
12+
13+
function hideSourceFiles(button, sideMenu, sourceCode) {
14+
button.title = "Show source sidebar";
15+
button.setAttribute("aria-label", button.title);
16+
button.setAttribute("aria-expanded", "false");
17+
18+
sideMenu.classList.add("collapsed");
19+
sourceCode.classList.add("expanded");
20+
}
21+
22+
function toggleSource(button) {
23+
var sideMenu = document.getElementById("side-menu");
24+
var sourceCode = document.getElementById("source-code");
25+
26+
if (sideMenu.classList.contains("collapsed")) {
27+
showSourceFiles(button, sideMenu, sourceCode);
28+
} else {
29+
hideSourceFiles(button, sideMenu, sourceCode);
30+
}
31+
}
32+
33+
document.addEventListener("DOMContentLoaded", function(event) {
34+
var toggleSourceButton = document.querySelector("li.toggle-source button");
35+
oldLabel = toggleSourceButton.getAttribute("aria-label");
36+
37+
toggleSourceButton.addEventListener("click", function() {
38+
toggleSource(toggleSourceButton);
39+
});
40+
});
41+
})();

templates/crate/source.html

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@
2424
{%- block body -%}
2525
<div class="container package-page-container">
2626
<div class="pure-g">
27-
<div class="pure-u-1 {% if file_content %}pure-u-sm-7-24 pure-u-md-5-24{% endif %}">
27+
<div id="side-menu" class="pure-u-1 {% if file_content %}pure-u-sm-7-24 pure-u-md-5-24{% endif %}">
2828
<div class="pure-menu package-menu">
2929
<ul class="pure-menu-list">
30+
{# If we are displaying a file, we also add a button to hide the file sidebar #}
31+
{% if file_content %}
32+
<li class="pure-menu-item toggle-source">
33+
<button aria-label="Hide source sidebar" title="Hide source sidebar" aria-expanded="true"><span class="left">{{ "chevron-left" | fas(fw=true) }}</span><span class="right">{{ "chevron-right" | fas(fw=true) }}</span> <span class="text">Hide files</span></button>
34+
</li>
35+
{% endif %}
3036
{# If this isn't the root folder, show a 'back' button #}
3137
{%- if show_parent_link -%}
3238
<li class="pure-menu-item">
33-
<a href="../" class="pure-menu-link">{{ "folder-open" | far(fw=true) }} ..</a>
39+
<a href="../" class="pure-menu-link">{{ "folder-open" | far(fw=true) }} <span class="text">..</span></a>
3440
</li>
3541
{%- endif -%}
3642

@@ -88,7 +94,7 @@
8894
{{ "file-archive" | far(fw=true) }}
8995
{%- endif -%}
9096

91-
{{ file.name }}
97+
<span class="text">{{ file.name }}</span>
9298
</a>
9399
</li>
94100
{%- endfor -%}
@@ -98,7 +104,7 @@
98104

99105
{# If the file has content, then display it in a codeblock #}
100106
{%- if file_content -%}
101-
<div class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24">
107+
<div id="source-code" class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24">
102108
<pre><code>{{ file_content }}</code></pre>
103109
</div>
104110
{%- endif -%}
@@ -114,4 +120,7 @@
114120
{%- block javascript -%}
115121
{# Highlight.js JavaScript #}
116122
{{ macros::highlight_js(languages=["rust", "ini", "markdown"]) }}
123+
{% if file_content %}
124+
<script nonce="{{ csp_nonce }}" type="text/javascript" src="/-/static/source.js?{{ docsrs_version() | slugify }}"></script>
125+
{% endif %}
117126
{%- endblock javascript -%}

templates/style/base.scss

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ div.package-page-container {
459459
margin-top: 0;
460460
}
461461

462-
a.pure-menu-link {
462+
.pure-menu-link {
463463
font-size: 14px;
464464
color: var(--color-standard);
465465
white-space: nowrap;
@@ -468,7 +468,7 @@ div.package-page-container {
468468
padding: 7px 8px;
469469
}
470470

471-
a.pure-menu-link:hover {
471+
.pure-menu-link:hover {
472472
background-color: var(--color-background-code);
473473
}
474474

@@ -780,3 +780,64 @@ h3 > code,
780780
h4 > code {
781781
display: inline-block;
782782
}
783+
784+
ul.pure-menu-list {
785+
li.toggle-source {
786+
cursor: pointer;
787+
border: 1px solid var(--color-border);
788+
display: none;
789+
}
790+
}
791+
792+
@media screen and (min-width: 35.5em) {
793+
ul.pure-menu-list {
794+
li.toggle-source {
795+
display: list-item;
796+
797+
button {
798+
font-size: 14px;
799+
color: var(--color-standard);
800+
white-space: nowrap;
801+
overflow: hidden;
802+
text-overflow: ellipsis;
803+
padding: 7px 8px;
804+
background: transparent;
805+
border: 0;
806+
width: 100%;
807+
}
808+
809+
.right {
810+
display: none;
811+
}
812+
}
813+
}
814+
815+
#side-menu.collapsed {
816+
max-width: 46px;
817+
818+
ul {
819+
li:not(.toggle-source), .text {
820+
display: none;
821+
}
822+
li.toggle-source {
823+
.left {
824+
display: none;
825+
}
826+
.right {
827+
display: inline-block;
828+
margin-left: -4px;
829+
}
830+
}
831+
}
832+
}
833+
834+
#source-code {
835+
pre {
836+
margin-top: 0;
837+
}
838+
839+
&.expanded {
840+
width: calc(100% - 46px);
841+
}
842+
}
843+
}

0 commit comments

Comments
 (0)