basalt-ui 2.26.0 → 2.27.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/MIGRATING.md +87 -12
  3. package/dist/charts/Bars.d.ts.map +1 -1
  4. package/dist/charts/Bars.js +37 -1
  5. package/dist/charts/ChartCard.d.ts +8 -2
  6. package/dist/charts/ChartCard.d.ts.map +1 -1
  7. package/dist/charts/ChartCard.js +2 -0
  8. package/dist/charts/Sparkline.d.ts.map +1 -1
  9. package/dist/charts/Sparkline.js +48 -10
  10. package/dist/charts/chart-box.module.css +76 -1
  11. package/dist/charts/chart-box.module.css.d.ts +1 -0
  12. package/dist/charts/chart-legend.module.css +0 -14
  13. package/dist/charts/compile.d.ts +20 -0
  14. package/dist/charts/compile.d.ts.map +1 -1
  15. package/dist/charts/compile.js +82 -12
  16. package/dist/charts/legend.d.ts.map +1 -1
  17. package/dist/charts/legend.js +37 -87
  18. package/dist/charts/state.d.ts +7 -0
  19. package/dist/charts/state.d.ts.map +1 -1
  20. package/dist/charts/state.js +14 -3
  21. package/dist/cli/shot/capture.d.ts +32 -3
  22. package/dist/cli/shot/capture.d.ts.map +1 -1
  23. package/dist/cli/shot/capture.js +17 -7
  24. package/dist/dashboard/meter-model.d.ts +17 -3
  25. package/dist/dashboard/meter-model.d.ts.map +1 -1
  26. package/dist/dashboard/meter-model.js +69 -19
  27. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  28. package/dist/dashboard/meter-parts.js +28 -12
  29. package/dist/dashboard/meter.module.css +12 -1
  30. package/dist/dashboard/stat-strip.d.ts.map +1 -1
  31. package/dist/dashboard/stat-strip.js +89 -21
  32. package/dist/dashboard/stat-strip.module.css +13 -3
  33. package/dist/dashboard/stat-strip.module.css.d.ts +1 -0
  34. package/llms.txt +3 -2
  35. package/package.json +1 -1
  36. package/src/charts/Bars.tsx +50 -2
  37. package/src/charts/ChartCard.tsx +9 -1
  38. package/src/charts/Sparkline.tsx +67 -9
  39. package/src/charts/chart-box.module.css +76 -1
  40. package/src/charts/chart-box.module.css.d.ts +1 -0
  41. package/src/charts/chart-legend.module.css +0 -14
  42. package/src/charts/compile.ts +129 -18
  43. package/src/charts/legend.tsx +10 -74
  44. package/src/charts/state.tsx +30 -5
  45. package/src/cli/shot/capture.ts +25 -9
  46. package/src/dashboard/meter-model.ts +122 -31
  47. package/src/dashboard/meter-parts.tsx +53 -19
  48. package/src/dashboard/meter.module.css +12 -1
  49. package/src/dashboard/stat-strip.module.css +13 -3
  50. package/src/dashboard/stat-strip.module.css.d.ts +1 -0
  51. package/src/dashboard/stat-strip.tsx +113 -25
