Skip to content

Commit d88b2a0

Browse files
committed
Add support for mobile screen sizes on Darkfish
This adds support for reading the Darkfish-generated docs in mobile devices. I tried to keep the changes minimal, and the current layout was mostly preserved. The most notable change is the navigation sidebar, which is now hidden by default on "small screens" (everything below 1024px). It can be toggled by the button on the top left corner. This button implements the ARIA pattern for a [disclosure widget]. The icon for the button was taken from [Iconoir], which is licensed under the MIT license. The design and some of the implementation were loosely inspired by the [Elixir lang docs]. [disclosure widget]: https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/ [Iconoir]: https://iconoir.com/ [Elixir docs]: https://hexdocs.pm/elixir/1.15.2/Kernel.html
1 parent e8f22ce commit d88b2a0

File tree

10 files changed

+131
-20
lines changed

10 files changed

+131
-20
lines changed

lib/rdoc/generator/template/darkfish/_head.rhtml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<meta charset="<%= @options.charset %>">
2+
<meta name="viewport" content="width=device-width, initial-scale=1" />
23

34
<title><%= h @title %></title>
45

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<button id="navigation-toggle" class="navigation-toggle" aria-label="Toggle sidebar" aria-expanded="true" aria-controls="navigation">
2+
<!--
3+
Copyright (c) 2021 Luca Burgio
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
-->
23+
<svg width="20px" height="24px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="#000000">
24+
<path d="M3 5h18M3 12h18M3 19h18" stroke="#000000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
25+
</svg>
26+
</button>

lib/rdoc/generator/template/darkfish/class.rhtml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<body id="top" role="document" class="<%= klass.type %>">
2-
<nav role="navigation">
2+
<%= render '_sidebar_toggle.rhtml' %>
3+
4+
<nav id="navigation" role="navigation">
35
<div id="project-navigation">
46
<%= render '_sidebar_navigation.rhtml' %>
57
<%= render '_sidebar_search.rhtml' %>

lib/rdoc/generator/template/darkfish/css/rdoc.css

+63-13
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ body {
1919
font-weight: 300;
2020

2121
/* Layout */
22-
display: grid;
23-
grid-template-columns: auto 1fr;
22+
display: flex;
23+
flex-wrap: wrap;
2424
}
2525

2626
body > :last-child {
27-
grid-column: 1 / 3;
27+
width: 100%;
2828
}
2929

3030
h1 span,
@@ -192,24 +192,40 @@ nav {
192192
font-family: Helvetica, sans-serif;
193193
font-size: 14px;
194194
border-right: 1px solid #ccc;
195-
position: sticky;
195+
position: fixed;
196196
top: 0;
197197
overflow: auto;
198+
z-index: 10;
198199

199200
/* Layout */
200-
width: 260px; /* fallback */
201-
width: max(50px, 20vw);
202-
min-width: 50px;
203-
max-width: 80vw;
204-
height: calc(100vh - 100px); /* reduce the footer height */
201+
width: 300px;
202+
min-height: 100vh;
203+
205204
resize: horizontal;
205+
background: white;
206+
}
207+
208+
@media (min-width: 1024px) {
209+
nav {
210+
min-height: unset;
211+
height: calc(100vh - 100px); /* reduce the footer height */
212+
}
206213
}
207214

208215
main {
209216
display: block;
210-
margin: 1em;
217+
margin: 3em 1em 1em;
211218
min-width: 340px;
212219
font-size: 16px;
220+
width: 100%;
221+
max-width: 64em;
222+
}
223+
224+
@media (min-width: 1024px) {
225+
main {
226+
margin-left: auto;
227+
margin-right: auto;
228+
}
213229
}
214230

215231
main h1,
@@ -221,8 +237,10 @@ main h6 {
221237
font-family: Helvetica, sans-serif;
222238
}
223239

