basalt-ui 2.7.3 → 2.8.1

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 (70) hide show
  1. package/CHANGELOG.md +194 -172
  2. package/MIGRATING.md +137 -16
  3. package/agent/rules/basalt-batteries.md +1 -1
  4. package/agent/rules/basalt-controls.md +1 -1
  5. package/agent/rules/basalt-mantine.md +1 -1
  6. package/agent/skills/basalt-app/SKILL.md +7 -7
  7. package/agent/templates/AGENTS-block.md.tpl +1 -1
  8. package/dist/charts/BandStrip.d.ts +13 -0
  9. package/dist/charts/BandStrip.d.ts.map +1 -1
  10. package/dist/charts/BandStrip.js +142 -44
  11. package/dist/cli/doctor-upgrade.d.ts +16 -4
  12. package/dist/cli/doctor-upgrade.d.ts.map +1 -1
  13. package/dist/cli/doctor-upgrade.js +132 -96
  14. package/dist/cli/doctor.d.ts +2 -1
  15. package/dist/cli/doctor.d.ts.map +1 -1
  16. package/dist/cli/doctor.js +21 -13
  17. package/dist/cli/help.js +1 -1
  18. package/dist/cli/index.d.ts +18 -4
  19. package/dist/cli/index.d.ts.map +1 -1
  20. package/dist/cli/index.js +25 -24
  21. package/dist/cli/init.d.ts.map +1 -1
  22. package/dist/cli/init.js +10 -4
  23. package/dist/cli/router-wiring.d.ts +37 -0
  24. package/dist/cli/router-wiring.d.ts.map +1 -0
  25. package/dist/cli/router-wiring.js +116 -0
  26. package/dist/cli/source-text.d.ts +12 -0
  27. package/dist/cli/source-text.d.ts.map +1 -0
  28. package/dist/cli/source-text.js +46 -0
  29. package/dist/cli/sync.d.ts.map +1 -1
  30. package/dist/cli/sync.js +32 -2
  31. package/dist/content/code-block.d.ts.map +1 -1
  32. package/dist/content/code-block.js +36 -27
  33. package/dist/content/code-block.module.css +19 -1
  34. package/dist/content/mermaid-sanitize.d.ts +15 -0
  35. package/dist/content/mermaid-sanitize.d.ts.map +1 -0
  36. package/dist/content/mermaid-sanitize.js +28 -0
  37. package/dist/content/mermaid.d.ts +6 -0
  38. package/dist/content/mermaid.d.ts.map +1 -1
  39. package/dist/content/mermaid.js +3 -1
  40. package/dist/provider/index.d.ts +22 -4
  41. package/dist/provider/index.d.ts.map +1 -1
  42. package/dist/provider/index.js +24 -8
  43. package/dist/provider/test-router.d.ts +2 -1
  44. package/dist/provider/test-router.d.ts.map +1 -1
  45. package/dist/provider/test-router.js +6 -7
  46. package/dist/vite-boot.d.ts +25 -0
  47. package/dist/vite-boot.d.ts.map +1 -0
  48. package/dist/vite-boot.js +36 -0
  49. package/dist/vite.d.ts +24 -3
  50. package/dist/vite.d.ts.map +1 -1
  51. package/dist/vite.js +7 -16
  52. package/llms.txt +37 -4
  53. package/package.json +1 -1
  54. package/src/charts/BandStrip.tsx +223 -48
  55. package/src/cli/doctor-upgrade.ts +181 -141
  56. package/src/cli/doctor.ts +23 -14
  57. package/src/cli/help.ts +1 -1
  58. package/src/cli/index.ts +37 -36
  59. package/src/cli/init.ts +10 -6
  60. package/src/cli/router-wiring.ts +185 -0
  61. package/src/cli/source-text.ts +62 -0
  62. package/src/cli/sync.ts +36 -2
  63. package/src/content/code-block.module.css +19 -1
  64. package/src/content/code-block.tsx +7 -2
  65. package/src/content/mermaid-sanitize.ts +49 -0
  66. package/src/content/mermaid.tsx +3 -2
  67. package/src/provider/index.tsx +71 -18
  68. package/src/provider/test-router.tsx +9 -9
  69. package/src/vite-boot.ts +109 -0
  70. package/src/vite.ts +32 -61
