basalt-ui 2.21.0 → 2.23.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 (254) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/MIGRATING.md +452 -12
  3. package/agent/agents/basalt-design-reviewer.md +1 -1
  4. package/agent/rules/basalt-tokens.md +1 -1
  5. package/agent/skills/basalt-design/SKILL.md +1 -1
  6. package/agent/skills/basalt-design/references/anti-defaults.md +1 -1
  7. package/agent/skills/basalt-design/references/catalogue.md +1 -0
  8. package/dist/charts/BandStrip.d.ts.map +1 -1
  9. package/dist/charts/BandStrip.js +73 -3
  10. package/dist/charts/Bars.d.ts.map +1 -1
  11. package/dist/charts/Bars.js +6 -0
  12. package/dist/charts/CartesianChart.d.ts +10 -0
  13. package/dist/charts/CartesianChart.d.ts.map +1 -1
  14. package/dist/charts/CartesianChart.js +8 -5
  15. package/dist/charts/ChartCard.d.ts +12 -3
  16. package/dist/charts/ChartCard.d.ts.map +1 -1
  17. package/dist/charts/ChartCard.js +39 -8
  18. package/dist/charts/DualPanel.d.ts.map +1 -1
  19. package/dist/charts/DualPanel.js +5 -8
  20. package/dist/charts/Heatmap.d.ts +12 -1
  21. package/dist/charts/Heatmap.d.ts.map +1 -1
  22. package/dist/charts/Heatmap.js +34 -5
  23. package/dist/charts/Lines.d.ts +3 -0
  24. package/dist/charts/Lines.d.ts.map +1 -1
  25. package/dist/charts/Lines.js +29 -6
  26. package/dist/charts/MirroredBars.d.ts.map +1 -1
  27. package/dist/charts/MirroredBars.js +4 -6
  28. package/dist/charts/Sparkline.d.ts +14 -2
  29. package/dist/charts/Sparkline.d.ts.map +1 -1
  30. package/dist/charts/Sparkline.js +101 -22
  31. package/dist/charts/StackedArea.d.ts +4 -1
  32. package/dist/charts/StackedArea.d.ts.map +1 -1
  33. package/dist/charts/StackedArea.js +9 -3
  34. package/dist/charts/band-fold.d.ts +38 -1
  35. package/dist/charts/band-fold.d.ts.map +1 -1
  36. package/dist/charts/band-fold.js +18 -1
  37. package/dist/charts/chart-box.module.css +19 -0
  38. package/dist/charts/chart-card.module.css +25 -0
  39. package/dist/charts/chart-dates.d.ts +12 -0
  40. package/dist/charts/chart-dates.d.ts.map +1 -0
  41. package/dist/charts/chart-dates.js +16 -0
  42. package/dist/charts/chart-legend.module.css +7 -0
  43. package/dist/charts/compile.d.ts +25 -6
  44. package/dist/charts/compile.d.ts.map +1 -1
  45. package/dist/charts/compile.js +152 -72
  46. package/dist/charts/date-tick-format.d.ts +5 -5
  47. package/dist/charts/date-tick-format.d.ts.map +1 -1
  48. package/dist/charts/date-tick-format.js +3 -7
  49. package/dist/charts/labels.d.ts +21 -6
  50. package/dist/charts/labels.d.ts.map +1 -1
  51. package/dist/charts/labels.js +52 -17
  52. package/dist/charts/legend.d.ts +2 -0
  53. package/dist/charts/legend.d.ts.map +1 -1
  54. package/dist/charts/legend.js +7 -21
  55. package/dist/charts/marks.js +1 -1
  56. package/dist/charts/panel.d.ts +0 -6
  57. package/dist/charts/panel.d.ts.map +1 -1
  58. package/dist/charts/panel.js +1 -10
  59. package/dist/charts/ref-label.d.ts +43 -0
  60. package/dist/charts/ref-label.d.ts.map +1 -0
  61. package/dist/charts/ref-label.js +75 -0
  62. package/dist/charts/scales.d.ts +4 -1
  63. package/dist/charts/scales.d.ts.map +1 -1
  64. package/dist/charts/scales.js +14 -3
  65. package/dist/charts/sparkline.module.css +13 -17
  66. package/dist/charts/sparkline.module.css.d.ts +1 -2
  67. package/dist/charts/state.d.ts +2 -0
  68. package/dist/charts/state.d.ts.map +1 -1
  69. package/dist/charts/state.js +5 -2
  70. package/dist/charts/types.d.ts +18 -8
  71. package/dist/charts/types.d.ts.map +1 -1
  72. package/dist/cli/doctor-upgrade.d.ts +1 -0
  73. package/dist/cli/doctor-upgrade.d.ts.map +1 -1
  74. package/dist/cli/doctor-upgrade.js +1 -0
  75. package/dist/cli/init.d.ts +2 -0
  76. package/dist/cli/init.d.ts.map +1 -1
  77. package/dist/cli/init.js +68 -4
  78. package/dist/cli/shot/capture.d.ts +21 -2
  79. package/dist/cli/shot/capture.d.ts.map +1 -1
  80. package/dist/cli/shot/capture.js +202 -36
  81. package/dist/cli/shot/detect.d.ts.map +1 -1
  82. package/dist/cli/shot/detect.js +17 -1
  83. package/dist/cli/shot/playwright.d.ts +13 -2
  84. package/dist/cli/shot/playwright.d.ts.map +1 -1
  85. package/dist/cli/shot/playwright.js +5 -1
  86. package/dist/cli/shot/types.d.ts +4 -0
  87. package/dist/cli/shot/types.d.ts.map +1 -1
  88. package/dist/cli/shot/types.js +2 -0
  89. package/dist/cli/tokens-css.d.ts +5 -7
  90. package/dist/cli/tokens-css.d.ts.map +1 -1
  91. package/dist/cli/tokens-css.js +11 -12
  92. package/dist/controls/controls.module.css +11 -1
  93. package/dist/controls/filter-pill.d.ts.map +1 -1
  94. package/dist/controls/filter-pill.js +1 -0
  95. package/dist/controls/sync-button.d.ts.map +1 -1
  96. package/dist/controls/sync-button.js +22 -3
  97. package/dist/controls/sync-button.module.css +34 -2
  98. package/dist/controls/sync-button.module.css.d.ts +1 -0
  99. package/dist/dashboard/bar-list.d.ts +6 -1
  100. package/dist/dashboard/bar-list.d.ts.map +1 -1
  101. package/dist/dashboard/bar-list.js +15 -8
  102. package/dist/dashboard/bar-list.module.css +83 -0
  103. package/dist/dashboard/bar-list.module.css.d.ts +1 -0
  104. package/dist/dashboard/delta-badge.d.ts.map +1 -1
  105. package/dist/dashboard/delta-badge.js +2 -2
  106. package/dist/dashboard/index.d.ts +1 -0
  107. package/dist/dashboard/index.d.ts.map +1 -1
  108. package/dist/dashboard/index.js +2 -0
  109. package/dist/dashboard/meter-model.d.ts +84 -5
  110. package/dist/dashboard/meter-model.d.ts.map +1 -1
  111. package/dist/dashboard/meter-model.js +100 -15
  112. package/dist/dashboard/meter-parts.d.ts +9 -1
  113. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  114. package/dist/dashboard/meter-parts.js +276 -74
  115. package/dist/dashboard/meter.d.ts +54 -65
  116. package/dist/dashboard/meter.d.ts.map +1 -1
  117. package/dist/dashboard/meter.js +21 -4
  118. package/dist/dashboard/meter.module.css +76 -0
  119. package/dist/dashboard/meter.module.css.d.ts +6 -0
  120. package/dist/dashboard/page-summary.d.ts +20 -0
  121. package/dist/dashboard/page-summary.d.ts.map +1 -0
  122. package/dist/dashboard/page-summary.js +28 -0
  123. package/dist/dashboard/page-summary.module.css +89 -0
  124. package/dist/dashboard/page-summary.module.css.d.ts +15 -0
  125. package/dist/dashboard/query-state.d.ts.map +1 -1
  126. package/dist/dashboard/query-state.js +1 -1
  127. package/dist/dashboard/section.d.ts +25 -2
  128. package/dist/dashboard/section.d.ts.map +1 -1
  129. package/dist/dashboard/section.js +10 -5
  130. package/dist/dashboard/section.module.css +23 -0
  131. package/dist/dashboard/section.module.css.d.ts +1 -0
  132. package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
  133. package/dist/dashboard/stat-card-parts.js +1 -1
  134. package/dist/dashboard/stat-card.d.ts +4 -2
  135. package/dist/dashboard/stat-card.d.ts.map +1 -1
  136. package/dist/dashboard/stat-card.js +7 -5
  137. package/dist/dashboard/stat-card.module.css +36 -0
  138. package/dist/dashboard/stat-group.module.css +7 -0
  139. package/dist/dashboard/stat-strip.d.ts +22 -6
  140. package/dist/dashboard/stat-strip.d.ts.map +1 -1
  141. package/dist/dashboard/stat-strip.js +104 -60
  142. package/dist/dashboard/stat-strip.module.css +122 -53
  143. package/dist/dashboard/stat-strip.module.css.d.ts +4 -0
  144. package/dist/dashboard/visual-slot.d.ts +7 -1
  145. package/dist/dashboard/visual-slot.d.ts.map +1 -1
  146. package/dist/dashboard/visual-slot.js +3 -1
  147. package/dist/guard/index.d.ts.map +1 -1
  148. package/dist/guard/index.js +0 -5
  149. package/dist/guard/vx-names.d.ts.map +1 -1
  150. package/dist/guard/vx-names.js +2 -0
  151. package/dist/index.d.ts +1 -1
  152. package/dist/index.d.ts.map +1 -1
  153. package/dist/index.js +3 -1
  154. package/dist/styles.css +43 -4
  155. package/dist/theme/chrome-vars.d.ts +20 -5
  156. package/dist/theme/chrome-vars.d.ts.map +1 -1
  157. package/dist/theme/chrome-vars.js +46 -3
  158. package/dist/theme/controls.module.css +37 -0
  159. package/dist/theme/controls.module.css.d.ts +2 -0
  160. package/dist/theme/index.d.ts.map +1 -1
  161. package/dist/theme/index.js +18 -27
  162. package/dist/theme/status-verdict.d.ts.map +1 -1
  163. package/dist/theme/status-verdict.js +9 -1
  164. package/dist/tokens/core.d.ts +2 -1
  165. package/dist/tokens/core.d.ts.map +1 -1
  166. package/dist/tokens/core.js +6 -1
  167. package/dist/tokens/derive.d.ts +3 -1
  168. package/dist/tokens/derive.d.ts.map +1 -1
  169. package/dist/tokens/derive.js +16 -1
  170. package/dist/tokens/palette.d.ts +11 -10
  171. package/dist/tokens/palette.d.ts.map +1 -1
  172. package/dist/tokens/palette.js +11 -10
  173. package/dist/tokens/status-text.d.ts.map +1 -1
  174. package/dist/tokens/status-text.js +3 -1
  175. package/llms.txt +42 -22
  176. package/package.json +1 -1
  177. package/src/charts/BandStrip.tsx +83 -3
  178. package/src/charts/Bars.tsx +7 -0
  179. package/src/charts/CartesianChart.tsx +18 -3
  180. package/src/charts/ChartCard.tsx +54 -13
  181. package/src/charts/DualPanel.tsx +3 -6
  182. package/src/charts/Heatmap.tsx +54 -5
  183. package/src/charts/Lines.tsx +56 -10
  184. package/src/charts/MirroredBars.tsx +3 -5
  185. package/src/charts/Sparkline.tsx +135 -25
  186. package/src/charts/StackedArea.tsx +14 -4
  187. package/src/charts/band-fold.ts +50 -1
  188. package/src/charts/chart-box.module.css +19 -0
  189. package/src/charts/chart-card.module.css +25 -0
  190. package/src/charts/chart-dates.ts +30 -0
  191. package/src/charts/chart-legend.module.css +7 -0
  192. package/src/charts/compile.ts +253 -87
  193. package/src/charts/date-tick-format.ts +10 -13
  194. package/src/charts/labels.ts +81 -26
  195. package/src/charts/legend.tsx +10 -20
  196. package/src/charts/marks.ts +1 -1
  197. package/src/charts/panel.tsx +1 -16
  198. package/src/charts/ref-label.ts +131 -0
  199. package/src/charts/scales.ts +22 -2
  200. package/src/charts/sparkline.module.css +13 -17
  201. package/src/charts/sparkline.module.css.d.ts +1 -2
  202. package/src/charts/state.tsx +9 -4
  203. package/src/charts/types.ts +18 -8
  204. package/src/cli/doctor-upgrade.ts +1 -1
  205. package/src/cli/init.ts +100 -3
  206. package/src/cli/shot/capture.ts +266 -35
  207. package/src/cli/shot/detect.ts +18 -1
  208. package/src/cli/shot/playwright.ts +20 -4
  209. package/src/cli/shot/types.ts +5 -0
  210. package/src/cli/tokens-css.ts +18 -20
  211. package/src/controls/controls.module.css +11 -1
  212. package/src/controls/filter-pill.tsx +2 -0
  213. package/src/controls/sync-button.module.css +34 -2
  214. package/src/controls/sync-button.module.css.d.ts +1 -0
  215. package/src/controls/sync-button.tsx +40 -4
  216. package/src/dashboard/bar-list.module.css +83 -0
  217. package/src/dashboard/bar-list.module.css.d.ts +1 -0
  218. package/src/dashboard/bar-list.tsx +107 -69
  219. package/src/dashboard/delta-badge.tsx +6 -2
  220. package/src/dashboard/index.ts +1 -0
  221. package/src/dashboard/meter-model.ts +207 -24
  222. package/src/dashboard/meter-parts.tsx +349 -67
  223. package/src/dashboard/meter.module.css +76 -0
  224. package/src/dashboard/meter.module.css.d.ts +6 -0
  225. package/src/dashboard/meter.tsx +72 -18
  226. package/src/dashboard/page-summary.module.css +89 -0
  227. package/src/dashboard/page-summary.module.css.d.ts +15 -0
  228. package/src/dashboard/page-summary.tsx +95 -0
  229. package/src/dashboard/query-state.tsx +1 -1
  230. package/src/dashboard/section.module.css +23 -0
  231. package/src/dashboard/section.module.css.d.ts +1 -0
  232. package/src/dashboard/section.tsx +59 -11
  233. package/src/dashboard/stat-card-parts.tsx +5 -1
  234. package/src/dashboard/stat-card.module.css +36 -0
  235. package/src/dashboard/stat-card.tsx +12 -6
  236. package/src/dashboard/stat-group.module.css +7 -0
  237. package/src/dashboard/stat-strip.module.css +122 -53
  238. package/src/dashboard/stat-strip.module.css.d.ts +4 -0
  239. package/src/dashboard/stat-strip.tsx +176 -91
  240. package/src/dashboard/visual-slot.ts +12 -3
  241. package/src/guard/index.ts +0 -5
  242. package/src/guard/vx-names.ts +2 -0
  243. package/src/index.ts +2 -0
  244. package/src/styles.css +43 -4
  245. package/src/theme/__snapshots__/plain-theme.test.ts.snap +200 -68
  246. package/src/theme/chrome-vars.ts +77 -8
  247. package/src/theme/controls.module.css +37 -0
  248. package/src/theme/controls.module.css.d.ts +2 -0
  249. package/src/theme/index.ts +22 -32
  250. package/src/theme/status-verdict.tsx +5 -1
  251. package/src/tokens/core.ts +4 -1
  252. package/src/tokens/derive.ts +32 -3
  253. package/src/tokens/palette.ts +11 -10
  254. package/src/tokens/status-text.ts +2 -0
