Skip to content

Commit e608a84

Browse files
authored
Improve method source toggling (#1176)
* Move method source block to the top Currently, if a method description is long (e.g. `Array.new`), users need to click the method toggle button next to the method title, and then scroll down to the source code expanded below the description. This commit changes the behavior so that the source code is expanded immediately below the method title. * Update method toggle's interface 1. Display the method toggle button by default instead of displaying on hover 2. Only toggle the source code when clicking the method toggle button, not when clicking the entire method title section. This will allow us to display an anchor link next to the method title 3. Simplify the toggle source button's appearance * Use details tag for method toggling * Rename method-click-advice to method-source-toggle * Improve method controls' display on mobile By moving the method controls out of the method header, we can display them to the right of the method name on desktop, and below the method name on mobile. * Add "Example" label to example code blocks The label should help users distinguish example code blocks from other code blocks, such as method source code. It's only applied to Ruby code examples. * Revert "Add "Example" label to example code blocks" This reverts commit 69fc9ce. * Give source code blocks a different background color * Change targeted method's highlighting color to work better with the new method source
1 parent ac5ddbd commit e608a84

File tree

3 files changed

+49
-46
lines changed

3 files changed

+49
-46
lines changed

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

+28-27
Original file line numberDiff line numberDiff line change
@@ -101,35 +101,42 @@
101101
<div id="<%= method.aref %>" class="method-detail <%= method.is_alias_for ? "method-alias" : '' %>">
102102
<div class="method-header">
103103
<%- if (call_seq = method.call_seq) then -%>
104-
<%- call_seq.strip.split("\n").each_with_index do |call_seq, i| -%>
105-
<div class="method-heading">
106-
<span class="method-callseq">
107-
<%= h(call_seq.strip.
108-
gsub( /^\w+\./m, '')).
109-
gsub(/(.*)[-=]&gt;/, '\1&rarr;') %>
110-
</span>
111-
<%- if i == 0 and method.token_stream then -%>
112-
<span class="method-click-advice">click to toggle source</span>
104+
<%- call_seq.strip.split("\n").each_with_index do |call_seq, i| -%>
105+
<div class="method-heading">
106+
<span class="method-callseq">
107+
<%= h(call_seq.strip.
108+
gsub( /^\w+\./m, '')).
109+
gsub(/(.*)[-=]&gt;/, '\1&rarr;') %>
110+
</span>
111+
</div>
113112
<%- end -%>
114-
</div>
115-
<%- end -%>
116113
<%- elsif method.has_call_seq? then -%>
117-
<div class="method-heading">
118-
<span class="method-name"><%= h method.name %></span>
119-
</div>
114+
<div class="method-heading">
115+
<span class="method-name"><%= h method.name %></span>
116+
</div>
120117
<%- else -%>
121-
<div class="method-heading">
122-
<span class="method-name"><%= h method.name %></span><span
123-
class="method-args"><%= h method.param_seq %></span>
124-
<%- if method.token_stream then -%>
125-
<span class="method-click-advice">click to toggle source</span>
126-
<%- end -%>
127-
</div>
118+
<div class="method-heading">
119+
<span class="method-name"><%= h method.name %></span>
120+
<span class="method-args"><%= h method.param_seq %></span>
121+
</div>
128122
<%- end -%>
129123
</div>
130124

125+
<%- if method.token_stream -%>
126+
<div class="method-controls">
127+
<details class="method-source-toggle">
128+
<summary>Source</summary>
129+
</details>
130+
</div>
131+
<%- end -%>
132+
131133
<%- unless method.skip_description? then -%>
132134
<div class="method-description">
135+
<%- if method.token_stream then -%>
136+
<div class="method-source-code" id="<%= method.html_name %>-source">
137+
<pre><%= method.markup_code %></pre>
138+
</div>
139+
<%- end -%>
133140
<%- if method.comment then -%>
134141
<%= method.description.strip %>
135142
<%- else -%>
@@ -144,12 +151,6 @@
144151
%>
145152
</div>
146153
<%- end -%>
147-
148-
<%- if method.token_stream then -%>
149-
<div class="method-source-code" id="<%= method.html_name %>-source">
150-
<pre><%= method.markup_code %></pre>
151-
</div>
152-
<%- end -%>
153154
</div>
154155
<%- end -%>
155156

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

+20-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
--link-hover-color: #25a28a; /* A slightly brighter teal-green */
2323
--border-color: #e0e0e0;
2424
--sidebar-text-color: #2c3e50; /* Dark blue-gray for contrast */
25+
--source-code-background-color: #e8f0eb;
2526

2627
/* Font family variables */
2728
--font-primary: 'Lato', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
@@ -595,6 +596,10 @@ main .method-source-code {
595596
transition-timing-function: ease-in-out;
596597
}
597598

599+
main .method-source-code pre {
600+
background: var(--source-code-background-color);
601+
}
602+
598603
main .method-source-code.active-menu {
599604
visibility: visible;
600605
max-height: 100vh;
@@ -607,12 +612,15 @@ main .method-description .method-calls-super {
607612

608613
main .method-detail {
609614
margin-bottom: 2.5em;
610-
cursor: pointer;
611615
}
612616

613617
main .method-detail:target {
614618
margin-left: -10px;
615-
border-left: 10px solid #f1edba;
619+
border-left: 10px solid var(--source-code-background-color);
620+
}
621+
622+
main .method-header {
623+
display: inline-block;
616624
}
617625

618626
main .method-heading {
@@ -622,23 +630,12 @@ main .method-heading {
622630
font-weight: bold;
623631
color: var(--text-color);
624632
}
625-
main .method-heading :link,
626-
main .method-heading :visited {
627-
color: inherit;
628-
}
629-
main .method-click-advice {
630-
position: absolute;
631-
top: 2px;
632-
right: 5px;
633-
font-size: 12px;
634-
color: #9b9877;
635-
visibility: hidden;
636-
padding-right: 20px;
633+
634+
main .method-controls {
637635
line-height: 20px;
638-
background: url(../images/zoom.png) no-repeat right top;
639-
}
640-
main .method-header:hover .method-click-advice {
641-
visibility: visible;
636+
float: right;
637+
color: var(--secondary-color);
638+
cursor: pointer;
642639
}
643640

644641
main .method-alias .method-heading {
@@ -743,6 +740,11 @@ main .attribute-access-type {
743740
overflow-x: auto;
744741
white-space: nowrap;
745742
}
743+
744+
main .method-controls {
745+
margin-top: 10px;
746+
float: none;
747+
}
746748
}
747749
/* @end */
748750

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function showSource( e ) {
3434
};
3535

3636
function hookSourceViews() {
37-
document.querySelectorAll('.method-heading').forEach(function (codeObject) {
37+
document.querySelectorAll('.method-source-toggle').forEach(function (codeObject) {
3838
codeObject.addEventListener('click', showSource);
3939
});
4040
};

0 commit comments

Comments
 (0)