Skip to content

Commit

Permalink
Add support for mobile screen sizes on Darkfish (#1025)
Browse files Browse the repository at this point in the history
* 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

* Make sidebar fixed-width

* Use U+2630 as sidebar toggle icon
  • Loading branch information
MatheusRich authored Aug 3, 2024
1 parent ffe5583 commit 755a2cf
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/rdoc/generator/template/darkfish/_head.rhtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<meta charset="<%= @options.charset %>">
<meta name="viewport" content="width=device-width, initial-scale=1" />

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

Expand Down
3 changes: 3 additions & 0 deletions lib/rdoc/generator/template/darkfish/_sidebar_toggle.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<button id="navigation-toggle" class="navigation-toggle" aria-label="Toggle sidebar" aria-expanded="true" aria-controls="navigation">
<span aria-hidden></span>
</button>
4 changes: 3 additions & 1 deletion lib/rdoc/generator/template/darkfish/class.rhtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<body id="top" role="document" class="<%= klass.type %>">
<nav role="navigation">
<%= render '_sidebar_toggle.rhtml' %>

<nav id="navigation" role="navigation">
<div id="project-navigation">
<%= render '_sidebar_navigation.rhtml' %>
<%= render '_sidebar_search.rhtml' %>
Expand Down
75 changes: 61 additions & 14 deletions lib/rdoc/generator/template/darkfish/css/rdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ body {
font-weight: 300;

/* Layout */
display: grid;
grid-template-columns: auto 1fr;
display: flex;
flex-wrap: wrap;
}

body > :last-child {
grid-column: 1 / 3;
width: 100%;
}

h1 span,
Expand Down Expand Up @@ -203,24 +203,38 @@ nav {
font-family: Helvetica, sans-serif;
font-size: 14px;
border-right: 1px solid #ccc;
position: sticky;
position: fixed;
top: 0;
overflow: auto;
z-index: 10;

/* Layout */
width: 260px; /* fallback */
width: max(50px, 20vw);
min-width: 50px;
max-width: 80vw;
height: calc(100vh - 100px); /* reduce the footer height */
resize: horizontal;
width: 300px;
min-height: 100vh;
background: white;
}

@media (min-width: 1024px) {
nav {
min-height: unset;
height: calc(100vh - 100px); /* reduce the footer height */
}
}

main {
display: block;
margin: 1em;
margin: 3em 1em 1em;
min-width: 340px;
font-size: 16px;
width: 100%;
max-width: 64em;
}

@media (min-width: 1024px) {
main {
margin-left: auto;
margin-right: auto;
}
}

main h1,
Expand All @@ -232,8 +246,10 @@ main h6 {
font-family: Helvetica, sans-serif;
}

.table-of-contents main {
margin-left: 2em;
@media (min-width: 1024px) {
.table-of-contents main {
margin-left: 20em;
}
}

#validator-badges {
Expand Down Expand Up @@ -280,6 +296,38 @@ nav p {
list-style: none;
}

.navigation-toggle {
position: fixed;
left: 4px;
z-index: 100;

background: transparent;
border: none;
cursor: pointer;
padding: 4px;
font-size: 24px;
}
.navigation-toggle[aria-expanded="true"] {
left: 260px;
}

/* Adds a suble gradient to help the toggle stand out from the background */
.navigation-toggle::before {
content: "";
background: linear-gradient(180deg, rgba(250,250,250,1) 40%, rgba(250,250,250,0.8) 70%, rgba(250,250,250,0) 100%);
display: block;
z-index: -1;
pointer-events: none;
position: fixed;
top: 0;
height: 50px;
width: 100vw;
}

.navigation-toggle[aria-expanded="true"]::before {
height: 0;
}

#project-navigation .nav-section {
margin: 0;
border-top: 0;
Expand Down Expand Up @@ -684,4 +732,3 @@ pre {
}

/* @end */

4 changes: 3 additions & 1 deletion lib/rdoc/generator/template/darkfish/index.rhtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<body id="top" role="document" class="file">
<nav role="navigation">
<%= render '_sidebar_toggle.rhtml' %>

<nav id="navigation" role="navigation">
<div id="project-navigation">
<%= render '_sidebar_navigation.rhtml' %>

Expand Down
17 changes: 17 additions & 0 deletions lib/rdoc/generator/template/darkfish/js/darkfish.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,25 @@ function hookFocus() {
});
}

function hookSidebar() {
var navigation = document.querySelector('#navigation');
var navigationToggle = document.querySelector('#navigation-toggle');

navigationToggle.addEventListener('click', function() {
navigation.hidden = !navigation.hidden;
navigationToggle.ariaExpanded = navigationToggle.ariaExpanded !== 'true';
});

var isSmallViewport = window.matchMedia("(max-width: 1024px)").matches;
if (isSmallViewport) {
navigation.hidden = true;
navigationToggle.ariaExpanded = false;
}
}

document.addEventListener('DOMContentLoaded', function() {
hookSourceViews();
hookSearch();
hookFocus();
hookSidebar();
});
5 changes: 3 additions & 2 deletions lib/rdoc/generator/template/darkfish/page.rhtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<body id="top" role="document" class="file">
<nav role="navigation">
<%= render '_sidebar_toggle.rhtml' %>

<nav id="navigation" role="navigation">
<div id="project-navigation">
<%= render '_sidebar_navigation.rhtml' %>
<%= render '_sidebar_search.rhtml' %>
Expand All @@ -15,4 +17,3 @@
<main role="main" aria-label="Page <%=h file.full_name%>">
<%= file.description %>
</main>

5 changes: 3 additions & 2 deletions lib/rdoc/generator/template/darkfish/servlet_not_found.rhtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<body role="document">
<nav role="navigation">
<%= render '_sidebar_toggle.rhtml' %>

<nav id="navigation" role="navigation">
<%= render '_sidebar_navigation.rhtml' %>

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

<p><%= message %>
</main>

4 changes: 3 additions & 1 deletion lib/rdoc/generator/template/darkfish/servlet_root.rhtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<body role="document">
<nav role="navigation">
<%= render '_sidebar_toggle.rhtml' %>

<nav id="navigation" role="navigation">
<div id="project-navigation">
<div id="home-section" class="nav-section">
<h2>
Expand Down
9 changes: 9 additions & 0 deletions lib/rdoc/generator/template/darkfish/table_of_contents.rhtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<body id="top" class="table-of-contents">
<%= render '_sidebar_toggle.rhtml' %>

<nav id="navigation" role="navigation">
<div id="project-navigation">
<%= render '_sidebar_navigation.rhtml' %>

<%= render '_sidebar_search.rhtml' %>
</div>
</nav>
<main role="main">
<h1 class="class"><%= h @title %></h1>

Expand Down

0 comments on commit 755a2cf

Please sign in to comment.