@@ -41,12 +41,35 @@ const pendingStyle: CSSProperties = {
41
41
  /** Bar ghost heights (% of the plot) of a pending plot: a mild rise, not data. */
42
42
  const GHOST_BARS = [38, 52, 44, 66, 58, 74, 62, 48] as const
43
43
 
44
+ /**
45
+ * The shape a pending plot draws: `'bars'` (default), `'line'` for a line or area chart, or a bar
46
+ * count when the loaded chart's data length is known (a 6-month chart loads 6 bars).
47
+ */
48
+ export type PlotGhostShape = 'bars' | 'line' | number
49
+
50
+ /** A mild rise across the width, in a 100 x 40 box stretched to the plot. */
51
+ const GHOST_LINE = '0,30 12,26 25,28 38,18 50,22 62,12 75,16 88,8 100,12'
52
+
44
53
  /** The loaded shape of a plot, drawn in neutral tone (`chart-box.module.css` `.ghost`). */
45
- function PlotGhost(): ReactElement {
54
+ function PlotGhost({ shape = 'bars' }: { shape?: PlotGhostShape }): ReactElement {
55
+ if (shape === 'line')
56
+ return (
57
+ <div aria-hidden="true" data-basalt-plot-ghost="line" className={boxClasses.ghost}>
58
+ <svg viewBox="0 0 100 40" preserveAspectRatio="none" className={boxClasses.ghostLine}>
59
+ <polyline points={GHOST_LINE} vectorEffect="non-scaling-stroke" />
60
+ </svg>
61
+ </div>
62
+ )
63
+ const count =
64
+ typeof shape === 'number' ? Math.max(1, Math.min(Math.floor(shape), 60)) : GHOST_BARS.length
46
65
  return (
47
- <div aria-hidden="true" data-basalt-plot-ghost="" className={boxClasses.ghost}>
48
- {GHOST_BARS.map((h, i) => (
49
- <span key={i} className={boxClasses.ghostBar} style={{ height: `${h}%` }} />
66
+ <div aria-hidden="true" data-basalt-plot-ghost="bars" className={boxClasses.ghost}>
67
+ {Array.from({ length: count }, (_, i) => (
68
+ <span
69
+ key={i}
70
+ className={boxClasses.ghostBar}
71
+ style={{ height: `${GHOST_BARS[i % GHOST_BARS.length]}%` }}
72
+ />
50
73
  ))}
51
74
  </div>
52
75
  )
@@ -105,6 +128,8 @@ export function ChartPlaceholder(props: {
105
128
  compact?: boolean
106
129
  /** Error only: what failed to load, "Could not load {noun}". Default: no noun. */
107
130
  noun?: string
131
+ /** Pending only: the ghost's shape. Default `'bars'`. */
132
+ ghost?: PlotGhostShape
108
133
  }): ReactElement | null {
109
134
  const kind = resolveChartState(props.state)
110
135
  if (kind === null) return null
@@ -127,7 +152,7 @@ export function ChartPlaceholder(props: {
127
152
  if (kind === 'pending')
128
153
  return fit(
129
154
  <output aria-label="Loading chart" aria-busy="true" className={stateClass} style={style}>
130
- {!props.compact && <PlotGhost />}
155
+ {!props.compact && <PlotGhost {...(props.ghost !== undefined && { shape: props.ghost })} />}
131
156
  </output>,
132
157
  )
133
158
  const label =
@@ -140,20 +140,22 @@ export function withParam(route: string, state: ShotState): string {
140
140
  return `${path}${path.includes('?') ? '&' : '?'}shot-state=${state}${fragment}`
141
141
  }
142
142
 
143
- /** The rect union of `boxes`, grown by `margin` and clamped to the viewport (`tall`: its current height). */
143
+ /** The rect union of `boxes`, grown by `margin` and clamped to the viewport (`tall`: its current height); null when nothing of it is inside. */
144
144
  export function unionClip(
145
145
  boxes: readonly Box[],
146
146
  margin: number,
147
147
  viewport: Viewport,
148
148
  tall?: number,
149
- ): Box {
149
+ ): Box | null {
150
150
  const { width, height: base } = VIEWPORT_SPECS[viewport]
151
151
  const height = tall ?? base
152
152
  const left = Math.max(0, Math.min(...boxes.map((b) => b.x)) - margin)
153
153
  const top = Math.max(0, Math.min(...boxes.map((b) => b.y)) - margin)
154
154
  const right = Math.min(width, Math.max(...boxes.map((b) => b.x + b.width)) + margin)
155
155
  const bottom = Math.min(height, Math.max(...boxes.map((b) => b.y + b.height)) + margin)
156
- return { x: left, y: top, width: Math.max(1, right - left), height: Math.max(1, bottom - top) }
156
+ return right <= left || bottom <= top
157
+ ? null
158
+ : { x: left, y: top, width: right - left, height: bottom - top }
157
159
  }
158
160
 
159
161
  /** One screenshot with the options every shot shares. */
@@ -170,6 +172,19 @@ const snap = (
170
172
  ...extra,
171
173
  })
172
174
 
175
+ /** A clip of `targets` re-measured against the live page height (`c.height` goes stale after a reflow); an empty clip is retried once after a frame, then fails. */
176
+ export async function snapClip(c: Ctx, path: string, targets: readonly PwLocator[]): Promise<void> {
177
+ for (let retry = true; ; retry = false) {
178
+ const live = await c.page.evaluate<number>('window.innerHeight')
179
+ const boxes = (await Promise.all(targets.map((t) => t.boundingBox()))).filter((b) => b !== null)
180
+ const clip =
181
+ boxes.length === targets.length && unionClip(boxes, FOCUS_MARGIN_PX, c.viewport, live)
182
+ if (clip) return void (await snap(c.page, path, { clip }))
183
+ if (!retry) throw new Error('Clipped area is either empty or outside the resulting image')
184
+ await c.page.evaluate(FRAMES)
185
+ }
186
+ }
187
+
173
188
  function record(c: Ctx, { name, state, status, file, note, stateful }: Outcome): void {
174
189
  const { route, scheme, viewport } = c
175
190
  c.shots.push({
@@ -545,6 +560,9 @@ export async function hoverTrigger(target: PwLocator): Promise<void> {
545
560
  await target.hover({ timeout: HOVER_TIMEOUT_MS, ...(position && { position }) })
546
561
  }
547
562
 
563
+ const tipOf = (c: Ctx, describedBy: string): PwLocator =>
564
+ c.page.locator(visibleTooltip(describedBy)).first()
565
+
548
566
  /**
549
567
  * The box of the tooltip an action opened, or null when none shows up. Call `markVisibleTooltips`
550
568
  * before the action; `describedBy` is the trigger's own tooltip id, when it has one.
@@ -554,7 +572,7 @@ async function openTooltipBox(
554
572
  describedBy: string,
555
573
  timeout = TIP_PROBE_MS,
556
574
  ): Promise<Box | null> {
557
- const tip = c.page.locator(visibleTooltip(describedBy)).first()
575
+ const tip = tipOf(c, describedBy)
558
576
  try {
559
577
  await tip.waitFor({ state: 'visible', timeout })
560
578
  } catch {
@@ -586,7 +604,7 @@ async function captureHover(c: Ctx, name: string): Promise<void> {
586
604
  const describedBy = idOf(await target.evaluate(describedByOf))
587
605
  const [box, tip] = [await settledBox(anchor), await openTooltipBox(c, describedBy)]
588
606
  if (!box || !tip) return void (await snap(anchor, path))
589
- await snap(c.page, path, { clip: unionClip([box, tip], FOCUS_MARGIN_PX, c.viewport, c.height) })
607
+ await snapClip(c, path, [anchor, tipOf(c, describedBy)])
590
608
  })
591
609
  await c.page.mouse.move(0, 0)
592
610
  }
@@ -645,9 +663,7 @@ async function captureFocus(c: Ctx, name: string): Promise<void> {
645
663
  ),
646
664
  )
647
665
  const tip = await openTooltipBox(c, focused)
648
- await snap(c.page, path, {
649
- clip: unionClip(tip ? [box, tip] : [box], FOCUS_MARGIN_PX, c.viewport, c.height),
650
- })
666
+ await snapClip(c, path, tip ? [anchor, tipOf(c, focused)] : [anchor])
651
667
  })
652
668
  await c.page.evaluate('document.activeElement?.blur()')
653
669
  }
@@ -666,7 +682,7 @@ async function captureTooltip(c: Ctx, name: string): Promise<void> {
666
682
  if (!t) throw new Error('no visible [role="tooltip"] after hover')
667
683
  const a = await settledBox(anchor)
668
684
  if (!a) throw new Error('tooltip or anchor has no box')
669
- await snap(c.page, path, { clip: unionClip([a, t], FOCUS_MARGIN_PX, c.viewport, c.height) })
685
+ await snapClip(c, path, [anchor, tipOf(c, describedBy)])
670
686
  })
671
687
  await c.page.mouse.move(0, 0)
672
688
  }
@@ -251,6 +251,11 @@ export type RowLabel = {
251
251
  holding?: boolean
252
252
  /** A tick at the track's start or end: it keeps its place over every zone name it would touch. */
253
253
  end?: boolean
254
+ /**
255
+ * The scale's own start figure (not a consumer tick): D38's caption order drops it before anything
256
+ * wraps, so it never takes the second row; it hides when the first row cannot hold it.
257
+ */
258
+ figure?: boolean
254
259
  short?: LabelBox
255
260
  }
256
261
 
@@ -261,6 +266,8 @@ export type RowPlacement = {
261
266
  short: boolean[]
262
267
  /** Pixels a shown label moves along the row's physical x axis to stay inside it (0 unless clamped). */
263
268
  shift: number[]
269
+ /** Index-aligned: the row a shown label stands on, 0 the first, 1 the second (staggered under it). */
270
+ row: (0 | 1)[]
264
271
  }
265
272
 
266
273
  /**
@@ -268,29 +275,106 @@ export type RowPlacement = {
268
275
  * is the reading's verdict, so it never hides for width: it may spill past its span, shifted inside
269
276
  * the row); the other ticks go next, then the end ticks (at the track's start or end: a scale's
270
277
  * figures); every other zone name then shows only when it lies inside the row and its own span and
271
- * clears every label already kept, trying its full name, then its `short` box, then hiding. An end
278
+ * clears every label already kept, trying its full name, then its `short` box, then hiding. A name
279
+ * that does not fit its span borrows room: its neighbours (the holding name too) slide away, each
280
+ * keeping its CENTRE inside its own span (it may overhang a span edge by up to half its width).
281
+ * Order per name: full on row one, full on row two, `short` on row one, `short` on row two, hidden;
282
+ * on an equal count of shown labels the full-name layout wins over the `short` one. An end
272
283
  * figure hides only when it cannot clear the holding name, an interior tick or the other end. A tick
273
- * keeps at least `gap` px (one gap step of the host's spacing) from every other label; zone names
274
- * among themselves only must not touch. A hidden name is still named in the described-by text.
284
+ * keeps at least `gap` px (one gap step of the host's spacing) from every other label, and so does
285
+ * a zone name from its neighbour. No zone name and no tick label is lost for width: one that cannot
286
+ * hold its place on the first row stands on a second row directly under it (centred on its span or
287
+ * mark, clamped inside the row, keeping the same gap), and only hides when neither row can hold it.
288
+ * A hidden label is still named in the described-by text.
275
289
  */
276
290
  export function placeRowLabels(
277
291
  labels: readonly RowLabel[],
278
292
  rowWidth: number,
279
293
  gap = 0,
280
294
  ): RowPlacement {
295
+ // A name's order: full on row one (borrowing included), full on row two, then its `short` on row
296
+ // one, `short` on row two, then hidden. So the full names are tried alone first and the `short`
297
+ // fallback only runs when some label would otherwise be lost.
298
+ const full = layOut(
299
+ labels.map(({ short: _short, ...l }) => l),
300
+ rowWidth,
301
+ gap,
302
+ )
303
+ if (full.shown.every(Boolean) || labels.every((l) => l.short === undefined)) return full
304
+ const withShort = layOut(labels, rowWidth, gap)
305
+ return withShort.shown.filter(Boolean).length > full.shown.filter(Boolean).length
306
+ ? withShort
307
+ : full
308
+ }
309
+
310
+ function layOut(labels: readonly RowLabel[], rowWidth: number, gap: number): RowPlacement {
281
311
  const first = placeOnce(labels, rowWidth, gap, false)
282
312
  // One register: once any name falls back to its `short`, every other name that has one follows
283
313
  // (the holding name, which never shortens, is the one exception). The row is placed again with
284
314
  // those names in their `short` box, so every shift is computed against the box that renders.
285
- return first.short.some(Boolean) ? placeOnce(labels, rowWidth, gap, true) : first
315
+ const registerShort = first.short.some(Boolean)
316
+ const one = registerShort ? placeOnce(labels, rowWidth, gap, true) : first
317
+ return stagger(labels, one, rowWidth, gap, registerShort)
286
318
  }
287
319
 
288
- function placeOnce(
320
+ /**
321
+ * The second row: every label the first row could not hold, ticks before names, each centred on its
322
+ * own box (a tick keeps its anchoring) and clamped inside the row like the holding name, keeping `gap`
323
+ * px from the labels already on that row. The register follows the first row; a label that fits
324
+ * neither row stays hidden.
325
+ */
326
+ function stagger(
289
327
  labels: readonly RowLabel[],
328
+ one: Omit<RowPlacement, 'row'>,
290
329
  rowWidth: number,
291
330
  gap: number,
292
331
  registerShort: boolean,
293
332
  ): RowPlacement {
333
+ const row = labels.map((): 0 | 1 => 0)
334
+ const { shown, short, shift } = one
335
+ const kept: LabelBox[] = []
336
+ const rank = (l: RowLabel) => (l.end ? 0 : l.span ? 2 : 1)
337
+ const pending = labels
338
+ .map((_, i) => i)
339
+ .filter((i) => !shown[i] && labels[i]?.figure !== true)
340
+ .toSorted((a, b) => rank(labels[a] as RowLabel) - rank(labels[b] as RowLabel))
341
+ const clamped = (b: LabelBox): LabelBox => {
342
+ const width = b.right - b.left
343
+ const left = Math.max(0, Math.min(b.left, rowWidth - width))
344
+ return { left, right: left + width }
345
+ }
346
+ const clears = (b: LabelBox) =>
347
+ !kept.some(
348
+ (k) => b.left < k.right + gap - LABEL_SUBPIXEL && k.left < b.right + gap - LABEL_SUBPIXEL,
349
+ )
350
+ for (const i of pending) {
351
+ const label = labels[i] as RowLabel
352
+ const useShort = registerShort && label.short !== undefined
353
+ const candidates = useShort
354
+ ? [label.short as LabelBox]
355
+ : label.short !== undefined
356
+ ? [label.box, label.short]
357
+ : [label.box]
358
+ for (const base of candidates) {
359
+ const box = clamped(base)
360
+ if (box.right - box.left > rowWidth + LABEL_SUBPIXEL || !clears(box)) continue
361
+ kept.push(box)
362
+ shown[i] = true
363
+ short[i] = base !== label.box
364
+ shift[i] = box.left - base.left
365
+ row[i] = 1
366
+ break
367
+ }
368
+ }
369
+ return { shown, short, shift, row }
370
+ }
371
+
372
+ function placeOnce(
373
+ labels: readonly RowLabel[],
374
+ rowWidth: number,
375
+ gap: number,
376
+ registerShort: boolean,
377
+ ): Omit<RowPlacement, 'row'> {
294
378
  const shown = labels.map(() => false)
295
379
  const short = labels.map(() => false)
296
380
  const shift = labels.map(() => 0)
@@ -299,24 +383,31 @@ function placeOnce(
299
383
  const order = labels
300
384
  .map((_, i) => i)
301
385
  .toSorted((a, b) => rank(labels[a] as RowLabel) - rank(labels[b] as RowLabel))
302
- // The gap step applies when either label of a pair is a tick; zone names keep to "not touching".
303
- const clears = (b: LabelBox, tick: boolean, self = -1) =>
304
- !kept.some(({ box: k, tick: kTick, i: ki }) => {
305
- if (ki === self) return false
306
- const pad = tick || kTick ? gap : 0
307
- return b.left < k.right + pad - LABEL_SUBPIXEL && k.left < b.right + pad - LABEL_SUBPIXEL
308
- })
386
+ // Every pair of labels keeps the gap step apart, a zone name from its neighbour as a tick does.
387
+ const clears = (b: LabelBox) =>
388
+ !kept.some(
389
+ ({ box: k }) =>
390
+ b.left < k.right + gap - LABEL_SUBPIXEL && k.left < b.right + gap - LABEL_SUBPIXEL,
391
+ )
309
392
  for (const i of order) {
310
393
  const label = labels[i] as RowLabel
311
394
  const { span } = label
312
395
  const tick = span === undefined && !label.holding
313
396
  let box = label.box
397
+ if (tick) {
398
+ // A tick may sit past the row's end (a wide label hugging a mark in the near half): it is
399
+ // shifted inside the row, as the holding name is, rather than lost.
400
+ const width = box.right - box.left
401
+ const left = Math.max(0, Math.min(box.left, rowWidth - width))
402
+ shift[i] = left - box.left
403
+ box = { left, right: left + width }
404
+ }
314
405
  if (label.holding) {
315
406
  const width = box.right - box.left
316
407
  const left = Math.max(0, Math.min(box.left, rowWidth - width))
317
408
  shift[i] = left - box.left
318
409
  box = { left, right: left + width }
319
- if (!clears(box, false)) continue
410
+ if (!clears(box)) continue
320
411
  shown[i] = true
321
412
  kept.push({ box, tick: false, i })
322
413
  continue
@@ -326,25 +417,29 @@ function placeOnce(
326
417
  b.right <= rowWidth + LABEL_SUBPIXEL &&
327
418
  (span === undefined ||
328
419
  (b.left >= span.left - LABEL_SUBPIXEL && b.right <= span.right + LABEL_SUBPIXEL)) &&
329
- clears(b, tick)
420
+ clears(b)
330
421
  const useShort =
331
422
  label.short !== undefined && (registerShort ? true : !fits(box)) && fits(label.short)
332
423
  if (registerShort && label.short !== undefined && !useShort) continue
333
424
  if (useShort) box = label.short as LabelBox
334
- else if (!fits(box)) continue
425
+ else if (!fits(box)) {
426
+ shift[i] = 0
427
+ continue
428
+ }
335
429
  shown[i] = true
336
430
  short[i] = useShort
337
431
  kept.push({ box, tick, i })
338
432
  }
339
433
  // A name that did not fit inside its own span borrows room from the row: the zone names beside it
340
- // slide inside THEIR spans, away from it, and it takes the gap, staying centred in its span. Full
341
- // name first, then `short`; holding names and ticks never move.
434
+ // (the holding one included) slide away from it and it takes the gap. The rule is D38's: a name's
435
+ // CENTRE stays inside its own span, so a slid name may overhang a span edge by up to half its width,
436
+ // never past its centre. Full name first, then `short`; ticks never move.
342
437
  for (const [i, label] of labels.entries()) {
343
438
  if (label.span === undefined || label.holding || (shown[i] && !short[i])) continue
344
439
  const { span } = label
345
440
  const centre = (b: LabelBox) => (b.left + b.right) / 2
346
441
  const others = kept.filter((k) => k.i !== i)
347
- const movable = (k: (typeof kept)[number]) => !k.tick && !labels[k.i]?.holding
442
+ const movable = (k: (typeof kept)[number]) => !k.tick
348
443
  const fixed = others.filter((k) => !movable(k))
349
444
  const slidable = others.filter(movable)
350
445
  const base = registerShort && label.short ? label.short : label.box
@@ -360,7 +455,7 @@ function placeOnce(
360
455
  )
361
456
  continue
362
457
  const rightOf = slidable
363
- .filter((k) => centre(k.box) > mid)
458
+ .filter((k) => centre(k.box) >= mid)
364
459
  .toSorted((a, b) => b.box.left - a.box.left)
365
460
  const leftOf = slidable
366
461
  .filter((k) => centre(k.box) < mid)
@@ -377,42 +472,38 @@ function placeOnce(
377
472
  const w = k.box.right - k.box.left
378
473
  const stop = right
379
474
  ? Math.min(
380
- (labels[k.i]?.span?.right ?? rowWidth) + LABEL_SUBPIXEL,
475
+ (labels[k.i]?.span?.right ?? rowWidth) + w / 2 + LABEL_SUBPIXEL,
381
476
  hi,
382
- ...fixed
383
- .filter((f) => f.box.left >= k.box.left)
384
- .map((f) => f.box.left - (f.tick ? gap : 0)),
477
+ ...fixed.filter((f) => f.box.left >= k.box.left).map((f) => f.box.left - gap),
385
478
  )
386
479
  : k.box.left + w
387
480
  const at = Math.max(k.box.left, stop - w)
388
481
  slides.set(k.i, at - k.box.left)
389
- hi = at
482
+ hi = at - gap
390
483
  }
391
484
  let lo = 0
392
485
  for (const k of leftOf) {
393
486
  const w = k.box.right - k.box.left
394
487
  const stop = left
395
488
  ? Math.max(
396
- (labels[k.i]?.span?.left ?? 0) - LABEL_SUBPIXEL,
489
+ (labels[k.i]?.span?.left ?? 0) - w / 2 - LABEL_SUBPIXEL,
397
490
  lo,
398
- ...fixed
399
- .filter((f) => f.box.right <= k.box.right)
400
- .map((f) => f.box.right + (f.tick ? gap : 0)),
491
+ ...fixed.filter((f) => f.box.right <= k.box.right).map((f) => f.box.right + gap),
401
492
  )
402
493
  : k.box.left
403
494
  const at = Math.min(k.box.left, stop)
404
495
  slides.set(k.i, at - k.box.left)
405
- lo = at + w
496
+ lo = at + w + gap
406
497
  }
407
498
  // The gap this name may take: after the nearest left neighbour and fixed label, before the right.
408
499
  const gapLo = Math.max(
409
500
  lo,
410
- ...fixed.filter((f) => f.box.right <= mid).map((f) => f.box.right + (f.tick ? gap : 0)),
501
+ ...fixed.filter((f) => f.box.right <= mid).map((f) => f.box.right + gap),
411
502
  0,
412
503
  )
413
504
  const gapHi = Math.min(
414
505
  hi,
415
- ...fixed.filter((f) => f.box.left >= mid).map((f) => f.box.left - (f.tick ? gap : 0)),
506
+ ...fixed.filter((f) => f.box.left >= mid).map((f) => f.box.left - gap),
416
507
  rowWidth,
417
508
  )
418
509
  return { slides, gapLo, gapHi }
@@ -18,6 +18,7 @@ import {
18
18
  zoneFill,
19
19
  } from './meter-model'
20
20
  import type {
21
+ LabelBox,
21
22
  MarkItem,
22
23
  MeterSegment,
23
24
  MeterTick,
@@ -246,13 +247,22 @@ function zoneStyle(zone: MeterZone, index: number, count: number, min: number, s
246
247
  const columnGapOf = (el: Element): number => Number.parseFloat(getComputedStyle(el).columnGap) || 0
247
248
 
248
249
  /** One label on the row: its span in percent of the row (a zone name) or none (an end tick). */
249
- type RowSlot = { spanPct?: readonly [number, number]; holding?: boolean; end?: boolean }
250
+ type RowSlot = {
251
+ spanPct?: readonly [number, number]
252
+ holding?: boolean
253
+ end?: boolean
254
+ figure?: boolean
255
+ }
250
256
 
251
- type RowLayout = { shown: boolean[]; short: boolean[]; shift: number[] }
257
+ type RowLayout = { shown: boolean[]; short: boolean[]; shift: number[]; row: (0 | 1)[] }
252
258
  const sameLayout = (a: RowLayout, b: RowLayout) =>
253
259
  a.shown.length === b.shown.length &&
254
260
  a.shown.every(
255
- (v, i) => v === b.shown[i] && a.short[i] === b.short[i] && a.shift[i] === b.shift[i],
261
+ (v, i) =>
262
+ v === b.shown[i] &&
263
+ a.short[i] === b.short[i] &&
264
+ a.shift[i] === b.shift[i] &&
265
+ a.row[i] === b.row[i],
256
266
  )
257
267
 
258
268
  /**
@@ -268,7 +278,7 @@ function useRowLabelLayout(
268
278
  shortOf: readonly number[],
269
279
  key: string,
270
280
  ): RowLayout {
271
- const [layout, setLayout] = useState<RowLayout>({ shown: [], short: [], shift: [] })
281
+ const [layout, setLayout] = useState<RowLayout>({ shown: [], short: [], shift: [], row: [] })
272
282
  const slotsRef = useRef(slots)
273
283
  const shortOfRef = useRef(shortOf)
274
284
  // Declared before the measuring effect so it reads this render's slots.
@@ -286,30 +296,41 @@ function useRowLabelLayout(
286
296
  const rtl = getComputedStyle(row).direction === 'rtl'
287
297
  const edge = (pct: number) => (origin.width * (rtl ? 100 - pct : pct)) / 100
288
298
  const children = [...row.children]
289
- const labels: RowLabel[] = children.slice(0, slotsRef.current.length).map((c, i) => {
299
+ // A label's natural box: the physical offset a previous placement applied (`--basalt-nudge`) is
300
+ // taken back out, so a re-measure (a resize, a font swap, the second row changing the height)
301
+ // never compounds it.
302
+ const naturalBox = (c: Element): LabelBox => {
290
303
  const r = c.getBoundingClientRect()
304
+ const by =
305
+ Number.parseFloat((c as HTMLElement).style.getPropertyValue('--basalt-nudge')) || 0
306
+ return { left: r.left - origin.left - by, right: r.right - origin.left - by }
307
+ }
308
+ const labels: RowLabel[] = children.slice(0, slotsRef.current.length).map((c, i) => {
291
309
  const slot = slotsRef.current[i]
292
310
  const pct = slot?.spanPct
293
311
  const [a, b] = pct ? [edge(pct[0]), edge(pct[1])] : []
294
312
  return {
295
- box: { left: r.left - origin.left, right: r.right - origin.left },
313
+ box: naturalBox(c),
296
314
  ...(a !== undefined &&
297
315
  b !== undefined && { span: { left: Math.min(a, b), right: Math.max(a, b) } }),
298
316
  ...(slot?.holding && { holding: true }),
299
317
  ...(slot?.end && { end: true }),
318
+ ...(slot?.figure && { figure: true }),
300
319
  }
301
320
  })
302
321
  shortOfRef.current.forEach((labelIndex, n) => {
303
- const r = children[slotsRef.current.length + n]?.getBoundingClientRect()
322
+ const twin = children[slotsRef.current.length + n]
304
323
  const label = labels[labelIndex]
305
- if (r && label) label.short = { left: r.left - origin.left, right: r.right - origin.left }
324
+ if (twin && label) label.short = naturalBox(twin)
306
325
  })
307
326
  const placed = placeRowLabels(labels, origin.width, columnGapOf(row))
308
- // The hook measures on the physical x axis; the margin is logical, so it flips with `dir`.
327
+ // Measured and applied on the physical x axis (`--basalt-nudge` is a physical `translate`), so
328
+ // it needs no flip under `dir="rtl"`.
309
329
  const next = {
310
330
  shown: placed.shown,
311
331
  short: placed.short,
312
- shift: placed.shift.map((v) => (rtl ? -v : v)),
332
+ shift: placed.shift,
333
+ row: placed.row,
313
334
  }
314
335
  setLayout((prev) => (sameLayout(prev, next) ? prev : next))
315
336
  }
@@ -378,15 +399,20 @@ function ZoneReading({ pct, children }: { pct: number; children: ReactNode }) {
378
399
  const TICK_LABEL_FLIP_PCT = 50
379
400
 
380
401
  /**
381
- * The one label row: each zone name centred under its own span and the labelled ticks hugging their
382
- * marks (the end ticks sit at the row's ends), all on one baseline. A name wider than its span, or
383
- * clashing with a tick or a neighbour, hides (still laid out, so a resize can measure it again) and is
384
- * still named in the described-by text; the holding zone is ink and heavier. Decorative.
402
+ * The label row: each zone name centred under its own span and the labelled ticks hugging their
403
+ * marks (the end ticks sit at the row's ends), all on one baseline. A label that cannot hold its place
404
+ * there stands on a second row directly under it, only while one needs it; it hides (still laid out,
405
+ * so a resize can measure it again, and named in the described-by text) when neither row holds it.
406
+ * The holding zone is ink and heavier. Decorative.
385
407
  */
386
408
  function LabelRow({ zones, ticks }: { zones: readonly ZoneLabel[]; ticks: readonly TickMark[] }) {
387
409
  const rowRef = useRef<HTMLDivElement>(null)
388
410
  const slots: RowSlot[] = [
389
- ...ticks.map((t): RowSlot => (t.pct <= 0 || t.pct >= 100 ? { end: true } : {})),
411
+ // The scale's own start figure (key `start`) drops before it wraps: it never takes the second row.
412
+ ...ticks.map((t): RowSlot => ({
413
+ ...(t.pct <= 0 || t.pct >= 100 ? { end: true } : {}),
414
+ ...(t.key === 'start' && { figure: true }),
415
+ })),
390
416
  ...zones.map((z): RowSlot => ({
391
417
  spanPct: [z.centrePct - z.widthPct / 2, z.centrePct + z.widthPct / 2],
392
418
  holding: z.holding,
@@ -398,19 +424,24 @@ function LabelRow({ zones, ticks }: { zones: readonly ZoneLabel[]; ticks: readon
398
424
  ].join('\u0000')
399
425
  // The holding name never shortens, so its zone has no twin.
400
426
  const twins = zones.flatMap((z, i) => (z.short !== undefined && !z.holding ? [i] : []))
401
- const { shown, short, shift } = useRowLabelLayout(
427
+ const { shown, short, shift, row } = useRowLabelLayout(
402
428
  rowRef,
403
429
  slots,
404
430
  twins.map((i) => ticks.length + i),
405
431
  `${key}\u0001${twins.map((i) => zones[i]?.short).join('\u0000')}`,
406
432
  )
407
433
  const hidden = (i: number) => (shown[i] === false || short[i] === true ? '' : undefined)
434
+ // The second row exists only while a shown label stands on it.
435
+ const second = (i: number) => (shown[i] === true && row[i] === 1 ? '' : undefined)
436
+ const rows = row.some((r, i) => r === 1 && shown[i] === true) ? 2 : 1
437
+ // theme-allow hidden-inline-style — the placement's offset is measured data, applied by `translate`
408
438
  const nudge = (i: number): CSSProperties =>
409
- shift[i] ? { marginInlineStart: `${shift[i]}px` } : {}
439
+ shift[i] ? ({ '--basalt-nudge': `${shift[i]}px` } as CSSProperties) : {}
410
440
  return (
411
441
  <div
412
442
  ref={rowRef}
413
443
  className={classes.labelRow}
444
+ data-meter-rows={rows}
414
445
  {...(zones.length > 0 ? { 'data-meter-zone-band': '' } : { 'data-meter-tick-band': '' })}
415
446
  aria-hidden
416
447
  >
@@ -420,10 +451,11 @@ function LabelRow({ zones, ticks }: { zones: readonly ZoneLabel[]; ticks: readon
420
451
  className={classes.rowLabel}
421
452
  data-meter-tick-label=""
422
453
  data-hidden={hidden(i)}
454
+ data-row2={second(i)}
423
455
  style={
424
456
  t.pct > TICK_LABEL_FLIP_PCT
425
- ? { insetInlineEnd: `${100 - t.pct}%` }
426
- : { insetInlineStart: `${t.pct}%` }
457
+ ? { insetInlineEnd: `${100 - t.pct}%`, ...nudge(i) }
458
+ : { insetInlineStart: `${t.pct}%`, ...nudge(i) }
427
459
  }
428
460
  >
429
461
  {t.label}
@@ -436,6 +468,7 @@ function LabelRow({ zones, ticks }: { zones: readonly ZoneLabel[]; ticks: readon
436
468
  data-meter-zone-label={z.label}
437
469
  data-holding={z.holding || undefined}
438
470
  data-hidden={hidden(ticks.length + i)}
471
+ data-row2={second(ticks.length + i)}
439
472
  style={{ insetInlineStart: `${z.centrePct}%`, ...nudge(ticks.length + i) }}
440
473
  >
441
474
  {z.label}
@@ -450,6 +483,7 @@ function LabelRow({ zones, ticks }: { zones: readonly ZoneLabel[]; ticks: readon
450
483
  className={cx(classes.rowLabel, classes.zoneLabel)}
451
484
  data-meter-zone-short={z.label}
452
485
  data-hidden={short[i] === true ? undefined : ''}
486
+ data-row2={second(i)}
453
487
  style={{ insetInlineStart: `${z.centrePct}%`, ...nudge(i) }}
454
488
  >
455
489
  {z.short}
@@ -101,10 +101,21 @@
101
101
  --basalt-reading-dir: -1;
102
102
  }
103
103
 
104
+ /* A label that cannot hold its place on the first row stands on a second, directly under it. */
105
+ .labelRow[data-meter-rows='2'] {
106
+ height: 2.5em;
107
+ }
108
+
109
+ .rowLabel[data-row2] {
110
+ top: 1.25em;
111
+ }
112
+
104
113
  .rowLabel {
105
114
  position: absolute;
106
115
  top: 0;
107
116
  white-space: nowrap;
117
+ /* The placement's physical offset (a label kept inside the row): both anchors, either direction. */
118
+ translate: var(--basalt-nudge, 0px) 0;
108
119
  }
109
120
 
110
121
  .rowLabel[data-hidden] {
@@ -113,7 +124,7 @@
113
124
 
114
125
  /* A zone name is centred on its span: the physical `translate` flips sign in RTL. */
115
126
  .zoneLabel {
116
- translate: calc(-50% * var(--basalt-reading-dir)) 0;
127
+ translate: calc(-50% * var(--basalt-reading-dir) + var(--basalt-nudge, 0px)) 0;
117
128
  }
118
129
 
119
130
  .zoneLabelHolding {
@@ -66,11 +66,22 @@
66
66
  * every cell of a row (one caption line; a reserved qualifier line in a narrow cell when any cell has
67
67
  * a qualifier), so a row shares one caption top and one value baseline. Below the value each cell
68
68
  * stacks its own parts and ends at its last one. */
69
- .content > * {
69
+ /* The HEAD (caption line, qualifier, value row) is one block with its own height: a longer qualifier
70
+ * pushes only its own value down. A visual cell whose head is shorter than a sibling's visual offset
71
+ * grows its head's min-height (`alignVisuals`), so the drawn visuals of a row share one top. */
72
+ .head {
73
+ display: flex;
74
+ flex-direction: column;
75
+ min-width: 0;
76
+ }
77
+
78
+ .content > *,
79
+ .head > * {
70
80
  min-width: 0;
71
81
  }
72
82
 
73
- .content > :not(.captionLine) {
83
+ .content > :not(.head),
84
+ .head > :not(.captionLine) {
74
85
  margin-block-start: var(--vx-space-stack-sm);
75
86
  }
76
87
 
@@ -272,7 +283,6 @@
272
283
  .visual:has([data-basalt-sparkline], [data-basalt-chart]) {
273
284
  padding-top: max(0px, calc(var(--basalt-spark-readout-h) - var(--vx-space-stack-sm)));
274
285
  }
275
-
276
286
  /* The state copy keeps the cell's inset on every side: the article ends at its own last track, so the
277
287
  * foot inset (carried by the row-high slot, further down) cannot reach it. */
278
288
  .content[data-strip-has-state] {
@@ -10,6 +10,7 @@ declare const classes: {
10
10
  readonly content: string
11
11
  readonly action: string
12
12
  readonly captionLine: string
13
+ readonly head: string
13
14
  readonly captionMain: string
14
15
  readonly lead: string
15
16
  readonly caption: string