basalt-ui 2.20.0 → 2.22.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 (289) hide show
  1. package/CHANGELOG.md +72 -0
  2. package/MIGRATING.md +394 -12
  3. package/agent/agents/basalt-design-reviewer.md +22 -5
  4. package/agent/rules/basalt-tokens.md +3 -3
  5. package/agent/skills/basalt-design/SKILL.md +10 -23
  6. package/agent/skills/basalt-design/references/anti-defaults.md +24 -0
  7. package/agent/skills/basalt-design/references/catalogue.md +49 -0
  8. package/agent/skills/basalt-design/references/concept.md +36 -0
  9. package/agent/skills/basalt-design/references/encoding-rules.md +45 -0
  10. package/agent/skills/basalt-design/references/stat-tile.md +24 -0
  11. package/agent/templates/AGENTS-block.md.tpl +3 -3
  12. package/configs/oxlint-plugin.js +1 -0
  13. package/dist/charts/BandStrip.d.ts.map +1 -1
  14. package/dist/charts/BandStrip.js +27 -3
  15. package/dist/charts/Bars.d.ts.map +1 -1
  16. package/dist/charts/Bars.js +5 -0
  17. package/dist/charts/CartesianChart.d.ts +8 -0
  18. package/dist/charts/CartesianChart.d.ts.map +1 -1
  19. package/dist/charts/CartesianChart.js +8 -5
  20. package/dist/charts/ChartCard.d.ts +17 -4
  21. package/dist/charts/ChartCard.d.ts.map +1 -1
  22. package/dist/charts/ChartCard.js +33 -8
  23. package/dist/charts/DualPanel.d.ts.map +1 -1
  24. package/dist/charts/DualPanel.js +5 -8
  25. package/dist/charts/Heatmap.d.ts +12 -1
  26. package/dist/charts/Heatmap.d.ts.map +1 -1
  27. package/dist/charts/Heatmap.js +34 -5
  28. package/dist/charts/Lines.d.ts +3 -0
  29. package/dist/charts/Lines.d.ts.map +1 -1
  30. package/dist/charts/Lines.js +29 -6
  31. package/dist/charts/MirroredBars.d.ts.map +1 -1
  32. package/dist/charts/MirroredBars.js +4 -6
  33. package/dist/charts/Sparkline.d.ts +7 -1
  34. package/dist/charts/Sparkline.d.ts.map +1 -1
  35. package/dist/charts/Sparkline.js +39 -14
  36. package/dist/charts/StackedArea.d.ts +4 -1
  37. package/dist/charts/StackedArea.d.ts.map +1 -1
  38. package/dist/charts/StackedArea.js +9 -3
  39. package/dist/charts/band-fold.d.ts +20 -1
  40. package/dist/charts/band-fold.d.ts.map +1 -1
  41. package/dist/charts/band-fold.js +9 -0
  42. package/dist/charts/chart-box.module.css +19 -0
  43. package/dist/charts/chart-card.module.css +25 -0
  44. package/dist/charts/chart-legend.module.css +7 -0
  45. package/dist/charts/compile.d.ts +17 -6
  46. package/dist/charts/compile.d.ts.map +1 -1
  47. package/dist/charts/compile.js +145 -74
  48. package/dist/charts/labels.d.ts +15 -4
  49. package/dist/charts/labels.d.ts.map +1 -1
  50. package/dist/charts/labels.js +34 -10
  51. package/dist/charts/legend.d.ts +2 -0
  52. package/dist/charts/legend.d.ts.map +1 -1
  53. package/dist/charts/legend.js +7 -21
  54. package/dist/charts/marks.js +1 -1
  55. package/dist/charts/panel.d.ts +0 -6
  56. package/dist/charts/panel.d.ts.map +1 -1
  57. package/dist/charts/panel.js +1 -10
  58. package/dist/charts/ref-label.d.ts +26 -0
  59. package/dist/charts/ref-label.d.ts.map +1 -0
  60. package/dist/charts/ref-label.js +63 -0
  61. package/dist/charts/scales.d.ts +4 -1
  62. package/dist/charts/scales.d.ts.map +1 -1
  63. package/dist/charts/scales.js +14 -3
  64. package/dist/charts/sparkline.module.css +10 -11
  65. package/dist/charts/sparkline.module.css.d.ts +1 -2
  66. package/dist/charts/state.d.ts +7 -0
  67. package/dist/charts/state.d.ts.map +1 -1
  68. package/dist/charts/state.js +4 -1
  69. package/dist/charts/types.d.ts +14 -5
  70. package/dist/charts/types.d.ts.map +1 -1
  71. package/dist/cli/check-theme.d.ts.map +1 -1
  72. package/dist/cli/check-theme.js +3 -0
  73. package/dist/cli/doctor-upgrade.d.ts +1 -0
  74. package/dist/cli/doctor-upgrade.d.ts.map +1 -1
  75. package/dist/cli/doctor-upgrade.js +1 -0
  76. package/dist/cli/help.d.ts.map +1 -1
  77. package/dist/cli/help.js +3 -2
  78. package/dist/cli/index.d.ts +4 -2
  79. package/dist/cli/index.d.ts.map +1 -1
  80. package/dist/cli/index.js +27 -15
  81. package/dist/cli/init.d.ts +2 -0
  82. package/dist/cli/init.d.ts.map +1 -1
  83. package/dist/cli/init.js +71 -6
  84. package/dist/cli/shot/capture.d.ts +21 -2
  85. package/dist/cli/shot/capture.d.ts.map +1 -1
  86. package/dist/cli/shot/capture.js +200 -33
  87. package/dist/cli/shot/detect.d.ts +51 -0
  88. package/dist/cli/shot/detect.d.ts.map +1 -0
  89. package/dist/cli/shot/detect.js +409 -0
  90. package/dist/cli/shot/index.d.ts.map +1 -1
  91. package/dist/cli/shot/index.js +7 -4
  92. package/dist/cli/shot/measure.d.ts +2 -0
  93. package/dist/cli/shot/measure.d.ts.map +1 -1
  94. package/dist/cli/shot/measure.js +2 -0
  95. package/dist/cli/shot/playwright.d.ts +5 -2
  96. package/dist/cli/shot/playwright.d.ts.map +1 -1
  97. package/dist/cli/shot/types.d.ts +6 -1
  98. package/dist/cli/shot/types.d.ts.map +1 -1
  99. package/dist/cli/shot/types.js +4 -0
  100. package/dist/cli/sync.d.ts +2 -0
  101. package/dist/cli/sync.d.ts.map +1 -1
  102. package/dist/cli/sync.js +2 -2
  103. package/dist/common/retry-button.d.ts +12 -0
  104. package/dist/common/retry-button.d.ts.map +1 -0
  105. package/dist/common/retry-button.js +22 -0
  106. package/dist/common/retry-button.module.css +58 -0
  107. package/dist/common/retry-button.module.css.d.ts +8 -0
  108. package/dist/controls/controls.module.css +11 -1
  109. package/dist/controls/filter-pill.d.ts.map +1 -1
  110. package/dist/controls/filter-pill.js +1 -0
  111. package/dist/controls/sync-button.d.ts.map +1 -1
  112. package/dist/controls/sync-button.js +22 -3
  113. package/dist/controls/sync-button.module.css +34 -2
  114. package/dist/controls/sync-button.module.css.d.ts +1 -0
  115. package/dist/dashboard/bar-list.d.ts +5 -2
  116. package/dist/dashboard/bar-list.d.ts.map +1 -1
  117. package/dist/dashboard/bar-list.js +9 -10
  118. package/dist/dashboard/bar-list.module.css +53 -0
  119. package/dist/dashboard/bar-list.module.css.d.ts +1 -0
  120. package/dist/dashboard/index.d.ts +1 -0
  121. package/dist/dashboard/index.d.ts.map +1 -1
  122. package/dist/dashboard/index.js +2 -0
  123. package/dist/dashboard/meter-model.d.ts +108 -6
  124. package/dist/dashboard/meter-model.d.ts.map +1 -1
  125. package/dist/dashboard/meter-model.js +123 -19
  126. package/dist/dashboard/meter-parts.d.ts +8 -0
  127. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  128. package/dist/dashboard/meter-parts.js +345 -146
  129. package/dist/dashboard/meter.d.ts +50 -63
  130. package/dist/dashboard/meter.d.ts.map +1 -1
  131. package/dist/dashboard/meter.js +24 -3
  132. package/dist/dashboard/meter.module.css +79 -65
  133. package/dist/dashboard/meter.module.css.d.ts +7 -7
  134. package/dist/dashboard/page-summary.d.ts +20 -0
  135. package/dist/dashboard/page-summary.d.ts.map +1 -0
  136. package/dist/dashboard/page-summary.js +28 -0
  137. package/dist/dashboard/page-summary.module.css +89 -0
  138. package/dist/dashboard/page-summary.module.css.d.ts +15 -0
  139. package/dist/dashboard/query-state.d.ts +1 -1
  140. package/dist/dashboard/query-state.d.ts.map +1 -1
  141. package/dist/dashboard/query-state.js +3 -2
  142. package/dist/dashboard/section.d.ts +25 -2
  143. package/dist/dashboard/section.d.ts.map +1 -1
  144. package/dist/dashboard/section.js +10 -5
  145. package/dist/dashboard/section.module.css +23 -0
  146. package/dist/dashboard/section.module.css.d.ts +1 -0
  147. package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
  148. package/dist/dashboard/stat-card-parts.js +1 -1
  149. package/dist/dashboard/stat-card.d.ts +4 -2
  150. package/dist/dashboard/stat-card.d.ts.map +1 -1
  151. package/dist/dashboard/stat-card.js +7 -4
  152. package/dist/dashboard/stat-card.module.css +17 -0
  153. package/dist/dashboard/stat-strip.d.ts +24 -1
  154. package/dist/dashboard/stat-strip.d.ts.map +1 -1
  155. package/dist/dashboard/stat-strip.js +97 -39
  156. package/dist/dashboard/stat-strip.module.css +147 -17
  157. package/dist/dashboard/stat-strip.module.css.d.ts +8 -0
  158. package/dist/dashboard/visual-slot.d.ts +8 -0
  159. package/dist/dashboard/visual-slot.d.ts.map +1 -0
  160. package/dist/dashboard/visual-slot.js +7 -0
  161. package/dist/guard/index.d.ts +15 -2
  162. package/dist/guard/index.d.ts.map +1 -1
  163. package/dist/guard/index.js +14 -0
  164. package/dist/guard/palette.d.ts +51 -0
  165. package/dist/guard/palette.d.ts.map +1 -0
  166. package/dist/guard/palette.js +190 -0
  167. package/dist/guard/series-file.d.ts +15 -0
  168. package/dist/guard/series-file.d.ts.map +1 -0
  169. package/dist/guard/series-file.js +29 -0
  170. package/dist/guard/types.d.ts +3 -3
  171. package/dist/guard/types.d.ts.map +1 -1
  172. package/dist/guard/vx-names.d.ts.map +1 -1
  173. package/dist/guard/vx-names.js +2 -0
  174. package/dist/index.d.ts +1 -1
  175. package/dist/index.d.ts.map +1 -1
  176. package/dist/index.js +3 -1
  177. package/dist/styles.css +34 -2
  178. package/dist/surfaces.d.ts +1 -1
  179. package/dist/surfaces.d.ts.map +1 -1
  180. package/dist/surfaces.js +2 -0
  181. package/dist/theme/chrome-vars.d.ts +20 -5
  182. package/dist/theme/chrome-vars.d.ts.map +1 -1
  183. package/dist/theme/chrome-vars.js +46 -3
  184. package/dist/theme/controls.module.css +33 -0
  185. package/dist/theme/controls.module.css.d.ts +1 -0
  186. package/dist/theme/index.d.ts.map +1 -1
  187. package/dist/theme/index.js +11 -5
  188. package/dist/tokens/core.d.ts +2 -1
  189. package/dist/tokens/core.d.ts.map +1 -1
  190. package/dist/tokens/core.js +6 -1
  191. package/dist/tokens/derive.d.ts +3 -1
  192. package/dist/tokens/derive.d.ts.map +1 -1
  193. package/dist/tokens/derive.js +16 -1
  194. package/dist/tokens/oklab.d.ts +28 -0
  195. package/dist/tokens/oklab.d.ts.map +1 -0
  196. package/dist/tokens/oklab.js +73 -0
  197. package/dist/tokens/palette.d.ts +4 -0
  198. package/dist/tokens/palette.d.ts.map +1 -1
  199. package/dist/tokens/palette.js +4 -0
  200. package/dist/tokens/status-text.d.ts.map +1 -1
  201. package/dist/tokens/status-text.js +3 -1
  202. package/llms.txt +43 -23
  203. package/package.json +1 -1
  204. package/src/charts/BandStrip.tsx +33 -3
  205. package/src/charts/Bars.tsx +6 -0
  206. package/src/charts/CartesianChart.tsx +16 -3
  207. package/src/charts/ChartCard.tsx +43 -7
  208. package/src/charts/DualPanel.tsx +3 -6
  209. package/src/charts/Heatmap.tsx +54 -5
  210. package/src/charts/Lines.tsx +56 -10
  211. package/src/charts/MirroredBars.tsx +3 -5
  212. package/src/charts/Sparkline.tsx +50 -13
  213. package/src/charts/StackedArea.tsx +14 -4
  214. package/src/charts/band-fold.ts +31 -1
  215. package/src/charts/chart-box.module.css +19 -0
  216. package/src/charts/chart-card.module.css +25 -0
  217. package/src/charts/chart-legend.module.css +7 -0
  218. package/src/charts/compile.ts +210 -85
  219. package/src/charts/labels.ts +48 -14
  220. package/src/charts/legend.tsx +10 -20
  221. package/src/charts/marks.ts +1 -1
  222. package/src/charts/panel.tsx +1 -16
  223. package/src/charts/ref-label.ts +104 -0
  224. package/src/charts/scales.ts +22 -2
  225. package/src/charts/sparkline.module.css +10 -11
  226. package/src/charts/sparkline.module.css.d.ts +1 -2
  227. package/src/charts/state.tsx +12 -1
  228. package/src/charts/types.ts +14 -5
  229. package/src/cli/check-theme.ts +3 -0
  230. package/src/cli/doctor-upgrade.ts +1 -1
  231. package/src/cli/help.ts +18 -1
  232. package/src/cli/index.ts +48 -17
  233. package/src/cli/init.ts +102 -4
  234. package/src/cli/shot/capture.ts +266 -34
  235. package/src/cli/shot/detect.ts +550 -0
  236. package/src/cli/shot/index.ts +8 -4
  237. package/src/cli/shot/measure.ts +4 -0
  238. package/src/cli/shot/playwright.ts +2 -2
  239. package/src/cli/shot/types.ts +8 -1
  240. package/src/cli/sync.ts +4 -2
  241. package/src/common/retry-button.module.css +58 -0
  242. package/src/common/retry-button.module.css.d.ts +8 -0
  243. package/src/common/retry-button.tsx +29 -0
  244. package/src/controls/controls.module.css +11 -1
  245. package/src/controls/filter-pill.tsx +2 -0
  246. package/src/controls/sync-button.module.css +34 -2
  247. package/src/controls/sync-button.module.css.d.ts +1 -0
  248. package/src/controls/sync-button.tsx +40 -4
  249. package/src/dashboard/bar-list.module.css +53 -0
  250. package/src/dashboard/bar-list.module.css.d.ts +1 -0
  251. package/src/dashboard/bar-list.tsx +88 -76
  252. package/src/dashboard/index.ts +1 -0
  253. package/src/dashboard/meter-model.ts +265 -28
  254. package/src/dashboard/meter-parts.tsx +424 -163
  255. package/src/dashboard/meter.module.css +79 -65
  256. package/src/dashboard/meter.module.css.d.ts +7 -7
  257. package/src/dashboard/meter.tsx +77 -17
  258. package/src/dashboard/page-summary.module.css +89 -0
  259. package/src/dashboard/page-summary.module.css.d.ts +15 -0
  260. package/src/dashboard/page-summary.tsx +95 -0
  261. package/src/dashboard/query-state.tsx +4 -7
  262. package/src/dashboard/section.module.css +23 -0
  263. package/src/dashboard/section.module.css.d.ts +1 -0
  264. package/src/dashboard/section.tsx +59 -11
  265. package/src/dashboard/stat-card-parts.tsx +5 -1
  266. package/src/dashboard/stat-card.module.css +17 -0
  267. package/src/dashboard/stat-card.tsx +14 -5
  268. package/src/dashboard/stat-strip.module.css +147 -17
  269. package/src/dashboard/stat-strip.module.css.d.ts +8 -0
  270. package/src/dashboard/stat-strip.tsx +182 -56
  271. package/src/dashboard/visual-slot.ts +17 -0
  272. package/src/guard/index.ts +16 -4
  273. package/src/guard/palette.ts +261 -0
  274. package/src/guard/series-file.ts +51 -0
  275. package/src/guard/types.ts +3 -2
  276. package/src/guard/vx-names.ts +2 -0
  277. package/src/index.ts +2 -0
  278. package/src/styles.css +34 -2
  279. package/src/surfaces.ts +2 -0
  280. package/src/theme/__snapshots__/plain-theme.test.ts.snap +220 -60
  281. package/src/theme/chrome-vars.ts +77 -8
  282. package/src/theme/controls.module.css +33 -0
  283. package/src/theme/controls.module.css.d.ts +1 -0
  284. package/src/theme/index.ts +18 -5
  285. package/src/tokens/core.ts +4 -1
  286. package/src/tokens/derive.ts +32 -3
  287. package/src/tokens/oklab.ts +97 -0
  288. package/src/tokens/palette.ts +4 -0
  289. package/src/tokens/status-text.ts +2 -0