@@ -5,11 +5,26 @@
5
5
  import type { ReactNode } from 'react'
6
6
  import { clamp01, toPct } from './scale'
7
7
 
8
- export type MeterSegment = { value: number; label: string; color?: string; forecast?: boolean }
8
+ export type MeterSegment = {
9
+ value: number
10
+ label: string
11
+ color?: string
12
+ /**
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
+ */
16
+ tone?: 'muted'
17
+ forecast?: boolean
18
+ }
9
19
  export type MeterZone = {
10
20
  from: number
11
21
  to: number
12
22
  label: string
23
+ /**
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
+ */
27
+ tone?: 'muted'
13
28
  /**
14
29
  * A shorter name for a narrow track. The row tries `label`, then `short`, and only then hides the
15
30
  * name; the holding zone always shows its full `label`. The described-by text keeps `label`.
@@ -17,11 +32,54 @@ export type MeterZone = {
17
32
  short?: string
18
33
  color?: string
19
34
  }
20
- /** A mark on the track at `at` (in the reading's units). A label is captioned beside it. */
21
- 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 }
40
+
41
+ /** A colour of a `stops` gradient at `at` (in the reading's units); the colour is the consumer's. */
42
+ type MeterStop = { at: number; color: string }
43
+
44
+ /** A ramp step's strength in percent of its neutral: from `lo`, spread over `step` across the steps. */
45
+ export const RAMP_ZONE = { lo: 15, step: 45, solo: 40 }
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
49
+ /** Neighbouring steps stay at least this far apart (the contrast the zone ramp always had), up to `RAMP_MAX`. */
50
+ export const RAMP_MIN_STEP = 15
51
+ export const RAMP_MAX = 95
52
+
53
+ /**
54
+ * Step `index` of `count`. The spread is floored at `RAMP_MIN_STEP` per step, so a ramp stays
55
+ * distinguishable by lightness up to six zones; past that the last steps clamp at
56
+ * `RAMP_MAX` and merge, which is the cue to group parts.
57
+ */
58
+ export function rampStrength(index: number, count: number, ramp: typeof RAMP_ZONE): number {
59
+ if (count <= 1) return ramp.solo
60
+ return Math.min(RAMP_MAX, ramp.lo + index * Math.max(RAMP_MIN_STEP, ramp.step / (count - 1)))
61
+ }
62
+
63
+ function rampFill(base: 'line', index: number, count: number, ramp: typeof RAMP_ZONE): string {
64
+ return `color-mix(in srgb, var(--vx-${base}) ${rampStrength(index, count, ramp)}%, transparent)`
65
+ }
66
+
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. */
70
+ export function zoneFill(zone: MeterZone, index: number, count: number): string {
71
+ if (zone.color !== undefined) return zone.color
72
+ return zone.tone === 'muted' ? mutedFill(MUTED_ZONE) : rampFill('line', index, count, RAMP_ZONE)
73
+ }
22
74
 
