basalt-ui 2.9.1 → 2.11.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 (82) hide show
  1. package/CHANGELOG.md +61 -38
  2. package/MIGRATING.md +34 -12
  3. package/agent/rules/basalt-charts.md +2 -2
  4. package/agent/skills/basalt-charts/SKILL.md +16 -17
  5. package/dist/charts/BandStrip.d.ts +9 -6
  6. package/dist/charts/BandStrip.d.ts.map +1 -1
  7. package/dist/charts/BandStrip.js +8 -8
  8. package/dist/charts/CartesianChart.d.ts +3 -0
  9. package/dist/charts/CartesianChart.d.ts.map +1 -1
  10. package/dist/charts/CartesianChart.js +13 -6
  11. package/dist/charts/DualPanel.d.ts.map +1 -1
  12. package/dist/charts/DualPanel.js +20 -15
  13. package/dist/charts/Heatmap.d.ts +33 -3
  14. package/dist/charts/Heatmap.d.ts.map +1 -1
  15. package/dist/charts/Heatmap.js +111 -68
  16. package/dist/charts/MirroredBars.d.ts +16 -5
  17. package/dist/charts/MirroredBars.d.ts.map +1 -1
  18. package/dist/charts/MirroredBars.js +46 -9
  19. package/dist/charts/Sparkline.d.ts +12 -0
  20. package/dist/charts/Sparkline.d.ts.map +1 -1
  21. package/dist/charts/Sparkline.js +68 -5
  22. package/dist/charts/band-fold.d.ts +13 -0
  23. package/dist/charts/band-fold.d.ts.map +1 -1
  24. package/dist/charts/band-fold.js +11 -1
  25. package/dist/charts/compile.d.ts +4 -1
  26. package/dist/charts/compile.d.ts.map +1 -1
  27. package/dist/charts/compile.js +22 -14
  28. package/dist/charts/heatmap-fill.d.ts +15 -0
  29. package/dist/charts/heatmap-fill.d.ts.map +1 -0
  30. package/dist/charts/heatmap-fill.js +13 -0
  31. package/dist/charts/host.d.ts +5 -1
  32. package/dist/charts/host.d.ts.map +1 -1
  33. package/dist/charts/host.js +37 -1
  34. package/dist/charts/index.d.ts +1 -1
  35. package/dist/charts/index.d.ts.map +1 -1
  36. package/dist/charts/legend.d.ts +2 -0
  37. package/dist/charts/legend.d.ts.map +1 -1
  38. package/dist/charts/legend.js +14 -10
  39. package/dist/charts/marks.d.ts +1 -1
  40. package/dist/charts/marks.d.ts.map +1 -1
  41. package/dist/charts/marks.js +28 -13
  42. package/dist/charts/panel.d.ts +7 -1
  43. package/dist/charts/panel.d.ts.map +1 -1
  44. package/dist/charts/panel.js +9 -4
  45. package/dist/charts/plot-context.d.ts +42 -0
  46. package/dist/charts/plot-context.d.ts.map +1 -0
  47. package/dist/charts/plot-context.js +64 -0
  48. package/dist/charts/scales.d.ts +3 -1
  49. package/dist/charts/scales.d.ts.map +1 -1
  50. package/dist/charts/scales.js +23 -5
  51. package/dist/charts/types.d.ts +36 -4
  52. package/dist/charts/types.d.ts.map +1 -1
  53. package/dist/controls/enum-filter.d.ts +4 -1
  54. package/dist/controls/enum-filter.d.ts.map +1 -1
  55. package/dist/controls/enum-filter.js +2 -1
  56. package/dist/controls/number-filter.d.ts.map +1 -1
  57. package/dist/controls/number-filter.js +2 -0
  58. package/dist/tokens/core.d.ts +6 -0
  59. package/dist/tokens/core.d.ts.map +1 -1
  60. package/dist/tokens/core.js +5 -3
  61. package/llms.txt +17 -8
  62. package/package.json +1 -1
  63. package/src/charts/BandStrip.tsx +19 -20
  64. package/src/charts/CartesianChart.tsx +16 -4
  65. package/src/charts/DualPanel.tsx +21 -16
  66. package/src/charts/Heatmap.tsx +181 -76
  67. package/src/charts/MirroredBars.tsx +71 -18
  68. package/src/charts/Sparkline.tsx +106 -5
  69. package/src/charts/band-fold.ts +32 -0
  70. package/src/charts/compile.ts +26 -14
  71. package/src/charts/heatmap-fill.ts +19 -0
  72. package/src/charts/host.tsx +59 -0
  73. package/src/charts/index.ts +1 -1
  74. package/src/charts/legend.tsx +21 -12
  75. package/src/charts/marks.ts +31 -14
  76. package/src/charts/panel.tsx +18 -5
  77. package/src/charts/plot-context.ts +109 -0
  78. package/src/charts/scales.ts +33 -5
  79. package/src/charts/types.ts +36 -4
  80. package/src/controls/enum-filter.tsx +5 -1
  81. package/src/controls/number-filter.tsx +6 -1
  82. package/src/tokens/core.ts +10 -3
