basalt-ui 2.19.0 → 2.20.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 (157) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/MIGRATING.md +174 -12
  3. package/agent/rules/basalt-charts.md +1 -1
  4. package/agent/rules/basalt-mantine.md +1 -1
  5. package/agent/skills/basalt-app/SKILL.md +1 -1
  6. package/agent/skills/basalt-design/SKILL.md +1 -1
  7. package/agent/templates/DESIGN.md.tpl +1 -1
  8. package/configs/oxlint-plugin.js +33 -19
  9. package/configs/oxlint-plugin.test.ts +32 -2
  10. package/dist/charts/ChartCard.d.ts +28 -3
  11. package/dist/charts/ChartCard.d.ts.map +1 -1
  12. package/dist/charts/ChartCard.js +33 -11
  13. package/dist/charts/Donut.d.ts.map +1 -1
  14. package/dist/charts/Donut.js +13 -1
  15. package/dist/charts/Heatmap.js +1 -1
  16. package/dist/charts/Sparkline.d.ts +7 -1
  17. package/dist/charts/Sparkline.d.ts.map +1 -1
  18. package/dist/charts/Sparkline.js +137 -18
  19. package/dist/charts/box.d.ts +2 -0
  20. package/dist/charts/box.d.ts.map +1 -1
  21. package/dist/charts/box.js +2 -0
  22. package/dist/charts/chart-box.module.css +30 -0
  23. package/dist/charts/chart-box.module.css.d.ts +1 -0
  24. package/dist/charts/chart-card.module.css +47 -0
  25. package/dist/charts/chart-card.module.css.d.ts +7 -0
  26. package/dist/charts/compile.d.ts.map +1 -1
  27. package/dist/charts/compile.js +35 -8
  28. package/dist/charts/host.d.ts.map +1 -1
  29. package/dist/charts/host.js +2 -1
  30. package/dist/charts/index.d.ts +1 -1
  31. package/dist/charts/index.d.ts.map +1 -1
  32. package/dist/charts/sparkline.module.css +59 -0
  33. package/dist/charts/sparkline.module.css.d.ts +13 -0
  34. package/dist/charts/state.d.ts.map +1 -1
  35. package/dist/charts/state.js +32 -4
  36. package/dist/charts/types.d.ts +6 -0
  37. package/dist/charts/types.d.ts.map +1 -1
  38. package/dist/controls/sync-button.d.ts +3 -0
  39. package/dist/controls/sync-button.d.ts.map +1 -1
  40. package/dist/dashboard/bar-list.d.ts +15 -4
  41. package/dist/dashboard/bar-list.d.ts.map +1 -1
  42. package/dist/dashboard/bar-list.js +39 -15
  43. package/dist/dashboard/bar-list.module.css +77 -2
  44. package/dist/dashboard/bar-list.module.css.d.ts +1 -0
  45. package/dist/dashboard/delta-badge.d.ts +8 -3
  46. package/dist/dashboard/delta-badge.d.ts.map +1 -1
  47. package/dist/dashboard/delta-badge.js +9 -2
  48. package/dist/dashboard/index.d.ts +2 -0
  49. package/dist/dashboard/index.d.ts.map +1 -1
  50. package/dist/dashboard/meter-model.d.ts +19 -0
  51. package/dist/dashboard/meter-model.d.ts.map +1 -1
  52. package/dist/dashboard/meter-model.js +18 -1
  53. package/dist/dashboard/meter-parts.d.ts +5 -1
  54. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  55. package/dist/dashboard/meter-parts.js +114 -74
  56. package/dist/dashboard/meter.d.ts +22 -1
  57. package/dist/dashboard/meter.d.ts.map +1 -1
  58. package/dist/dashboard/meter.js +4 -1
  59. package/dist/dashboard/meter.module.css +90 -12
  60. package/dist/dashboard/meter.module.css.d.ts +5 -0
  61. package/dist/dashboard/query-state.d.ts.map +1 -1
  62. package/dist/dashboard/query-state.js +14 -9
  63. package/dist/dashboard/settings-section.d.ts.map +1 -1
  64. package/dist/dashboard/settings-section.js +14 -25
  65. package/dist/dashboard/stat-card-parts.js +1 -1
  66. package/dist/dashboard/stat-card.d.ts +1 -1
  67. package/dist/dashboard/stat-card.d.ts.map +1 -1
  68. package/dist/dashboard/stat-card.js +3 -6
  69. package/dist/dashboard/stat-card.module.css +15 -7
  70. package/dist/dashboard/stat-group.d.ts +11 -2
  71. package/dist/dashboard/stat-group.d.ts.map +1 -1
  72. package/dist/dashboard/stat-group.js +9 -7
  73. package/dist/dashboard/stat-strip.d.ts +50 -0
  74. package/dist/dashboard/stat-strip.d.ts.map +1 -0
  75. package/dist/dashboard/stat-strip.js +85 -0
  76. package/dist/dashboard/stat-strip.module.css +208 -0
  77. package/dist/dashboard/stat-strip.module.css.d.ts +28 -0
  78. package/dist/dashboard/widget-header.d.ts +3 -3
  79. package/dist/dashboard/widget-header.module.css +10 -6
  80. package/dist/format/date.d.ts.map +1 -1
  81. package/dist/format/date.js +4 -3
  82. package/dist/format/shared.d.ts +6 -0
  83. package/dist/format/shared.d.ts.map +1 -1
  84. package/dist/format/shared.js +6 -2
  85. package/dist/guard/index.d.ts +1 -1
  86. package/dist/guard/index.js +1 -1
  87. package/dist/index.d.ts +3 -1
  88. package/dist/index.d.ts.map +1 -1
  89. package/dist/index.js +4 -0
  90. package/dist/styles.css +6 -0
  91. package/dist/theme/card-inset.module.css +76 -0
  92. package/dist/theme/card-inset.module.css.d.ts +9 -0
  93. package/dist/theme/controls.module.css +3 -0
  94. package/dist/theme/index.d.ts.map +1 -1
  95. package/dist/theme/index.js +23 -0
  96. package/dist/theme/nav-link.module.css +8 -10
  97. package/dist/theme/status-dot.d.ts +3 -2
  98. package/dist/theme/status-dot.d.ts.map +1 -1
  99. package/dist/theme/status-dot.js +12 -3
  100. package/dist/theme/status-verdict.d.ts +3 -2
  101. package/dist/theme/status-verdict.d.ts.map +1 -1
  102. package/dist/theme/status-verdict.js +3 -2
  103. package/dist/theme/status-verdict.module.css +4 -2
  104. package/llms.txt +30 -6
  105. package/package.json +1 -1
  106. package/src/charts/ChartCard.tsx +70 -4
  107. package/src/charts/Donut.tsx +9 -1
  108. package/src/charts/Heatmap.tsx +1 -1
  109. package/src/charts/Sparkline.tsx +180 -18
  110. package/src/charts/box.tsx +2 -0
  111. package/src/charts/chart-box.module.css +30 -0
  112. package/src/charts/chart-box.module.css.d.ts +1 -0
  113. package/src/charts/chart-card.module.css +47 -0
  114. package/src/charts/chart-card.module.css.d.ts +7 -0
  115. package/src/charts/compile.ts +42 -9
  116. package/src/charts/host.tsx +6 -3
  117. package/src/charts/index.ts +1 -1
  118. package/src/charts/sparkline.module.css +59 -0
  119. package/src/charts/sparkline.module.css.d.ts +13 -0
  120. package/src/charts/state.tsx +44 -4
  121. package/src/charts/types.ts +6 -0
  122. package/src/controls/sync-button.tsx +3 -0
  123. package/src/dashboard/bar-list.module.css +77 -2
  124. package/src/dashboard/bar-list.module.css.d.ts +1 -0
  125. package/src/dashboard/bar-list.tsx +86 -18
  126. package/src/dashboard/delta-badge.tsx +21 -5
  127. package/src/dashboard/index.ts +2 -0
  128. package/src/dashboard/meter-model.ts +35 -0
  129. package/src/dashboard/meter-parts.tsx +119 -23
  130. package/src/dashboard/meter.module.css +90 -12
  131. package/src/dashboard/meter.module.css.d.ts +5 -0
  132. package/src/dashboard/meter.tsx +26 -2
  133. package/src/dashboard/query-state.tsx +28 -19
  134. package/src/dashboard/settings-section.tsx +1 -8
  135. package/src/dashboard/stat-card-parts.tsx +1 -1
  136. package/src/dashboard/stat-card.module.css +15 -7
  137. package/src/dashboard/stat-card.tsx +5 -8
  138. package/src/dashboard/stat-group.tsx +19 -8
  139. package/src/dashboard/stat-strip.module.css +208 -0
  140. package/src/dashboard/stat-strip.module.css.d.ts +28 -0
  141. package/src/dashboard/stat-strip.tsx +183 -0
  142. package/src/dashboard/widget-header.module.css +10 -6
  143. package/src/dashboard/widget-header.tsx +3 -3
  144. package/src/format/date.ts +6 -3
  145. package/src/format/shared.ts +11 -2
  146. package/src/guard/index.ts +1 -1
  147. package/src/index.ts +7 -0
  148. package/src/styles.css +6 -0
  149. package/src/theme/__snapshots__/plain-theme.test.ts.snap +168 -24
  150. package/src/theme/card-inset.module.css +76 -0
  151. package/src/theme/card-inset.module.css.d.ts +9 -0
  152. package/src/theme/controls.module.css +3 -0
  153. package/src/theme/index.ts +30 -0
  154. package/src/theme/nav-link.module.css +8 -10
  155. package/src/theme/status-dot.tsx +17 -5
  156. package/src/theme/status-verdict.module.css +4 -2
  157. package/src/theme/status-verdict.tsx +9 -4
