@n1kname/components 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -2
- package/src/Facts.astro +189 -42
- package/src/Stat.astro +3 -0
- package/src/StatStrip.astro +5 -1
- package/src/facts.js +139 -15
- package/src/facts.test.mjs +143 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -45,7 +45,10 @@
|
|
|
45
45
|
"src",
|
|
46
46
|
"THIRD_PARTY.md"
|
|
47
47
|
],
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"linkedom": "0.18.12"
|
|
50
|
+
},
|
|
48
51
|
"scripts": {
|
|
49
|
-
"check": "node src/yearcalendar.test.mjs"
|
|
52
|
+
"check": "node src/yearcalendar.test.mjs && node src/facts.test.mjs"
|
|
50
53
|
}
|
|
51
54
|
}
|
package/src/Facts.astro
CHANGED
|
@@ -1,22 +1,40 @@
|
|
|
1
1
|
---
|
|
2
2
|
/**
|
|
3
|
-
* Label and figure pairs, three ways: tiles (
|
|
4
|
-
* a line) and a list (its facts). Each figure can carry an icon and an accent of its
|
|
5
|
-
* in every form, so distance is always one colour and heart rate another.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
3
|
+
* Label and figure pairs, three ways: tiles (the figures that lead a page), pills (a thing's
|
|
4
|
+
* figures in a line) and a list (its facts). Each figure can carry an icon and an accent of its
|
|
5
|
+
* own, the same in every form, so distance is always one colour and heart rate another.
|
|
6
|
+
*
|
|
7
|
+
* A tile can also carry a unit, small after the figure, and a line under it: `context`, led by a
|
|
8
|
+
* bold `delta` when there's a change to show. The tiles balance their rows (four go 4 or 2 + 2,
|
|
9
|
+
* never 3 + 1), and the labels, figures and lines line up across a row however they wrap.
|
|
10
|
+
*
|
|
11
|
+
* Two options, off by default: the highlighter under a tile's figure, sweeping in once; and icons
|
|
12
|
+
* that draw themselves in once. Fill it at build time with `items`, or later with setFacts() and
|
|
13
|
+
* setFact() from facts.js.
|
|
8
14
|
*/
|
|
9
15
|
import type { IconName } from './icons';
|
|
10
|
-
import
|
|
16
|
+
import { factsHTML } from './facts.js';
|
|
11
17
|
import './highlight.css';
|
|
12
18
|
|
|
13
19
|
interface Fact {
|
|
14
20
|
label: string;
|
|
15
21
|
value?: string;
|
|
22
|
+
/** Small, after the figure: m³, bpm, km. */
|
|
23
|
+
unit?: string;
|
|
24
|
+
/** Tiles: the line under the figure, such as a period or a comparison. */
|
|
25
|
+
context?: string;
|
|
26
|
+
/** Tiles: a change, bold at the start of the line. The text carries its sign; the tone a quiet colour. */
|
|
27
|
+
delta?: string | { text: string; tone?: 'good' | 'bad' | 'neutral' };
|
|
16
28
|
/** What the figure measures, from the icon set: route, timer, heart-pulse, flame, … */
|
|
17
29
|
icon?: IconName;
|
|
18
30
|
/** A chart token, such as var(--c-chart-3), for the icon and the highlighter. */
|
|
19
31
|
accent?: string;
|
|
32
|
+
/** Names the fact for setFact(). */
|
|
33
|
+
key?: string;
|
|
34
|
+
/** Id on the figure, for a page that writes it itself, as with Stat. */
|
|
35
|
+
valueId?: string;
|
|
36
|
+
/** Id on the line under it. The line keeps its height meanwhile. */
|
|
37
|
+
contextId?: string;
|
|
20
38
|
}
|
|
21
39
|
interface Props {
|
|
22
40
|
items?: Fact[];
|
|
@@ -25,29 +43,28 @@ interface Props {
|
|
|
25
43
|
highlight?: boolean;
|
|
26
44
|
/** Icons that draw themselves in once, as the facts come into view. */
|
|
27
45
|
iconMotion?: boolean;
|
|
46
|
+
/** Tiles: how many lines of context to keep room for while the figures load. 1 by default. */
|
|
47
|
+
lines?: 1 | 2;
|
|
28
48
|
id?: string;
|
|
29
49
|
}
|
|
30
|
-
const { items = [], variant = 'tiles', highlight = false, iconMotion = false, id } = Astro.props;
|
|
31
|
-
const
|
|
50
|
+
const { items = [], variant = 'tiles', highlight = false, iconMotion = false, lines = 1, id } = Astro.props;
|
|
51
|
+
const { html, count, context } = factsHTML(items, { variant, highlight });
|
|
52
|
+
const hl = highlight && variant === 'tiles';
|
|
32
53
|
---
|
|
33
54
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
<
|
|
38
|
-
<
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
</div>
|
|
48
|
-
))
|
|
49
|
-
}
|
|
50
|
-
</dl>
|
|
55
|
+
{
|
|
56
|
+
variant === 'tiles' ? (
|
|
57
|
+
<div class="facts-box">
|
|
58
|
+
<dl class="facts tiles" id={id} data-facts data-variant="tiles" data-count={count} data-context={context ? '' : undefined} data-lines={lines === 2 ? '2' : undefined} data-highlight={hl ? '' : undefined} data-icon-motion={iconMotion ? '' : undefined}>
|
|
59
|
+
<Fragment set:html={html} />
|
|
60
|
+
</dl>
|
|
61
|
+
</div>
|
|
62
|
+
) : (
|
|
63
|
+
<dl class:list={['facts', variant]} id={id} data-facts data-variant={variant} data-count={count} data-icon-motion={iconMotion ? '' : undefined}>
|
|
64
|
+
<Fragment set:html={html} />
|
|
65
|
+
</dl>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
51
68
|
|
|
52
69
|
<script>
|
|
53
70
|
import { arrive } from './facts.js';
|
|
@@ -59,6 +76,10 @@ const size = variant === 'tiles' ? 16 : 14;
|
|
|
59
76
|
margin: 0;
|
|
60
77
|
}
|
|
61
78
|
|
|
79
|
+
.facts :global(dd) {
|
|
80
|
+
margin: 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
62
83
|
.facts :global(.fact-icon) {
|
|
63
84
|
display: inline-grid;
|
|
64
85
|
flex: none;
|
|
@@ -70,27 +91,111 @@ const size = variant === 'tiles' ? 16 : 14;
|
|
|
70
91
|
display: block;
|
|
71
92
|
}
|
|
72
93
|
|
|
73
|
-
|
|
94
|
+
.facts :global(.fact-value) {
|
|
95
|
+
font-weight: 600;
|
|
96
|
+
font-variant-numeric: tabular-nums;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* The unit beside the figure, a step quieter. */
|
|
100
|
+
.facts :global(.fact-unit) {
|
|
101
|
+
margin-inline-start: 0.25em;
|
|
102
|
+
font-weight: 500;
|
|
103
|
+
color: var(--c-text-secondary);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.facts :global(.fact-unit:empty) {
|
|
107
|
+
display: none;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* ── Tiles ──────────────────────────────────────────────────────────────────
|
|
111
|
+
The strip measures itself, so it reflows in a card or a side panel as on a page. Two across
|
|
112
|
+
on a phone; wider, every tile in one row when they fit, else rows that balance: 4 as 2 + 2,
|
|
113
|
+
6 as 3 + 3, never 3 + 1. */
|
|
114
|
+
.facts-box {
|
|
115
|
+
container-type: inline-size;
|
|
116
|
+
}
|
|
117
|
+
|
|
74
118
|
.tiles {
|
|
119
|
+
--cols: 2;
|
|
120
|
+
--rows: 2;
|
|
75
121
|
display: grid;
|
|
122
|
+
grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
|
|
76
123
|
gap: var(--sp-3);
|
|
77
|
-
grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr));
|
|
78
124
|
}
|
|
79
125
|
|
|
126
|
+
.tiles[data-count='0'],
|
|
127
|
+
.tiles[data-count='1'] {
|
|
128
|
+
--cols: 1;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
.tiles[data-context] {
|
|
132
|
+
--rows: 3;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/* An odd last tile on two columns takes the row, so no hole is left beside it. */
|
|
136
|
+
.tiles:is([data-count='3'], [data-count='5']) > :global(div:last-child) {
|
|
137
|
+
grid-column: span 2;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@container (min-width: 28.5rem) {
|
|
141
|
+
.tiles:is([data-count='3'], [data-count='5'], [data-count='6']) {
|
|
142
|
+
--cols: 3;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.tiles[data-count='3'] > :global(div:last-child) {
|
|
146
|
+
grid-column: auto;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
@container (min-width: 38.25rem) {
|
|
151
|
+
.tiles[data-count='4'] {
|
|
152
|
+
--cols: 4;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
@container (min-width: 48rem) {
|
|
157
|
+
.tiles[data-count='5'] {
|
|
158
|
+
--cols: 5;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
.tiles[data-count='5'] > :global(div:last-child) {
|
|
162
|
+
grid-column: auto;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
@container (min-width: 57.75rem) {
|
|
167
|
+
.tiles[data-count='6'] {
|
|
168
|
+
--cols: 6;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
.tiles[data-count='many'] {
|
|
173
|
+
grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/* Each tile takes its rows from the strip (subgrid): a label that wraps, or a line that runs
|
|
177
|
+
to two, moves that row in every tile beside it, so the figures share one baseline. */
|
|
80
178
|
.tiles > :global(div) {
|
|
81
179
|
display: grid;
|
|
82
|
-
|
|
180
|
+
grid-row: span var(--rows);
|
|
181
|
+
grid-template-rows: subgrid;
|
|
182
|
+
row-gap: 0;
|
|
183
|
+
align-content: start;
|
|
184
|
+
min-width: 0;
|
|
83
185
|
padding: var(--sp-4);
|
|
84
186
|
border: 1px solid var(--c-border);
|
|
85
187
|
border-radius: var(--radius-lg);
|
|
86
188
|
background: var(--c-surface);
|
|
87
189
|
}
|
|
88
190
|
|
|
191
|
+
/* A short label beside one that wraps keeps to the top of its tile. */
|
|
89
192
|
.tiles :global(dt) {
|
|
90
193
|
display: flex;
|
|
91
194
|
align-items: center;
|
|
195
|
+
align-self: start;
|
|
92
196
|
gap: var(--sp-2);
|
|
93
197
|
font-size: var(--text-caption);
|
|
198
|
+
line-height: var(--leading-caption);
|
|
94
199
|
color: var(--c-text-secondary);
|
|
95
200
|
}
|
|
96
201
|
|
|
@@ -101,25 +206,71 @@ const size = variant === 'tiles' ? 16 : 14;
|
|
|
101
206
|
background: color-mix(in oklab, var(--accent, var(--c-text-muted)) 16%, var(--c-surface));
|
|
102
207
|
}
|
|
103
208
|
|
|
104
|
-
.
|
|
105
|
-
|
|
209
|
+
/* The figure keeps a line's height while it waits, so nothing jumps when it arrives. A unit
|
|
210
|
+
that doesn't fit goes under it whole, never split from a number mid-word. */
|
|
211
|
+
.tiles :global(.fact-value) {
|
|
212
|
+
display: flex;
|
|
213
|
+
flex-wrap: wrap;
|
|
214
|
+
align-items: baseline;
|
|
215
|
+
align-self: end;
|
|
216
|
+
margin-top: var(--sp-2);
|
|
217
|
+
min-height: 1lh;
|
|
106
218
|
font-size: var(--text-h3, 1.35rem);
|
|
219
|
+
line-height: 1.2;
|
|
220
|
+
color: var(--c-text);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
.tiles :global(.fact-unit) {
|
|
224
|
+
font-size: var(--text-body-sm);
|
|
225
|
+
white-space: nowrap;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.tiles :global(.fact-context) {
|
|
229
|
+
margin-top: 2px;
|
|
230
|
+
min-height: 1lh;
|
|
231
|
+
font-size: var(--text-caption);
|
|
232
|
+
line-height: var(--leading-caption);
|
|
233
|
+
color: var(--c-text-muted);
|
|
234
|
+
text-wrap: pretty;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/* Lines that will wrap: room for two while they wait. */
|
|
238
|
+
.tiles[data-lines='2'] :global(.fact-context) {
|
|
239
|
+
min-height: 2lh;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
.tiles:not([data-context]) :global(.fact-context) {
|
|
243
|
+
display: none;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/* The change, bold, in a quiet tone that still reads as text: 75% of the status colour, the
|
|
247
|
+
rest the text colour (5:1 or more on a tile in both themes). The text carries the sign, so
|
|
248
|
+
the colour is never the only cue. */
|
|
249
|
+
.tiles :global(.fact-delta) {
|
|
107
250
|
font-weight: 600;
|
|
108
251
|
font-variant-numeric: tabular-nums;
|
|
109
|
-
|
|
252
|
+
color: var(--c-text-secondary);
|
|
110
253
|
}
|
|
111
254
|
|
|
112
|
-
.tiles :global(
|
|
255
|
+
.tiles :global(.fact-delta[data-tone='good']) {
|
|
256
|
+
color: color-mix(in oklab, var(--c-status-good) 75%, var(--c-text));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
.tiles :global(.fact-delta[data-tone='bad']) {
|
|
260
|
+
color: color-mix(in oklab, var(--c-status-critical) 75%, var(--c-text));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
.tiles :global(.fact-figure mark) {
|
|
113
264
|
--highlight: color-mix(in oklab, var(--accent, var(--c-accent)) 34%, var(--c-bg));
|
|
114
265
|
}
|
|
115
266
|
|
|
116
267
|
/* The highlighter waits until the tiles are in view. */
|
|
117
|
-
.tiles:not(:global(.in)) :global(
|
|
268
|
+
.tiles:not(:global(.in)) :global(.fact-figure mark) {
|
|
118
269
|
animation: none;
|
|
119
270
|
background-size: 0% var(--highlight-band);
|
|
120
271
|
}
|
|
121
272
|
|
|
122
|
-
/* Pills: a tint of the accent. */
|
|
273
|
+
/* ── Pills: a tint of the accent. ───────────────────────────────────────────── */
|
|
123
274
|
.pills {
|
|
124
275
|
display: flex;
|
|
125
276
|
flex-wrap: wrap;
|
|
@@ -144,14 +295,11 @@ const size = variant === 'tiles' ? 16 : 14;
|
|
|
144
295
|
color: var(--c-text-secondary);
|
|
145
296
|
}
|
|
146
297
|
|
|
147
|
-
.pills :global(
|
|
148
|
-
margin: 0;
|
|
298
|
+
.pills :global(.fact-value) {
|
|
149
299
|
font-size: var(--text-body-sm);
|
|
150
|
-
font-weight: 600;
|
|
151
|
-
font-variant-numeric: tabular-nums;
|
|
152
300
|
}
|
|
153
301
|
|
|
154
|
-
/* A list: hairlines, no fills. */
|
|
302
|
+
/* ── A list: hairlines, no fills. ───────────────────────────────────────────── */
|
|
155
303
|
.list {
|
|
156
304
|
display: grid;
|
|
157
305
|
grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
|
|
@@ -174,9 +322,8 @@ const size = variant === 'tiles' ? 16 : 14;
|
|
|
174
322
|
color: var(--c-text-muted);
|
|
175
323
|
}
|
|
176
324
|
|
|
177
|
-
.list :global(
|
|
178
|
-
margin:
|
|
179
|
-
font-weight: 600;
|
|
325
|
+
.list :global(.fact-value) {
|
|
326
|
+
margin-inline-start: auto;
|
|
180
327
|
text-align: right;
|
|
181
328
|
}
|
|
182
329
|
|
package/src/Stat.astro
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
/**
|
|
3
|
+
* @deprecated Since 0.20.0: use Facts tiles, which take label, value, context, valueId and
|
|
4
|
+
* contextId as the same fields, plus a unit and a change. Kept working until 1.0.
|
|
5
|
+
*
|
|
3
6
|
* One number someone reads at a glance, inside a StatStrip. The value keeps its height
|
|
4
7
|
* while a page waits, so nothing jumps when it arrives. Unknown is –, never 0.
|
|
5
8
|
*/
|
package/src/StatStrip.astro
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
---
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated Since 0.20.0: use Facts (tiles), one fact per Stat. Kept working until 1.0.
|
|
4
|
+
*
|
|
5
|
+
* A row of Stats between two rules. Four at most: past that, you probably want a table.
|
|
6
|
+
*/
|
|
3
7
|
---
|
|
4
8
|
|
|
5
9
|
<dl class="stat-strip"><slot /></dl>
|
package/src/facts.js
CHANGED
|
@@ -1,31 +1,155 @@
|
|
|
1
|
-
// Drives Facts:
|
|
2
|
-
//
|
|
3
|
-
// unless the Facts asks
|
|
1
|
+
// Drives Facts: draws the facts (factsHTML, also used by Facts.astro at build time), fills them from
|
|
2
|
+
// script (setFacts, setFact) and plays its two options once, as it comes into view: the highlighter
|
|
3
|
+
// under a tile's figure, and icons that draw themselves in. Both are off unless the Facts asks.
|
|
4
|
+
//
|
|
5
|
+
// A fact: { label, value?, unit?, context?, delta?, icon?, accent?, key?, valueId?, contextId? }.
|
|
6
|
+
// unit small, after the figure: '638' + 'm³'.
|
|
7
|
+
// context the line under a tile's figure: 'against 699 in 2025'.
|
|
8
|
+
// delta a change, bold at the start of that line: { text: '−8.7%', tone: 'good' | 'bad' | 'neutral' },
|
|
9
|
+
// or a string for neutral. Tiles only, like context.
|
|
10
|
+
// key names a fact for setFact(); valueId and contextId put ids on the figure and the line,
|
|
11
|
+
// for a page that writes them itself, as with Stat.
|
|
12
|
+
import { settle } from '@n1kname/charts/update';
|
|
4
13
|
import { ICONS } from './icons';
|
|
5
14
|
|
|
6
|
-
const esc = (s) => String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
15
|
+
const esc = (s) => String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
7
16
|
const icon = (name, size) =>
|
|
8
17
|
name && ICONS[name]
|
|
9
18
|
? `<svg width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="${size < 20 ? 1.75 : 2}" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[name]}</svg>`
|
|
10
19
|
: '';
|
|
11
20
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
export const PLACEHOLDER = '–';
|
|
22
|
+
const TONES = ['good', 'bad', 'neutral'];
|
|
23
|
+
// Unknown is a dash, never 0 and never a unit on its own.
|
|
24
|
+
const known = (v) => v != null && v !== '' && v !== '–' && v !== '—';
|
|
25
|
+
const deltaOf = (d) => (d == null || d === '' ? null : typeof d === 'string' ? { text: d, tone: 'neutral' } : { text: d.text, tone: TONES.includes(d.tone) ? d.tone : 'neutral' });
|
|
26
|
+
const hasLine = (f) => f.context != null || f.contextId != null || deltaOf(f.delta) != null;
|
|
27
|
+
const sizeOf = (variant) => (variant === 'tiles' ? 16 : 14);
|
|
28
|
+
|
|
29
|
+
const dtHTML = (f, size) => `${f.icon ? `<span class="fact-icon">${icon(f.icon, size)}</span>` : ''}${esc(f.label)}`;
|
|
30
|
+
const lineHTML = (f) => {
|
|
31
|
+
const d = deltaOf(f.delta);
|
|
32
|
+
return `${d ? `<strong class="fact-delta" data-tone="${d.tone}">${esc(d.text)}</strong> ` : ''}${esc(f.context)}`;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** The facts inside a Facts' <dl>, and the attributes the <dl> needs for them. */
|
|
36
|
+
export const factsHTML = (items = [], { variant = 'tiles', highlight = false } = {}) => {
|
|
37
|
+
const size = sizeOf(variant);
|
|
38
|
+
const tiles = variant === 'tiles';
|
|
39
|
+
const hl = highlight && tiles;
|
|
40
|
+
const html = items
|
|
41
|
+
.map((f, n) => {
|
|
42
|
+
const value = known(f.value) ? f.value : PLACEHOLDER;
|
|
43
|
+
const figure = hl ? `<mark class="highlight">${esc(value)}</mark>` : esc(value);
|
|
44
|
+
return (
|
|
45
|
+
`<div style="--n:${n}${f.accent ? `;--accent:${esc(f.accent)}` : ''}"${f.key != null ? ` data-key="${esc(f.key)}"` : ''}${f.icon ? ` data-icon="${esc(f.icon)}"` : ''}>` +
|
|
46
|
+
`<dt>${dtHTML(f, size)}</dt>` +
|
|
47
|
+
`<dd class="fact-value"><span class="fact-figure"${f.valueId ? ` id="${esc(f.valueId)}"` : ''}>${figure}</span><span class="fact-unit" data-unit="${esc(f.unit)}">${known(f.value) ? esc(f.unit) : ''}</span></dd>` +
|
|
48
|
+
(tiles ? `<dd class="fact-context"${f.contextId ? ` id="${esc(f.contextId)}"` : ''}>${lineHTML(f)}</dd>` : '') +
|
|
49
|
+
`</div>`
|
|
50
|
+
);
|
|
51
|
+
})
|
|
22
52
|
.join('');
|
|
53
|
+
return { html, count: countOf(items.length), context: tiles && items.some(hasLine) };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// The tile strip balances its rows by how many there are; past six it simply fills.
|
|
57
|
+
const countOf = (n) => (n > 6 ? 'many' : String(n));
|
|
58
|
+
|
|
59
|
+
const mark = (dl, count, context) => {
|
|
60
|
+
dl.dataset.count = count;
|
|
61
|
+
if (context) dl.dataset.context = '';
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Writes one fact's fields into its tile. Fields left out stay as they are, so setFact can change
|
|
66
|
+
* just the figure; setFacts passes every field.
|
|
67
|
+
*/
|
|
68
|
+
const apply = (dl, div, f, all) => {
|
|
69
|
+
const size = sizeOf(dl.dataset.variant);
|
|
70
|
+
const has = (k) => all || k in f;
|
|
71
|
+
if (has('label') || has('icon')) {
|
|
72
|
+
const dt = div.querySelector('dt');
|
|
73
|
+
const label = has('label') ? f.label : dt.textContent;
|
|
74
|
+
const name = has('icon') ? f.icon : div.dataset.icon;
|
|
75
|
+
// Only when it changes, so icons that drew themselves in don't draw again.
|
|
76
|
+
if (dt.textContent !== String(label ?? '') || (div.dataset.icon ?? '') !== (name ?? '')) {
|
|
77
|
+
dt.innerHTML = dtHTML({ label, icon: name }, size);
|
|
78
|
+
if (name) div.dataset.icon = name;
|
|
79
|
+
else delete div.dataset.icon;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (has('accent')) {
|
|
83
|
+
if (f.accent) div.style.setProperty('--accent', f.accent);
|
|
84
|
+
else div.style.removeProperty('--accent');
|
|
85
|
+
}
|
|
86
|
+
if (has('key')) {
|
|
87
|
+
if (f.key != null) div.dataset.key = f.key;
|
|
88
|
+
else delete div.dataset.key;
|
|
89
|
+
}
|
|
90
|
+
const fig = div.querySelector('.fact-figure');
|
|
91
|
+
const unit = div.querySelector('.fact-unit');
|
|
92
|
+
if (has('value')) {
|
|
93
|
+
const target = fig.querySelector('mark') ?? fig;
|
|
94
|
+
const value = known(f.value) ? String(f.value) : PLACEHOLDER;
|
|
95
|
+
const before = target.textContent;
|
|
96
|
+
if (before !== value) {
|
|
97
|
+
target.textContent = value;
|
|
98
|
+
// A figure that changes settles in, on a page that asks for update motion, as charts do.
|
|
99
|
+
if (known(before) && dl.closest('[data-chart-motion="update"]')) settle(fig.parentElement);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (has('value') || has('unit')) {
|
|
103
|
+
const value = has('value') ? f.value : fig.textContent;
|
|
104
|
+
const u = has('unit') ? f.unit : unit.dataset.unit;
|
|
105
|
+
unit.dataset.unit = u ?? '';
|
|
106
|
+
unit.textContent = known(value) && u ? u : '';
|
|
107
|
+
}
|
|
108
|
+
const line = div.querySelector('.fact-context');
|
|
109
|
+
if (line && (has('context') || has('delta'))) {
|
|
110
|
+
const d = has('delta') ? f.delta : line.querySelector('.fact-delta') && { text: line.querySelector('.fact-delta').textContent, tone: line.querySelector('.fact-delta').dataset.tone };
|
|
111
|
+
const c = has('context') ? f.context : [...line.childNodes].filter((n) => n.nodeType === 3).map((n) => n.textContent).join('').trim();
|
|
112
|
+
line.innerHTML = lineHTML({ context: c, delta: d });
|
|
113
|
+
if (hasLine({ context: c || undefined, delta: d })) dl.dataset.context = '';
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const tilesOf = (dl) => [...dl.children].filter((el) => el.tagName === 'DIV');
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Fills a Facts from script. items: facts in the order they read. With as many facts as it has, it
|
|
121
|
+
* changes them in place, so a figure that changes can settle and nothing redraws; otherwise it draws
|
|
122
|
+
* them again and plays the options once more.
|
|
123
|
+
*/
|
|
124
|
+
export const setFacts = (dl, items) => {
|
|
125
|
+
const tiles = tilesOf(dl);
|
|
126
|
+
if (tiles.length === items.length && tiles.length) {
|
|
127
|
+
items.forEach((f, i) => apply(dl, tiles[i], f, true));
|
|
128
|
+
// Every line gone: the strip drops the row it kept for them.
|
|
129
|
+
if (dl.dataset.variant === 'tiles' && !items.some(hasLine)) delete dl.dataset.context;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const { html, count, context } = factsHTML(items, { variant: dl.dataset.variant, highlight: dl.hasAttribute('data-highlight') });
|
|
133
|
+
dl.innerHTML = html;
|
|
134
|
+
delete dl.dataset.context;
|
|
135
|
+
mark(dl, count, context);
|
|
23
136
|
arrive(dl);
|
|
24
137
|
};
|
|
25
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Changes one fact: `which` is its key or its place (0 first). Only the fields given change:
|
|
141
|
+
* setFact(dl, 'gas', { value: '638', delta: { text: '−8.7%', tone: 'good' } }).
|
|
142
|
+
*/
|
|
143
|
+
export const setFact = (dl, which, f) => {
|
|
144
|
+
const tiles = tilesOf(dl);
|
|
145
|
+
const div = typeof which === 'number' ? tiles[which] : tiles.find((t) => t.dataset.key === String(which));
|
|
146
|
+
if (div) apply(dl, div, f, false);
|
|
147
|
+
return div;
|
|
148
|
+
};
|
|
149
|
+
|
|
26
150
|
const seen = typeof IntersectionObserver === 'undefined' ? null : new IntersectionObserver((entries) => entries.forEach((e) => e.isIntersecting && (e.target.classList.add('in'), seen.unobserve(e.target))), { threshold: 0.4 });
|
|
27
151
|
|
|
28
|
-
// Once, as it comes into view; again after setFacts.
|
|
152
|
+
// Once, as it comes into view; again after setFacts draws the facts anew.
|
|
29
153
|
export const arrive = (dl) => {
|
|
30
154
|
dl.classList.remove('in');
|
|
31
155
|
if (seen) seen.observe(dl);
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Made-up figures only.
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { registerHooks } from 'node:module';
|
|
4
|
+
import { parseHTML } from 'linkedom';
|
|
5
|
+
|
|
6
|
+
// The icons live in a .ts module that Vite resolves without its extension; Node needs it named.
|
|
7
|
+
registerHooks({ resolve: (spec, ctx, next) => next(spec === './icons' ? './icons.ts' : spec, ctx) });
|
|
8
|
+
const { factsHTML, setFacts, setFact, PLACEHOLDER } = await import('./facts.js');
|
|
9
|
+
|
|
10
|
+
const { document, window } = parseHTML('<html><body></body></html>');
|
|
11
|
+
globalThis.document = document;
|
|
12
|
+
// What settle needs: motion allowed, the tokens unset, and a record of what moved.
|
|
13
|
+
const settled = [];
|
|
14
|
+
globalThis.matchMedia = () => ({ matches: false });
|
|
15
|
+
globalThis.getComputedStyle = () => ({ getPropertyValue: () => '' });
|
|
16
|
+
window.HTMLElement.prototype.animate = function () {
|
|
17
|
+
settled.push(this);
|
|
18
|
+
};
|
|
19
|
+
const strip = (attrs = '') => {
|
|
20
|
+
document.body.innerHTML = `<div data-chart-motion="update"><dl data-facts data-variant="tiles" ${attrs}></dl></div>`;
|
|
21
|
+
return document.querySelector('dl');
|
|
22
|
+
};
|
|
23
|
+
const text = (el, sel) => el.querySelector(sel)?.textContent;
|
|
24
|
+
|
|
25
|
+
// Plain, with a unit, and with a line led by a change.
|
|
26
|
+
{
|
|
27
|
+
const { html, count, context } = factsHTML([
|
|
28
|
+
{ label: 'Avg sleep', value: '7h 40m' },
|
|
29
|
+
{ label: 'Resting HR', value: '52', unit: 'bpm', icon: 'heart-pulse', accent: 'var(--c-chart-8)' },
|
|
30
|
+
{ label: 'Gas', value: '612', unit: 'm³', delta: { text: '−6.1%', tone: 'good' }, context: 'against 652 last year', key: 'gas' },
|
|
31
|
+
]);
|
|
32
|
+
assert.equal(count, '3');
|
|
33
|
+
assert.equal(context, true, 'one line is enough to keep the row for all');
|
|
34
|
+
const dl = strip();
|
|
35
|
+
dl.innerHTML = html;
|
|
36
|
+
const [a, b, c] = dl.querySelectorAll(':scope > div');
|
|
37
|
+
assert.equal(text(a, '.fact-figure'), '7h 40m');
|
|
38
|
+
assert.equal(text(a, '.fact-unit'), '');
|
|
39
|
+
assert.equal(text(b, '.fact-unit'), 'bpm');
|
|
40
|
+
assert.match(b.getAttribute('style'), /--accent:var\(--c-chart-8\)/);
|
|
41
|
+
assert.ok(b.querySelector('dt svg[aria-hidden="true"]'), 'icon drawn, hidden');
|
|
42
|
+
assert.equal(c.dataset.key, 'gas');
|
|
43
|
+
assert.equal(c.querySelector('.fact-delta').dataset.tone, 'good');
|
|
44
|
+
assert.equal(text(c, '.fact-context'), '−6.1% against 652 last year');
|
|
45
|
+
assert.equal(c.querySelectorAll('dd').length, 2, 'figure and line, each a dd of the label');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Unknown is a dash, and a unit never stands alone.
|
|
49
|
+
{
|
|
50
|
+
const { html } = factsHTML([{ label: 'VO₂ max', unit: 'mL/kg/min' }, { label: 'Steps', value: '—' }, { label: 'Zero', value: '0', unit: 'km' }]);
|
|
51
|
+
const dl = strip();
|
|
52
|
+
dl.innerHTML = html;
|
|
53
|
+
const [a, b, c] = dl.querySelectorAll(':scope > div');
|
|
54
|
+
assert.equal(text(a, '.fact-figure'), PLACEHOLDER);
|
|
55
|
+
assert.equal(text(a, '.fact-unit'), '');
|
|
56
|
+
assert.equal(text(b, '.fact-figure'), PLACEHOLDER);
|
|
57
|
+
assert.equal(text(c, '.fact-figure'), '0', '0 is a figure');
|
|
58
|
+
assert.equal(text(c, '.fact-unit'), 'km');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Pills and lists: units, but no line under the figure.
|
|
62
|
+
{
|
|
63
|
+
const { html, context } = factsHTML([{ label: 'Distance', value: '12.4', unit: 'km', context: 'ignored' }], { variant: 'pills' });
|
|
64
|
+
assert.equal(context, false);
|
|
65
|
+
assert.ok(!html.includes('fact-context'));
|
|
66
|
+
assert.ok(html.includes('>km<'));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Text is escaped, attributes too.
|
|
70
|
+
{
|
|
71
|
+
const { html } = factsHTML([{ label: '<b>', value: '"1"', key: 'a"b', context: '<i>x</i>' }]);
|
|
72
|
+
assert.ok(!html.includes('<b>') && !html.includes('<i>'));
|
|
73
|
+
assert.ok(html.includes('data-key="a"b"'));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Ids for a page that writes its own figures, as with Stat.
|
|
77
|
+
{
|
|
78
|
+
const { html, context } = factsHTML([{ label: 'Costs', valueId: 'tile-total', contextId: 'sub-total' }]);
|
|
79
|
+
assert.equal(context, true, 'a contextId keeps the line');
|
|
80
|
+
const dl = strip();
|
|
81
|
+
dl.innerHTML = html;
|
|
82
|
+
assert.equal(document.getElementById('tile-total').className, 'fact-figure');
|
|
83
|
+
assert.equal(document.getElementById('sub-total').className, 'fact-context');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// setFacts: drawn anew when the count changes, changed in place when it doesn't.
|
|
87
|
+
{
|
|
88
|
+
const dl = strip();
|
|
89
|
+
setFacts(dl, [{ label: 'A', value: '1' }, { label: 'B', value: '2' }]);
|
|
90
|
+
assert.equal(dl.dataset.count, '2');
|
|
91
|
+
assert.equal(dl.dataset.context, undefined, 'no lines, no row for them');
|
|
92
|
+
const first = dl.querySelector(':scope > div');
|
|
93
|
+
setFacts(dl, [{ label: 'A', value: '3', unit: 'km', context: 'this week' }, { label: 'B', value: '4' }]);
|
|
94
|
+
assert.equal(dl.querySelector(':scope > div'), first, 'same tile, changed in place');
|
|
95
|
+
assert.equal(text(first, '.fact-figure'), '3');
|
|
96
|
+
assert.deepEqual(settled, [first.querySelector('.fact-value'), dl.querySelectorAll('.fact-value')[1]], 'changed figures settle, on a page that asks for update motion');
|
|
97
|
+
assert.equal(text(first, '.fact-unit'), 'km');
|
|
98
|
+
assert.equal(text(first, '.fact-context'), 'this week');
|
|
99
|
+
assert.equal(dl.dataset.context, '', 'a line arrived: the row is kept');
|
|
100
|
+
setFacts(dl, [{ label: 'A', value: '3' }, { label: 'B', value: '4' }]);
|
|
101
|
+
assert.equal(dl.dataset.context, undefined, 'every line gone: the row goes too');
|
|
102
|
+
setFacts(dl, Array.from({ length: 8 }, (_, i) => ({ label: `F${i}`, value: String(i) })));
|
|
103
|
+
assert.equal(dl.dataset.count, 'many');
|
|
104
|
+
assert.equal(dl.querySelectorAll(':scope > div').length, 8);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// The highlighter survives an update in place.
|
|
108
|
+
{
|
|
109
|
+
const dl = strip('data-highlight');
|
|
110
|
+
setFacts(dl, [{ label: 'A', value: '1' }]);
|
|
111
|
+
assert.equal(text(dl, '.fact-figure mark'), '1');
|
|
112
|
+
setFacts(dl, [{ label: 'A', value: '2' }]);
|
|
113
|
+
assert.equal(text(dl, '.fact-figure mark'), '2');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// setFact: only the fields given change, found by key or by place.
|
|
117
|
+
{
|
|
118
|
+
const dl = strip();
|
|
119
|
+
setFacts(dl, [
|
|
120
|
+
{ label: 'Gas', key: 'gas', unit: 'm³', context: 'against 652', icon: 'flame' },
|
|
121
|
+
{ label: 'Degree days', key: 'hdd' },
|
|
122
|
+
]);
|
|
123
|
+
const gas = dl.querySelector('[data-key="gas"]');
|
|
124
|
+
const icon = gas.querySelector('dt svg');
|
|
125
|
+
assert.equal(text(gas, '.fact-unit'), '', 'no unit while the figure waits');
|
|
126
|
+
settled.length = 0;
|
|
127
|
+
setFact(dl, 'gas', { value: '612' });
|
|
128
|
+
assert.equal(settled.length, 0, 'a figure arriving in place of the dash just appears');
|
|
129
|
+
assert.equal(text(gas, '.fact-figure'), '612');
|
|
130
|
+
assert.equal(text(gas, '.fact-unit'), 'm³', 'the unit given earlier comes with the figure');
|
|
131
|
+
assert.equal(text(gas, '.fact-context'), 'against 652', 'the line is left as it was');
|
|
132
|
+
assert.equal(gas.querySelector('dt svg'), icon, 'the label is left as it was, icon and all');
|
|
133
|
+
setFact(dl, 'gas', { delta: { text: '+2.0%', tone: 'bad' } });
|
|
134
|
+
assert.equal(gas.querySelector('.fact-delta').dataset.tone, 'bad');
|
|
135
|
+
assert.equal(text(gas, '.fact-context'), '+2.0% against 652', 'the change leads the line it was given');
|
|
136
|
+
setFact(dl, 1, { label: 'Degree days, Oct–Mar' });
|
|
137
|
+
assert.equal(text(dl.querySelector('[data-key="hdd"]'), 'dt'), 'Degree days, Oct–Mar');
|
|
138
|
+
assert.equal(setFact(dl, 'nope', { value: '1' }), undefined);
|
|
139
|
+
setFact(dl, 'gas', { delta: 'flat' });
|
|
140
|
+
assert.equal(gas.querySelector('.fact-delta').dataset.tone, 'neutral', 'a string is a neutral change');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
console.log('facts: ok');
|