@@ -5,11 +5,70 @@
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 }
9
- export type MeterZone = { from: number; to: number; label: string; color?: string }
8
+ export type MeterSegment = {
9
+ value: number
10
+ label: string
11
+ color?: string
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.
15
+ */
16
+ tone?: 'muted'
17
+ forecast?: boolean
18
+ }
19
+ export type MeterZone = {
20
+ from: number
21
+ to: number
22
+ label: string
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.
26
+ */
27
+ tone?: 'muted'
28
+ /**
29
+ * A shorter name for a narrow track. The row tries `label`, then `short`, and only then hides the
30
+ * name; the holding zone always shows its full `label`. The described-by text keeps `label`.
31
+ */
32
+ short?: string
33
+ color?: string
34
+ }
10
35
  /** A mark on the track at `at` (in the reading's units). A label is captioned beside it. */
11
36
  export type MeterTick = { at: number; label?: string }
12
37
 
38
+ /** A colour of a `stops` gradient at `at` (in the reading's units); the colour is the consumer's. */
39
+ type MeterStop = { at: number; color: string }
40
+
41
+ /** A ramp step's strength in percent of its neutral: from `lo`, spread over `step` across the steps. */
42
+ export const RAMP_ZONE = { lo: 15, step: 45, solo: 40 }
43
+ export const RAMP_PART = { lo: 35, step: 50, solo: 60 }
44
+ /** Neighbouring steps stay at least this far apart (the contrast the zone ramp always had), up to `RAMP_MAX`. */
45
+ export const RAMP_MIN_STEP = 15
46
+ export const RAMP_MAX = 95
47
+
48
+ /**
49
+ * 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
51
+ * `RAMP_MAX` and merge, which is the cue to group parts.
52
+ */
53
+ export function rampStrength(index: number, count: number, ramp: typeof RAMP_ZONE): number {
54
+ if (count <= 1) return ramp.solo
55
+ return Math.min(RAMP_MAX, ramp.lo + index * Math.max(RAMP_MIN_STEP, ramp.step / (count - 1)))
56
+ }
57
+
58
+ function rampFill(
59
+ base: 'line' | 'muted',
60
+ index: number,
61
+ count: number,
62
+ ramp: typeof RAMP_ZONE,
63
+ ): string {
64
+ return `color-mix(in srgb, var(--vx-${base}) ${rampStrength(index, count, ramp)}%, transparent)`
65
+ }
66
+
67
+ /** A zone's fill: its own colour, else its step of the line (or, `tone="muted"`, the muted) ramp. */
68
+ export function zoneFill(zone: MeterZone, index: number, count: number): string {
69
+ return zone.color ?? rampFill(zone.tone === 'muted' ? 'muted' : 'line', index, count, RAMP_ZONE)
70
+ }
71
+
13
72
  /** One tick on the track: its percent position and what it says (caption `label`, aria `text`). */