224-
.table-of-contents main {
225-
margin-left: 2em;
240+
@media (min-width: 1024px) {
241+
.table-of-contents main {
242+
margin-left: 20em;
243+
}
226244
}
227245

228246
#validator-badges {
@@ -269,6 +287,39 @@ nav p {
269287
list-style: none;
270288
}
271289

290+
.navigation-toggle {
291+
position: fixed;
292+
left: 4px;
293+
top: 6px;
294+
z-index: 100;
295+
296+
background: transparent;
297+
border: none;
298+
cursor: pointer;
299+
margin-left: 7px;
300+
padding: 4px;
301+
}
302+
.navigation-toggle[aria-expanded="true"] {
303+
left: 260px;
304+
}
305+
306+
/* Adds a suble gradient to help the toggle stand out from the background */
307+
.navigation-toggle::before {
308+
content: "";
309+
background: linear-gradient(180deg, rgba(250,250,250,1) 40%, rgba(250,250,250,0.8) 70%, rgba(250,250,250,0) 100%);
310+
display: block;
311+
z-index: -1;
312+
pointer-events: none;
313+
position: fixed;
314+
top: 0;
315+
height: 50px;
316+
width: 100vw;
317+
}
318+
319+
.navigation-toggle[aria-expanded="true"]::before {
320+
height: 0;
321+
}
322+
272323
#project-navigation .nav-section {
273324
margin: 0;
274325
border-top: 0;
@@ -673,4 +724,3 @@ pre {
673724
}
674725

675726
/* @end */
676-

lib/rdoc/generator/template/darkfish/index.rhtml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<body id="top" role="document" class="file">
2-
<nav role="navigation">
2+
<%= render '_sidebar_toggle.rhtml' %>
3+
4+
<nav id="navigation" role="navigation">
35
<div id="project-navigation">
46
<%= render '_sidebar_navigation.rhtml' %>
57

lib/rdoc/generator/template/darkfish/js/darkfish.js

+17
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,25 @@ function hookFocus() {
9090
});
9191
}
9292

93+
function hookSidebar() {
94+
var navigation = document.querySelector('#navigation');
95+
var navigationToggle = document.querySelector('#navigation-toggle');
96+
97+
navigationToggle.addEventListener('click', function() {
98+
navigation.hidden = !navigation.hidden;
99+
navigationToggle.ariaExpanded = navigationToggle.ariaExpanded !== 'true';
100+
});
101+
102+
var isSmallViewport = window.matchMedia("(max-width: 1024px)").matches;
103+
if (isSmallViewport) {
104+
navigation.hidden = true;
105+
navigationToggle.ariaExpanded = false;
106+
}
107+
}
108+
93109
document.addEventListener('DOMContentLoaded', function() {
94110
hookSourceViews();
95111
hookSearch();
96112
hookFocus();
113+
hookSidebar();
97114
});

lib/rdoc/generator/template/darkfish/page.rhtml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<body id="top" role="document" class="file">
2-
<nav role="navigation">
2+
<%= render '_sidebar_toggle.rhtml' %>
3+
4+
<nav id="navigation" role="navigation">
35
<div id="project-navigation">
46
<%= render '_sidebar_navigation.rhtml' %>
57
<%= render '_sidebar_search.rhtml' %>
@@ -15,4 +17,3 @@
1517
<main role="main" aria-label="Page <%=h file.full_name%>">
1618
<%= file.description %>
1719
</main>
18-

lib/rdoc/generator/template/darkfish/servlet_not_found.rhtml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<body role="document">
2-
<nav role="navigation">
2+
<%= render '_sidebar_toggle.rhtml' %>
3+
4+
<nav id="navigation" role="navigation">
35
<%= render '_sidebar_navigation.rhtml' %>
46

57
<%= render '_sidebar_search.rhtml' %>
@@ -15,4 +17,3 @@
1517

1618
<p><%= message %>
1719
</main>
18-

lib/rdoc/generator/template/darkfish/servlet_root.rhtml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<body role="document">
2-
<nav role="navigation">
2+
<%= render '_sidebar_toggle.rhtml' %>
3+
4+
<nav id="navigation" role="navigation">
35
<div id="project-navigation">
46
<div id="home-section" class="nav-section">
57
<h2>

lib/rdoc/generator/template/darkfish/table_of_contents.rhtml

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
<body id="top" class="table-of-contents">
2+
<%= render '_sidebar_toggle.rhtml' %>
3+
4+
<nav id="navigation" role="navigation">
5+
<div id="project-navigation">
6+
<%= render '_sidebar_navigation.rhtml' %>
7+
8+
<%= render '_sidebar_search.rhtml' %>
9+
</div>
10+
</nav>
211
<main role="main">
312
<h1 class="class"><%= h @title %></h1>
413

0 commit comments

Comments
 (0)