@@ -87,14 +87,25 @@ const bodyClipStyle: CSSProperties = {
87
87
  padding: '2px var(--vx-space-card-inset-x, 1rem) var(--vx-space-card-inset-y, 0.75rem)',
88
88
  }
89
89
 
90
+ /** One headline figure: the value is the hero, then its unit, a comparison node and the period. */
91
+ export type ChartFact = {
92
+ value: ReactNode
93
+ unit?: string | undefined
94
+ /** Usually a `DeltaBadge`. */
95
+ comparison?: ReactNode
96
+ /** Plain text the figure covers, e.g. `last 30 d`. */
97
+ period?: string | undefined
98
+ }
99
+
90
100
  /** The three boxes a `ChartCard` paints. */
91
101
  export type ChartCardSlot = 'root' | 'header' | 'body'
92
102
 
93
- export type ChartCardProps = BasaltProps &
103
+ type ChartCardBaseProps = BasaltProps &
94
104
  SlotStylesProps<ChartCardSlot> & {
95
105
  title?: string | undefined
96
106
  icon?: ReactNode
97
- /** A muted line below the title row; does not by itself render the header. */
107
+ /** A muted line below the title row; does not by itself render the header. With `facts` it is the
108
+ * basis/as-of line after the facts row (always visible, never folded into the info glyph). */
98
109
  subtitle?: string | undefined
99
110
  /** `WidgetHeader`'s info glyph beside the title. */
100
111
  info?: string | undefined
@@ -118,6 +129,23 @@ export type ChartCardProps = BasaltProps &
118
129
  children?: ReactNode
119
130
  }