14
73
  export type TickMark = { key: string; pct: number; label: string | undefined; text: string }
15
74
 
@@ -25,10 +84,25 @@ export type Part = {
25
84
  }
26
85
 
27
86
  /** One categorical part's caption: its position key, name, formatted amount and fill colour (`undefined` = neutral). */
28
- export type PartCaption = { key: string; label: string; text: string; color: string | undefined }
87
+ export type PartCaption = {
88
+ key: string
89
+ label: string
90
+ text: string
91
+ color: string | undefined
92
+ /** Where the part starts on the track, in percent: the caption sits under it when they all fit. */
93
+ startPct: number
94
+ }
29
95
 
30
- /** One zone's name on the band: where its band starts (percent) and whether it holds the reading. */
31
- export type ZoneLabel = { key: string; label: string; startPct: number; holding: boolean }
96
+ /** One zone's name on the label row: its span's centre and width (percent) and whether it holds the reading. */
97
+ export type ZoneLabel = {
98
+ key: string
99
+ label: string
100
+ /** The fallback name, when the zone has one. */
101
+ short?: string
102
+ centrePct: number
103
+ widthPct: number
104
+ holding: boolean
105
+ }
32
106
 
33
107
  /**
34
108
  * What the caption row says. `reading`: the formatted reading; `parts`: categorical segments name
@@ -63,6 +137,36 @@ export function tickMarks(
63
137
  )
64
138
  }
65
139
 
140
+ /** A `stops` gradient's colour list, in order along the track; `at` outside `[min, max]` clamps. */
141
+ export function stopsGradient(stops: readonly MeterStop[], min: number, span: number): string {
142
+ return stops
143
+ .toSorted((a, b) => a.at - b.at)
144
+ .map((s) => `${s.color} ${toPct(clamp01((s.at - min) / span))}%`)
145
+ .join(', ')
146
+ }
147
+
148
+ const ringAngle = (pct: number) => Math.round(pct * 3.6 * 100) / 100
149
+
150
+ /** A ring's marks (target, ticks): the angle clockwise from the top, and the accessible text. */
151
+ export function ringMarks(
152
+ target: number | undefined,
153
+ ticks: readonly MeterTick[] | undefined,
154
+ min: number,
155
+ max: number,
156
+ format: Format,
157
+ ): { angle: number; text: string }[] {
158
+ const { span } = spanOf(min, max)
159
+ const marks = tickMarks(ticks, min, max, format).map((t) => ({
160
+ angle: ringAngle(t.pct),
161
+ text: t.text,
162
+ }))
163
+ if (target === undefined || !Number.isFinite(target) || target < min || target > max) return marks
164
+ return [
165
+ { angle: ringAngle(toPct((target - min) / span)), text: `Target ${format(target)}` },
166
+ ...marks,
167
+ ]
168
+ }
169
+
66
170
  export const isReading = (n: number | null | undefined): n is number =>
