basalt-ui 2.22.0 → 2.24.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 (169) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/MIGRATING.md +400 -12
  3. package/agent/agents/basalt-design-reviewer.md +3 -3
  4. package/agent/skills/basalt-design/SKILL.md +2 -2
  5. package/agent/skills/basalt-design/references/catalogue.md +22 -19
  6. package/agent/skills/basalt-design/references/states.md +39 -0
  7. package/dist/charts/BandStrip.d.ts.map +1 -1
  8. package/dist/charts/BandStrip.js +51 -5
  9. package/dist/charts/Bars.d.ts.map +1 -1
  10. package/dist/charts/Bars.js +19 -3
  11. package/dist/charts/CartesianChart.d.ts +2 -0
  12. package/dist/charts/CartesianChart.d.ts.map +1 -1
  13. package/dist/charts/CartesianChart.js +7 -3
  14. package/dist/charts/ChartCard.d.ts +1 -1
  15. package/dist/charts/ChartCard.d.ts.map +1 -1
  16. package/dist/charts/ChartCard.js +25 -15
  17. package/dist/charts/Sparkline.d.ts +8 -2
  18. package/dist/charts/Sparkline.d.ts.map +1 -1
  19. package/dist/charts/Sparkline.js +95 -15
  20. package/dist/charts/StackedArea.d.ts.map +1 -1
  21. package/dist/charts/StackedArea.js +5 -1
  22. package/dist/charts/band-fold.d.ts +22 -4
  23. package/dist/charts/band-fold.d.ts.map +1 -1
  24. package/dist/charts/band-fold.js +10 -2
  25. package/dist/charts/chart-dates.d.ts +12 -0
  26. package/dist/charts/chart-dates.d.ts.map +1 -0
  27. package/dist/charts/chart-dates.js +16 -0
  28. package/dist/charts/chart-legend.module.css +16 -0
  29. package/dist/charts/compile.d.ts +10 -0
  30. package/dist/charts/compile.d.ts.map +1 -1
  31. package/dist/charts/compile.js +77 -24
  32. package/dist/charts/date-tick-format.d.ts +5 -5
  33. package/dist/charts/date-tick-format.d.ts.map +1 -1
  34. package/dist/charts/date-tick-format.js +3 -7
  35. package/dist/charts/labels.d.ts +7 -3
  36. package/dist/charts/labels.d.ts.map +1 -1
  37. package/dist/charts/labels.js +28 -17
  38. package/dist/charts/legend.d.ts +0 -6
  39. package/dist/charts/legend.d.ts.map +1 -1
  40. package/dist/charts/legend.js +87 -35
  41. package/dist/charts/ref-label.d.ts +17 -0
  42. package/dist/charts/ref-label.d.ts.map +1 -1
  43. package/dist/charts/ref-label.js +13 -1
  44. package/dist/charts/sparkline.module.css +62 -12
  45. package/dist/charts/sparkline.module.css.d.ts +1 -0
  46. package/dist/charts/state.d.ts.map +1 -1
  47. package/dist/charts/state.js +4 -2
  48. package/dist/charts/types.d.ts +15 -3
  49. package/dist/charts/types.d.ts.map +1 -1
  50. package/dist/cli/shot/capture.d.ts.map +1 -1
  51. package/dist/cli/shot/capture.js +9 -5
  52. package/dist/cli/shot/detect.js +8 -5
  53. package/dist/cli/shot/measure.js +19 -23
  54. package/dist/cli/shot/playwright.d.ts +8 -0
  55. package/dist/cli/shot/playwright.d.ts.map +1 -1
  56. package/dist/cli/shot/playwright.js +5 -1
  57. package/dist/cli/shot/types.d.ts +2 -0
  58. package/dist/cli/shot/types.d.ts.map +1 -1
  59. package/dist/cli/tokens-css.d.ts +5 -7
  60. package/dist/cli/tokens-css.d.ts.map +1 -1
  61. package/dist/cli/tokens-css.js +11 -12
  62. package/dist/dashboard/bar-list.d.ts +6 -1
  63. package/dist/dashboard/bar-list.d.ts.map +1 -1
  64. package/dist/dashboard/bar-list.js +8 -0
  65. package/dist/dashboard/bar-list.module.css +18 -0
  66. package/dist/dashboard/delta-badge.d.ts.map +1 -1
  67. package/dist/dashboard/delta-badge.js +2 -2
  68. package/dist/dashboard/empty-state.d.ts.map +1 -1
  69. package/dist/dashboard/meter-model.d.ts +55 -21
  70. package/dist/dashboard/meter-model.d.ts.map +1 -1
  71. package/dist/dashboard/meter-model.js +98 -17
  72. package/dist/dashboard/meter-parts.d.ts +9 -2
  73. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  74. package/dist/dashboard/meter-parts.js +99 -21
  75. package/dist/dashboard/meter.d.ts +21 -8
  76. package/dist/dashboard/meter.d.ts.map +1 -1
  77. package/dist/dashboard/meter.js +14 -3
  78. package/dist/dashboard/meter.module.css +57 -2
  79. package/dist/dashboard/meter.module.css.d.ts +5 -0
  80. package/dist/dashboard/query-state.d.ts +7 -3
  81. package/dist/dashboard/query-state.d.ts.map +1 -1
  82. package/dist/dashboard/query-state.js +15 -7
  83. package/dist/dashboard/section.d.ts +2 -1
  84. package/dist/dashboard/section.d.ts.map +1 -1
  85. package/dist/dashboard/section.module.css +12 -0
  86. package/dist/dashboard/settings-section.d.ts +1 -1
  87. package/dist/dashboard/settings-section.d.ts.map +1 -1
  88. package/dist/dashboard/settings-section.js +3 -3
  89. package/dist/dashboard/stat-card-parts.js +1 -1
  90. package/dist/dashboard/stat-card.module.css +43 -10
  91. package/dist/dashboard/stat-group.module.css +15 -0
  92. package/dist/dashboard/stat-strip.d.ts +16 -5
  93. package/dist/dashboard/stat-strip.d.ts.map +1 -1
  94. package/dist/dashboard/stat-strip.js +100 -74
  95. package/dist/dashboard/stat-strip.module.css +68 -62
  96. package/dist/dashboard/stat-strip.module.css.d.ts +1 -0
  97. package/dist/dashboard/widget-header.d.ts.map +1 -1
  98. package/dist/dashboard/widget-header.js +19 -1
  99. package/dist/dashboard/widget-header.module.css +6 -0
  100. package/dist/guard/index.d.ts +2 -2
  101. package/dist/guard/index.d.ts.map +1 -1
  102. package/dist/guard/index.js +1 -17
  103. package/dist/styles.css +18 -11
  104. package/dist/table/data-table.js +1 -1
  105. package/dist/theme/controls.module.css +19 -0
  106. package/dist/theme/controls.module.css.d.ts +1 -0
  107. package/dist/theme/index.d.ts.map +1 -1
  108. package/dist/theme/index.js +12 -7
  109. package/dist/theme/status-verdict.d.ts.map +1 -1
  110. package/dist/theme/status-verdict.js +9 -1
  111. package/dist/tokens/palette.d.ts +7 -10
  112. package/dist/tokens/palette.d.ts.map +1 -1
  113. package/dist/tokens/palette.js +7 -10
  114. package/llms.txt +7 -4
  115. package/package.json +1 -1
  116. package/src/charts/BandStrip.tsx +55 -5
  117. package/src/charts/Bars.tsx +31 -3
  118. package/src/charts/CartesianChart.tsx +9 -3
  119. package/src/charts/ChartCard.tsx +32 -19
  120. package/src/charts/Sparkline.tsx +139 -21
  121. package/src/charts/StackedArea.tsx +12 -1
  122. package/src/charts/band-fold.ts +25 -6
  123. package/src/charts/chart-dates.ts +30 -0
  124. package/src/charts/chart-legend.module.css +16 -0
  125. package/src/charts/compile.ts +148 -25
  126. package/src/charts/date-tick-format.ts +10 -13
  127. package/src/charts/labels.ts +43 -22
  128. package/src/charts/legend.tsx +70 -3
  129. package/src/charts/ref-label.ts +27 -0
  130. package/src/charts/sparkline.module.css +62 -12
  131. package/src/charts/sparkline.module.css.d.ts +1 -0
  132. package/src/charts/state.tsx +7 -4
  133. package/src/charts/types.ts +13 -3
  134. package/src/cli/shot/capture.ts +15 -3
  135. package/src/cli/shot/detect.ts +9 -9
  136. package/src/cli/shot/measure.ts +27 -27
  137. package/src/cli/shot/playwright.ts +18 -2
  138. package/src/cli/shot/types.ts +2 -0
  139. package/src/cli/tokens-css.ts +18 -20
  140. package/src/dashboard/bar-list.module.css +18 -0
  141. package/src/dashboard/bar-list.tsx +29 -0
  142. package/src/dashboard/delta-badge.tsx +6 -2
  143. package/src/dashboard/empty-state.tsx +4 -2
  144. package/src/dashboard/meter-model.ts +181 -47
  145. package/src/dashboard/meter-parts.tsx +187 -30
  146. package/src/dashboard/meter.module.css +57 -2
  147. package/src/dashboard/meter.module.css.d.ts +5 -0
  148. package/src/dashboard/meter.tsx +33 -10
  149. package/src/dashboard/query-state.tsx +20 -9
  150. package/src/dashboard/section.module.css +12 -0
  151. package/src/dashboard/section.tsx +2 -1
  152. package/src/dashboard/settings-section.tsx +4 -4
  153. package/src/dashboard/stat-card-parts.tsx +1 -1
  154. package/src/dashboard/stat-card.module.css +43 -10
  155. package/src/dashboard/stat-group.module.css +15 -0
  156. package/src/dashboard/stat-strip.module.css +68 -62
  157. package/src/dashboard/stat-strip.module.css.d.ts +1 -0
  158. package/src/dashboard/stat-strip.tsx +118 -59
  159. package/src/dashboard/widget-header.module.css +6 -0
  160. package/src/dashboard/widget-header.tsx +28 -1
  161. package/src/guard/index.ts +3 -19
  162. package/src/styles.css +18 -11
  163. package/src/table/data-table.tsx +1 -1
  164. package/src/theme/__snapshots__/plain-theme.test.ts.snap +14 -8
  165. package/src/theme/controls.module.css +19 -0
  166. package/src/theme/controls.module.css.d.ts +1 -0
  167. package/src/theme/index.ts +12 -7
  168. package/src/theme/status-verdict.tsx +5 -1
  169. package/src/tokens/palette.ts +7 -10
