basalt-ui 2.23.0 → 2.25.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 (122) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/MIGRATING.md +264 -12
  3. package/agent/agents/basalt-design-reviewer.md +2 -2
  4. package/agent/skills/basalt-design/SKILL.md +1 -1
  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/Bars.d.ts.map +1 -1
  8. package/dist/charts/Bars.js +21 -3
  9. package/dist/charts/CartesianChart.d.ts +2 -0
  10. package/dist/charts/CartesianChart.d.ts.map +1 -1
  11. package/dist/charts/CartesianChart.js +7 -3
  12. package/dist/charts/ChartCard.d.ts +6 -2
  13. package/dist/charts/ChartCard.d.ts.map +1 -1
  14. package/dist/charts/ChartCard.js +1 -1
  15. package/dist/charts/Sparkline.d.ts.map +1 -1
  16. package/dist/charts/Sparkline.js +32 -6
  17. package/dist/charts/StackedArea.d.ts.map +1 -1
  18. package/dist/charts/StackedArea.js +5 -1
  19. package/dist/charts/chart-legend.module.css +16 -0
  20. package/dist/charts/compile.d.ts +4 -0
  21. package/dist/charts/compile.d.ts.map +1 -1
  22. package/dist/charts/compile.js +65 -3
  23. package/dist/charts/legend.d.ts +0 -6
  24. package/dist/charts/legend.d.ts.map +1 -1
  25. package/dist/charts/legend.js +87 -35
  26. package/dist/charts/sparkline.module.css +59 -6
  27. package/dist/charts/sparkline.module.css.d.ts +1 -0
  28. package/dist/charts/state.d.ts +3 -1
  29. package/dist/charts/state.d.ts.map +1 -1
  30. package/dist/charts/state.js +10 -2
  31. package/dist/charts/types.d.ts +11 -0
  32. package/dist/charts/types.d.ts.map +1 -1
  33. package/dist/cli/shot/detect.d.ts.map +1 -1
  34. package/dist/cli/shot/detect.js +25 -36
  35. package/dist/cli/shot/measure.js +19 -23
  36. package/dist/dashboard/bar-list.d.ts.map +1 -1
  37. package/dist/dashboard/bar-list.js +16 -2
  38. package/dist/dashboard/bar-list.module.css +10 -17
  39. package/dist/dashboard/bar-list.module.css.d.ts +1 -0
  40. package/dist/dashboard/empty-state.d.ts.map +1 -1
  41. package/dist/dashboard/meter-model.d.ts +30 -1
  42. package/dist/dashboard/meter-model.d.ts.map +1 -1
  43. package/dist/dashboard/meter-model.js +74 -5
  44. package/dist/dashboard/meter-parts.d.ts +8 -1
  45. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  46. package/dist/dashboard/meter-parts.js +64 -21
  47. package/dist/dashboard/meter.d.ts +8 -1
  48. package/dist/dashboard/meter.d.ts.map +1 -1
  49. package/dist/dashboard/meter.js +14 -3
  50. package/dist/dashboard/meter.module.css +42 -2
  51. package/dist/dashboard/meter.module.css.d.ts +4 -0
  52. package/dist/dashboard/query-state.d.ts +7 -3
  53. package/dist/dashboard/query-state.d.ts.map +1 -1
  54. package/dist/dashboard/query-state.js +14 -6
  55. package/dist/dashboard/section.d.ts +2 -1
  56. package/dist/dashboard/section.d.ts.map +1 -1
  57. package/dist/dashboard/section.module.css +12 -0
  58. package/dist/dashboard/settings-section.d.ts +1 -1
  59. package/dist/dashboard/settings-section.d.ts.map +1 -1
  60. package/dist/dashboard/settings-section.js +3 -3
  61. package/dist/dashboard/stat-card.module.css +24 -10
  62. package/dist/dashboard/stat-group.module.css +8 -0
  63. package/dist/dashboard/stat-strip.d.ts +3 -2
  64. package/dist/dashboard/stat-strip.d.ts.map +1 -1
  65. package/dist/dashboard/stat-strip.js +83 -73
  66. package/dist/dashboard/stat-strip.module.css +24 -39
  67. package/dist/dashboard/stat-strip.module.css.d.ts +1 -0
  68. package/dist/dashboard/widget-header.d.ts.map +1 -1
  69. package/dist/dashboard/widget-header.js +19 -1
  70. package/dist/dashboard/widget-header.module.css +6 -0
  71. package/dist/guard/index.d.ts +2 -2
  72. package/dist/guard/index.d.ts.map +1 -1
  73. package/dist/guard/index.js +1 -12
  74. package/dist/table/data-table.js +1 -1
  75. package/dist/theme/controls.module.css +7 -2
  76. package/dist/theme/index.d.ts.map +1 -1
  77. package/dist/theme/index.js +7 -5
  78. package/dist/tokens/palette.d.ts +2 -2
  79. package/dist/tokens/palette.d.ts.map +1 -1
  80. package/dist/tokens/palette.js +2 -2
  81. package/llms.txt +4 -3
  82. package/package.json +1 -1
  83. package/src/charts/Bars.tsx +36 -4
  84. package/src/charts/CartesianChart.tsx +9 -3
  85. package/src/charts/ChartCard.tsx +7 -3
  86. package/src/charts/Sparkline.tsx +66 -21
  87. package/src/charts/StackedArea.tsx +12 -1
  88. package/src/charts/chart-legend.module.css +16 -0
  89. package/src/charts/compile.ts +104 -2
  90. package/src/charts/legend.tsx +70 -3
  91. package/src/charts/sparkline.module.css +59 -6
  92. package/src/charts/sparkline.module.css.d.ts +1 -0
  93. package/src/charts/state.tsx +16 -7
  94. package/src/charts/types.ts +9 -0
  95. package/src/cli/shot/detect.ts +26 -40
  96. package/src/cli/shot/measure.ts +27 -27
  97. package/src/dashboard/bar-list.module.css +10 -17
  98. package/src/dashboard/bar-list.module.css.d.ts +1 -0
  99. package/src/dashboard/bar-list.tsx +14 -2
  100. package/src/dashboard/empty-state.tsx +4 -2
  101. package/src/dashboard/meter-model.ts +124 -7
  102. package/src/dashboard/meter-parts.tsx +127 -31
  103. package/src/dashboard/meter.module.css +42 -2
  104. package/src/dashboard/meter.module.css.d.ts +4 -0
  105. package/src/dashboard/meter.tsx +22 -3
  106. package/src/dashboard/query-state.tsx +19 -8
  107. package/src/dashboard/section.module.css +12 -0
  108. package/src/dashboard/section.tsx +2 -1
  109. package/src/dashboard/settings-section.tsx +4 -4
  110. package/src/dashboard/stat-card.module.css +24 -10
  111. package/src/dashboard/stat-group.module.css +8 -0
  112. package/src/dashboard/stat-strip.module.css +24 -39
  113. package/src/dashboard/stat-strip.module.css.d.ts +1 -0
  114. package/src/dashboard/stat-strip.tsx +63 -43
  115. package/src/dashboard/widget-header.module.css +6 -0
  116. package/src/dashboard/widget-header.tsx +28 -1
  117. package/src/guard/index.ts +3 -14
  118. package/src/table/data-table.tsx +1 -1
  119. package/src/theme/__snapshots__/plain-theme.test.ts.snap +6 -6
  120. package/src/theme/controls.module.css +7 -2
  121. package/src/theme/index.ts +7 -5
  122. package/src/tokens/palette.ts +2 -2