23
75
  /** One tick on the track: its percent position and what it says (caption `label`, aria `text`). */
24
- 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
+ }
25
83
 
26
84
  /** A fill on the track, as fractions (0–1) of the span, with the segment it was laid out from. */
27
85
  export type Part = {
@@ -29,13 +87,22 @@ export type Part = {
29
87
  readonly size: number
30
88
  readonly color: string | undefined
31
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
32
92
  readonly label: string
33
93
  /** The segment's own (normalised) amount, in the reading's units — what its caption states. */
34
94
  readonly amount: number
35
95
  }
36
96
 
37
97
  /** One categorical part's caption: its position key, name, formatted amount and fill colour (`undefined` = neutral). */
38
- export type PartCaption = { key: string; label: string; text: string; color: string | undefined }
98
+ export type PartCaption = {
99
+ key: string
100
+ label: string
101
+ text: string
102
+ color: string | undefined
103
+ /** Where the part starts on the track, in percent: the caption sits under it when they all fit. */
104
+ startPct: number
105
+ }
39
106
 
40
107
  /** One zone's name on the label row: its span's centre and width (percent) and whether it holds the reading. */
41
108
  export type ZoneLabel = {
@@ -74,6 +141,7 @@ export function tickMarks(
74
141
  key: String(i),
75
142
  pct: toPct((t.at - min) / span),
76
143
  label: t.label,
144
+ caption: t.caption,
77
145
  text: `${t.label ?? 'Tick'} ${format(t.at)}`,
78
146
  },
79
147
  ]
@@ -81,6 +149,36 @@ export function tickMarks(
81
149
  )
82
150
  }
