Skip to content

Commit 8de9c39

Browse files
committed
Refactor: use css variables for ring size and color values
removed more canvas related code removed an em
1 parent 5ccf05c commit 8de9c39

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

lib/reporters/html.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports = module.exports = HTML;
3737

3838
var statsTemplate =
3939
'<ul id="mocha-stats">' +
40-
'<li class="progress-contain"><progress class="progress-element" max="100" value="0"></progress><svg class="progress-ring"><circle class="ring-whole"/><circle class="ring-highlight" stroke-dasharray="0%,100%"/></svg><div class="progress-text"><em>0</em>%</div></li>' +
40+
'<li class="progress-contain"><progress class="progress-element" max="100" value="0"></progress><svg class="progress-ring"><circle class="ring-whole"/><circle class="ring-highlight" stroke-dasharray="0%,100%"/></svg><div class="progress-text">0%</div></li>' +
4141
'<li class="passes"><a href="javascript:void(0);">passes:</a> <em>0</em></li>' +
4242
'<li class="failures"><a href="javascript:void(0);">failures:</a> <em>0</em></li>' +
4343
'<li class="duration">duration: <em>0</em>s</li>' +
@@ -69,7 +69,7 @@ function HTML(runner, options) {
6969
var duration = items[3].getElementsByTagName('em')[0];
7070
var report = fragment('<ul id="mocha-report"></ul>');
7171
var stack = [report];
72-
var progressText = items[0].getElementsByTagName('em')[0];
72+
var progressText = items[0].getElementsByTagName('div')[0];
7373
var progressBar = items[0].getElementsByTagName('progress')[0];
7474
var progressRing = items[0].getElementsByClassName('ring-highlight')[0];
7575
var root = document.getElementById('mocha');
@@ -226,7 +226,7 @@ function HTML(runner, options) {
226226
var decmalPlaces = Math.ceil(Math.log10(runner.total / 100));
227227
text(
228228
progressText,
229-
percent.toFixed(Math.min(Math.max(decmalPlaces, 0), 100))
229+
percent.toFixed(Math.min(Math.max(decmalPlaces, 0), 100)) + '%'
230230
);
231231
}
232232
if (progressRing) {

mocha.css

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
--mocha-stats-color: #888;
2323
--mocha-stats-em-color: #000;
2424
--mocha-stats-hover-color: #eee;
25+
--mocha-progress-ring-color: #eee;
26+
--mocha-progress-ring-highlight-color: #9f9f9f;
27+
--mocha-progress-text-color: #000;
2528
--mocha-error-color: #c00;
2629

2730
--mocha-code-comment: #ddd;
@@ -54,6 +57,9 @@
5457
--mocha-stats-color: #aaa;
5558
--mocha-stats-em-color: #fff;
5659
--mocha-stats-hover-color: #444;
60+
--mocha-progress-ring-color: #444;
61+
--mocha-progress-ring-highlight-color: #888;
62+
--mocha-progress-text-color: #fff;
5763
--mocha-error-color: #f44;
5864

5965
--mocha-code-comment: #ddd;
@@ -325,6 +331,9 @@ body {
325331
}
326332

327333
#mocha-stats {
334+
--ring-size: 40px;
335+
--ring-radius: calc(var(--ring-size) / 2);
336+
328337
position: fixed;
329338
top: 15px;
330339
right: 10px;
@@ -341,8 +350,8 @@ body {
341350

342351
#mocha-stats .progress-element {
343352
visibility: hidden;
344-
width: 40px;
345-
height: 20px;
353+
width: var(--ring-size);
354+
height: calc(var(--ring-size) / 2);
346355
position: absolute;
347356
top: 11px; /* padding */
348357
display: block;
@@ -351,30 +360,36 @@ body {
351360
#mocha-stats .progress-text {
352361
text-align: center;
353362
position: absolute;
354-
width: 40px;
363+
width: var(--ring-size);
355364
display: block;
356365
top: 11px; /* padding */
366+
text-overflow: clip;
367+
overflow: hidden;
368+
color: var(--mocha-stats-em-color);
357369
}
358370

359371
#mocha-stats .progress-ring {
360-
width: 40px;
361-
height: 40px;
372+
width: var(--ring-size);
373+
height: var(--ring-size);
362374
}
363375

364376
#mocha-stats .ring-whole, #mocha-stats .ring-highlight {
365-
cx: 20px; /* half of width */
366-
cy: 20px; /* half of height */
367-
r: 19px; /* radius - stroke */
368-
fill: var(--mocha-bg-color);
369-
stroke-width: 2px;
377+
--stroke-thickness: 1px;
378+
cx: var(--ring-radius);
379+
cy: var(--ring-radius);
380+
r: calc(var(--ring-radius) - var(--stroke-thickness)); /* radius - stroke */
381+
fill: hsla(0, 0%, 0%, 0);
382+
stroke-width: calc(var(--stroke-thickness) * 2);
370383
}
371384

372385
#mocha-stats .ring-whole {
373-
stroke: var(--mocha-stats-color);
386+
stroke: var(--mocha-progress-ring-color);
387+
stroke-width: calc(var(--stroke-thickness) * 1.8);
388+
/* slightly smaller to fix strange edge issue */
374389
}
375390

376391
#mocha-stats .ring-highlight {
377-
stroke: var(--mocha-stats-em-color);
392+
stroke: var(--mocha-progress-ring-highlight-color);
378393
}
379394

380395
#mocha-stats em {
@@ -397,11 +412,6 @@ body {
397412
padding-top: 11px;
398413
}
399414

400-
#mocha-stats canvas {
401-
width: 40px;
402-
height: 40px;
403-
}
404-
405415
#mocha code .comment { color: var(--mocha-code-comment); }
406416
#mocha code .init { color: var(--mocha-code-init); }
407417
#mocha code .string { color: var(--mocha-code-string); }

0 commit comments

Comments
 (0)