@@ -94,12 +94,12 @@ export function finishMeasurement({ texts, radius, nested, ...rest }: Reading):
94
94
  /* oxlint-disable unicorn/consistent-function-scoping -- the helpers below are serialised into the page with it: no outer scope */
95
95
  export function measureAnchor(root: Element): Reading {
96
96
  const px = (v: string | number): number => Math.round(parseFloat(String(v)) * 100) / 100
97
- const sides = (s: CSSStyleDeclaration, p: 'padding'): Measurement['padding'] => ({
98
- top: px(s[`${p}Top`]),
99
- right: px(s[`${p}Right`]),
100
- bottom: px(s[`${p}Bottom`]),
101
- left: px(s[`${p}Left`]),
102
- })
97
+ const edges = (s: CSSStyleDeclaration, p: string, x = ''): number[] =>
98
+ ['top', 'right', 'bottom', 'left'].map((e) => px(s.getPropertyValue(`${p}-${e}${x}`)))
99
+ const sides = (s: CSSStyleDeclaration): Measurement['padding'] => {
100
+ const [top = 0, right = 0, bottom = 0, left = 0] = edges(s, 'padding')
101
+ return { top, right, bottom, left }
102
+ }
103
103
  const ctx = document.createElement('canvas').getContext('2d', { willReadFrequently: true })
104
104
  if (!ctx) throw new Error('no 2d canvas context: cannot read colours')
105
105
  const parsed = new Map<string, number[]>()
@@ -126,12 +126,13 @@ export function measureAnchor(root: Element): Reading {
126
126
  const a = top[3] ?? 1
127
127
  return [0, 1, 2, 3].map((i) => (i === 3 ? 1 : (top[i] ?? 0) * a + (under[i] ?? 0) * (1 - a)))
128
128
  }
129
+ const shown = (s: CSSStyleDeclaration): boolean =>
130
+ s.visibility !== 'hidden' && s.display !== 'none' && s.opacity !== '0'
131
+ const fills = (s: CSSStyleDeclaration): boolean =>
132
+ shown(s) && s.content !== 'none' && (rgba(s.backgroundColor)[3] ?? 0) > 0
129
133
  const visible = (el: Element): CSSStyleDeclaration | null => {
130
134
  const s = getComputedStyle(el)
131
- const r = el.getBoundingClientRect()
132
- return s.visibility === 'hidden' || s.display === 'none' || s.opacity === '0' || r.width === 0
133
- ? null
134
- : s
135
+ return shown(s) && el.getBoundingClientRect().width !== 0 ? s : null
135
136
  }
136
137
  /** The effective background: layers up to the first opaque ancestor (html/body included), composited. */
137
138
  const backdrop = (el: Element): { color: number[]; assumedWhite: boolean } => {
@@ -148,18 +149,13 @@ export function measureAnchor(root: Element): Reading {
148
149
 
149
150
  const cs = getComputedStyle(root)
150
151
  const rect = root.getBoundingClientRect()
151
- const widest = Math.max(
152
- ...['Top', 'Right', 'Bottom', 'Left'].map((e) =>
153
- px(cs.getPropertyValue(`border-${e.toLowerCase()}-width`)),
154
- ),
155
- )
152
+ const widest = Math.max(...edges(cs, 'border', '-width'))
156
153
 
157
154
  // gap: the first flex/grid box (breadth-first) that actually spaces its children.
158
155
  let gap = { row: 0, column: 0 }
159
156
  const queue: Element[] = [root]
160
157
  for (let i = 0; i < queue.length && i < 80; i++) {
161
- const item = queue[i]
162
- if (!item) break
158
+ const item = queue[i] as Element
163
159
  const s = getComputedStyle(item)
164
160
  const row = px(s.rowGap) || 0
165
161
  const column = px(s.columnGap) || 0
@@ -187,22 +183,27 @@ export function measureAnchor(root: Element): Reading {
187
183
  const s = visible(el)
188
184
  if (!s) continue
189
185
  const r = el.getBoundingClientRect()
190
- const hasBg = (rgba(s.backgroundColor)[3] ?? 0) > 0
191
- const hasBorder = ['Top', 'Right', 'Bottom', 'Left'].some(
192
- (e) => px(s.getPropertyValue(`border-${e.toLowerCase()}-width`)) > 0,
193
- )
186
+ const hasBg = fills(s)
187
+ const hasBorder = edges(s, 'border', '-width').some((w) => w > 0)
194
188
  if (el !== root && (hasBg || hasBorder) && nested.length < 60) {
195
189
  nested.push({
196
190
  path: pathOf(el),
197
191
  radius: { value: s.borderTopLeftRadius, width: px(r.width), height: px(r.height) },
198
- padding: sides(s, 'padding'),
192
+ padding: sides(s),
199
193
  })
200
194
  }
201
- // Wrappers that span the anchor are layout, not content: only smaller painted boxes count.
195
+ // Only boxes smaller than the anchor count (wrappers are layout); a pseudo fill is paint (Skeleton) only if it covers its host.
196
+ const pseudo = ['::before', '::after'].some((p) => {
197
+ const ps = getComputedStyle(el, p)
198
+ return (
199
+ fills(ps) &&
200
+ [px(ps.width) - r.width, px(ps.height) - r.height].every((d) => Math.abs(d) <= 2)
201
+ )
202
+ })
202
203
  if (
203
204
  el !== root &&
204
205
  (/^(svg|img|canvas|video|input|textarea)$/i.test(el.tagName) ||
205
- ((hasBg || hasBorder) && r.height < rect.height * 0.9))
206
+ ((hasBg || hasBorder || pseudo) && r.height < rect.height * 0.9))
206
207
  ) {
207
208
  painted.push([r.top, r.bottom])
208
209
  }
@@ -230,8 +231,7 @@ export function measureAnchor(root: Element): Reading {
230
231
  }
231
232
 
232
233
  // emptyRatio: the largest vertical run (edges included) with nothing painted in it.
233
- const top = rect.top
234
- const bottom = rect.bottom
234
+ const { top, bottom } = rect
235
235
  let cursor = top
236
236
  let longest = 0
237
237
  for (const [a, b] of painted.toSorted((x, y) => x[0] - y[0])) {
@@ -244,7 +244,7 @@ export function measureAnchor(root: Element): Reading {
244
244
 
245
245
  return {
246
246
  box: { width: px(rect.width), height: px(rect.height) },
247
- padding: sides(cs, 'padding'),
247
+ padding: sides(cs),
248
248
  gap,
249
249
  radius: { value: cs.borderTopLeftRadius, width: px(rect.width), height: px(rect.height) },
250
250
  borderWidth: widest,
@@ -100,9 +100,14 @@
100
100
  color: var(--vx-muted);
101
101
  }
102
102
 
103
+ /* A real row with a skeleton laid over it: the row sets the height, the skeleton paints it. */
103
104
  .skeletonRow {
104
- grid-column: 1 / -1;
105
- height: calc(var(--vx-space-control-height-widget, 1.5rem) * var(--mantine-scale, 1));
105
+ position: relative;
106
+ }
107
+
108
+ .skeletonFill {
109
+ position: absolute;
110
+ inset: 0;
106
111
  }
107
112
 
108
113
  /* A row's threshold: a hairline across the bar at a position on the row's own scale. */
@@ -228,22 +233,10 @@
228
233
  }
229
234
 
230
235
  /*
231
- * A column of DeltaBadges: a two-column grid, the glyph in a fixed-width slot and the figure (the badge's
232
- * bare text) end-aligned in a tabular slot of four characters, so "▲ 9%" and "▲13%" share both their glyph
233
- * x and their right edge. A zero delta has no glyph; an empty `::before` holds its slot. Scoped here because
234
- * a hero metric has no column to line up with (D33: tabular figures only in a table-like column).
236
+ * A column of DeltaBadges: glyph and figure are ONE unit, end-aligned as a whole, so the glyph never drifts
237
+ * from its figure however wide the column ("▲ 9%" and "▲13%" share their right edge and keep the badge's own
238
+ * gap). Tabular figures keep the right edges even (D33: only in a table-like column, not a hero metric).
235
239
  */
236
240
  .secondary [data-delta] {
237
- display: inline-grid;
238
- grid-template-columns: 1em minmax(4ch, auto);
239
- justify-items: end;
240
241
  font-variant-numeric: tabular-nums;
241
242
  }
242
-
243
- .secondary [data-delta]:not(:has([data-delta-glyph]))::before {
244
- content: '';
245
- }
246
-
247
- .secondary [data-delta-glyph] {
248
- justify-self: center;
249
- }
@@ -15,5 +15,6 @@ declare const classes: {
15
15
  readonly secondary: string
16
16
  readonly empty: string
17
17
  readonly skeletonRow: string
18
+ readonly skeletonFill: string
18
19
  }
19
20
  export default classes
@@ -132,12 +132,24 @@ export function BarList({
132
132
  style,
133
133
  }: BarListProps) {
134
134
  if (loading) {
135
+ // Each row is a real, empty row with a skeleton laid over it, so the list takes the height of
136
+ // the rows it will become in every layout instead of a fixed guess.
135
137
  return (
136
138
  <div className={cx(classes.frame, className)} style={style}>
137
- <div className={classes.root} aria-busy="true">
139
+ <div className={classes.root} aria-busy="true" data-variant={variant}>
138
140
  <VisuallyHidden>{`Loading ${label}`}</VisuallyHidden>
139
141
  {Array.from({ length: loadingRows }, (_, i) => (
140
- <Skeleton key={i} className={classes.skeletonRow} data-bar-skeleton />
142
+ <div
143
+ key={i}
144
+ className={cx(classes.row, classes.skeletonRow)}
145
+ data-bar-skeleton
146
+ aria-hidden
147
+ >
148
+ <span className={classes.label}>{'\u00a0'}</span>
149
+ <span className={classes.value} />
150
+ <span className={classes.track} />
151
+ <Skeleton className={classes.skeletonFill} />
152
+ </div>
141
153
  ))}
142
154
  </div>
143
155
  </div>
@@ -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
  * />
@@ -320,6 +320,14 @@ export function placeRowLabels(
320
320
  short[i] = useShort
321
321
  kept.push({ box, tick })
322
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
+ }
323
331
  return { shown, short, shift }
324
332
  }
325
333
 
@@ -363,6 +371,8 @@ export type BarModel = {
363
371
  zoneLabels: readonly ZoneLabel[]
364
372
  ticks: readonly TickMark[]
365
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
366
376
  targetPct: number | undefined
367
377
  /** The default start caption (the zone is named on the band, so it is the bare reading); `startCaption` decides whether it shows. */
368
378
  startText: string
@@ -475,6 +485,12 @@ export function modelBar(input: BarInput): BarModel {
475
485
  }),
476
486
  ticks,
477
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
+ ),
478
494
  targetPct: target === undefined ? undefined : toPct((target - min) / span),
479
495
  startText: format(reading),
480
496
  endText: endCaptionText(reading, target, format),
@@ -486,8 +502,26 @@ export function modelBar(input: BarInput): BarModel {
486
502
  export const ringFraction = (reading: number, min: number, max: number): number =>
487
503
  clamp01((reading - min) / spanOf(min, max).span)
488
504
 
489
- /** One caption on the marks row: its natural width and where it is anchored (`pct`), else its edge. */
490
- 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
+ }
491
525
 
492
526
  /**
493
527
  * Where each caption of the marks row starts (px, logical start edge), index-aligned with `items`: an
@@ -504,11 +538,13 @@ export function placeMarkCaptions(
504
538
  ): number[] | null {
505
539
  if (rowWidth <= 0 || !items.some((i) => i.pct !== undefined)) return null
506
540
  if (items.some((i) => i.width > rowWidth)) return null
507
- const lefts = items.map((i) => {
508
- if (i.pct === undefined) return i.edge === 'end' ? rowWidth - i.width : 0
509
- return Math.max(0, Math.min((rowWidth * i.pct) / 100 - i.width / 2, rowWidth - i.width))
510
- })
511
- const order = lefts.map((_, i) => i).toSorted((a, b) => (lefts[a] ?? 0) - (lefts[b] ?? 0))
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)
512
548
  const widthOf = (i: number) => items[i]?.width ?? 0
513
549
  for (const [n, i] of order.entries()) {
514
550
  const prev = order[n - 1]
@@ -524,6 +560,87 @@ export function placeMarkCaptions(
524
560
  return first !== undefined && (lefts[first] ?? 0) < -LABEL_SUBPIXEL ? null : lefts
525
561
  }
526
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,
604
+ )
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
642
+ }
643
+
527
644
  /**
528
645
  * Whether every categorical part's caption fits under its own segment: left-aligned at the part's
529
646
  * start and ending before the next part's start (less `gap`) or the row's end. A zero-width part never fits.