@@ -2,7 +2,8 @@
2
2
  * `BandStrip` — a 1-D state strip: one gap-free band per slot over the shared x axis, no y
3
3
  * dimension (`docs/V2-SPEC.md` § 9.3). Each band is in one of the `states`, tinted by the state's
4
4
  * color (or an intensity `fill` of the datum's own), optionally carrying an inset `marker`.
5
- * Absence is a state like any other: color it `VX.neutral` for a flat, unhatched fill.
5
+ * Absence is a state like any other: color it `VX.neutral` for a flat, unhatched fill. Name it
6
+ * with `absentState` and a folded band can carry an `absentFraction` share of it.
6
7
  *
7
8
  * The rung supplies the legend, cursor, crosshair and tooltip. The visible bands are decorative
8
9
  * rects on the rung's `[0, 1]` zone scale (a band scale pads its slots, a strip must not), and an
@@ -14,7 +15,7 @@ import { decorative } from '@tanstack/charts/mark/decorative'
14
15
  import { rect } from '@tanstack/charts/rect'
15
16
  import { useCallback, useMemo } from 'react'
16
17
  import type { ReactElement } from 'react'
17
- import { oneOf } from '../common/errors'
18
+ import { oneOf, requiredProp } from '../common/errors'
18
19
  import { NON_FINITE } from '../format/shared'
19
20
  import { VX, alpha } from '../tokens'
20
21
  import { CartesianRung } from './CartesianChart'
@@ -48,6 +49,12 @@ export type BandSpan = {
48
49
  state: string
49
50
  /** Overrides the state's fill — an intensity ramp WITHIN a state. `alpha(VX.*, …)`, never a hex. */
50
51
  fill?: string | undefined
52
+ /**
53
+ * 0..1 of the band's width that no source sample covers, painted from its right edge in the
54
+ * `absentState` color. A fold owes the reader this: a band whose merge reports the one measured
55
+ * member of three would otherwise read as fully measured. Needs `absentState` on the strip.
56
+ */
57
+ absentFraction?: number | undefined
51
58
  /**
52
59
  * A shorter, inset bar over the band, for a fact that must not be read off the fill ramp (a
53
60
  * renegotiation, a restart). Its color is a `states` entry, so the legend names it.
@@ -82,6 +89,13 @@ export type BandStripProps<T, X extends ChartX = ChartX> = Omit<
82
89
  /** Which state, fill and marker each datum has. Memoize it: a new function recompiles the chart. */
83
90
  getBand: (d: T) => BandSpan
84
91
  fold?: BandFold<T> | undefined
92
+ /**
93
+ * The `states` key that means "not measured". A band in it is flat-filled like any state; a
94
+ * band in another state paints its `absentFraction` share in this state's color, and the
95
+ * tooltip adds this state's row. Absent stays a legended, flat `VX.neutral` state, never a
96
+ * hatch: it reads as "no data" by hue and by sitting at the band's right edge, not as zero.
97
+ */
98
+ absentState?: string | undefined
85
99
  }
86
100
 
87
101
  const DEFAULT_MIN_BAND_PX = 3
@@ -106,7 +120,7 @@ type Layer = { fill: string; opacity: number; cells: Cell[] }
106
120
  /** A run of adjacent slots drawn as one band; `items` are the source data it stands for. */
107
121
  type Group<T> = { start: number; items: T[] }
108
122
  /** A slot that answers the pointer: its own source datum, and the (possibly merged) one it reads. */
109
- type Hit<T> = { source: T; state: string; datum: T }
123
+ type Hit<T> = { slot: number; source: T; state: string; datum: T }
110
124
 
111
125
  /** Adjacent data in groups of equal size, the last possibly shorter, at most `cap` groups. */
112
126
  function groupsOf<T>(data: readonly T[], cap: number): Group<T>[] {
@@ -132,6 +146,7 @@ type LayoutInput<T> = {
132
146
  data: readonly T[]
133
147
  getBand: (d: T) => BandSpan
134
148
  fold: BandFold<T> | undefined
149
+ absentState: string | undefined
135
150
  states: ReadonlyMap<string, BandStripSeries<T>>
136
151
  /** State keys the legend has not toggled off. */
137
152
  visible: ReadonlySet<string>
@@ -141,9 +156,113 @@ type LayoutInput<T> = {
141
156
  emphasis: (key: string) => number
142
157
  }
143
158
 
159
+ /** NaN and out-of-range fractions (a 0/0 on an un-folded slot) must not reach an SVG attribute. */
160
+ const clampFraction = (f: number | undefined): number =>
161
+ f === undefined || Number.isNaN(f) ? 0 : Math.min(1, Math.max(0, f))
162
+
163
+ /**
164
+ * The share of a band the strip's absent state owns. A band already in that state is absent in
165
+ * whole, so the fraction only qualifies the others.
166
+ */
167
+ function absentShareOf(span: BandSpan, absentKey: string | undefined): number {
168
+ return absentKey === undefined || span.state === absentKey
169
+ ? 0
170
+ : clampFraction(span.absentFraction)
171
+ }
172
+
173
+ /**
174
+ * The one visibility-aware split that paint, hits and the tooltip all read: the absent share that
175
+ * is drawn (a hidden absent state gives its share back to the primary part) and whether the
176
+ * primary part is drawn (a hidden primary state leaves its part unpainted, the absent part
177
+ * untouched). `isVisible` is the legend's test, in whichever form the caller holds it.
178
+ */
179
+ function resolveAbsent(
180
+ span: BandSpan,
181
+ absentKey: string | undefined,
182
+ isVisible: (key: string) => boolean,
183
+ ): { share: number; primaryShown: boolean } {
184
+ const share = absentKey !== undefined && isVisible(absentKey) ? absentShareOf(span, absentKey) : 0
185
+ return { share, primaryShown: share < 1 && isVisible(span.state) }
186
+ }
187
+
188
+ /** A state's tooltip value: its own reading, an em dash for `null`, empty when the name says it all. */
189
+ const stateValue = <T,>(s: BandStripSeries<T>, d: T): string =>
190
+ s.formatValue ? (s.formatValue(d) ?? NON_FINITE) : ''
191
+
144
192
  const unknownState = (prop: string, got: string, known: Iterable<string>): Error =>
145
193
  new Error(oneOf('BandStrip', prop, [...known], got))
146
194
 
195
+ type PaintCtx<T> = {
196
+ base: Map<string, Layer>
197
+ markers: Map<string, Layer>
198
+ states: ReadonlyMap<string, BandStripSeries<T>>
199
+ visible: ReadonlySet<string>
200
+ height: number
201
+ emphasis: (key: string) => number
202
+ /** The strip's absent state, when it names a real entry of `states`. */
203
+ absentKey: string | undefined
204
+ }
205
+
206
+ /**
207
+ * Paints one band: its primary part (when shown), its absent share from the right edge, and its
208
+ * marker over the primary part. Returns which states answer the pointer for the band's slots.
209
+ */
210
+ function paintSpan<T>(
211
+ ctx: PaintCtx<T>,
212
+ span: BandSpan,
213
+ cell: { f0: number; f1: number },
214
+ ): { primary: boolean; absent: boolean } {
215
+ const { states, visible, absentKey } = ctx
216
+ const state = states.get(span.state)
217
+ if (!state && process.env.NODE_ENV !== 'production')
218
+ throw unknownState('BandSpan.state', span.state, states.keys())
219
+ if (
220
+ absentKey === undefined &&
221
+ clampFraction(span.absentFraction) > 0 &&
222
+ process.env.NODE_ENV !== 'production'
223
+ )
224
+ throw new Error(
225
+ requiredProp('BandStrip', 'absentState', 'a BandSpan carries an absentFraction'),
226
+ )
227
+ const { share, primaryShown } = resolveAbsent(span, absentKey, (k) => visible.has(k))
228
+ // An unknown state (production only) has no visibility to toggle: it always paints.
229
+ const primary = state ? primaryShown : share < 1
230
+ const absentStyle = absentKey === undefined ? undefined : states.get(absentKey)
231
+ const split = cell.f1 - (cell.f1 - cell.f0) * share
232
+ if (primary)
233
+ paintInto(
234
+ ctx.base,
235
+ span.fill ?? (state ? alpha(state.color, state.fillOpacity ?? 1) : UNKNOWN_FILL),
236
+ ctx.emphasis(span.state),
237
+ { ...cell, f1: split, y0: 0, y1: 1 },
238
+ )
239
+ if (share > 0 && absentStyle && absentKey !== undefined)
240
+ paintInto(
241
+ ctx.base,
242
+ alpha(absentStyle.color, absentStyle.fillOpacity ?? 1),
243
+ ctx.emphasis(absentKey),
244
+ { f0: split, f1: cell.f1, y0: 0, y1: 1 },
245
+ )
246
+
247
+ if (span.marker && primary) {
248
+ const markerKey = span.marker.state ?? span.state
249
+ const markerState = states.get(markerKey)
250
+ if (!markerState && process.env.NODE_ENV !== 'production')
251
+ throw unknownState('BandSpan.marker.state', markerKey, states.keys())
252
+ if (!markerState || visible.has(markerKey)) {
253
+ const inset = (span.marker.inset ?? DEFAULT_MARKER_INSET) / Math.max(ctx.height, 1)
254
+ paintInto(ctx.markers, markerState?.color ?? UNKNOWN_FILL, ctx.emphasis(markerKey), {
255
+ ...cell,
256
+ f1: split,
257
+ y0: inset,
258
+ y1: 1 - inset,
259
+ })
260
+ }
261
+ }
262
+ // An unknown state (production only) answers no pointer: a tooltip row needs its series.
263
+ return { primary: primary && state !== undefined, absent: share > 0 }
264
+ }
265
+
147
266
  /**
148
267
  * Where every band and marker goes, and which slots answer the pointer. Pure: base layers and
149
268
  * marker layers come back separately so the caller can paint all markers after all bands (a
@@ -154,56 +273,59 @@ function layoutBands<T>(input: LayoutInput<T>): {
154
273
  markers: Layer[]
155
274
  hits: Hit<T>[]
156
275
  } {
157
- const { data, getBand, fold, states, visible } = input
276
+ const { data, getBand, fold, absentState: absentKey } = input
158
277
  const cap = Math.floor(input.width / (fold?.minBandPx ?? DEFAULT_MIN_BAND_PX))
159
278
  const groups: Group<T>[] =
160
279
  fold && data.length > cap
161
280
  ? groupsOf(data, cap)
162
281
  : data.map((d, start) => ({ start, items: [d] }))
163
- const base = new Map<string, Layer>()
164
- const markers = new Map<string, Layer>()
282
+ const ctx: PaintCtx<T> = {
283
+ base: new Map(),
284
+ markers: new Map(),
285
+ states: input.states,
286
+ visible: input.visible,
287
+ height: input.height,
288
+ emphasis: input.emphasis,
289
+ absentKey,
290
+ }
165
291
  const hits: Hit<T>[] = []
166
292
 
167
293
  for (const g of groups) {
168
294
  const datum = fold && g.items.length > 1 ? fold.merge(g.items) : g.items[0]
169
295
  if (datum === undefined) continue
170
296
  const span = getBand(datum)
171
- const state = states.get(span.state)
172
- if (!state && process.env.NODE_ENV !== 'production')
173
- throw unknownState('BandSpan.state', span.state, states.keys())
174
- if (state && !visible.has(span.state)) continue
175
297
  const cell = { f0: g.start / data.length, f1: (g.start + g.items.length) / data.length }
176
- const opacity = input.emphasis(span.state)
177
- paintInto(
178
- base,
179
- span.fill ?? (state ? alpha(state.color, state.fillOpacity ?? 1) : UNKNOWN_FILL),
180
- opacity,
181
- { ...cell, y0: 0, y1: 1 },
182
- )
183
-
184
- if (span.marker) {
185
- const markerKey = span.marker.state ?? span.state
186
- const markerState = states.get(markerKey)
187
- if (!markerState && process.env.NODE_ENV !== 'production')
188
- throw unknownState('BandSpan.marker.state', markerKey, states.keys())
189
- if (!markerState || visible.has(markerKey)) {
190
- const inset = (span.marker.inset ?? DEFAULT_MARKER_INSET) / Math.max(input.height, 1)
191
- paintInto(markers, markerState?.color ?? UNKNOWN_FILL, input.emphasis(markerKey), {
192
- ...cell,
193
- y0: inset,
194
- y1: 1 - inset,
195
- })
196
- }
197
- }
198
- // An unknown state (production only) answers no pointer: a tooltip row needs its series.
199
- if (state) for (const source of g.items) hits.push({ source, state: span.state, datum })
298
+ const answers = paintSpan(ctx, span, cell)
299
+ g.items.forEach((source, i) => {
300
+ const slot = g.start + i
301
+ if (answers.primary) hits.push({ slot, source, state: span.state, datum })
302
+ if (answers.absent && absentKey !== undefined)
303
+ hits.push({ slot, source, state: absentKey, datum })
304
+ })
200
305
  }
201
- return { base: [...base.values()], markers: [...markers.values()], hits }
306
+ return { base: [...ctx.base.values()], markers: [...ctx.markers.values()], hits }
202
307
  }
203
308
 
204
309
  export function BandStrip<T, X extends ChartX = ChartX>(props: BandStripProps<T, X>): ReactElement {
205
- const { states, getBand, fold, cursorResolution = 'leading', tooltip, ...rest } = props
310
+ const {
311
+ states,
312
+ getBand,
313
+ fold,
314
+ absentState,
315
+ cursorResolution = 'leading',
316
+ tooltip,
317
+ ...rest
318
+ } = props
206
319
  const { data, x } = rest
320
+ // Resolved once: a misconfigured absent state degrades to primary-only paint, hits and rows.
321
+ const absentKey =
322
+ absentState !== undefined && states.some((s) => s.key === absentState) ? absentState : undefined
323
+ if (absentState !== undefined && absentKey === undefined && process.env.NODE_ENV !== 'production')
324
+ throw unknownState(
325
+ 'BandStrip.absentState',
326
+ absentState,
327
+ states.map((s) => s.key),
328
+ )
207
329
 
208
330
  const series = useMemo<ChartSeries<T>[]>(
209
331
  () =>
@@ -215,10 +337,15 @@ export function BandStrip<T, X extends ChartX = ChartX>(props: BandStripProps<T,
215
337
  ...(s.tooltip !== undefined && { tooltip: s.tooltip }),
216
338
  ...(s.legend !== undefined && { legend: s.legend }),
217
339
  ...(s.note !== undefined && { note: s.note }),
218
- getValue: (d) => (getBand(d).state === s.key ? 1 : null),
219
- formatValue: (_v, d) => (s.formatValue ? (s.formatValue(d) ?? NON_FINITE) : ''),
340
+ getValue: (d) => {
341
+ const span = getBand(d)
342
+ const share = absentShareOf(span, absentKey)
343
+ if (span.state === s.key) return share < 1 ? 1 : null
344
+ return s.key === absentKey && share > 0 ? 1 : null
345
+ },
346
+ formatValue: (_v, d) => stateValue(s, d),
220
347
  })),
221
- [states, getBand],
348
+ [states, getBand, absentKey],
222
349
  )
223
350
 
224
351
  const marks = useCallback(
@@ -227,18 +354,19 @@ export function BandStrip<T, X extends ChartX = ChartX>(props: BandStripProps<T,
227
354
  data,
228
355
  getBand,
229
356
  fold,
357
+ absentState: absentKey,
230
358
  states: new Map(states.map((s) => [s.key, s])),
231
359
  visible: new Set(plot.visible.map((s) => s.key)),
232
360
  width: plot.width,
233
361
  height: plot.height,
234
362
  emphasis: (key) => emphasis(plot.highlighted, { key }),
235
363
  })
236
- const rows: PlotRow<T, X>[] = hits.map((h) => ({
237
- x: x(h.source),
238
- y: 1,
239
- series: h.state,
240
- datum: h.datum,
241
- }))
364
+ const rowKeys = new Map<PlotRow<T, X>, string>()
365
+ const rows: PlotRow<T, X>[] = hits.map((h) => {
366
+ const row = { x: x(h.source), y: 1, series: h.state, datum: h.datum }
367
+ rowKeys.set(row, `${h.slot}|${h.state}`)
368
+ return row
369
+ })
242
370
  return [
243
371
  ...[...base, ...markers].map(({ fill, opacity, cells }, i) =>
244
372
  decorative(
@@ -256,13 +384,60 @@ export function BandStrip<T, X extends ChartX = ChartX>(props: BandStripProps<T,
256
384
  ),
257
385
  ),
258
386
  ...(rows.length > 0
259
- ? [barY(rows, { id: 'hit', x: rowX, y1: 0, y2: 1, fill: 'transparent' })]
387
+ ? [
388
+ barY(rows, {
389
+ id: 'hit',
390
+ // One row per source slot and state, whatever the x values look like.
391
+ key: (r) => rowKeys.get(r) ?? '',
392
+ x: rowX,
393
+ y1: 0,
394
+ y2: 1,
395
+ fill: 'transparent',
396
+ }),
397
+ ]
260
398
  : []),
261
399
  ]
262
400
  },
263
- [states, getBand, fold, data, x],
401
+ [states, getBand, fold, absentKey, data, x],
264
402
  )
265
403
 
404
+ // `getValue` cannot see the legend, so a fully absent band has no primary row. When the absent
405
+ // state is hidden the strip paints the primary fill instead, and its row is added back here by
406
+ // the same `resolveAbsent` that paints it.
407
+ const tooltipWithPrimary = useMemo<BandStripProps<T, X>['tooltip']>(() => {
408
+ if (tooltip === false || absentKey === undefined) return tooltip
409
+ return {
410
+ ...tooltip,
411
+ rows: (d, ctx) => {
412
+ const span = getBand(d)
413
+ const primary = states.find((s) => s.key === span.state)
414
+ const { primaryShown } = resolveAbsent(span, absentKey, (k) =>
415
+ ctx.visible.some((s) => s.key === k),
416
+ )
417
+ const readdPrimary =
418
+ primary?.tooltip !== false &&
419
+ primary &&
420
+ primaryShown &&
421
+ absentShareOf(span, absentKey) === 1
422
+ return [
423
+ ...(readdPrimary
424
+ ? [
425
+ {
426
+ label:
427
+ primary.note === undefined
428
+ ? primary.label
429
+ : `${primary.label} · ${primary.note}`,
430
+ value: stateValue(primary, d),
431
+ color: primary.color,
432
+ },
433
+ ]
434
+ : []),
435
+ ...(tooltip?.rows?.(d, ctx) ?? []),
436
+ ]
437
+ },
438
+ }
439
+ }, [tooltip, absentKey, getBand, states])
440
+
266
441
  return (
267
442
  <CartesianRung
268
443
  {...rest}
@@ -272,7 +447,7 @@ export function BandStrip<T, X extends ChartX = ChartX>(props: BandStripProps<T,
272
447
  y={STRIP_AXIS}
273
448
  xZones={ZX_ANCHOR}
274
449
  cursorResolution={cursorResolution}
275
- {...(tooltip !== undefined && { tooltip })}
450
+ {...(tooltipWithPrimary !== undefined && { tooltip: tooltipWithPrimary })}
276
451
  />
277
452
  )
278
453
  }