@@ -0,0 +1,109 @@
1
+ /**
2
+ * The one place a `PlotContext` is built (`docs/V2-SPEC.md` § 9.2) and the plot-size feedback loop
3
+ * around it. The engine lays a plot out AFTER `marks` runs, so `plotWidth`/`plotHeight` cannot be
4
+ * known when `marks(plot)` is called: the first pass reports the frame, the host measures the laid-out
5
+ * plot per pane and hands it back, and a definition that READ the size is recompiled with it.
6
+ * `compileCartesian` and every composed kind (`panelDefinition`) build their context here, so all
7
+ * of them report the plot area the same way.
8
+ */
9
+ import type { ChartX, PlotContext } from './types'
10
+
11
+ /** A plot area in px. */
12
+ export type PlotSize = { width: number; height: number }
13
+
14
+ /** The laid-out plot area of every pane, by pane id. A single chart is one pane, {@link MAIN_PANE}. */
15
+ export type PlotSizes = Readonly<Record<string, PlotSize>>
16
+
17
+ export const MAIN_PANE = 'main'
18
+
19
+ /** Two measurements closer than this are the same plot: layout jitter must not trigger a recompile. */
20
+ export const PLOT_EPSILON_PX = 0.01
21
+
22
+ export const samePlot = (a: PlotSize, b: PlotSize): boolean =>
23
+ Math.abs(a.width - b.width) < PLOT_EPSILON_PX && Math.abs(a.height - b.height) < PLOT_EPSILON_PX
24
+
25
+ export function samePlots(a: PlotSizes | null, b: PlotSizes): boolean {
26
+ if (a === null) return false
27
+ const keys = Object.keys(b)
28
+ return (
29
+ keys.length === Object.keys(a).length &&
30
+ keys.every((k) => {
31
+ const prev = a[k]
32
+ const next = b[k]
33
+ return prev !== undefined && next !== undefined && samePlot(prev, next)
34
+ })
35
+ )
36
+ }
37
+
38
+ /** Per definition: did any `marks` read the plot size, and who to tell when that read comes late. */
39
+ type PlotReads = {
40
+ read: boolean
41
+ onLateRead: (() => void) | undefined
42
+ measured: PlotSizes | null
43
+ }
44
+ const READS = new WeakMap<object, PlotReads>()
45
+ const readsOf = (definition: object): PlotReads => {
46
+ const found = READS.get(definition)
47
+ if (found) return found
48
+ const created: PlotReads = { read: false, onLateRead: undefined, measured: null }
49
+ READS.set(definition, created)
50
+ return created
51
+ }
52
+
53
+ /** Whether `marks` read `plotWidth`/`plotHeight` while building this definition: only such a chart
54
+ * recompiles on the measured plot. A read is registered when the getter runs, so a branch-only read,
55
+ * a destructure and a lazy channel accessor all count. */
56
+ export const readsPlot = (definition: object): boolean => READS.get(definition)?.read === true
57
+
58
+ /** The host's hook for a read that happens AFTER it measured (an accessor the engine calls later):
59
+ * it fires once, on the first read, so the host can report what it already measured. */
60
+ export function watchPlotReads(definition: object, onLateRead: (() => void) | undefined): void {
61
+ readsOf(definition).onLateRead = onLateRead
62
+ }
63
+
64
+ /** The host records what it measured for a definition, so a late read can be answered at once. */
65
+ export const recordPlots = (definition: object, sizes: PlotSizes): void => {
66
+ readsOf(definition).measured = sizes
67
+ }
68
+ export const measuredPlots = (definition: object): PlotSizes | null =>
69
+ READS.get(definition)?.measured ?? null
70
+
71
+ /** What a pane's `PlotContext` reads its plot size through: the measurement and the read hook. */
72
+ export type PlotAccess = { size: () => PlotSize | null; read: () => void }
73
+
74
+ export function plotAccess(
75
+ definition: object,
76
+ sizes: PlotSizes | null | undefined,
77
+ pane: string,
78
+ ): PlotAccess {
79
+ return {
80
+ size: () => sizes?.[pane] ?? null,
81
+ read: () => {
82
+ const reads = readsOf(definition)
83
+ if (reads.read) return
84
+ reads.read = true
85
+ reads.onLateRead?.()
86
+ },
87
+ }
88
+ }
89
+
90
+ export type PlotBase<T, X extends ChartX> = Omit<PlotContext<T, X>, 'plotWidth' | 'plotHeight'>
91
+
92
+ /** A `PlotContext`: `base` plus `plotWidth`/`plotHeight` as read-registering getters, the frame
93
+ * (`base.width`/`height`) until the pane has been measured. */
94
+ export function plotContext<T, X extends ChartX>(
95
+ base: PlotBase<T, X>,
96
+ access: PlotAccess,
97
+ ): PlotContext<T, X> {
98
+ const measured = (side: keyof PlotSize): number => {
99
+ access.read()
100
+ return access.size()?.[side] ?? (side === 'width' ? base.width : base.height)
101
+ }
102
+ return Object.defineProperties(
103
+ { ...base },
104
+ {
105
+ plotWidth: { enumerable: true, get: () => measured('width') },
106
+ plotHeight: { enumerable: true, get: () => measured('height') },
107
+ },
108
+ ) as PlotContext<T, X>
109
+ }
@@ -94,6 +94,9 @@ export function xKindOf(values: readonly ChartX[]): XKind {
94
94
  return 'string'
95
95
  }