83
151
 
152
+ /** A `stops` gradient's colour list, in order along the track; `at` outside `[min, max]` clamps. */
153
+ export function stopsGradient(stops: readonly MeterStop[], min: number, span: number): string {
154
+ return stops
155
+ .toSorted((a, b) => a.at - b.at)
156
+ .map((s) => `${s.color} ${toPct(clamp01((s.at - min) / span))}%`)
157
+ .join(', ')
158
+ }
159
+
160
+ const ringAngle = (pct: number) => Math.round(pct * 3.6 * 100) / 100
161
+
162
+ /** A ring's marks (target, ticks): the angle clockwise from the top, and the accessible text. */
163
+ export function ringMarks(
164
+ target: number | undefined,
165
+ ticks: readonly MeterTick[] | undefined,
166
+ min: number,
167
+ max: number,
168
+ format: Format,
169
+ ): { angle: number; text: string }[] {
170
+ const { span } = spanOf(min, max)
171
+ const marks = tickMarks(ticks, min, max, format).map((t) => ({
172
+ angle: ringAngle(t.pct),
173
+ text: t.text,
174
+ }))
175
+ if (target === undefined || !Number.isFinite(target) || target < min || target > max) return marks
176
+ return [
177
+ { angle: ringAngle(toPct((target - min) / span)), text: `Target ${format(target)}` },
178
+ ...marks,
179
+ ]
180
+ }
181
+
84
182
  export const isReading = (n: number | null | undefined): n is number =>
