Skip to content

Commit 8deabc7

Browse files
committed
Mirror styles changes to progress ring
renamed ring-whole to ring-flatlight and have ring highlight and flatlight be the inverse of each other fix spelling error with decimalPlaces use getComputedStyle for get the radius of the ring defined in CSS use :is() CSS to simplify CSS code Change stroke thickness math to better match previous look
1 parent 8de9c39 commit 8deabc7

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

lib/reporters/html.js

Lines changed: 13 additions & 6 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">0%</div></li>' +
40+
'<li class="progress-contain"><progress class="progress-element" max="100" value="0"></progress><svg class="progress-ring"><circle class="ring-flatlight" stroke-dasharray="100%,0%"/><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>' +
@@ -71,7 +71,10 @@ function HTML(runner, options) {
7171
var stack = [report];
7272
var progressText = items[0].getElementsByTagName('div')[0];
7373
var progressBar = items[0].getElementsByTagName('progress')[0];
74-
var progressRing = items[0].getElementsByClassName('ring-highlight')[0];
74+
var progressRing = [
75+
items[0].getElementsByClassName('ring-flatlight')[0],
76+
items[0].getElementsByClassName('ring-highlight')[0]];
77+
var progressRingRadius = null; // computed CSS unavailable now, so set later
7578
var root = document.getElementById('mocha');
7679

7780
if (!root) {
@@ -223,17 +226,21 @@ function HTML(runner, options) {
223226
// setting a toFixed that is too low, makes small changes to progress not shown
224227
// setting it too high, makes the progress text longer then it needs to
225228
// to address this, calculate the toFixed based on the magnitude of total
226-
var decmalPlaces = Math.ceil(Math.log10(runner.total / 100));
229+
var decimalPlaces = Math.ceil(Math.log10(runner.total / 100));
227230
text(
228231
progressText,
229-
percent.toFixed(Math.min(Math.max(decmalPlaces, 0), 100)) + '%'
232+
percent.toFixed(Math.min(Math.max(decimalPlaces, 0), 100)) + '%'
230233
);
231234
}
232235
if (progressRing) {
233-
var radius = 19; // to do, figure out how to match with css
236+
var radius = parseFloat(getComputedStyle(progressRing[0]).getPropertyValue('r'));
234237
var wholeArc = Math.PI * 2 * radius;
235238
var highlightArc = percent * (wholeArc / 100);
236-
progressRing.style['stroke-dasharray'] = `${highlightArc}px,${wholeArc}px`;
239+
// The progress ring is in 2 parts, the flatlight color and highlight color.
240+
// Rendering both on top of the other, seems to make a 3rd color on the edges.
241+
// To create 1 whole ring with 2 colors, both parts are inverse of the other.
242+
progressRing[0].style['stroke-dasharray'] = `0,${highlightArc}px,${wholeArc}px`;
243+
progressRing[1].style['stroke-dasharray'] = `${highlightArc}px,${wholeArc}px`;
237244
}
238245

239246
// update stats

mocha.css

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -348,44 +348,42 @@ body {
348348
padding: 0;
349349
}
350350

351+
#mocha-stats :is(.progress-element, .progress-text) {
352+
width: var(--ring-size);
353+
display: block;
354+
top: 12px;
355+
position: absolute;
356+
}
357+
351358
#mocha-stats .progress-element {
352359
visibility: hidden;
353-
width: var(--ring-size);
354360
height: calc(var(--ring-size) / 2);
355-
position: absolute;
356-
top: 11px; /* padding */
357-
display: block;
358361
}
359362

360363
#mocha-stats .progress-text {
361364
text-align: center;
362-
position: absolute;
363-
width: var(--ring-size);
364-
display: block;
365-
top: 11px; /* padding */
366365
text-overflow: clip;
367366
overflow: hidden;
368367
color: var(--mocha-stats-em-color);
368+
font-size: 11px;
369369
}
370370

371371
#mocha-stats .progress-ring {
372372
width: var(--ring-size);
373373
height: var(--ring-size);
374374
}
375375

376-
#mocha-stats .ring-whole, #mocha-stats .ring-highlight {
376+
#mocha-stats :is(.ring-flatlight, .ring-highlight) {
377377
--stroke-thickness: 1px;
378378
cx: var(--ring-radius);
379379
cy: var(--ring-radius);
380-
r: calc(var(--ring-radius) - var(--stroke-thickness)); /* radius - stroke */
380+
r: calc(var(--ring-radius) - calc(var(--stroke-thickness) / 2));
381381
fill: hsla(0, 0%, 0%, 0);
382-
stroke-width: calc(var(--stroke-thickness) * 2);
382+
stroke-width: var(--stroke-thickness);
383383
}
384384

385-
#mocha-stats .ring-whole {
385+
#mocha-stats .ring-flatlight {
386386
stroke: var(--mocha-progress-ring-color);
387-
stroke-width: calc(var(--stroke-thickness) * 1.8);
388-
/* slightly smaller to fix strange edge issue */
389387
}
390388

391389
#mocha-stats .ring-highlight {

0 commit comments

Comments
 (0)