120
131
 
132
+ /**
133
+ * `facts` replaces the legacy `value`/`delta` hero: the two are mutually exclusive in the type. The
134
+ * facts show only at rest: while `state` is pending, errored or empty the figure would sit above a
135
+ * skeleton or a placeholder that contradicts it, so the header keeps its title row alone.
136
+ */
137
+ export type ChartCardProps = ChartCardBaseProps &
138
+ (
139
+ | {
140
+ /** The headline figure(s) under the title: value, unit, comparison, period. Wraps at half
141
+ * width, never clips. Replaces `value`/`delta`. */
142
+ facts: ChartFact | readonly ChartFact[]
143
+ value?: undefined
144
+ delta?: undefined
145
+ }
146
+ | { facts?: undefined }
147
+ )
148
+
121
149
  /** Drops explicit `undefined`s: `WidgetHeader` is exact-optional, this props type is not. */
122
150
  const definedOnly = <T extends object>(o: T): { [K in keyof T]?: Exclude<T[K], undefined> } =>
123
151
  Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined)) as {
@@ -127,6 +155,37 @@ const definedOnly = <T extends object>(o: T): { [K in keyof T]?: Exclude<T[K], u
127
155
  /** A header renders when one of these is set (a subtitle or delta alone does not). */
128
156
  const HEADER_TRIGGERS = ['title', 'info', 'value', 'icon', 'count'] as const
129
157
 
158
+ function Facts({
159
+ facts,
160
+ basis,
161
+ }: {
162
+ facts: ChartFact | readonly ChartFact[]
163
+ basis?: string | undefined
164
+ }): ReactElement {
165
+ const list: readonly ChartFact[] = Array.isArray(facts) ? facts : [facts as ChartFact]
166
+ return (
167
+ <div data-basalt-facts="" className={classes.facts}>
168
+ <div className={classes.factsRow}>
169
+ {list.map((f, i) => (
170
+ <span key={i} data-basalt-fact="" className={classes.fact}>
171
+ <span data-basalt-fact-value="" className={classes.factValue}>
172
+ {f.value}
173
+ </span>
174
+ {f.unit !== undefined && <span className={classes.factUnit}>{f.unit}</span>}
175
+ {f.comparison}
176
+ {f.period !== undefined && <span className={classes.factPeriod}>{f.period}</span>}
177
+ </span>
178
+ ))}
179
+ </div>
180
+ {basis !== undefined && (
181
+ <p data-basalt-facts-basis="" className={classes.factsBasis}>
182
+ {basis}
183
+ </p>
184
+ )}
185
+ </div>
186
+ )
187
+ }
188
+
130
189
  export function ChartCard({
131
190
  actions,
132
191
  state,
@@ -136,11 +195,17 @@ export function ChartCard({
136
195
  classNames,
137
196
  style,
138
197
  children,
198
+ facts,
139
199
  ...header
140
200
  }: ChartCardProps): ReactElement {
141
201
  const { ref, short } = useShortCard()
142
202
  const resolved = resolveChartState(state)
143
- const hasHeader = actions !== undefined || HEADER_TRIGGERS.some((k) => header[k] !== undefined)
203
+ const showFacts = facts !== undefined && resolved === null
204
+ const hasHeader =
205
+ actions !== undefined || showFacts || HEADER_TRIGGERS.some((k) => header[k] !== undefined)
206
+ // With facts the subtitle is the basis line after them, not WidgetHeader's foldable row.
207
+ const { subtitle, ...rest } = header
208
+ const headerProps = facts === undefined ? header : rest
144
209
 
145
210
  return (
146
211
  <ChartCardContext.Provider value>
@@ -156,12 +221,13 @@ export function ChartCard({
156
221
  <div className={cx(classes.header, classNames?.header)}>
157
222
  <WidgetHeader
158
223
  tier="widget"
159
- {...definedOnly(header)}
224
+ {...definedOnly(headerProps)}
160
225
  title={header.title ?? ''}
161
226
  {...(actions !== undefined && {
162
227
  actions: <span data-basalt-tier="widget">{actions}</span>,
163
228
  })}
164
229
  />
230
+ {showFacts && <Facts facts={facts} basis={subtitle} />}
165
231
  </div>
166
232
  )}
167
233
  <div
@@ -239,7 +239,15 @@ export function Donut<K extends string = string>(props: DonutProps<K>): ReactEle
239
239
  {resolved === null && (centerLabel !== undefined || centerSubLabel !== undefined) && (
240
240
  <div style={centerStyle}>
241
241
  {centerLabel !== undefined && (
242
- <span style={{ color: VX.ink, fontSize: VX.text.lg, fontWeight: 600 }}>
242
+ <span
243
+ style={{
244
+ color: VX.ink,
245
+ fontFamily: 'var(--basalt-font-numeral, var(--basalt-font-sans))',
246
+ fontVariantNumeric: 'lining-nums',
247
+ fontSize: VX.text.lg,
248
+ fontWeight: 600,
249
+ }}
250
+ >
243
251
  {centerLabel}
244
252
  </span>
245
253
  )}
@@ -261,7 +261,7 @@ export function Heatmap<T, K extends string = string>(props: HeatmapProps<T, K>)
261
261
  const ticks = { line: false as const, size: 0 }
262
262
  const cls = resolveContainerClass(width)
263
263
  const tickLabels = {
264
- fontSize: cls === 'wide' ? VX.text.micro : VX.text.nano,
264
+ fontSize: VX.text.micro,
265
265
  opacity: 1,
266
266
  thin: true,
267
267
  }
@@ -8,18 +8,22 @@
8
8
  import { dot } from '@tanstack/charts/dot'
9
9
  import { decorative } from '@tanstack/charts/mark/decorative'
10
10
  import { lineY } from '@tanstack/charts/line'
11
+ import { ruleY } from '@tanstack/charts/rule'
11
12
  import { scaleBand } from '@tanstack/charts/scales/band'
12
13
  import { scaleLinear } from '@tanstack/charts/scales/linear'
13
- import { useId, useMemo } from 'react'
14
- import type { ReactElement } from 'react'
14
+ import { useId, useMemo, useState } from 'react'
15
+ import type { FocusEvent, KeyboardEvent, PointerEvent, ReactElement } from 'react'
16
+ import { cx } from '../common/props'
15
17
  import type { BasaltProps } from '../common/props'
16
18
  import { VX } from '../tokens'
17
19
  import type { CartesianDefinition } from './compile'
18
20
  import { chartCurve } from './curves'
19
21
  import { barMark } from './marks'
22
+ import { defaultNumberFormat } from './compile'
20
23
  import { ChartEngine, idPrefixOf } from './host'
24
+ import classes from './sparkline.module.css'
21
25
  import { ChartPlaceholder, resolveChartState } from './state'
22
- import { CHART_THEME } from './theme'
26
+ import { CHART_THEME, DASH } from './theme'
23
27
  import type { ChartState } from './types'
24
28
 
25
29
  export type SparklineProps = BasaltProps & {
@@ -43,6 +47,13 @@ export type SparklineProps = BasaltProps & {
43
47
  /** Pending / error / empty, the same contract as every other kind. The placeholder fills the
44
48
  * sparkline's own `width` × `height`, so a loading tile does not shift when the trend lands. */
45
49
  state?: ChartState | undefined
50
+ /** A quiet dashed hairline at a value, or at the mean of the plotted values (`'mean'`). The y
51
+ * domain widens to include it. Not a legend item, never a status colour. */
52
+ reference?: number | 'mean' | undefined
53
+ /** Formats the readout's value. Default: the compact number format. */
54
+ format?: ((value: number) => string) | undefined
55
+ /** The readout's x label per point, index-aligned with `data`. Omitted: the value alone. */
56
+ labels?: readonly string[] | undefined
46
57
  }
47
58
 
48
59
  type Point = { i: number; v: number | null }
@@ -165,13 +176,45 @@ export function poolSparkline(
165
176
  return mark === 'bar' ? poolBar(rows, cap) : poolLine(rows, cap)
166
177
  }
167
178
 
179
+ type Frame = { x: readonly [number, number]; y: readonly [number, number] }
180
+
181
+ /** The domains a series is drawn in — shared by the compile and the readout's dot, which
182
+ * must land where the mark did. A bar's x is a band, so only its y is a continuous domain. */
183
+ function frameOf(
184
+ [lo, hi]: readonly [number, number],
185
+ mark: 'line' | 'bar',
186
+ count: number,
187
+ hasEnd: boolean,
188
+ ): Frame {
189
+ if (mark === 'bar') return { x: [0, count], y: [Math.min(lo, 0), Math.max(hi, 0) || 1] }
190
+ // A flat or single-point series needs an extent; 10% air keeps the stroke inside the box.
191
+ const pad = (hi - lo) * 0.1 || 1
192
+ // Half a dot of air at each end, in index units, so the last-point dot is not clipped.
193
+ const xPad = hasEnd ? (count - 1) * 0.04 || 0.5 : 0
194
+ return { x: [-xPad, Math.max(count - 1, 1) + xPad], y: [lo - pad, hi + pad] }
195
+ }
196
+
168
197
  function compileSparkline(
169
198
  source: readonly Point[],
170
- [lo, hi]: readonly [number, number],
199
+ extentOf: readonly [number, number],
171
200
  mark: 'line' | 'bar',
172
201
  color: string,
173
202
  emphasisColor: string | null,
203
+ reference: number | null,
174
204
  ): CartesianDefinition {
205
+ const frame = frameOf(extentOf, mark, source.length, emphasisColor !== null)
206
+ const referenceMark = (): readonly ReturnType<typeof ruleY>[] =>
207
+ reference === null
208
+ ? []
209
+ : [
210
+ ruleY([reference], {
211
+ id: 'reference',
212
+ stroke: VX.line,
213
+ strokeOpacity: 1,
214
+ strokeWidth: 1,
215
+ strokeDasharray: DASH,
216
+ }),
217
+ ]
175
218
  return {
176
219
  chart: ({ width }) => {
177
220
  const rows = poolSparkline(source, width, mark)
@@ -179,6 +222,7 @@ function compileSparkline(
179
222
  if (mark === 'bar') {
180
223
  return {
181
224
  marks: [
225
+ ...referenceMark(),
182
226
  barMark(rows, {
183
227
  id: 'bars',
184
228
  x: 'i',
@@ -195,7 +239,7 @@ function compileSparkline(
195
239
  axis: false as const,
196
240
  },
197
241
  y: {
198
- scale: scaleLinear().domain([Math.min(lo, 0), Math.max(hi, 0) || 1]),
242
+ scale: scaleLinear().domain(frame.y),
199
243
  axis: false as const,
200
244
  },
201
245
  },
@@ -204,12 +248,9 @@ function compileSparkline(
204
248
  margin: 0,
205
249
  }
206
250
  }
207
- // A flat or single-point series needs an extent; 10% air keeps the stroke inside the box.
208
- const pad = (hi - lo) * 0.1 || 1
209
- // Half a dot of air at each end, in index units, so the last-point dot is not clipped.
210
- const xPad = emphasisColor === null ? 0 : (source.length - 1) * 0.04 || 0.5
211
251
  return {
212
252
  marks: [
253
+ ...referenceMark(),
213
254
  lineY(rows, {
214
255
  id: 'line',
215
256
  x: 'i',
@@ -228,10 +269,10 @@ function compileSparkline(
228
269
  ],
229
270
  scales: {
230
271
  x: {
231
- scale: scaleLinear().domain([-xPad, Math.max(source.length - 1, 1) + xPad]),
272
+ scale: scaleLinear().domain(frame.x),
232
273
  axis: false as const,
233
274
  },
234
- y: { scale: scaleLinear().domain([lo - pad, hi + pad]), axis: false as const },
275
+ y: { scale: scaleLinear().domain(frame.y), axis: false as const },
235
276
  },
236
277
  theme: CHART_THEME,
237
278
  guides: false,
@@ -241,7 +282,31 @@ function compileSparkline(
241
282
  }
242
283
  }
243
284
 
244
- /** `useId` output (`«r1»`, `:r1:`) is not a safe SVG id fragment. */
285
+ /** An `aria-hidden` ancestor makes the sparkline decorative, whatever its own props say. */
286
+ const hidden = (el: HTMLElement): boolean => el.closest('[aria-hidden="true"]') !== null
287
+
288
+ /** The point `from` steps to along `dir`, skipping gaps; `from` itself when there is none. */
289
+ function stepFrom(rows: readonly Point[], from: number, dir: 1 | -1): number {
290
+ for (let i = from + dir; i >= 0 && i < rows.length; i += dir) if (rows[i]?.v !== null) return i
291
+ return from
292
+ }
293
+
294
+ const lastFinite = (rows: readonly Point[]): number | null => {
295
+ const i = rows.findLastIndex((r) => r.v !== null)
296
+ return i < 0 ? null : i
297
+ }
298
+
299
+ /** The mean of the plotted (finite) values. */
300
+ function meanOf(rows: readonly Point[]): number {
301
+ let sum = 0
302
+ let n = 0
303
+ for (const r of rows) {
304
+ if (r.v === null) continue
305
+ sum += r.v
306
+ n += 1
307
+ }
308
+ return sum / n
309
+ }
245
310
 
246
311
  export function Sparkline(props: SparklineProps): ReactElement {
247
312
  const {
@@ -254,34 +319,131 @@ export function Sparkline(props: SparklineProps): ReactElement {
254
319
  emphasizeLast = true,
255
320
  emphasisColor = VX.line,
256
321
  state,
322
+ reference,
323
+ format = defaultNumberFormat,
324
+ labels,
257
325
  className,
258
326
  style,
259
327
  } = props
260
328
  const id = useId()
261
- const definition = useMemo(() => {
329
+ // Two sources, so a pointer leaving a focused chart hands the readout back to the focused point.
330
+ const [hover, setHover] = useState<number | null>(null)
331
+ const [keyed, setKeyed] = useState<number | null>(null)
332
+ const active = hover ?? keyed
333
+ const model = useMemo(() => {
262
334
  const rows: Point[] = data.map((v, i) => ({ i, v: finite(v) }))
263
335
  const range = extent(rows)
264
336
  // No finite value means nothing to scale: the empty render, never an Infinity domain.
265
337
  if (range === null) return null
266
- return compileSparkline(rows, range, mark, color, emphasizeLast ? emphasisColor : null)
267
- }, [data, mark, color, emphasizeLast, emphasisColor])
338
+ const at = reference === 'mean' ? meanOf(rows) : (reference ?? null)
339
+ const ref = at !== null && Number.isFinite(at) ? at : null
340
+ // The reference is part of the extent, so it is never clipped.
341
+ const full: readonly [number, number] =
342
+ ref === null ? range : [Math.min(range[0], ref), Math.max(range[1], ref)]
343
+ const end = emphasizeLast ? emphasisColor : null
344
+ return {
345
+ rows,
346
+ frame: frameOf(full, mark, rows.length, end !== null),
347
+ definition: compileSparkline(rows, full, mark, color, end, ref),
348
+ }
349
+ }, [data, mark, color, emphasizeLast, emphasisColor, reference])
268
350
  const resolved = resolveChartState(state)
351
+ const rows = model?.rows
352
+ const interactive = resolved === null && model !== null
353
+
354
+ const onPointerMove = (e: PointerEvent<HTMLDivElement>): void => {
355
+ if (!interactive || rows === undefined || hidden(e.currentTarget)) return
356
+ const box = e.currentTarget.getBoundingClientRect()
357
+ if (box.width <= 0) return
358
+ const frac = Math.min(Math.max((e.clientX - box.left) / box.width, 0), 1)
359
+ const [x0, x1] = model.frame.x
360
+ const i =
361
+ mark === 'bar'
362
+ ? Math.min(Math.floor(frac * rows.length), rows.length - 1)
363
+ : Math.min(Math.max(Math.round(x0 + frac * (x1 - x0)), 0), rows.length - 1)
364
+ setHover(rows[i]?.v === null ? null : i)
365
+ }
366
+ const onKeyDown = (e: KeyboardEvent<HTMLDivElement>): void => {
367
+ if (!interactive || rows === undefined || hidden(e.currentTarget)) return
368
+ const from = keyed ?? lastFinite(rows)
369
+ if (from === null) return
370
+ const next =
371
+ e.key === 'ArrowLeft'
372
+ ? stepFrom(rows, from, -1)
373
+ : e.key === 'ArrowRight'
374
+ ? stepFrom(rows, from, 1)
375
+ : e.key === 'Home'
376
+ ? stepFrom(rows, -1, 1)
377
+ : e.key === 'End'
378
+ ? stepFrom(rows, rows.length, -1)
379
+ : e.key === 'Escape'
380
+ ? null
381
+ : undefined
382
+ if (next === undefined) return
383
+ e.preventDefault()
384
+ setHover(null)
385
+ setKeyed(next)
386
+ }
387
+ const onFocus = (e: FocusEvent<HTMLDivElement>): void => {
388
+ if (!interactive || rows === undefined || keyed !== null || hidden(e.currentTarget)) return
389
+ setKeyed(lastFinite(rows))
390
+ }
391
+
392
+ const value = active === null ? null : (rows?.[active]?.v ?? null)
393
+ const at =
394
+ interactive && active !== null && value !== null
395
+ ? {
396
+ value,
397
+ fx:
398
+ mark === 'bar'
399
+ ? (active + 0.5) / model.rows.length
400
+ : (active - model.frame.x[0]) / (model.frame.x[1] - model.frame.x[0]),
401
+ fy: (model.frame.y[1] - value) / (model.frame.y[1] - model.frame.y[0]),
402
+ label: labels?.[active],
403
+ }
404
+ : null
269
405
  return (
270
406
  <div
271
407
  data-basalt-sparkline=""
272
- {...(className !== undefined && { className })}
408
+ role="presentation"
409
+ className={cx(classes.root, className)}
273
410
  style={{ width: width ?? '100%', height, minWidth: 0, ...style }}
411
+ onPointerMove={onPointerMove}
412
+ onPointerLeave={() => setHover(null)}
413
+ onKeyDown={onKeyDown}
414
+ onFocus={onFocus}
415
+ onBlur={() => {
416
+ setHover(null)
417
+ setKeyed(null)
418
+ }}
274
419
  >
275
420
  {resolved !== null && <ChartPlaceholder state={state} height={height} compact />}
276
- {resolved === null && definition !== null && (
421
+ {resolved === null && model !== null && (
277
422
  <ChartEngine
278
- definition={definition}
423
+ definition={model.definition}
279
424
  ariaLabel={ariaLabel}
280
425
  height={height}
281
426
  idPrefix={idPrefixOf(id, 'spark')}
282
427
  coarse={false}
283
428
  />
284
429
  )}
430
+ {at !== null && (
431
+ <>
432
+ <span
433
+ data-basalt-sparkline-dot=""
434
+ className={classes.dot}
435
+ style={{ left: `${at.fx * 100}%`, top: `${at.fy * 100}%` }}
436
+ />
437
+ <output
438
+ data-basalt-sparkline-readout=""
439
+ aria-live={hover === null ? 'polite' : 'off'}
440
+ className={cx(classes.readout, at.fx > 0.5 ? classes.start : classes.end)}
441
+ >
442
+ {at.label !== undefined && <span className={classes.label}>{at.label} </span>}
443
+ {format(at.value)}
444
+ </output>
445
+ </>
446
+ )}
285
447
  </div>
286
448
  )
287
449
  }
@@ -94,3 +94,5 @@ export function ChartBox(props: {
94
94
  }
95
95
 
96
96
  export const stateClass = classes.state
97
+ /** Lets the box outgrow its stated height (an error body holding an action). */
98
+ export const growClass = classes.grow
@@ -62,3 +62,33 @@
62
62
  width: 100%;
63
63
  height: 100%;
64
64
  }
65
+
66
+ /* An error body: the box's height is the floor, so its content (and the action) always fit. */
67
+ .grow {
68
+ height: auto;
69
+ max-height: none;
70
+ min-height: var(--bc-micro);
71
+ }
72
+
73
+ @container (min-width: 240px) {
74
+ .grow {
75
+ min-height: var(--bc-compact);
76
+ }
77
+ }
78
+
79
+ @container (min-width: 480px) {
80
+ .grow {
81
+ min-height: var(--bc-regular);
82
+ }
83
+ }
84
+
85
+ @container (min-width: 800px) {
86
+ .grow {
87
+ min-height: var(--bc-wide);
88
+ }
89
+ }
90
+
91
+ .grow > .state {
92
+ height: auto;
93
+ min-height: inherit;
94
+ }
@@ -8,5 +8,6 @@ declare const classes: {
8
8
  readonly fillFrame: string
9
9
  readonly fill: string
10
10
  readonly state: string
11
+ readonly grow: string
11
12
  }
12
13
  export default classes
@@ -23,3 +23,50 @@
23
23
  :is([data-basalt-card-short], [data-basalt-chart-card]:has([data-basalt-chart-short])) .header {
24
24
  padding-top: 4px;
25
25
  }
26
+
27
+ /*
28
+ * The facts line (`facts` prop): the headline figure under the title, in place of a subtitle. It wraps
29
+ * (rows and the pieces inside a row), so a half-width card never clips the value. The value is the
30
+ * proportional sans with lining figures (Wave 35); the unit and period are muted. Weight and tracking
31
+ * are StatCard's hero values (`stat-card.module.css` `.value`: 500 / -0.02em, which no token names).
32
+ */
33
+ .facts {
34
+ margin-top: var(--vx-space-stack-xs);
35
+ }
36
+
37
+ .factsRow {
38
+ display: flex;
39
+ flex-wrap: wrap;
40
+ align-items: baseline;
41
+ gap: var(--vx-space-stack-xs) var(--vx-space-stack-md);
42
+ }
43
+
44
+ .fact {
45
+ display: flex;
46
+ flex-wrap: wrap;
47
+ align-items: baseline;
48
+ gap: var(--vx-space-stack-xs);
49
+ min-width: 0;
50
+ }
51
+
52
+ .factValue {
53
+ min-width: 0;
54
+ overflow-wrap: anywhere;
55
+ font-family: var(--basalt-font-numeral, var(--basalt-font-sans));
56
+ font-size: var(--vx-text-kpi);
57
+ font-weight: 500;
58
+ font-variant-numeric: lining-nums;
59
+ letter-spacing: -0.02em;
60
+ color: var(--vx-ink);
61
+ }
62
+
63
+ .factUnit,
64
+ .factPeriod,
65
+ .factsBasis {
66
+ font-size: var(--vx-text-sm);
67
+ color: var(--vx-muted);
68
+ }
69
+
70
+ .factsBasis {
71
+ margin: var(--vx-space-stack-xs) 0 0;
72
+ }
@@ -4,5 +4,12 @@
4
4
  */
5
5
  declare const classes: {
6
6
  readonly header: string
7
+ readonly facts: string
8
+ readonly factsRow: string
9
+ readonly factsBasis: string
10
+ readonly fact: string
11
+ readonly factValue: string
12
+ readonly factUnit: string
13
+ readonly factPeriod: string
7
14
  }
8
15
  export default classes
@@ -9,6 +9,7 @@ import { dateTickFormat, monthName, pad2, utcAxisDate } from './date-tick-format
9
9
  import { crosshair } from '@tanstack/charts/crosshair'
10
10
  import { decorative } from '@tanstack/charts/mark/decorative'
11
11
  import { rect } from '@tanstack/charts/rect'
12
+ import { text } from '@tanstack/charts/text'
12
13
  import { ruleX, ruleY } from '@tanstack/charts/rule'
13
14
  import { scaleLinear } from '@tanstack/charts/scales/linear'
14
15
  import { scaleUtc } from 'd3-scale'
@@ -92,13 +93,16 @@ export type CartesianInput<T, X extends ChartX> = Pick<
92
93
  plot?: PlotSizes | null
93
94
  }
94
95
 
95
- /** Font size and tick counts per container class (§ 9.6). */
96
+ /** The one tick-label size, at every container width: the caption size, so a card never mixes sizes. */
97
+ const TICK_FONT = VX.text.micro
98
+
99
+ /** Tick counts per container class (§ 9.6). */
96
100
  const LADDER = {
97
- micro: { font: VX.text.nano, xTicks: 2, yTicks: 0 },
98
- compact: { font: VX.text.nano, xTicks: 3, yTicks: 3 },
99
- regular: { font: VX.text.micro, xTicks: 5, yTicks: 5 },
100
- wide: { font: VX.text.micro, xTicks: 6, yTicks: 5 },
101
- } as const satisfies Record<ContainerClass, { font: number; xTicks: number; yTicks: number }>
101
+ micro: { xTicks: 2, yTicks: 0 },
102
+ compact: { xTicks: 3, yTicks: 3 },
103
+ regular: { xTicks: 5, yTicks: 5 },
104
+ wide: { xTicks: 6, yTicks: 5 },
105
+ } as const satisfies Record<ContainerClass, { xTicks: number; yTicks: number }>
102
106
 
103
107
  /** Space between the plot's left edge and a compact y label drawn inside it. */
104
108
  const INSIDE_LABEL_DX = 4
@@ -269,7 +273,7 @@ export function yGuide<T>(opts: {
269
273
  line: false,
270
274
  ticks: { ...ticks, format: axis.format, line: false, ...(inside && { size: 0, padding: 0 }) },
271
275
  tickLabels: {
272
- fontSize: ladder.font,
276
+ fontSize: TICK_FONT,
273
277
  opacity: 1,
274
278
  ...(inside && {
275
279
  anchor: side === 'left' ? ('start' as const) : ('end' as const),
@@ -355,7 +359,7 @@ export function xGuide<X extends ChartX>(opts: {
355
359
  line: AXIS_LINE,
356
360
  ticks: { ...ticks, format, line: false, size: 0 },
357
361
  tickLabels: {
358
- fontSize: ladder.font,
362
+ fontSize: TICK_FONT,
359
363
  opacity: 1,
360
364
  thin: true,
361
365
  // A label centred on a tick near the plot's left edge hangs left of it, under the gutter's
@@ -682,6 +686,34 @@ function underlayMarks<T, X extends ChartX>(
682
686
  return [...zones, ...xZones]
683
687
  }
684
688
 
689
+ /** A ref label within this share of the plot height of the top flips below its line. */
690
+ const REF_LABEL_TOP_SHARE = 0.12
691
+ const REF_LABEL_DX = 6
692
+ const REF_LABEL_DY_ABOVE = -5
693
+ const REF_LABEL_DY_BELOW = 11
694
+
695
+ /** The caption naming a reference line, at the plot end clear of its own axis' tick labels. */
696
+ const refLabelMark = <T>(r: RefLine, i: number, axes: Axes<T>): ChartMarks[number] => {
697
+ const [lo, hi] = axes.axisOf(r.axis).domain
698
+ const nearTop = hi !== lo && (r.value - lo) / (hi - lo) > 1 - REF_LABEL_TOP_SHARE
699
+ const atStart = r.axis === 'right' && axes.scaleId(r.axis) === 'y2'
700
+ return decorative(
701
+ text([{ f: atStart ? 0 : 1 }], {
702
+ id: `ref-${i}-label`,
703
+ x: (d: { f: number }) => d.f,
704
+ y: () => r.value,
705
+ xScale: 'zx',
706
+ yScale: axes.scaleId(r.axis),
707
+ text: () => r.label ?? '',
708
+ fill: VX.ink2,
709
+ fontSize: VX.text.micro,
710
+ anchor: atStart ? 'start' : 'end',
711
+ dx: atStart ? REF_LABEL_DX : -REF_LABEL_DX,
712
+ dy: nearTop ? REF_LABEL_DY_BELOW : REF_LABEL_DY_ABOVE,
713
+ }),
714
+ )
715
+ }
716
+
685
717
  /** Reference lines, over the marks. */
686
718
  const refLineMarks = <T, X extends ChartX>(
687
719
  input: CartesianInput<T, X>,
@@ -700,6 +732,7 @@ const refLineMarks = <T, X extends ChartX>(
700
732
  yScale: axes.scaleId(r.axis),
701
733
  ...(r.dashed && { strokeDasharray: DASH }),
702
734
  }),
735
+ ...(r.label ? [refLabelMark(r, i, axes)] : []),
703
736
  ],
704
737
  )
705
738
 
@@ -921,7 +954,7 @@ export function compileCartesian<T, X extends ChartX>(
921
954
  formatX,
922
955
  cls,
923
956
  height,
924
- zoneScale: under.length > 0,
957
+ zoneScale: under.length > 0 || (input.refLines ?? []).some((r) => r.label),
925
958
  }),
926
959
  theme: CHART_THEME,
927
960
  ...(gradients.length > 0 && { gradients }),