85
183
  typeof n === 'number' && Number.isFinite(n)
86
184
 
@@ -110,6 +208,8 @@ export function normalizeSegments(segments: readonly MeterSegment[]): MeterSegme
110
208
  /** Segment amounts (already normalised) accumulate from `min`; a part never passes the track end. */
111
209
  export function layoutSegments(segments: readonly MeterSegment[], span: number): Part[] {
112
210
  let cursor = 0
211
+ const isMuted = (s: MeterSegment) => s.tone === 'muted' && s.color === undefined && !s.forecast
212
+ let prevMuted = false
113
213
  return segments.map((s) => {
114
214
  const size = Math.max(0, Math.min(clamp01(s.value / span), 1 - cursor))
115
215
  const forecast = s.forecast === true
@@ -117,12 +217,14 @@ export function layoutSegments(segments: readonly MeterSegment[], span: number):
117
217
  const part = {
118
218
  start: cursor,
119
219
  size,
120
- color: forecast ? undefined : s.color,
220
+ color: forecast ? undefined : isMuted(s) ? mutedFill(MUTED_PART) : s.color,
121
221
  forecast,
222
+ seam: isMuted(s) && prevMuted,
122
223
  label: s.label,
123
224
  amount: s.value,
124
225
  }
125
226
  cursor += size
227
+ prevMuted = isMuted(s)
126
228
  return part
127
229
  })
128
230
  }