@@ -4,13 +4,15 @@
4
4
  * generous vertical padding for a full-page empty state; `tier="section"` is compact, for an
5
5
  * empty card/panel region.
6
6
  *
7
+ * Its copy says the cause and the next step, never a bare "No data".
8
+ *
7
9
  * @example
8
10
  * import { EmptyState } from 'basalt-ui'
9
11
  *
10
12
  * <EmptyState
11
13
  * icon={<IconInboxEmpty />}
12
- * title="No results"
13
- * description="Try adjusting your filters or search terms."
14
+ * title="No decisions match"
15
+ * description="The queue is filtered to Blocked; clear it to see all 12."
14
16
  * action={<Button onClick={onReset}>Clear filters</Button>}
15
17
  * tier="section"
16
18
  * />
@@ -10,8 +10,8 @@ export type MeterSegment = {
10
10
  label: string
11
11
  color?: string
12
12
  /**
13
- * `'muted'`: the part takes a step of the muted ramp (a lightness step per muted part). A `color`
14
- * wins. Steps stay ≥15 points apart up to five muted parts; more merge at the cap.
13
+ * `'muted'`: the part takes the one flat muted fill every muted part shares, a hairline seam
14
+ * between neighbours (never a lightness step). A `color` wins.
15
15
  */
16
16
  tone?: 'muted'
17
17
  forecast?: boolean
@@ -21,8 +21,8 @@ export type MeterZone = {
21
21
  to: number
22
22
  label: string
23
23
  /**
24
- * `'muted'`: the band takes its step of the muted ramp instead of the line ramp. A `color` wins.
25
- * Steps stay ≥15 points apart up to six zones; more merge at the cap.
24
+ * `'muted'`: the band takes the one flat muted fill every muted zone shares, a hairline gap
25
+ * between neighbours (never a lightness step): a non-verdict band is not a value bar. A `color` wins.
26
26
  */
27
27
  tone?: 'muted'
28
28
  /**
@@ -32,22 +32,27 @@ export type MeterZone = {
32
32
  short?: string
33
33
  color?: string
34
34
  }
35
- /** A mark on the track at `at` (in the reading's units). A label is captioned beside it. */
36
- export type MeterTick = { at: number; label?: string }
35
+ /**
36
+ * A mark on the track at `at` (in the reading's units). A `label` is captioned beside it, above the
37
+ * track; a `caption` sits under the track, centred on the mark and clamped inside the track.
38
+ */
39
+ export type MeterTick = { at: number; label?: string; caption?: string }
37
40
 
38
41
  /** A colour of a `stops` gradient at `at` (in the reading's units); the colour is the consumer's. */
39
42
  type MeterStop = { at: number; color: string }
40
43
 
41
44
  /** A ramp step's strength in percent of its neutral: from `lo`, spread over `step` across the steps. */
42
45
  export const RAMP_ZONE = { lo: 15, step: 45, solo: 40 }
43
- export const RAMP_PART = { lo: 35, step: 50, solo: 60 }
46
+ /** The flat muted fill, in percent of `--vx-muted`: a zone band (argo's alpha) and a segment (clears 3:1 on the track). */
47
+ const MUTED_ZONE = 35
48
+ const MUTED_PART = 65
44
49
  /** Neighbouring steps stay at least this far apart (the contrast the zone ramp always had), up to `RAMP_MAX`. */
45
50
  export const RAMP_MIN_STEP = 15
46
51
  export const RAMP_MAX = 95
47
52
 
48
53
  /**
49
54
  * Step `index` of `count`. The spread is floored at `RAMP_MIN_STEP` per step, so a ramp stays
50
- * distinguishable by lightness up to six zones (five segments); past that the last steps clamp at
55
+ * distinguishable by lightness up to six zones; past that the last steps clamp at
51
56
  * `RAMP_MAX` and merge, which is the cue to group parts.
52
57
  */
53
58
  export function rampStrength(index: number, count: number, ramp: typeof RAMP_ZONE): number {
@@ -55,22 +60,26 @@ export function rampStrength(index: number, count: number, ramp: typeof RAMP_ZON
55
60
  return Math.min(RAMP_MAX, ramp.lo + index * Math.max(RAMP_MIN_STEP, ramp.step / (count - 1)))
56
61
  }
57
62
 
58
- function rampFill(
59
- base: 'line' | 'muted',
60
- index: number,
61
- count: number,
62
- ramp: typeof RAMP_ZONE,
63
- ): string {
63
+ function rampFill(base: 'line', index: number, count: number, ramp: typeof RAMP_ZONE): string {
64
64
  return `color-mix(in srgb, var(--vx-${base}) ${rampStrength(index, count, ramp)}%, transparent)`
65
65
  }
66
66
 
67
- /** A zone's fill: its own colour, else its step of the line (or, `tone="muted"`, the muted) ramp. */
67
+ const mutedFill = (pct: number) => `color-mix(in srgb, var(--vx-muted) ${pct}%, transparent)`
68
+
69
+ /** A zone's fill: its own colour, else the one flat muted fill (`tone="muted"`), else its step of the line ramp. */
68
70
  export function zoneFill(zone: MeterZone, index: number, count: number): string {
69
- return zone.color ?? rampFill(zone.tone === 'muted' ? 'muted' : 'line', index, count, RAMP_ZONE)
71
+ if (zone.color !== undefined) return zone.color
72
+ return zone.tone === 'muted' ? mutedFill(MUTED_ZONE) : rampFill('line', index, count, RAMP_ZONE)
70
73
  }
71
74
 
72
75
  /** One tick on the track: its percent position and what it says (caption `label`, aria `text`). */
73
- export type TickMark = { key: string; pct: number; label: string | undefined; text: string }
76
+ export type TickMark = {
77
+ key: string
78
+ pct: number
79
+ label: string | undefined
80
+ caption: string | undefined
81
+ text: string
82
+ }
74
83
 
75
84
  /** A fill on the track, as fractions (0–1) of the span, with the segment it was laid out from. */
76
85
  export type Part = {
@@ -78,6 +87,8 @@ export type Part = {
78
87
  readonly size: number
79
88
  readonly color: string | undefined
80
89
  readonly forecast: boolean
90
+ /** A muted part right after another muted part: a hairline seam keeps the shared fill apart. */
91
+ readonly seam: boolean
81
92
  readonly label: string
82
93
  /** The segment's own (normalised) amount, in the reading's units — what its caption states. */
83
94
  readonly amount: number
@@ -130,6 +141,7 @@ export function tickMarks(
130
141
  key: String(i),
131
142
  pct: toPct((t.at - min) / span),
132
143
  label: t.label,
144
+ caption: t.caption,
133
145
  text: `${t.label ?? 'Tick'} ${format(t.at)}`,
134
146
  },
135
147
  ]
@@ -197,8 +209,7 @@ export function normalizeSegments(segments: readonly MeterSegment[]): MeterSegme
197
209
  export function layoutSegments(segments: readonly MeterSegment[], span: number): Part[] {
198
210
  let cursor = 0
199
211
  const isMuted = (s: MeterSegment) => s.tone === 'muted' && s.color === undefined && !s.forecast
200
- const mutedCount = segments.filter(isMuted).length
201
- let muted = 0
212
+ let prevMuted = false
202
213
  return segments.map((s) => {
203
214
  const size = Math.max(0, Math.min(clamp01(s.value / span), 1 - cursor))
204
215
  const forecast = s.forecast === true
@@ -206,16 +217,14 @@ export function layoutSegments(segments: readonly MeterSegment[], span: number):
206
217
  const part = {
207
218
  start: cursor,
208
219
  size,
209
- color: forecast
210
- ? undefined
211
- : isMuted(s)
212
- ? rampFill('muted', muted++, mutedCount, RAMP_PART)
213
- : s.color,
220
+ color: forecast ? undefined : isMuted(s) ? mutedFill(MUTED_PART) : s.color,
214
221
  forecast,
222
+ seam: isMuted(s) && prevMuted,
215
223
  label: s.label,
216
224
  amount: s.value,
217
225
  }
218
226
  cursor += size
227
+ prevMuted = isMuted(s)
219
228
  return part
220
229
  })
221
230
  }
@@ -240,7 +249,7 @@ export type RowLabel = {
240
249
  box: LabelBox
241
250
  span?: LabelBox
242
251
  holding?: boolean
243
- /** A tick at the track's start or end: it yields to every zone name it would touch. */
252
+ /** A tick at the track's start or end: it keeps its place over every zone name it would touch. */
244
253
  end?: boolean
245
254
  short?: LabelBox
246
255
  }
@@ -257,11 +266,12 @@ export type RowPlacement = {
257
266
  /**
258
267
  * Which labels of the row show, in input order. The holding zone's name claims its place first (it
259
268
  * is the reading's verdict, so it never hides for width: it may spill past its span, shifted inside
260
- * the row); the other ticks go next; every other zone name then shows only
261
- * when it lies inside the row and its own span and clears every label already kept, trying its full
262
- * name and then its `short` box; the end ticks (at the track's start or end) go last, so they yield
263
- * to any name they would touch. A tick keeps at least `gap` px (one gap step of the host's
264
- * spacing) from every other label; zone names among themselves only must not touch. A hidden name is still named in the described-by text.
269
+ * the row); the other ticks go next, then the end ticks (at the track's start or end: a scale's
270
+ * 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
272
+ * 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.
265
275
  */
266
276
  export function placeRowLabels(
267
277
  labels: readonly RowLabel[],
@@ -272,7 +282,7 @@ export function placeRowLabels(
272
282
  const short = labels.map(() => false)
273
283
  const shift = labels.map(() => 0)
274
284
  const kept: { box: LabelBox; tick: boolean }[] = []
275
- const rank = (l: RowLabel) => (l.holding ? 0 : l.end ? 3 : l.span ? 2 : 1)
285
+ const rank = (l: RowLabel) => (l.holding ? 0 : l.span ? 3 : l.end ? 2 : 1)
276
286
  const order = labels
277
287
  .map((_, i) => i)
278
288
  .toSorted((a, b) => rank(labels[a] as RowLabel) - rank(labels[b] as RowLabel))
@@ -310,6 +320,14 @@ export function placeRowLabels(
310
320
  short[i] = useShort
311
321
  kept.push({ box, tick })
312
322
  }
323
+ // One register: once any name falls back to its `short`, every other shown name that has one
324
+ // follows (the holding name, which never shortens, is the one exception). A shorter box only
325
+ // clears more, so the placement above stays valid.
326
+ if (short.some(Boolean)) {
327
+ for (const [i, label] of labels.entries()) {
328
+ if (shown[i] && !label.holding && label.short !== undefined) short[i] = true
329
+ }
330
+ }
313
331
  return { shown, short, shift }
314
332
  }
315
333
 
@@ -353,6 +371,8 @@ export type BarModel = {
353
371
  zoneLabels: readonly ZoneLabel[]
354
372
  ticks: readonly TickMark[]
355
373
  markerPct: number | undefined
374
+ /** Percent where the committed fill ends: the reading caption's anchor. Absent on a marked track. */
375
+ fillEndPct: number | undefined
356
376
  targetPct: number | undefined
357
377
  /** The default start caption (the zone is named on the band, so it is the bare reading); `startCaption` decides whether it shows. */
358
378
  startText: string
@@ -465,6 +485,12 @@ export function modelBar(input: BarInput): BarModel {
465
485
  }),
466
486
  ticks,
467
487
  markerPct: zoned || striped ? toPct((reading - min) / span) : undefined,
488
+ fillEndPct:
489
+ zoned || striped
490
+ ? undefined
491
+ : toPct(
492
+ parts.filter((p) => !p.forecast).reduce((end, p) => Math.max(end, p.start + p.size), 0),
493
+ ),
468
494
  targetPct: target === undefined ? undefined : toPct((target - min) / span),
469
495
  startText: format(reading),
470
496
  endText: endCaptionText(reading, target, format),
@@ -476,14 +502,34 @@ export function modelBar(input: BarInput): BarModel {
476
502
  export const ringFraction = (reading: number, min: number, max: number): number =>
477
503
  clamp01((reading - min) / spanOf(min, max).span)
478
504
 
479
- /** One caption on the marks row: its natural width and where it is anchored (`pct`), else its edge. */
480
- export type MarkItem = { width: number; pct?: number; edge?: 'end' }
505
+ /**
506
+ * One caption on the marks row: its natural width and where it is anchored (`pct`), else its edge.
507
+ * `align` is which edge of the caption sits on the mark (default: its centre). `drop` ranks it for
508
+ * dropping when the row is too narrow: the lowest rank goes first, none never drops.
509
+ */
510
+ export type MarkItem = {
511
+ width: number
512
+ pct?: number
513
+ edge?: 'end'
514
+ align?: 'start' | 'center' | 'end'
515
+ drop?: number
516
+ }
517
+
518
+ /** Where a caption starts before any caption is pushed off it: on its mark, else at its edge. */
519
+ function idealLeft(i: MarkItem, rowWidth: number): number {
520
+ if (i.pct === undefined) return i.edge === 'end' ? rowWidth - i.width : 0
521
+ const at = (rowWidth * i.pct) / 100
522
+ const left = i.align === 'end' ? at - i.width : i.align === 'start' ? at : at - i.width / 2
523
+ return Math.max(0, Math.min(left, rowWidth - i.width))
524
+ }
481
525
 
482
526
  /**
483
527
  * Where each caption of the marks row starts (px, logical start edge), index-aligned with `items`: an
484
528
  * anchored caption is centred on its mark and clamped inside the row, the others sit at the start (or,
485
- * `edge: 'end'`, the end). `null` when nothing is anchored, the row is unmeasured, a caption is wider
486
- * than the row, or two captions would sit closer than `gap`: the row then flows and wraps instead.
529
+ * `edge: 'end'`, the end). Two captions closer than `gap` are pushed apart, in order, the least that
530
+ * clears them, so a caption stays as near its mark as the row allows. `null` when nothing is anchored,
531
+ * the row is unmeasured, a caption is wider than the row, or the captions cannot all clear each other
532
+ * inside the row: the row then flows and wraps instead.
487
533
  */
488
534
  export function placeMarkCaptions(
489
535
  items: readonly MarkItem[],
@@ -492,19 +538,107 @@ export function placeMarkCaptions(
492
538
  ): number[] | null {
493
539
  if (rowWidth <= 0 || !items.some((i) => i.pct !== undefined)) return null
494
540
  if (items.some((i) => i.width > rowWidth)) return null
495
- const lefts = items.map((i) => {
496
- if (i.pct === undefined) return i.edge === 'end' ? rowWidth - i.width : 0
497
- return Math.max(0, Math.min((rowWidth * i.pct) / 100 - i.width / 2, rowWidth - i.width))
498
- })
499
- const order = lefts.map((_, i) => i).toSorted((a, b) => (lefts[a] ?? 0) - (lefts[b] ?? 0))
500
- const crowded = order.some((i, n) => {
501
- const next = order[n + 1]
502
- return (
503
- next !== undefined &&
504
- (lefts[next] ?? 0) < (lefts[i] ?? 0) + (items[i]?.width ?? 0) + gap - LABEL_SUBPIXEL
541
+ const lefts = items.map((i) => idealLeft(i, rowWidth))
542
+ // Captions keep their order along the track: by their mark (an edge caption by its edge), not by
543
+ // where clamping left them, so a reading near the end never ends up right of the target.
544
+ const markAt = (i: MarkItem) => i.pct ?? (i.edge === 'end' ? 100 : 0)
545
+ const order = lefts
546
+ .map((_, i) => i)
547
+ .toSorted((a, b) => markAt(items[a] as MarkItem) - markAt(items[b] as MarkItem) || a - b)
548
+ const widthOf = (i: number) => items[i]?.width ?? 0
549
+ for (const [n, i] of order.entries()) {
550
+ const prev = order[n - 1]
551
+ if (prev !== undefined)
552
+ lefts[i] = Math.max(lefts[i] ?? 0, (lefts[prev] ?? 0) + widthOf(prev) + gap)
553
+ }
554
+ for (const [n, i] of order.toReversed().entries()) {
555
+ const next = order.toReversed()[n - 1]
556
+ const limit = next === undefined ? rowWidth : (lefts[next] ?? 0) - gap
557
+ lefts[i] = Math.min(lefts[i] ?? 0, limit - widthOf(i))
558
+ }
559
+ const first = order[0]
560
+ return first !== undefined && (lefts[first] ?? 0) < -LABEL_SUBPIXEL ? null : lefts
561
+ }
562
+
563
+ /** The furthest a droppable caption may sit from its own mark (Wave 37 r2's "not far"); past it, it drops. */
564
+ export const MAX_CAPTION_SHIFT = 24
565
+
566
+ /**
567
+ * `placeMarkCaptions` with dropping: when the captions cannot all clear each other on one line, the
568
+ * droppable ones go one at a time in `drop` rank order (a caption without a rank never goes) until
569
+ * the rest fit. A droppable caption stays while it sits within `MAX_CAPTION_SHIFT` of its mark and
570
+ * moves no undroppable caption (the reading, the target) further than those move with every droppable
571
+ * one dropped: two undroppables that collide on their own are not a reason to drop anything.
572
+ * `starts[i]` is the start of caption `i`, `undefined` when it dropped; `dropped[i]` marks the drop.
573
+ * `null` when even the undroppable ones do not fit, so the row flows instead.
574
+ */
575
+ export function placeCaptionRow(
576
+ items: readonly MarkItem[],
577
+ rowWidth: number,
578
+ gap: number,
579
+ ): { starts: (number | undefined)[]; dropped: boolean[] } | null {
580
+ const fixed = items.map((_, i) => i).filter((i) => items[i]?.drop === undefined)
581
+ const baseline = placeMarkCaptions(
582
+ fixed.map((i) => items[i] as MarkItem),
583
+ rowWidth,
584
+ gap,
585
+ )
586
+ if (baseline === null) return null
587
+ const allowed = new Map(
588
+ fixed.map((i, n) => [
589
+ i,
590
+ Math.abs((baseline[n] ?? 0) - idealLeft(items[i] as MarkItem, rowWidth)) + LABEL_SUBPIXEL,
591
+ ]),
592
+ )
593
+ const dropped = items.map(() => false)
594
+ const order = items
595
+ .map((item, i) => ({ rank: item.drop, i }))
596
+ .filter((d): d is { rank: number; i: number } => d.rank !== undefined)
597
+ .toSorted((a, b) => a.rank - b.rank)
598
+ for (let step = 0; step <= order.length; step++) {
599
+ const kept = items.map((_, i) => i).filter((i) => !dropped[i])
600
+ const lefts = placeMarkCaptions(
601
+ kept.map((i) => items[i] as MarkItem),
602
+ rowWidth,
603
+ gap,
505
604
  )
506
- })
507
- return crowded ? null : lefts
605
+ const settled =
606
+ lefts !== null &&
607
+ (step === order.length ||
608
+ kept.every((i, n) => {
609
+ const item = items[i] as MarkItem
610
+ const moved = Math.abs((lefts[n] ?? 0) - idealLeft(item, rowWidth))
611
+ if (item.drop === undefined) return moved <= (allowed.get(i) ?? 0)
612
+ return item.pct === undefined || moved <= MAX_CAPTION_SHIFT
613
+ }))
614
+ if (lefts !== null && settled) {
615
+ const starts = items.map((): number | undefined => undefined)
616
+ for (const [n, i] of kept.entries()) starts[i] = lefts[n]
617
+ return { starts, dropped }
618
+ }
619
+ const next = order[step]
620
+ if (next === undefined) return null
621
+ dropped[next.i] = true
622
+ }
623
+ return null
624
+ }
625
+
626
+ /**
627
+ * How many columns the categorical part captions take when they do not fit under their segments:
628
+ * the most columns whose widest-per-column entries fit the row, with no lone entry on the last row
629
+ * (`n % cols === 1`). All in one row when they fit; one column is always valid.
630
+ */
631
+ export function partColumns(widths: readonly number[], rowWidth: number, gap: number): number {
632
+ const n = widths.length
633
+ for (let cols = n; cols > 1; cols--) {
634
+ if (n > cols && n % cols === 1) continue
635
+ const widest = Array.from({ length: cols }, (_, c) =>
636
+ Math.max(0, ...widths.filter((_w, i) => i % cols === c)),
637
+ )
638
+ const total = widest.reduce((a, b) => a + b, 0) + gap * (cols - 1)
639
+ if (total <= rowWidth + LABEL_SUBPIXEL) return cols
640
+ }
641
+ return 1
508
642
  }
509
643
 
510
644
  /**