96
96
 
97
+ /** A continuous x value as a number: a `Date` by timestamp. */
98
+ const toNumber = (v: ChartX): number => (v instanceof Date ? v.getTime() : (v as number))
99
+
97
100
  const keyOf = (v: ChartX): number | string => (v instanceof Date ? v.getTime() : v)
98
101
 
99
102
  /** Distinct x values in data order (dates by timestamp). */
@@ -137,9 +140,31 @@ export type XScaleModel<X extends ChartX> = {
137
140
 
138
141
  const DAY_MS = 86_400_000
139
142
 
140
- export function xScaleFor<X extends ChartX>(raw: readonly X[], band: boolean): XScaleModel<X> {
143
+ /** The explicit x domain as `[min, max]` numbers, or `null` (unset, reversed, flat or not finite:
144
+ * the data's own extent stands, with a dev warning for a domain that was set but unusable). */
145
+ function explicitDomain(xDomain: readonly [ChartX, ChartX] | undefined): [number, number] | null {
146
+ if (xDomain === undefined) {
147
+ return null
148
+ }
149
+ const [min, max] = [toNumber(xDomain[0]), toNumber(xDomain[1])]
150
+ if (Number.isFinite(min) && Number.isFinite(max) && min < max) {
151
+ return [min, max]
152
+ }
153
+ if (process.env.NODE_ENV !== 'production') {
154
+ console.warn('[basalt] CartesianChart: `xDomain` needs finite min < max; using the data extent')
155
+ }
156
+ return null
157
+ }
158
+
159
+ export function xScaleFor<X extends ChartX>(
160
+ raw: readonly X[],
161
+ band: boolean,
162
+ /** Continuous scales only: the explicit `[min, max]` that replaces the data's own extent. */
163
+ xDomain?: readonly [X, X],
164
+ ): XScaleModel<X> {
141
165
  const values = distinctX(raw)
142
- const kind = xKindOf(values)
166
+ // The kind comes from the data; an empty series has none to read, so the domain stands in.
167
+ const kind = xKindOf(values.length > 0 || xDomain === undefined ? values : [xDomain[0]])
143
168
  const n = values.length
144
169
 
145
170
  if (band || kind === 'string') {
@@ -180,7 +205,7 @@ export function xScaleFor<X extends ChartX>(raw: readonly X[], band: boolean): X
180
205
  }
181
206
  }
182
207
 
183
- const nums = values.map((v) => (v instanceof Date ? v.getTime() : (v as number)))
208
+ const nums = values.map(toNumber)
184
209
  // A loop, not Math.min(...nums): a spread of a long series overflows the call stack.
185
210
  let lo = Infinity
186
211
  let hi = -Infinity
@@ -194,7 +219,10 @@ export function xScaleFor<X extends ChartX>(raw: readonly X[], band: boolean): X
194
219
  if (v > hi) hi = v
195
220
  }
196
221
  if (!Number.isFinite(lo)) [lo, hi] = [0, 1]
197
- if (lo === hi) {
222
+ const explicit = explicitDomain(xDomain)
223
+ if (explicit !== null) {
224
+ ;[lo, hi] = explicit
225
+ } else if (lo === hi) {
198
226
  // One sample: give the axis a day (or a unit) of width so the point sits in the middle.
199
227
  const half = kind === 'date' ? DAY_MS / 2 : 0.5
200
228
  lo -= half
@@ -217,7 +245,7 @@ export function xScaleFor<X extends ChartX>(raw: readonly X[], band: boolean): X
217
245
  extent: [lo, hi],
218
246
  step: medianGap,
219
247
  fraction: (x) => {
220
- const v = x instanceof Date ? x.getTime() : (x as number)
248
+ const v = toNumber(x)
221
249
  return typeof v === 'number' && Number.isFinite(v) ? (v - lo) / span : null
222
250
  },
223
251
  }
@@ -63,10 +63,13 @@ export type ChartSeries<T> = {
63
63
  /**
64
64
  * A cursor dot for a series whose mark the consumer draws itself (`marks`, so `getValue` stays
65
65
  * `null`): the y of the dot at each datum, `null` for none. The dot shows while the cursor is on
66
- * that datum's x and takes the series color. A series drawn from `plot.rows` already has one.
66
+ * that datum's x and takes the series color, or `getMarker(d).color` when the series has one: the
67
+ * dot follows the datum. A series drawn from `plot.rows` already has one.
67
68
  */
68
69
  cursorValue?: ((d: T) => number | null) | undefined
69
- /** A marker at a point (a PR star, a status dot); `null` for none. */
70
+ /** A marker at a point (a PR star, a status dot); `null` for none. On a `cursorValue` series
71
+ * only `color` applies, to the cursor dot, and each distinct color is one extra mark: keep colors
72
+ * a small categorical set (statuses, buckets), never a continuous scale. */
70
73
  getMarker?:
71
74
  | ((d: T) => {
72
75
  color?: string | undefined
@@ -241,9 +244,25 @@ export type PlotContext<T, X extends ChartX = ChartX> = {
241
244
  highlighted: string | null
242
245
  /** The measured container class (`regular` on the server). */
243
246
  containerClass: ContainerClass
244
- /** The chart's size in px, for a kind that lays out labels against the plot. */
247
+ /** The chart's FRAME in px (plot plus axis gutters), for a kind that lays out labels against it. */
245
248
  width: number
246
249
  height: number
250
+ /**
251
+ * The plot area in px: the frame less the axis gutters. Pin a mark in pixels with it (a 3 px
252
+ * rail along the top is `yMax - 3 * (yMax - yMin) / plot.plotHeight` down from `yMax`). The
253
+ * engine measures its gutters after `marks` runs, so the first pass (the server HTML, the first
254
+ * client frame) reports the FRAME, one frame-sized draw; the chart then re-renders once with the
255
+ * measured area, and only a chart whose `marks` read these fields pays that render. Every kind,
256
+ * single or composed, reports it the same way.
257
+ */
258
+ plotWidth: number
259
+ plotHeight: number
260
+ /**
261
+ * Every row of `data`, whatever the legend hides: raw datums (`T`), NOT `PlotRow`s. For bucket-level marks only (absent columns,
262
+ * rails: the slot grid must not vanish with its last visible series); a series' own marks still
263
+ * draw `plot.rows(key)` and gate on `plot.visible`.
264
+ */
265
+ data: readonly T[]
247
266
  /** Bind a mark to the right axis with `yScale: plot.y2` (present only with a `y2` config). */
248
267
  y2: 'y2' | undefined
249
268
  /** The resolved y domain (and the right axis', with `y2`): basalt's, never the engine's. */
@@ -268,6 +287,13 @@ export type CartesianChartProps<T, X extends ChartX = ChartX> = BasaltProps & {
268
287
  y2?: AxisConfig<T> | undefined
269
288
  /** A tick count, or the exact x tick values. Default: by container class. */
270
289
  xTicks?: number | readonly X[] | undefined
290
+ /**
291
+ * `[min, max]` of the x scale, in `x`'s type. Default: the data's own extent. Charts given the
292
+ * same domain put the same instant on the same pixel column, whatever their data spans; rows
293
+ * outside it are clipped. A domain that is reversed, flat or not finite falls back to the data
294
+ * extent (and warns in dev). A no-op on a string or band x (`Bars`, `MirroredBars`, `BandStrip`).
295
+ */
296
+ xDomain?: readonly [X, X] | undefined
271
297
  /** Tick label for an x value. Default: `DD.MM` for dates, compact numbers, strings verbatim. */
272
298
  formatX?: ((x: X) => string) | undefined
273
299
  zones?: readonly ZoneSpec[] | undefined
@@ -275,7 +301,13 @@ export type CartesianChartProps<T, X extends ChartX = ChartX> = BasaltProps & {
275
301
  refLines?: readonly RefLine[] | undefined
276
302
  /** `false` hides the legend. */
277
303
  legend?: ChartLegendConfig | false | undefined
278
- /** Default: the height law (measured width × class ratio, 160–420px, ≤ 45vh on touch). */
304
+ /**
305
+ * Default: the height law (measured width × class ratio, 160–420px, ≤ 45vh on touch). Always the
306
+ * FRAME, in every kind: the plot plus the axis the engine measures. A `BandStrip` band is the
307
+ * frame less the x axis (about 21 px at the default font and density, so `height: 65` for a
308
+ * 44 px band). The box is CSS-owned before any measurement exists, so the plot area is read back
309
+ * from the engine's layout (`PlotContext.plotWidth`/`plotHeight`), never an input.
310
+ */
279
311
  height?: number | ResponsiveChartHeight | undefined
280
312
  state?: ChartState | undefined
281
313
  /** Joins a named cursor group instead of the page-shared default. */
@@ -65,6 +65,9 @@ export type EnumFilterProps<T extends string> = BasaltProps & {
65
65
  * into, and `exactOptionalPropertyTypes` rejects a possibly-undefined member on a bare optional.
66
66
  */
67
67
  readonly options?: readonly FilterOption[] | undefined
68
+ /** Pill text for a value no rendered option matches, ahead of `label` — the name `.labels()` gave
69
+ * an unset state that is deliberately not a row (`NumberFilter`'s preset form). */
70
+ readonly unmatchedLabel?: string | undefined
68
71
  /**
69
72
  * Mono pill label — forwarded to `FilterPill.numeric`. Set by `NumberFilter`, whose values ARE
70
73
  * numbers (`1` / `2` / `7`), and by nothing else: the same law `[data-numeric]` applies to a
@@ -80,6 +83,7 @@ export function EnumFilter<T extends string>({
80
83
  clearable,
81
84
  numeric,
82
85
  options: optionsProp,
86
+ unmatchedLabel,
83
87
  className,
84
88
  style,
85
89
  }: EnumFilterProps<T>): ReactNode {
@@ -163,7 +167,7 @@ export function EnumFilter<T extends string>({
163
167
 
164
168
  return (
165
169
  <FilterPill
166
- label={selected?.label ?? label}
170
+ label={selected?.label ?? unmatchedLabel ?? label}
167
171
  active={!isDefault}
168
172
  {...(numeric === true && { numeric: true })}
169
173
  {...(icon !== undefined && { icon })}
@@ -58,7 +58,8 @@
58
58
  *
59
59
  * @example
60
60
  * // "No filter" is the fallback, not a row: `clearable` writes it back, and the pill reads the
61
- * // filter's name while no preset is picked.
61
+ * // filter's name while no preset is picked — or the name `.labels({ difficulty: { 0: 'Any' } })`
62
+ * // gave the fallback, in this form as in the stepper's.
62
63
  * // difficulty: field.number({ fallback: 0, min: 0, max: 5, int: true })
63
64
  * <NumberFilter field={library.field.difficulty} label="Difficulty" options={LEVELS} clearable />
64
65
  */
@@ -176,6 +177,9 @@ function NumberChoice({
176
177
  }): ReactNode {
177
178
  const [value, setValue] = field.use()
178
179
  const rows = options.map((option) => ({ value: String(option.value), label: option.label }))
180
+ // The same `.labels()` naming the stepper pill reads: a value it names (`{ 0: 'Any' }`) prints
181
+ // that name though it is not a row, so "no filter" needs no literal `Any` option.
182
+ const named = field.options.find((option) => option.value === String(value))?.label
179
183
  const choice: ChoiceHandle<string> = {
180
184
  fallback: String(field.fallback),
181
185
  options: rows,
@@ -201,6 +205,7 @@ function NumberChoice({
201
205
  label={label}
202
206
  numeric
203
207
  options={rows}
208
+ {...(named !== undefined && { unmatchedLabel: named })}
204
209
  {...(clearable === true && { clearable: true })}
205
210
  {...(icon !== undefined && { icon })}
206
211
  {...(className !== undefined && { className })}
@@ -347,6 +347,13 @@ export function pxRem(px: number): string {
347
347
  return `${px / 16}rem`
348
348
  }
349
349
 
350
+ /**
351
+ * Reference-line strength: the status solid's share over the panel, in percent. One value for the
352
+ * three lines so they read as one family; 60 keeps the faintest (light-scheme `warn`) at a 2:1
353
+ * composite against the page, where the old 20–30% sat at 1.25–1.5 (`ref-line-contrast.test.ts`).
354
+ */
355
+ export const REF_LINE_ALPHA = 60
356
+
350
357
  /** Emit one prefixed group of pairs for a given scheme side. */
351
358
  const groupSide = (prefix: string, map: SeriesMap, side: Side): string =>
352
359
  Object.entries(map)
@@ -713,9 +720,9 @@ function frameworkDerived(data: PaletteData, only: 'core' | 'all'): string {
713
720
  decl('good-soft', 'color-mix(in srgb, var(--vx-good-solid) 8%, transparent)'),
714
721
  decl('bad', 'color-mix(in srgb, var(--vx-bad-solid) 18%, transparent)'),
715
722
  decl('warn', 'color-mix(in srgb, var(--vx-warn-solid) 8%, transparent)'),
716
- decl('good-ref', 'color-mix(in srgb, var(--vx-good-solid) 30%, transparent)'),
717
- decl('bad-ref', 'color-mix(in srgb, var(--vx-bad-solid) 30%, transparent)'),
718
- decl('warn-ref', 'color-mix(in srgb, var(--vx-warn-solid) 20%, transparent)'),
723
+ ...(['good', 'bad', 'warn'] as const).map((k) =>
724
+ decl(`${k}-ref`, `color-mix(in srgb, var(--vx-${k}-solid) ${REF_LINE_ALPHA}%, transparent)`),
725
+ ),
719
726
  ].join('\n')
720
727
  return canonical
721
728
  }