@@ -143,7 +245,14 @@ export type LabelBox = { left: number; right: number }
143
245
  * A label on the shared row: its measured box and, for a zone name, the span it must stay inside.
144
246
  * The holding zone's name never hides for width: it ignores its span and is shifted into the row.
145
247
  */
146
- export type RowLabel = { box: LabelBox; span?: LabelBox; holding?: boolean; short?: LabelBox }
248
+ export type RowLabel = {
249
+ box: LabelBox
250
+ span?: LabelBox
251
+ holding?: boolean
252
+ /** A tick at the track's start or end: it keeps its place over every zone name it would touch. */
253
+ end?: boolean
254
+ short?: LabelBox
255
+ }
147
256
 
148
257
  /** The placement of the row's labels, index-aligned with the input. */
149
258
  export type RowPlacement = {
@@ -157,33 +266,45 @@ export type RowPlacement = {
157
266
  /**
158
267
  * Which labels of the row show, in input order. The holding zone's name claims its place first (it
159
268
  * is the reading's verdict, so it never hides for width: it may spill past its span, shifted inside
160
- * the row); the labels without a span (the end ticks) go next; every other zone name then shows only
161
- * when it lies inside the row and its own span and clears every label already kept, trying its full
162
- * name and then its `short` box. 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.
163
275
  */
164
- export function placeRowLabels(labels: readonly RowLabel[], rowWidth: number): RowPlacement {
276
+ export function placeRowLabels(
277
+ labels: readonly RowLabel[],
278
+ rowWidth: number,
279
+ gap = 0,
280
+ ): RowPlacement {
165
281
  const shown = labels.map(() => false)
166
282
  const short = labels.map(() => false)
167
283
  const shift = labels.map(() => 0)
168
- const kept: LabelBox[] = []
169
- const rank = (l: RowLabel) => (l.holding ? 0 : l.span ? 2 : 1)
284
+ const kept: { box: LabelBox; tick: boolean }[] = []
285
+ const rank = (l: RowLabel) => (l.holding ? 0 : l.span ? 3 : l.end ? 2 : 1)
170
286
  const order = labels
171
287
  .map((_, i) => i)
172
288
  .toSorted((a, b) => rank(labels[a] as RowLabel) - rank(labels[b] as RowLabel))
173
- const clears = (b: LabelBox) =>
174
- !kept.some((k) => b.left < k.right - LABEL_SUBPIXEL && k.left < b.right - LABEL_SUBPIXEL)
289
+ // The gap step applies when either label of a pair is a tick; zone names keep to "not touching".
290
+ const clears = (b: LabelBox, tick: boolean) =>
291
+ !kept.some(({ box: k, tick: kTick }) => {
292
+ const pad = tick || kTick ? gap : 0
293
+ return b.left < k.right + pad - LABEL_SUBPIXEL && k.left < b.right + pad - LABEL_SUBPIXEL
294
+ })
175
295
  for (const i of order) {
176
296
  const label = labels[i] as RowLabel
177
297
  const { span } = label
298
+ const tick = span === undefined && !label.holding
178
299
  let box = label.box
179
300
  if (label.holding) {
180
301
  const width = box.right - box.left
181
302
  const left = Math.max(0, Math.min(box.left, rowWidth - width))
182
303
  shift[i] = left - box.left
183
304
  box = { left, right: left + width }
184
- if (!clears(box)) continue
305
+ if (!clears(box, false)) continue
185
306
  shown[i] = true
186
- kept.push(box)
307
+ kept.push({ box, tick: false })
187
308
  continue
188
309
  }
189
310
  const fits = (b: LabelBox) =>
@@ -191,13 +312,13 @@ export function placeRowLabels(labels: readonly RowLabel[], rowWidth: number): R
191
312
  b.right <= rowWidth + LABEL_SUBPIXEL &&
192
313
  (span === undefined ||
193
314
  (b.left >= span.left - LABEL_SUBPIXEL && b.right <= span.right + LABEL_SUBPIXEL)) &&
194
- clears(b)
315
+ clears(b, tick)
195
316
  const useShort = !fits(box) && label.short !== undefined && fits(label.short)
196
317
  if (useShort) box = label.short as LabelBox
197
318
  else if (!fits(box)) continue
198
319
  shown[i] = true
199
320
  short[i] = useShort
200
- kept.push(box)
321
+ kept.push({ box, tick })
201
322
  }
202
323
  return { shown, short, shift }
203
324
  }
@@ -219,6 +340,7 @@ export type BarInput = {
219
340
  format: Format
220
341
  segments?: readonly MeterSegment[] | undefined
221
342
  zones?: readonly MeterZone[] | undefined
343
+ stops?: readonly MeterStop[] | undefined
222
344
  target?: number | undefined
223
345
  ticks?: readonly MeterTick[] | undefined
224
346
  color?: string | undefined
@@ -235,6 +357,8 @@ export type BarModel = {
235
357
  captionMode: CaptionMode
236
358
  /** Only in `parts` mode: one caption per part, in order. Empty otherwise. */
237
359
  partCaptions: readonly PartCaption[]
360
+ /** The `stops` gradient's colour list; absent without `stops`. */
361
+ stopsCss: string | undefined
238
362
  /** Only on a zoned track: every zone's name, in zone order. Empty otherwise. */
239
363
  zoneLabels: readonly ZoneLabel[]
240
364
  ticks: readonly TickMark[]
@@ -252,8 +376,8 @@ function endCaptionText(reading: number, target: number | undefined, format: For
252
376
  return `Target ${format(target)}`
253
377
  }
254
378
 
255
- function fillParts(input: BarInput, span: number, zoned: boolean): Part[] {
256
- if (zoned) return []
379
+ function fillParts(input: BarInput, span: number, marked: boolean): Part[] {
380
+ if (marked) return []
257
381
  if (input.segments !== undefined) return layoutSegments(input.segments, span)
258
382
  const fill = { value: input.reading - input.min, label: '' }
259
383
  return layoutSegments([input.color === undefined ? fill : { ...fill, color: input.color }], span)
@@ -270,6 +394,7 @@ function partCaptionsOf(parts: readonly Part[], format: Format): PartCaption[] {
270
394
  label: p.label,
271
395
  text: format(p.amount),
272
396
  color: p.color,
397
+ startPct: toPct(p.start),
273
398
  }))
274
399
  }
275
400
 
@@ -294,13 +419,14 @@ export function startCaptionText(model: BarModel, override: ReactNode | undefine
294
419
  }
295
420
 
296
421
  export function modelBar(input: BarInput): BarModel {
297
- const { reading, min, max, format, segments, zones, target } = input
422
+ const { reading, min, max, format, segments, zones, target, stops } = input
298
423
  const ticks = tickMarks(input.ticks, min, max, format)
299
424
  const { span } = spanOf(min, max)
300
425
  const zoned = zones !== undefined && zones.length > 0
301
426
  const holding = zoned ? findHoldingZone(zones, reading) : undefined
302
427
 
303
- const parts = fillParts(input, span, zoned)
428
+ const striped = !zoned && stops !== undefined && stops.length > 0
429
+ const parts = fillParts(input, span, zoned || striped)
304
430
  const firstForecast = parts.findIndex((p) => p.forecast)
305
431
  const forecastLabels =
306
432
  segments?.filter((s) => s.forecast === true).map((s) => `${s.label} ${format(s.value)}`) ?? []
@@ -333,6 +459,7 @@ export function modelBar(input: BarInput): BarModel {
333
459
  : (input.forecastCaption ?? `Forecast: ${forecastLabels.join(', ')}`),
334
460
  captionMode,
335
461
  partCaptions,
462
+ stopsCss: striped ? stopsGradient(stops, min, span) : undefined,
336
463
  zoneLabels: !zoned
337
464
  ? []
338
465
  : zones.map((z, i) => {
@@ -347,7 +474,7 @@ export function modelBar(input: BarInput): BarModel {
347
474
  }
348
475
  }),
349
476
  ticks,
350
- markerPct: zoned ? toPct((reading - min) / span) : undefined,
477
+ markerPct: zoned || striped ? toPct((reading - min) / span) : undefined,
351
478
  targetPct: target === undefined ? undefined : toPct((target - min) / span),
352
479
  startText: format(reading),
353
480
  endText: endCaptionText(reading, target, format),
@@ -358,3 +485,59 @@ export function modelBar(input: BarInput): BarModel {
358
485
  /** The ring's one arc, as a fraction of the circumference. */
359
486
  export const ringFraction = (reading: number, min: number, max: number): number =>
360
487
  clamp01((reading - min) / spanOf(min, max).span)
488
+
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' }
491
+
492
+ /**
493
+ * Where each caption of the marks row starts (px, logical start edge), index-aligned with `items`: an
494
+ * anchored caption is centred on its mark and clamped inside the row, the others sit at the start (or,
495
+ * `edge: 'end'`, the end). Two captions closer than `gap` are pushed apart, in order, the least that
496
+ * clears them, so a caption stays as near its mark as the row allows. `null` when nothing is anchored,
497
+ * the row is unmeasured, a caption is wider than the row, or the captions cannot all clear each other
498
+ * inside the row: the row then flows and wraps instead.
499
+ */
500
+ export function placeMarkCaptions(
501
+ items: readonly MarkItem[],
502
+ rowWidth: number,
503
+ gap: number,
504
+ ): number[] | null {
505
+ if (rowWidth <= 0 || !items.some((i) => i.pct !== undefined)) return null
506
+ 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))
512
+ const widthOf = (i: number) => items[i]?.width ?? 0
513
+ for (const [n, i] of order.entries()) {
514
+ const prev = order[n - 1]
515
+ if (prev !== undefined)
516
+ lefts[i] = Math.max(lefts[i] ?? 0, (lefts[prev] ?? 0) + widthOf(prev) + gap)
517
+ }
518
+ for (const [n, i] of order.toReversed().entries()) {
519
+ const next = order.toReversed()[n - 1]
520
+ const limit = next === undefined ? rowWidth : (lefts[next] ?? 0) - gap
521
+ lefts[i] = Math.min(lefts[i] ?? 0, limit - widthOf(i))
522
+ }
523
+ const first = order[0]
524
+ return first !== undefined && (lefts[first] ?? 0) < -LABEL_SUBPIXEL ? null : lefts
525
+ }
526
+
527
+ /**
528
+ * Whether every categorical part's caption fits under its own segment: left-aligned at the part's
529
+ * start and ending before the next part's start (less `gap`) or the row's end. A zero-width part never fits.
530
+ */
531
+ export function partsFitUnder(
532
+ parts: readonly { startPct: number; width: number }[],
533
+ rowWidth: number,
534
+ gap: number,
535
+ ): boolean {
536
+ if (rowWidth <= 0) return false
537
+ return parts.every((p, i) => {
538
+ const left = (rowWidth * p.startPct) / 100
539
+ const next = parts[i + 1]
540
+ const limit = next === undefined ? rowWidth : (rowWidth * next.startPct) / 100 - gap
541
+ return left + p.width <= limit + LABEL_SUBPIXEL
542
+ })
543
+ }