67
171
  typeof n === 'number' && Number.isFinite(n)
68
172
 
@@ -92,6 +196,9 @@ export function normalizeSegments(segments: readonly MeterSegment[]): MeterSegme
92
196
  /** Segment amounts (already normalised) accumulate from `min`; a part never passes the track end. */
93
197
  export function layoutSegments(segments: readonly MeterSegment[], span: number): Part[] {
94
198
  let cursor = 0
199
+ const isMuted = (s: MeterSegment) => s.tone === 'muted' && s.color === undefined && !s.forecast
200
+ const mutedCount = segments.filter(isMuted).length
201
+ let muted = 0
95
202
  return segments.map((s) => {
96
203
  const size = Math.max(0, Math.min(clamp01(s.value / span), 1 - cursor))
97
204
  const forecast = s.forecast === true
@@ -99,7 +206,11 @@ export function layoutSegments(segments: readonly MeterSegment[], span: number):
99
206
  const part = {
100
207
  start: cursor,
101
208
  size,
102
- color: forecast ? undefined : s.color,
209
+ color: forecast
210
+ ? undefined
211
+ : isMuted(s)
212
+ ? rampFill('muted', muted++, mutedCount, RAMP_PART)
213
+ : s.color,
103
214
  forecast,
104
215
  label: s.label,
105
216
  amount: s.value,
@@ -122,18 +233,84 @@ export const LABEL_SUBPIXEL = 0.5
122
233
  export type LabelBox = { left: number; right: number }
123
234
 
124
235
  /**
125
- * Whether every zone name fits on the band: inside `[0, rowWidth]` and clear of every other name
126
- * (pairwise, so a long name cannot hide behind a short one between it and a distant neighbour).
127
- * Boxes are relative to the row's own start edge.
236
+ * A label on the shared row: its measured box and, for a zone name, the span it must stay inside.
237
+ * The holding zone's name never hides for width: it ignores its span and is shifted into the row.
128
238
  */
129
- export function zoneLabelsFit(boxes: readonly LabelBox[], rowWidth: number): boolean {
130
- const sorted = boxes.toSorted((a, b) => a.left - b.left)
131
- return sorted.every(
132
- (a, i) =>
133
- a.left >= -LABEL_SUBPIXEL &&
134
- a.right <= rowWidth + LABEL_SUBPIXEL &&
135
- sorted.slice(i + 1).every((b) => a.right <= b.left + LABEL_SUBPIXEL),
136
- )
239
+ export type RowLabel = {
240
+ box: LabelBox
241
+ span?: LabelBox
242
+ holding?: boolean
243
+ /** A tick at the track's start or end: it yields to every zone name it would touch. */
244
+ end?: boolean
245
+ short?: LabelBox
246
+ }
247
+
248
+ /** The placement of the row's labels, index-aligned with the input. */
249
+ export type RowPlacement = {
250
+ shown: boolean[]
251
+ /** Index-aligned with `shown`: the label shows as its `short` box rather than the full one. */
252
+ short: boolean[]
253
+ /** Pixels a shown label moves along the row's physical x axis to stay inside it (0 unless clamped). */
254
+ shift: number[]
255
+ }
256
+
257
+ /**
258
+ * Which labels of the row show, in input order. The holding zone's name claims its place first (it
259
+ * 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.
265
+ */
266
+ export function placeRowLabels(
267
+ labels: readonly RowLabel[],
268
+ rowWidth: number,
269
+ gap = 0,
270
+ ): RowPlacement {
271
+ const shown = labels.map(() => false)
272
+ const short = labels.map(() => false)
273
+ const shift = labels.map(() => 0)
274
+ const kept: { box: LabelBox; tick: boolean }[] = []
275
+ const rank = (l: RowLabel) => (l.holding ? 0 : l.end ? 3 : l.span ? 2 : 1)
276
+ const order = labels
277
+ .map((_, i) => i)
278
+ .toSorted((a, b) => rank(labels[a] as RowLabel) - rank(labels[b] as RowLabel))
279
+ // The gap step applies when either label of a pair is a tick; zone names keep to "not touching".
280
+ const clears = (b: LabelBox, tick: boolean) =>
281
+ !kept.some(({ box: k, tick: kTick }) => {
282
+ const pad = tick || kTick ? gap : 0
283
+ return b.left < k.right + pad - LABEL_SUBPIXEL && k.left < b.right + pad - LABEL_SUBPIXEL
284
+ })
285
+ for (const i of order) {
286
+ const label = labels[i] as RowLabel
287
+ const { span } = label
288
+ const tick = span === undefined && !label.holding
289
+ let box = label.box
290
+ if (label.holding) {
291
+ const width = box.right - box.left
292
+ const left = Math.max(0, Math.min(box.left, rowWidth - width))
293
+ shift[i] = left - box.left
294
+ box = { left, right: left + width }
295
+ if (!clears(box, false)) continue
296
+ shown[i] = true
297
+ kept.push({ box, tick: false })
298
+ continue
299
+ }
300
+ const fits = (b: LabelBox) =>
301
+ b.left >= -LABEL_SUBPIXEL &&
302
+ b.right <= rowWidth + LABEL_SUBPIXEL &&
303
+ (span === undefined ||
304
+ (b.left >= span.left - LABEL_SUBPIXEL && b.right <= span.right + LABEL_SUBPIXEL)) &&
305
+ clears(b, tick)
306
+ const useShort = !fits(box) && label.short !== undefined && fits(label.short)
307
+ if (useShort) box = label.short as LabelBox
308
+ else if (!fits(box)) continue
309
+ shown[i] = true
310
+ short[i] = useShort
311
+ kept.push({ box, tick })
312
+ }
313
+ return { shown, short, shift }
137
314
  }
138
315
 
139
316
  /** A zone band as left edge + width in percent, clipped to the track. */
@@ -153,6 +330,7 @@ export type BarInput = {
153
330
  format: Format
154
331
  segments?: readonly MeterSegment[] | undefined
155
332
  zones?: readonly MeterZone[] | undefined
333
+ stops?: readonly MeterStop[] | undefined
156
334
  target?: number | undefined
157
335
  ticks?: readonly MeterTick[] | undefined
158
336
  color?: string | undefined
@@ -169,6 +347,8 @@ export type BarModel = {
169
347
  captionMode: CaptionMode
170
348
  /** Only in `parts` mode: one caption per part, in order. Empty otherwise. */
171
349
  partCaptions: readonly PartCaption[]
350
+ /** The `stops` gradient's colour list; absent without `stops`. */
351
+ stopsCss: string | undefined
172
352
  /** Only on a zoned track: every zone's name, in zone order. Empty otherwise. */
173
353
  zoneLabels: readonly ZoneLabel[]
174
354
  ticks: readonly TickMark[]
@@ -186,8 +366,8 @@ function endCaptionText(reading: number, target: number | undefined, format: For
186
366
  return `Target ${format(target)}`
187
367
  }
188
368
 
189
- function fillParts(input: BarInput, span: number, zoned: boolean): Part[] {
190
- if (zoned) return []
369
+ function fillParts(input: BarInput, span: number, marked: boolean): Part[] {
370
+ if (marked) return []
191
371
  if (input.segments !== undefined) return layoutSegments(input.segments, span)
192
372
  const fill = { value: input.reading - input.min, label: '' }
193
373
  return layoutSegments([input.color === undefined ? fill : { ...fill, color: input.color }], span)
@@ -204,6 +384,7 @@ function partCaptionsOf(parts: readonly Part[], format: Format): PartCaption[] {
204
384
  label: p.label,
205
385
  text: format(p.amount),
206
386
  color: p.color,
387
+ startPct: toPct(p.start),
207
388
  }))
208
389
  }
209
390
 
@@ -228,13 +409,14 @@ export function startCaptionText(model: BarModel, override: ReactNode | undefine
228
409
  }
229
410
 
230
411
  export function modelBar(input: BarInput): BarModel {
231
- const { reading, min, max, format, segments, zones, target } = input
412
+ const { reading, min, max, format, segments, zones, target, stops } = input
232
413
  const ticks = tickMarks(input.ticks, min, max, format)
233
414
  const { span } = spanOf(min, max)
234
415
  const zoned = zones !== undefined && zones.length > 0
235
416
  const holding = zoned ? findHoldingZone(zones, reading) : undefined
236
417
 
237
- const parts = fillParts(input, span, zoned)
418
+ const striped = !zoned && stops !== undefined && stops.length > 0
419
+ const parts = fillParts(input, span, zoned || striped)
238
420
  const firstForecast = parts.findIndex((p) => p.forecast)
239
421
  const forecastLabels =
240
422
  segments?.filter((s) => s.forecast === true).map((s) => `${s.label} ${format(s.value)}`) ?? []
@@ -267,16 +449,22 @@ export function modelBar(input: BarInput): BarModel {
267
449
  : (input.forecastCaption ?? `Forecast: ${forecastLabels.join(', ')}`),
268
450
  captionMode,
269
451
  partCaptions,
452
+ stopsCss: striped ? stopsGradient(stops, min, span) : undefined,
270
453
  zoneLabels: !zoned
271
454
  ? []
272
- : zones.map((z, i) => ({
273
- key: String(i),
274
- label: z.label,
275
- startPct: zoneBand(z, min, span).start,
276
- holding: z === holding,
277
- })),
455
+ : zones.map((z, i) => {
456
+ const band = zoneBand(z, min, span)
457
+ return {
458
+ key: String(i),
459
+ label: z.label,
460
+ ...(z.short !== undefined && { short: z.short }),
461
+ centrePct: band.start + band.width / 2,
462
+ widthPct: band.width,
463
+ holding: z === holding,
464
+ }
465
+ }),
278
466
  ticks,
279
- markerPct: zoned ? toPct((reading - min) / span) : undefined,
467
+ markerPct: zoned || striped ? toPct((reading - min) / span) : undefined,
280
468
  targetPct: target === undefined ? undefined : toPct((target - min) / span),
281
469
  startText: format(reading),
282
470
  endText: endCaptionText(reading, target, format),
@@ -287,3 +475,52 @@ export function modelBar(input: BarInput): BarModel {
287
475
  /** The ring's one arc, as a fraction of the circumference. */
288
476
  export const ringFraction = (reading: number, min: number, max: number): number =>
289
477
  clamp01((reading - min) / spanOf(min, max).span)
478
+
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' }
481
+
482
+ /**
483
+ * Where each caption of the marks row starts (px, logical start edge), index-aligned with `items`: an
484
+ * 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.
487
+ */
488
+ export function placeMarkCaptions(
489
+ items: readonly MarkItem[],
490
+ rowWidth: number,
491
+ gap: number,
492
+ ): number[] | null {
493
+ if (rowWidth <= 0 || !items.some((i) => i.pct !== undefined)) return null
494
+ 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
505
+ )
506
+ })
507
+ return crowded ? null : lefts
508
+ }
509
+
510
+ /**
511
+ * Whether every categorical part's caption fits under its own segment: left-aligned at the part's
512
+ * start and ending before the next part's start (less `gap`) or the row's end. A zero-width part never fits.
513
+ */
514
+ export function partsFitUnder(
515
+ parts: readonly { startPct: number; width: number }[],
516
+ rowWidth: number,
517
+ gap: number,
518
+ ): boolean {
519
+ if (rowWidth <= 0) return false
520
+ return parts.every((p, i) => {
521
+ const left = (rowWidth * p.startPct) / 100
522
+ const next = parts[i + 1]
523
+ const limit = next === undefined ? rowWidth : (rowWidth * next.startPct) / 100 - gap
524
+ return left + p.width <= limit + LABEL_SUBPIXEL
525
+ })
526
+ }