Skip to content

Commit 0a6b239

Browse files
authored
Add keyboard navigation for ruby-bench benchmarks (#12)
1 parent 1bff280 commit 0a6b239

3 files changed

Lines changed: 74 additions & 67 deletions

File tree

‎source/benchmarks/ruby-bench.html.haml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@
159159
// Setup navigation
160160
var navigation = RubyBench.setupBenchmarkNavigation({
161161
activateChart: activateChart,
162-
defaultBenchmark: $('.benchmark_graph##{category_benchmarks.first.first}')
162+
defaultBenchmark: $('.benchmark_graph##{category_benchmarks.first.first}'),
163+
enableKeyboardNavigation: true
163164
});
164165

165166
// Setup metric type handlers

‎source/javascripts/site.js‎

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,14 @@ var RubyBench = (function() {
7373
}
7474

7575
// Plot a Highcharts chart
76-
function plotChart(graphElement, config, retryCount) {
77-
retryCount = retryCount || 0;
76+
function plotChart(graphElement, config) {
7877
initHighchartsDefaults();
7978

80-
// Ensure the element is visible and has dimensions
79+
// Ensure the element is visible before Highcharts measures it
8180
if (!graphElement.is(':visible')) {
8281
graphElement.show();
8382
}
8483

85-
// Check if element has actual dimensions
86-
var width = graphElement.width();
87-
var height = graphElement.height();
88-
89-
if (width === 0 || height === 0) {
90-
console.error('Chart container has no dimensions (width: ' + width + ', height: ' + height + ')');
91-
// Try to use setTimeout to allow DOM to update, but only retry once
92-
if (retryCount < 1) {
93-
setTimeout(function() {
94-
plotChart(graphElement, config, retryCount + 1); // Retry once
95-
}, 50);
96-
} else {
97-
graphElement.html('<div style="padding: 100px; text-align: center; color: red;">Chart container has no dimensions</div>');
98-
}
99-
return null;
100-
}
101-
10284
// Use the DOM element directly instead of ID string
10385
// graphElement is a jQuery object, so get the first DOM element
10486
var domElement = graphElement.get(0);
@@ -121,18 +103,47 @@ var RubyBench = (function() {
121103
function setupBenchmarkNavigation(options) {
122104
var activateChart = options.activateChart;
123105
var defaultBenchmark = options.defaultBenchmark;
106+
var enableKeyboardNavigation = options.enableKeyboardNavigation;
124107

125-
// Switch active tab and render it
126-
$('.activate-chart').on('click', function(event) {
127-
event.preventDefault();
108+
function activateBenchmarkLink(link) {
128109
$('.benchmark_navbar .nav-link').removeClass('active');
129-
$(this).addClass('active');
130-
var id = $(this).data('id');
110+
link.addClass('active');
111+
var id = link.data('id');
131112
var graphElement = $("#" + id);
132113
activateChart(graphElement);
133114
window.location.hash = id;
115+
}
116+
117+
// Switch active tab and render it
118+
$('.activate-chart').on('click', function(event) {
119+
event.preventDefault();
120+
activateBenchmarkLink($(this));
134121
});
135122

123+
if (enableKeyboardNavigation) {
124+
$(document).on('keydown', function(event) {
125+
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') {
126+
return;
127+
}
128+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
129+
return;
130+
}
131+
if ($(event.target).closest('input, textarea, select, [contenteditable="true"]').length > 0) {
132+
return;
133+
}
134+
135+
var links = $('.benchmark_navbar .nav-link');
136+
var activeIndex = links.index(links.filter('.active'));
137+
var nextIndex = activeIndex + (event.key === 'ArrowLeft' ? -1 : 1);
138+
if (activeIndex < 0 || nextIndex < 0 || nextIndex >= links.length) {
139+
return;
140+
}
141+
142+
event.preventDefault();
143+
activateBenchmarkLink(links.eq(nextIndex));
144+
});
145+
}
146+
136147
// Handle URL hash navigation
137148
function handleHashNavigation() {
138149
var url = document.location.toString();
@@ -207,4 +218,4 @@ var RubyBench = (function() {
207218
showError: showError,
208219
showOnlyGraph: showOnlyGraph
209220
};
210-
})();
221+
})();
Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,29 @@ body {
77
}
88

99
a {
10-
color: rgba(#000, 0.7);
10+
color: rgba(0, 0, 0, 0.7);
11+
}
1112

12-
&:focus,
13-
&:hover {
14-
color: rgba(#000, 0.6);
15-
}
13+
a:focus,
14+
a:hover {
15+
color: rgba(0, 0, 0, 0.6);
1616
}
1717

1818
.footer {
1919
margin-top: 40px;
2020
}
2121

22-
.index_page {
23-
.large_title {
24-
font-size: 48px;
25-
margin-bottom: 24px;
26-
}
22+
.index_page .large_title {
23+
font-size: 48px;
24+
margin-bottom: 24px;
25+
}
2726

28-
.description_box {
29-
margin-bottom: 40px;
30-
}
27+
.index_page .description_box {
28+
margin-bottom: 40px;
29+
}
3130

32-
.large_explanation {
33-
font-size: 24px;
34-
}
31+
.index_page .large_explanation {
32+
font-size: 24px;
3533
}
3634

3735
.benchmark_page {
@@ -50,10 +48,10 @@ a {
5048
margin-bottom: 10px;
5149
display: inline-flex;
5250
flex-wrap: wrap;
51+
}
5352

54-
.nav-link {
55-
padding: 10px 13px;
56-
}
53+
.benchmark_page .nav.benchmark_navbar .nav-link {
54+
padding: 10px 13px;
5755
}
5856

5957
.metric_type {
@@ -67,31 +65,28 @@ a {
6765
.benchmarks-table {
6866
width: 70%;
6967
margin: 0 auto 60px auto;
70-
a {
71-
text-decoration: underline;
72-
}
7368
}
7469

75-
.margined-links {
76-
a,
77-
span {
78-
margin-right: 10px;
79-
}
70+
.benchmarks-table a {
71+
text-decoration: underline;
8072
}
8173

82-
.filter-section {
83-
.form-check-inline {
84-
cursor: pointer;
85-
padding: 2px 12px;
86-
border-radius: 3px;
87-
transition: background-color 0.2s ease;
74+
.margined-links a,
75+
.margined-links span {
76+
margin-right: 10px;
77+
}
78+
79+
.filter-section .form-check-inline {
80+
cursor: pointer;
81+
padding: 2px 12px;
82+
border-radius: 3px;
83+
transition: background-color 0.2s ease;
84+
}
8885

89-
&:hover {
90-
background-color: #f5f5f5;
91-
}
92-
}
86+
.filter-section .form-check-inline:hover {
87+
background-color: #f5f5f5;
88+
}
9389

94-
input[type="checkbox"] {
95-
cursor: pointer;
96-
}
90+
.filter-section input[type="checkbox"] {
91+
cursor: pointer;
9792
}

0 commit comments

Comments
 (0)