@n1kname/components 0.19.0 → 0.21.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n1kname/components",
3
- "version": "0.19.0",
3
+ "version": "0.21.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 (a few figures that lead), pills (a thing's figures in
4
- * a line) and a list (its facts). Each figure can carry an icon and an accent of its own, the same
5
- * in every form, so distance is always one colour and heart rate another. Two options, off by
6
- * default: the highlighter under a tile's figure, sweeping in once; and icons that draw themselves
7
- * in once. Fill it at build time with `items`, or later with setFacts() from facts.js.
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 Icon from './Icon.astro';
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 size = variant === 'tiles' ? 16 : 14;
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
- <dl class:list={['facts', variant]} id={id} data-facts data-variant={variant} data-highlight={highlight && variant === 'tiles' ? '' : undefined} data-icon-motion={iconMotion ? '' : undefined}>
35
- {
36
- items.map((f, n) => (
37
- <div style={`--n:${n}${f.accent ? `;--accent:${f.accent}` : ''}`}>
38
- <dt>
39
- {f.icon && (
40
- <span class="fact-icon">
41
- <Icon name={f.icon} size={size} />
42
- </span>
43
- )}
44
- {f.label}
45
- </dt>
46
- <dd>{highlight && variant === 'tiles' ? <mark class="highlight">{f.value ?? '–'}</mark> : (f.value ?? '–')}</dd>
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
- /* Tiles: the icon in a soft disc of its accent. */
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
- gap: var(--sp-2);
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
- .tiles :global(dd) {
105
- margin: 0;
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
- line-height: 1.2;
252
+ color: var(--c-text-secondary);
110
253
  }
111
254
 
112
- .tiles :global(dd mark) {
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(dd mark) {
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(dd) {
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(dd) {
178
- margin: 0 0 0 auto;
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
  */
@@ -1,5 +1,9 @@
1
1
  ---
2
- /** A row of Stats between two rules. Four at most: past that, you probably want a table. */
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>
@@ -1,134 +1,298 @@
1
1
  ---
2
2
  /**
3
- * A project timeline: mono period in a left column, content on the right. The column
4
- * fits the longest period, so every entry lines up, and wraps past 200px.
5
- * Needs type.css for its t-meta and t-sub classes.
3
+ * A project timeline: a marker and a line down the left, and each entry's period, title and text
4
+ * beside it. The period sits above the title, small, so a year, a phase or a name all read the
5
+ * same, and the text keeps the width.
6
6
  *
7
- * The final entry is the current state, so its marker is filled.
7
+ * Entries up to `current` are done (filled markers on a solid line); `current` is now (a marker
8
+ * with a halo); any after it are still to come (hollow markers on a dashed line). By default the
9
+ * last entry is now. An entry can carry an icon, shown in a soft disc of the accent.
10
+ *
11
+ * Once, as each entry comes into view, its marker settles in, its text rises and its line draws on
12
+ * to the next. Nothing moves under reduced motion, and without script everything is simply there.
8
13
  */
14
+ import type { IconName } from './icons';
15
+ import Icon from './Icon.astro';
16
+
9
17
  interface Entry {
10
18
  period: string;
11
19
  label: string;
12
20
  body: string;
21
+ /** What the step was, from the icon set: users, search, route, … */
22
+ icon?: IconName;
13
23
  }
14
24
 
15
25
  interface Props {
16
26
  entries: Entry[];
27
+ /** Which entry is now, 0 first. The last by default; false for a history that is over. */
28
+ current?: number | false;
17
29
  }
18
30
 
19
- const { entries } = Astro.props;
31
+ const { entries, current = entries.length - 1 } = Astro.props;
32
+ const icons = entries.some((e) => e.icon);
33
+ const state = (i: number) => (current === false || i < current ? 'done' : i === current ? 'current' : 'next');
20
34
  ---
21
35
 
22
- <ol class="timeline">
36
+ <ol class="timeline" data-icons={icons ? '' : undefined} data-timeline>
23
37
  {
24
38
  entries.map((e, i) => (
25
- <li class={i === entries.length - 1 ? 'last' : ''}>
26
- <span class="rail" aria-hidden="true" />
27
- <span class="period t-meta">{e.period}</span>
28
- <div class="body">
29
- <p class="t-sub">{e.label}</p>
39
+ <li data-state={state(i)} aria-current={state(i) === 'current' ? 'step' : undefined} data-to={i < entries.length - 1 ? state(i + 1) : undefined}>
40
+ <span class="marker" aria-hidden="true">
41
+ {e.icon && <Icon name={e.icon} size={16} />}
42
+ </span>
43
+ <div class="content">
44
+ <p class="period">{e.period}</p>
45
+ <p class="label">{e.label}</p>
30
46
  <p class="text">{e.body}</p>
31
47
  </div>
32
48
  </li>
33
49
  ))
34
50
  }
35
51
  </ol>
52
+ <script is:inline>
53
+ // Before the first paint, so entries waiting to arrive never show and then vanish.
54
+ (function (list) {
55
+ if (list && 'IntersectionObserver' in window && !matchMedia('(prefers-reduced-motion: reduce)').matches) list.classList.add('arrive');
56
+ })(document.currentScript && document.currentScript.previousElementSibling);
57
+ </script>
58
+
59
+ <script>
60
+ // Each entry arrives once, as it comes into view. Entries that come into view together follow
61
+ // one another a beat apart.
62
+ document.querySelectorAll<HTMLElement>('[data-timeline].arrive').forEach((list) => {
63
+ const seen = new IntersectionObserver(
64
+ (entries) => {
65
+ entries
66
+ .filter((e) => e.isIntersecting)
67
+ .forEach((e, k) => {
68
+ const li = e.target as HTMLElement;
69
+ li.style.setProperty('--d', `${k * 110}ms`);
70
+ li.classList.add('in');
71
+ seen.unobserve(li);
72
+ });
73
+ },
74
+ { threshold: 0.25, rootMargin: '0px 0px -8% 0px' },
75
+ );
76
+ list.querySelectorAll('li').forEach((li) => seen.observe(li));
77
+ });
78
+ </script>
36
79
 
37
80
  <style>
38
81
  .timeline {
39
- display: grid;
40
- grid-template-columns: 20px fit-content(200px) minmax(0, 1fr);
41
- column-gap: var(--sp-4);
82
+ /* The marker's size and how far it sits down the first line, so the line can meet it. */
83
+ --m: 12px;
84
+ --mt: 4px;
85
+ --col: 16px;
86
+ --gap: var(--sp-6);
87
+ --done: var(--c-accent);
88
+ --rail-done: color-mix(in oklab, var(--c-accent) 45%, var(--c-border));
89
+ --rail-next: var(--c-border-strong);
42
90
  list-style: none;
43
91
  margin: var(--sp-5) 0 var(--sp-7);
44
92
  padding: 0;
45
93
  }
46
94
 
47
- /* Each entry shares the list's columns, so the periods all take the same width. */
95
+ .timeline[data-icons] {
96
+ --m: 32px;
97
+ --mt: 0px;
98
+ --col: 32px;
99
+ }
100
+
48
101
  li {
49
- display: grid;
50
- grid-column: 1 / -1;
51
- grid-template-columns: subgrid;
52
- padding-bottom: var(--sp-6);
53
102
  position: relative;
103
+ display: grid;
104
+ grid-template-columns: var(--col) minmax(0, 1fr);
105
+ column-gap: var(--sp-4);
106
+ padding-bottom: var(--gap);
54
107
  }
55
108
 
56
109
  li:last-child {
57
110
  padding-bottom: 0;
58
111
  }
59
112
 
60
- /* Marker plus the connecting line, drawn as one element. */
61
- .rail {
62
- position: relative;
63
- width: 20px;
64
- margin-top: 3px;
65
- }
66
-
67
- .rail::before {
113
+ /* The line from this marker to the next, a small gap clear of each. */
114
+ li:not(:last-child)::before {
68
115
  content: '';
69
116
  position: absolute;
70
- inset-inline-start: 0;
71
- top: 0;
72
- width: 14px;
73
- height: 14px;
117
+ inset-inline-start: calc(var(--col) / 2 - 1px);
118
+ top: calc(var(--mt) + var(--m) + 6px);
119
+ bottom: calc(6px - var(--mt));
120
+ width: 2px;
121
+ border-radius: 1px;
122
+ background: var(--rail-done);
123
+ transform-origin: top;
124
+ }
125
+
126
+ /* On to what's still to come: dashed, and quieter. */
127
+ li[data-to='next']::before {
128
+ background: repeating-linear-gradient(to bottom, var(--rail-next) 0 4px, transparent 4px 9px);
129
+ opacity: 0.7;
130
+ }
131
+
132
+ /* ── Markers ─────────────────────────────────────────────────────────────── */
133
+ .marker {
134
+ position: relative;
135
+ display: grid;
136
+ place-items: center;
137
+ width: var(--m);
138
+ height: var(--m);
139
+ margin-top: var(--mt);
140
+ justify-self: center;
74
141
  border-radius: var(--radius-full);
75
- border: 3px solid var(--c-accent);
142
+ background: var(--done);
143
+ color: var(--c-text-accent);
144
+ }
145
+
146
+ .marker :global(svg) {
147
+ display: block;
148
+ }
149
+
150
+ /* Now: a halo round it, so it reads as where things stand. */
151
+ [data-state='current'] .marker {
152
+ box-shadow: 0 0 0 4px color-mix(in oklab, var(--c-accent) 28%, transparent);
153
+ }
154
+
155
+ /* Still to come: hollow. */
156
+ [data-state='next'] .marker {
76
157
  background: var(--c-bg);
158
+ box-shadow: inset 0 0 0 2px var(--c-border-strong);
77
159
  }
78
160
 
79
- .last .rail::before {
80
- border-color: var(--c-text);
161
+ /* With icons: a soft disc of the accent for what's done, the accent itself for now. */
162
+ [data-icons] [data-state='done'] .marker {
163
+ background: color-mix(in oklab, var(--c-accent) 16%, var(--c-surface));
81
164
  }
82
165
 
83
- .rail::after {
84
- content: '';
85
- position: absolute;
86
- inset-inline-start: 6px;
87
- top: 18px;
88
- bottom: calc(var(--sp-6) * -1);
89
- width: 2px;
90
- background: var(--c-border);
166
+ [data-icons] [data-state='current'] .marker {
167
+ background: var(--c-accent);
168
+ color: var(--c-on-accent);
169
+ }
170
+
171
+ [data-icons] [data-state='next'] .marker {
172
+ color: var(--c-text-muted);
173
+ box-shadow: inset 0 0 0 1.5px var(--c-border-strong);
91
174
  }
92
175
 
93
- .last .rail::after {
94
- display: none;
176
+ /* ── Text ────────────────────────────────────────────────────────────────── */
177
+ .content {
178
+ min-width: 0;
95
179
  }
96
180
 
97
- .period {
98
- min-width: 84px;
99
- padding-top: 1px;
181
+ /* With icons, the period sits level with the middle of the disc. */
182
+ [data-icons] .content {
183
+ padding-top: calc((var(--m) - var(--text-caption) * 1.5) / 2);
100
184
  }
101
185
 
102
- .body p {
186
+ .content p {
103
187
  max-width: none;
188
+ margin: 0;
189
+ }
190
+
191
+ .period {
192
+ font-family: var(--font-body);
193
+ font-size: var(--text-caption);
194
+ line-height: 1.5;
195
+ font-weight: 600;
196
+ letter-spacing: 0.02em;
197
+ font-variant-numeric: tabular-nums;
198
+ color: var(--c-text-muted);
104
199
  }
105
200
 
106
- .t-sub {
107
- margin-bottom: var(--sp-1);
201
+ [data-state='current'] .period {
202
+ color: var(--c-text-accent);
203
+ }
204
+
205
+ .label {
206
+ margin-top: 2px;
207
+ font-family: var(--font-body);
208
+ font-size: var(--text-sub);
209
+ line-height: var(--leading-sub);
210
+ letter-spacing: var(--tracking-sub);
211
+ font-weight: 600;
212
+ color: var(--c-text);
108
213
  }
109
214
 
110
215
  .text {
216
+ margin-top: var(--sp-1);
217
+ color: var(--c-text-secondary);
218
+ text-wrap: pretty;
219
+ }
220
+
221
+ [data-state='next'] .text {
111
222
  color: var(--c-text-muted);
112
223
  }
113
224
 
114
- /* Below the fold of a phone the three columns stop earning their keep. */
115
- @media (max-width: 560px) {
116
- .timeline {
117
- grid-template-columns: 20px minmax(0, 1fr);
225
+ /* ── Arriving, once ─────────────────────────────────────────────────────────
226
+ Only on a page whose script has run and that allows motion, so the timeline is never hidden
227
+ without it. The marker settles on the spring, the text rises a little, then the line draws on
228
+ to the next entry. Now's halo ripples out twice. */
229
+ .arrive li:not(.in) .marker {
230
+ opacity: 0;
231
+ transform: scale(0.4);
232
+ }
233
+
234
+ .arrive li:not(.in) .content {
235
+ opacity: 0;
236
+ transform: translateY(6px);
237
+ }
238
+
239
+ .arrive li:not(.in)::before {
240
+ transform: scaleY(0);
241
+ }
242
+
243
+ /* The transitions belong to arriving only: going back to waiting is instant. */
244
+ .arrive li.in .marker {
245
+ transition:
246
+ opacity var(--motion-base) var(--ease-out) var(--d, 0ms),
247
+ transform var(--motion-spring) var(--ease-spring) var(--d, 0ms);
248
+ }
249
+
250
+ .arrive li.in .content {
251
+ transition:
252
+ opacity var(--motion-gentle) var(--ease-out) calc(var(--d, 0ms) + 60ms),
253
+ transform var(--motion-gentle) var(--ease-out) calc(var(--d, 0ms) + 60ms);
254
+ }
255
+
256
+ .arrive li.in::before {
257
+ transition: transform 520ms var(--ease-soft) calc(var(--d, 0ms) + 200ms);
258
+ }
259
+
260
+ .arrive [data-state='current'].in .marker::after {
261
+ content: '';
262
+ position: absolute;
263
+ inset: 0;
264
+ border-radius: inherit;
265
+ box-shadow: 0 0 0 2px var(--c-accent);
266
+ opacity: 0;
267
+ animation: ripple 1100ms var(--ease-out) calc(var(--d, 0ms) + 260ms) 2;
268
+ }
269
+
270
+ @keyframes ripple {
271
+ from {
272
+ opacity: 0.6;
273
+ transform: scale(1);
118
274
  }
119
275
 
120
- li {
121
- row-gap: var(--sp-1);
276
+ to {
277
+ opacity: 0;
278
+ transform: scale(2.2);
279
+ }
280
+ }
281
+
282
+ @media (forced-colors: active) {
283
+ .marker {
284
+ forced-color-adjust: none;
285
+ background: CanvasText;
286
+ color: Canvas;
122
287
  }
123
288
 
124
- /* Spans both rows so the connecting line still reaches the next marker. */
125
- .rail {
126
- grid-row: span 2;
289
+ [data-state='next'] .marker {
290
+ background: Canvas;
291
+ box-shadow: inset 0 0 0 2px CanvasText;
127
292
  }
128
293
 
129
- .period,
130
- .body {
131
- grid-column: 2;
294
+ li:not(:last-child)::before {
295
+ background: CanvasText;
132
296
  }
133
297
  }
134
298
  </style>
package/src/facts.js CHANGED
@@ -1,31 +1,155 @@
1
- // Drives Facts: fills one from script (setFacts) and plays its two options once, as it comes into
2
- // view: the highlighter under a tile's figure, and icons that draw themselves in. Both are off
3
- // unless the Facts asks for them.
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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
15
+ const esc = (s) => String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
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
- // items: [{ label, value, icon?, accent? }], in the order they read.
13
- export const setFacts = (dl, items) => {
14
- const size = dl.dataset.variant === 'tiles' ? 16 : 14;
15
- const hl = dl.hasAttribute('data-highlight') && dl.dataset.variant === 'tiles';
16
- dl.innerHTML = items
17
- .map(
18
- (f, n) =>
19
- `<div style="--n:${n}${f.accent ? `;--accent:${f.accent}` : ''}"><dt>${f.icon ? `<span class="fact-icon">${icon(f.icon, size)}</span>` : ''}${esc(f.label)}</dt>` +
20
- `<dd>${hl ? `<mark class="highlight">${esc(f.value)}</mark>` : esc(f.value)}</dd></div>`,
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&quot;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');