basalt-ui 2.27.1 → 2.28.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 (243) hide show
  1. package/CHANGELOG.md +111 -0
  2. package/MIGRATING.md +172 -12
  3. package/agent/rules/basalt-batteries.md +1 -1
  4. package/agent/rules/basalt-mantine.md +1 -1
  5. package/agent/rules/basalt-tokens.md +2 -2
  6. package/agent/skills/basalt-design/SKILL.md +1 -1
  7. package/agent/skills/basalt-design/references/catalogue.md +40 -36
  8. package/agent/skills/basalt-design/references/recipes.md +47 -0
  9. package/configs/oxlint-basalt.json +2 -1
  10. package/configs/oxlint-plugin.js +110 -2
  11. package/configs/oxlint-plugin.test.ts +99 -4
  12. package/dist/agent/chat/thread-message.d.ts +1 -160
  13. package/dist/agent/chat/thread-message.d.ts.map +1 -1
  14. package/dist/agent/chat/thread-message.js +7 -162
  15. package/dist/agent/chat/virtualize.d.ts +5 -5
  16. package/dist/charts/ChartCard.d.ts +5 -2
  17. package/dist/charts/ChartCard.d.ts.map +1 -1
  18. package/dist/charts/ChartCard.js +46 -13
  19. package/dist/charts/Lines.d.ts.map +1 -1
  20. package/dist/charts/Lines.js +11 -4
  21. package/dist/charts/Sparkline.d.ts +9 -2
  22. package/dist/charts/Sparkline.d.ts.map +1 -1
  23. package/dist/charts/Sparkline.js +104 -41
  24. package/dist/charts/chart-box.module.css +69 -3
  25. package/dist/charts/chart-box.module.css.d.ts +5 -0
  26. package/dist/charts/chart-card.module.css +12 -4
  27. package/dist/charts/chart-card.module.css.d.ts +1 -0
  28. package/dist/charts/compile.d.ts.map +1 -1
  29. package/dist/charts/compile.js +21 -19
  30. package/dist/charts/labels.d.ts +13 -3
  31. package/dist/charts/labels.d.ts.map +1 -1
  32. package/dist/charts/labels.js +131 -20
  33. package/dist/charts/legend.d.ts.map +1 -1
  34. package/dist/charts/legend.js +1 -1
  35. package/dist/charts/ref-label.d.ts +31 -8
  36. package/dist/charts/ref-label.d.ts.map +1 -1
  37. package/dist/charts/ref-label.js +40 -9
  38. package/dist/charts/sparkline.module.css +6 -0
  39. package/dist/charts/state.d.ts +3 -3
  40. package/dist/charts/state.d.ts.map +1 -1
  41. package/dist/charts/state.js +20 -2
  42. package/dist/cli/shot/capture.d.ts +6 -1
  43. package/dist/cli/shot/capture.d.ts.map +1 -1
  44. package/dist/cli/shot/capture.js +18 -5
  45. package/dist/common/empty-copy.d.ts +16 -0
  46. package/dist/common/empty-copy.d.ts.map +1 -0
  47. package/dist/common/empty-copy.js +9 -0
  48. package/dist/common/virtual-rows.d.ts +196 -0
  49. package/dist/common/virtual-rows.d.ts.map +1 -0
  50. package/dist/common/virtual-rows.js +290 -0
  51. package/dist/controls/controls.module.css +33 -1
  52. package/dist/controls/controls.module.css.d.ts +2 -0
  53. package/dist/controls/enum-filter.d.ts.map +1 -1
  54. package/dist/controls/enum-filter.js +8 -2
  55. package/dist/controls/filter-pill.d.ts +27 -3
  56. package/dist/controls/filter-pill.d.ts.map +1 -1
  57. package/dist/controls/filter-pill.js +15 -4
  58. package/dist/controls/form-layout.d.ts +9 -3
  59. package/dist/controls/form-layout.d.ts.map +1 -1
  60. package/dist/controls/form-layout.js +74 -12
  61. package/dist/controls/form-layout.module.css +112 -7
  62. package/dist/controls/form-layout.module.css.d.ts +1 -0
  63. package/dist/controls/multi-select-filter.d.ts +2 -2
  64. package/dist/controls/multi-select-filter.d.ts.map +1 -1
  65. package/dist/controls/multi-select-filter.js +12 -6
  66. package/dist/controls/range-filter.d.ts +1 -1
  67. package/dist/controls/range-filter.d.ts.map +1 -1
  68. package/dist/controls/range-filter.js +8 -2
  69. package/dist/dashboard/bar-list.d.ts +1 -1
  70. package/dist/dashboard/bar-list.d.ts.map +1 -1
  71. package/dist/dashboard/bar-list.js +6 -2
  72. package/dist/dashboard/bar-list.module.css +0 -2
  73. package/dist/dashboard/board-nav.d.ts +22 -0
  74. package/dist/dashboard/board-nav.d.ts.map +1 -0
  75. package/dist/dashboard/board-nav.js +49 -0
  76. package/dist/dashboard/board.d.ts +52 -0
  77. package/dist/dashboard/board.d.ts.map +1 -0
  78. package/dist/dashboard/board.js +228 -0
  79. package/dist/dashboard/board.module.css +160 -0
  80. package/dist/dashboard/board.module.css.d.ts +26 -0
  81. package/dist/dashboard/empty-state.d.ts +3 -3
  82. package/dist/dashboard/empty-state.d.ts.map +1 -1
  83. package/dist/dashboard/empty-state.js +13 -6
  84. package/dist/dashboard/event-list-model.d.ts +51 -0
  85. package/dist/dashboard/event-list-model.d.ts.map +1 -0
  86. package/dist/dashboard/event-list-model.js +72 -0
  87. package/dist/dashboard/event-list-rows.d.ts +23 -0
  88. package/dist/dashboard/event-list-rows.d.ts.map +1 -0
  89. package/dist/dashboard/event-list-rows.js +93 -0
  90. package/dist/dashboard/event-list-scroll.d.ts +27 -0
  91. package/dist/dashboard/event-list-scroll.d.ts.map +1 -0
  92. package/dist/dashboard/event-list-scroll.js +103 -0
  93. package/dist/dashboard/event-list-skeleton.d.ts +10 -0
  94. package/dist/dashboard/event-list-skeleton.d.ts.map +1 -0
  95. package/dist/dashboard/event-list-skeleton.js +36 -0
  96. package/dist/dashboard/event-list.d.ts +65 -0
  97. package/dist/dashboard/event-list.d.ts.map +1 -0
  98. package/dist/dashboard/event-list.js +178 -0
  99. package/dist/dashboard/event-list.module.css +283 -0
  100. package/dist/dashboard/event-list.module.css.d.ts +33 -0
  101. package/dist/dashboard/index.d.ts +2 -0
  102. package/dist/dashboard/index.d.ts.map +1 -1
  103. package/dist/dashboard/index.js +4 -0
  104. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  105. package/dist/dashboard/meter-parts.js +5 -3
  106. package/dist/dashboard/meter.module.css +2 -1
  107. package/dist/dashboard/query-state.js +1 -1
  108. package/dist/dashboard/settings-section.d.ts +4 -1
  109. package/dist/dashboard/settings-section.d.ts.map +1 -1
  110. package/dist/dashboard/settings-section.js +1 -1
  111. package/dist/dashboard/settings-section.module.css +19 -4
  112. package/dist/dashboard/stat-card-parts.d.ts +5 -1
  113. package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
  114. package/dist/dashboard/stat-card-parts.js +8 -3
  115. package/dist/dashboard/stat-card.d.ts.map +1 -1
  116. package/dist/dashboard/stat-card.js +26 -7
  117. package/dist/dashboard/stat-card.module.css +31 -7
  118. package/dist/dashboard/stat-card.module.css.d.ts +3 -0
  119. package/dist/dashboard/stat-strip.js +1 -1
  120. package/dist/dashboard/stat-strip.module.css +4 -1
  121. package/dist/dashboard/widget-header.d.ts.map +1 -1
  122. package/dist/dashboard/widget-header.js +15 -2
  123. package/dist/dashboard/widget-header.module.css +17 -1
  124. package/dist/guard/index.d.ts.map +1 -1
  125. package/dist/guard/index.js +2 -0
  126. package/dist/index.d.ts +1 -1
  127. package/dist/index.d.ts.map +1 -1
  128. package/dist/index.js +4 -0
  129. package/dist/shell/app-main.module.css +18 -0
  130. package/dist/styles.css +46 -8
  131. package/dist/surfaces.d.ts +2 -2
  132. package/dist/surfaces.d.ts.map +1 -1
  133. package/dist/surfaces.js +8 -2
  134. package/dist/table/cell-kinds.d.ts +44 -0
  135. package/dist/table/cell-kinds.d.ts.map +1 -0
  136. package/dist/table/cell-kinds.js +169 -0
  137. package/dist/table/cell-kinds.module.css +55 -0
  138. package/dist/table/cell-kinds.module.css.d.ts +15 -0
  139. package/dist/table/data-table-fold.d.ts +147 -0
  140. package/dist/table/data-table-fold.d.ts.map +1 -0
  141. package/dist/table/data-table-fold.js +288 -0
  142. package/dist/table/data-table-state.d.ts +10 -3
  143. package/dist/table/data-table-state.d.ts.map +1 -1
  144. package/dist/table/data-table-state.js +11 -6
  145. package/dist/table/data-table.d.ts +11 -26
  146. package/dist/table/data-table.d.ts.map +1 -1
  147. package/dist/table/data-table.js +354 -254
  148. package/dist/table/data-table.module.css +210 -3
  149. package/dist/table/data-table.module.css.d.ts +12 -0
  150. package/dist/table/table-features.d.ts +34 -4
  151. package/dist/table/table-features.d.ts.map +1 -1
  152. package/dist/table/table-features.js +10 -0
  153. package/dist/theme/card-inset.module.css +35 -0
  154. package/dist/theme/controls.module.css +32 -0
  155. package/dist/theme/ctl-theme.d.ts.map +1 -1
  156. package/dist/theme/ctl-theme.js +5 -0
  157. package/dist/theme/index.d.ts.map +1 -1
  158. package/dist/theme/index.js +14 -10
  159. package/dist/tokens/core.d.ts +1 -0
  160. package/dist/tokens/core.d.ts.map +1 -1
  161. package/dist/tokens/core.js +1 -0
  162. package/dist/tokens/derive.js +1 -1
  163. package/dist/tokens/palette.d.ts +2 -1
  164. package/dist/tokens/palette.d.ts.map +1 -1
  165. package/llms.txt +34 -5
  166. package/package.json +1 -1
  167. package/src/agent/chat/thread-message.tsx +10 -403
  168. package/src/agent/chat/virtualize.ts +5 -5
  169. package/src/charts/ChartCard.tsx +79 -17
  170. package/src/charts/Lines.tsx +13 -4
  171. package/src/charts/Sparkline.tsx +131 -45
  172. package/src/charts/chart-box.module.css +69 -3
  173. package/src/charts/chart-box.module.css.d.ts +5 -0
  174. package/src/charts/chart-card.module.css +12 -4
  175. package/src/charts/chart-card.module.css.d.ts +1 -0
  176. package/src/charts/compile.ts +24 -20
  177. package/src/charts/labels.ts +175 -24
  178. package/src/charts/legend.tsx +3 -2
  179. package/src/charts/ref-label.ts +86 -17
  180. package/src/charts/sparkline.module.css +6 -0
  181. package/src/charts/state.tsx +36 -7
  182. package/src/cli/shot/capture.ts +29 -6
  183. package/src/common/empty-copy.ts +17 -0
  184. package/src/common/virtual-rows.tsx +682 -0
  185. package/src/controls/controls.module.css +33 -1
  186. package/src/controls/controls.module.css.d.ts +2 -0
  187. package/src/controls/enum-filter.tsx +8 -2
  188. package/src/controls/filter-pill.tsx +80 -27
  189. package/src/controls/form-layout.module.css +112 -7
  190. package/src/controls/form-layout.module.css.d.ts +1 -0
  191. package/src/controls/form-layout.tsx +115 -22
  192. package/src/controls/multi-select-filter.tsx +22 -11
  193. package/src/controls/range-filter.tsx +9 -3
  194. package/src/dashboard/bar-list.module.css +0 -2
  195. package/src/dashboard/bar-list.tsx +8 -5
  196. package/src/dashboard/board-nav.ts +67 -0
  197. package/src/dashboard/board.module.css +160 -0
  198. package/src/dashboard/board.module.css.d.ts +26 -0
  199. package/src/dashboard/board.tsx +394 -0
  200. package/src/dashboard/empty-state.tsx +16 -9
  201. package/src/dashboard/event-list-model.ts +113 -0
  202. package/src/dashboard/event-list-rows.tsx +120 -0
  203. package/src/dashboard/event-list-scroll.ts +145 -0
  204. package/src/dashboard/event-list-skeleton.tsx +58 -0
  205. package/src/dashboard/event-list.module.css +283 -0
  206. package/src/dashboard/event-list.module.css.d.ts +33 -0
  207. package/src/dashboard/event-list.tsx +309 -0
  208. package/src/dashboard/index.ts +2 -0
  209. package/src/dashboard/meter-parts.tsx +9 -2
  210. package/src/dashboard/meter.module.css +2 -1
  211. package/src/dashboard/query-state.tsx +1 -1
  212. package/src/dashboard/settings-section.module.css +19 -4
  213. package/src/dashboard/settings-section.tsx +7 -2
  214. package/src/dashboard/stat-card-parts.tsx +20 -3
  215. package/src/dashboard/stat-card.module.css +31 -7
  216. package/src/dashboard/stat-card.module.css.d.ts +3 -0
  217. package/src/dashboard/stat-card.tsx +25 -6
  218. package/src/dashboard/stat-strip.module.css +4 -1
  219. package/src/dashboard/stat-strip.tsx +1 -1
  220. package/src/dashboard/widget-header.module.css +17 -1
  221. package/src/dashboard/widget-header.tsx +29 -5
  222. package/src/guard/index.ts +2 -0
  223. package/src/index.ts +4 -0
  224. package/src/shell/app-main.module.css +18 -0
  225. package/src/styles.css +46 -8
  226. package/src/surfaces.ts +7 -1
  227. package/src/table/cell-kinds.module.css +55 -0
  228. package/src/table/cell-kinds.module.css.d.ts +15 -0
  229. package/src/table/cell-kinds.tsx +234 -0
  230. package/src/table/data-table-fold.tsx +514 -0
  231. package/src/table/data-table-state.ts +24 -13
  232. package/src/table/data-table.module.css +210 -3
  233. package/src/table/data-table.module.css.d.ts +12 -0
  234. package/src/table/data-table.tsx +406 -359
  235. package/src/table/table-features.ts +44 -4
  236. package/src/theme/__snapshots__/plain-theme.test.ts.snap +2 -66
  237. package/src/theme/card-inset.module.css +35 -0
  238. package/src/theme/controls.module.css +32 -0
  239. package/src/theme/ctl-theme.tsx +8 -0
  240. package/src/theme/index.ts +12 -8
  241. package/src/tokens/core.ts +1 -0
  242. package/src/tokens/derive.ts +1 -1
  243. package/src/tokens/palette.ts +4 -1
@@ -38,8 +38,9 @@ import { cursorBinding, followerOptions, quietWhileFollowing } from './cursor'
38
38
  import { cursorDots } from './marks'
39
39
  import { bucketTicks, strideEvery } from './ticks'
40
40
  import { MAIN_PANE, plotAccess, plotContext } from './plot-context'
41
+ import { claimedLabelBoxes } from './labels'
41
42
  import { refLabelPosition } from './ref-label'
42
- import type { RefLabelPoint } from './ref-label'
43
+ import type { LabelBox, RefLabelPoint } from './ref-label'
43
44
  import type { PlotSizes } from './plot-context'
44
45
  import { resolveContainerClass } from '../tokens/size-classes'
45
46
  import { resolveYDomain, xScaleFor, xZoneFractions, yScaleFor } from './scales'
@@ -796,6 +797,8 @@ type RefLabelContext<T, X extends ChartX> = {
796
797
  plotHeight: number
797
798
  /** One polyline per visible series on the line's axis. */
798
799
  lines: (side: 'left' | 'right' | undefined) => readonly (readonly RefLabelPoint[])[]
800
+ /** The end labels placed on the left axis: a caption there keeps clear of them. */
801
+ claimed: readonly LabelBox[]
799
802
  axes: Axes<T>
800
803
  model: XScaleModel<X>
801
804
  }
@@ -818,6 +821,7 @@ const refLabelMark = <T, X extends ChartX>(
818
821
  label: r.label ?? '',
819
822
  fontSize: VX.text.micro,
820
823
  series: ctx.lines(r.axis),
824
+ claimed: startFirst ? [] : ctx.claimed,
821
825
  })
822
826
  const atStart = position.startsWith('start')
823
827
  return decorative(
@@ -1041,8 +1045,8 @@ const slotAnchor = (
1041
1045
  return { x: (Math.min(...xs) + Math.max(...xs)) / 2, y: plot.y }
1042
1046
  }
1043
1047
  const SLOT_TOOLTIP_PLACEMENT = ['top'] as const
1044
- /** The host's focus ring sits outside the plot: `.basalt-chart:focus-visible` is a 2px outline at a 0.125rem offset. */
1045
- const SLOT_RING_CLEARANCE = 2 + 2
1048
+ /** The plot's focus ring (`svg.ts-chart:focus-visible`, `chart-box.module.css`) is a 2px outline at a 0.125rem offset, and the chip keeps 1px of air above it. Mirrors that CSS ring at `--mantine-scale` 1; the chip's `block-size` in the same file is the reserve less this. */
1049
+ const SLOT_RING_CLEARANCE = 2 + 2 + 1
1046
1050
 
1047
1051
  /** A tooltip's chrome (title row, padding) and each series row, px: the guess the bar anchor reserves. */
1048
1052
  const TIP_CHROME = 30
@@ -1337,24 +1341,23 @@ export function compileCartesian<T, X extends ChartX>(
1337
1341
  const cls = resolveContainerClass(width)
1338
1342
  const axes = axesAt(cls)
1339
1343
  const access = plotAccess(definition, input.plot, MAIN_PANE)
1340
- const kindMarks = input.marks(
1341
- plotContext(
1342
- {
1343
- rows,
1344
- visible,
1345
- highlighted: input.highlighted ?? null,
1346
- containerClass: cls,
1347
- width,
1348
- height,
1349
- data: input.data,
1350
- xFraction: model.fraction,
1351
- y2: axes.right ? 'y2' : undefined,
1352
- yDomain: axes.left.domain,
1353
- y2Domain: axes.right?.domain,
1354
- },
1355
- access,
1356
- ),
1344
+ const plot = plotContext(
1345
+ {
1346
+ rows,
1347
+ visible,
1348
+ highlighted: input.highlighted ?? null,
1349
+ containerClass: cls,
1350
+ width,
1351
+ height,
1352
+ data: input.data,
1353
+ xFraction: model.fraction,
1354
+ y2: axes.right ? 'y2' : undefined,
1355
+ yDomain: axes.left.domain,
1356
+ y2Domain: axes.right?.domain,
1357
+ },
1358
+ access,
1357
1359
  )
1360
+ const kindMarks = input.marks(plot)
1358
1361
  const under = underlayMarks(input, model, axes)
1359
1362
  const over = refLineMarks(input, axes, () => {
1360
1363
  // Reading the plot size recompiles once on the measured area; the frame stands in before it.
@@ -1364,6 +1367,7 @@ export function compileCartesian<T, X extends ChartX>(
1364
1367
  plotWidth: size?.width ?? width,
1365
1368
  plotHeight: size?.height ?? height,
1366
1369
  lines: linesOn,
1370
+ claimed: claimedLabelBoxes(plot),
1367
1371
  axes,
1368
1372
  model,
1369
1373
  }
@@ -5,12 +5,14 @@
5
5
  import { decorative } from '@tanstack/charts/mark/decorative'
6
6
  import { text } from '@tanstack/charts/text'
7
7
  import { VX } from '../tokens'
8
+ import { yRangeWithin } from './ref-label'
9
+ import type { LabelBox, RefLabelPoint } from './ref-label'
8
10
  import { rowX } from './types'
9
11
  import type { ChartMarks, ChartSeries, ChartX, PlotContext, PlotRow } from './types'
10
12
 
11
13
  const MAX_END_LABELS = 3
12
- /** Two end labels closer than this (px, vertically) are pushed apart to it. */
13
- const END_LABEL_MIN_GAP = 13
14
+ /** Two end labels closer than this (px, vertically) are pushed apart to it: one label line (11px at 1.35) plus two pixels of air. */
15
+ const END_LABEL_MIN_GAP = 17
14
16
  const END_LABEL_DX = 6
15
17
  /**
16
18
  * A glyph's advance as a share of the font size, for the room check. 0.85 covers proportional
@@ -24,6 +26,16 @@ const END_LABEL_FLIP_DY = -9
24
26
  const END_LABEL_FLIP_DY_BELOW = 13
25
27
  /** The room a label's centre keeps from the plot's top and bottom edge, px: half its line. */
26
28
  const END_LABEL_EDGE = 7
29
+ /** A glyph's advance as a share of the font size, for a label's drawn box (the room check's {@link END_LABEL_GLYPH_EM} errs wide). */
30
+ const BOX_GLYPH_EM = 0.68
31
+ /** A label's ink is this share of its font size tall, centred on its text's y. */
32
+ const BOX_INK = 0.75
33
+ /** Air a label keeps from any line, px, and the margin a claimed box carries for a caption. */
34
+ const LINE_AIR = 4.5
35
+ /** What a label across a reference rule from its own line costs in {@link place}'s air score, px. */
36
+ const CROSSED_PENALTY = 100
37
+ /** The most flipped labels whose two sides are all tried (2 ** n assignments). */
38
+ const MAX_SIDE_SEARCH = 5
27
39
 
28
40
  export type EndLabelAnchor = 'start' | 'end'
29
41
  /** Which side of its line a flipped label sits: away from the line's last segment. */
@@ -56,8 +68,20 @@ export type EndLabel<T, X extends ChartX> = {
56
68
  anchor: EndLabelAnchor
57
69
  /** Only a flipped label (left of its point) can meet its own line; it takes the clear side. */
58
70
  side: EndLabelSide
59
- /** A vertical shift in px (negative is up) that keeps this label clear of its neighbours. */
71
+ /** A vertical shift in px (negative is up) that keeps this label clear of its neighbours and the lines. */
60
72
  nudge: number
73
+ /** The room the drawn label takes, for a reference caption to keep clear of. */
74
+ box: LabelBox
75
+ }
76
+
77
+ const claimedBoxes = new WeakMap<object, readonly LabelBox[]>()
78
+ /** Records the boxes the end labels of `plot` take (once per plot, by `Lines`). */
79
+ export function claimLabelBoxes(plot: object, boxes: readonly LabelBox[]): void {
80
+ claimedBoxes.set(plot, boxes)
81
+ }
82
+ /** The boxes {@link claimLabelBoxes} recorded for `plot`: what a reference caption avoids. */
83
+ export function claimedLabelBoxes(plot: object): readonly LabelBox[] {
84
+ return claimedBoxes.get(plot) ?? []
61
85
  }
62
86
 
63
87
  type LastPoint<T, X extends ChartX> = {
@@ -72,16 +96,21 @@ type LastPoint<T, X extends ChartX> = {
72
96
  /**
73
97
  * Pushes labels apart: `ys` are the labels' heights in px from the plot's bottom, returned as the
74
98
  * shift each needs (positive is up) so no two sit closer than {@link END_LABEL_MIN_GAP} and the
75
- * lowest and the topmost stay {@link END_LABEL_EDGE} inside the plot. Order is kept.
99
+ * lowest and the topmost stay {@link END_LABEL_EDGE} inside the plot; a stack too tall for the plot
100
+ * shrinks its gap to fit. Order is kept.
76
101
  */
77
102
  export function spreadLabels(ys: readonly number[], plotHeight: number): number[] {
78
103
  const order = ys.map((y, i) => i).toSorted((a, b) => (ys[a] as number) - (ys[b] as number))
79
104
  const placed = new Map<number, number>()
105
+ // A stack taller than the plot closes its gaps evenly rather than leave the plot.
106
+ const room = Math.max(plotHeight - 2 * END_LABEL_EDGE, 0)
107
+ const gap =
108
+ ys.length > 1 ? Math.min(END_LABEL_MIN_GAP, room / (ys.length - 1)) : END_LABEL_MIN_GAP
80
109
  let floor = END_LABEL_EDGE
81
110
  for (const i of order) {
82
111
  const at = Math.max(ys[i] as number, floor)
83
112
  placed.set(i, at)
84
- floor = at + END_LABEL_MIN_GAP
113
+ floor = at + gap
85
114
  }
86
115
  // Pushed past the top: slide the whole stack back down, then re-sweep from the top.
87
116
  const top = plotHeight - END_LABEL_EDGE
@@ -89,7 +118,7 @@ export function spreadLabels(ys: readonly number[], plotHeight: number): number[
89
118
  for (const i of order.toReversed()) {
90
119
  const at = Math.min(placed.get(i) as number, ceiling, top)
91
120
  placed.set(i, at)
92
- ceiling = at - END_LABEL_MIN_GAP
121
+ ceiling = at - gap
93
122
  }
94
123
  return ys.map((y, i) => (placed.get(i) as number) - y)
95
124
  }
@@ -105,6 +134,8 @@ export function endLabelPlan<T, X extends ChartX>(
105
134
  plot: PlotContext<T, X>,
106
135
  lines: readonly ChartSeries<T>[],
107
136
  naming: LinesNaming = 'auto',
137
+ /** The values of the reference lines on the left axis: rules a label must not sit on. */
138
+ rules: readonly number[] = [],
108
139
  ): EndLabel<T, X>[] {
109
140
  if (naming === 'legend' || plot.y2 !== undefined || lines.length === 0) return []
110
141
  if (naming === 'auto' && (plot.containerClass !== 'wide' || lines.length > MAX_END_LABELS))
@@ -119,30 +150,150 @@ export function endLabelPlan<T, X extends ChartX>(
119
150
  })
120
151
  // No `y2` here (returned above), so every label reads the left domain.
121
152
  const [lo, hi] = plot.yDomain
122
- const placed = last.map(({ series, fraction, y, prev }) => {
123
- const anchor = endLabelAnchor({
153
+ const anchors = last.map(({ series, fraction }) =>
154
+ endLabelAnchor({
124
155
  fraction,
125
156
  plotWidth: plot.plotWidth,
126
157
  label: series.label,
127
158
  fontSize: VX.text.micro,
128
- })
129
- // A line falling into its end point comes from above: the label drops below it.
130
- const side = prev !== null && prev > y ? ('below' as const) : ('above' as const)
131
- // The label's centre, px from the plot's bottom, with its flip: what the spread keeps apart.
132
- const centre = ((y - lo) / (hi - lo || 1)) * plot.plotHeight - endLabelOffset(anchor, side).dy
133
- return { anchor, side, centre }
159
+ }),
160
+ )
161
+ // Flipped labels (left of their point) sit over their own line. A line falling into its end
162
+ // comes from above, so its label drops below; a rising line's rises above. Several flipped lines
163
+ // that move the same way would all take the same side and stack in the gap between them, so they
164
+ // wrap the lines instead: falling, the lowest below and the rest above; rising, the mirror.
165
+ const falls = last.map(({ y, prev }) => prev !== null && prev > y)
166
+ const flipped = last.flatMap((_, i) => (anchors[i] === 'end' ? [i] : []))
167
+ const lineY = (i: number): number => (last[i] as (typeof last)[number]).y
168
+ const sameWay =
169
+ flipped.length > 1 && flipped.every((i) => falls[i] === falls[flipped[0] as number])
170
+ const wrapped = sameWay ? flipped.toSorted((a, b) => lineY(a) - lineY(b)) : []
171
+ // The one that keeps the far side: the lowest when falling, the highest when rising.
172
+ const outlier = falls[flipped[0] as number] ? wrapped[0] : wrapped.at(-1)
173
+ const preferred = last.map((_, i): EndLabelSide => {
174
+ const fall = falls[i] as boolean
175
+ return sameWay && anchors[i] === 'end'
176
+ ? (i === outlier) === fall
177
+ ? 'below'
178
+ : 'above'
179
+ : fall
180
+ ? 'below'
181
+ : 'above'
182
+ })
183
+ const toPx = (y: number): number => ((y - lo) / (hi - lo || 1)) * plot.plotHeight
184
+ const nominal = (i: number, side: EndLabelSide): number =>
185
+ toPx(lineY(i)) - endLabelOffset(anchors[i] as EndLabelAnchor, side).dy
186
+ const half = (VX.text.micro * BOX_INK) / 2
187
+ const plotW = Math.max(plot.plotWidth, 1)
188
+ const spans = last.map(({ series, fraction }, i) => {
189
+ const room = (END_LABEL_DX + series.label.length * VX.text.micro * BOX_GLYPH_EM) / plotW
190
+ const edge = END_LABEL_DX / plotW
191
+ return anchors[i] === 'end'
192
+ ? ([fraction - room, fraction - edge] as const)
193
+ : ([fraction + edge, fraction + room] as const)
134
194
  })
135
- const shifts = spreadLabels(
136
- placed.map((p) => p.centre),
137
- plot.plotHeight,
195
+ // Every line a label could sit on, as polylines in px from the plot's bottom: each visible
196
+ // series and each reference rule. Only the stretch under the labels matters.
197
+ const from = Math.min(...spans.map(([x0]) => x0))
198
+ const obstacles: RefLabelPoint[][] = [
199
+ ...plot.visible.map((s) => {
200
+ const pts: RefLabelPoint[] = []
201
+ let before: RefLabelPoint | undefined
202
+ for (const r of plot.rows(s.key)) {
203
+ const fx = r.y === null ? null : plot.xFraction(r.x)
204
+ if (fx === null || r.y === null) continue
205
+ const pt = { fx, y: toPx(r.y) }
206
+ if (fx < from) before = pt
207
+ else {
208
+ if (before !== undefined) pts.push(before)
209
+ before = undefined
210
+ pts.push(pt)
211
+ }
212
+ }
213
+ if (before !== undefined) pts.push(before)
214
+ return pts
215
+ }),
216
+ ...rules.map((v) => [
217
+ { fx: 0, y: toPx(v) },
218
+ { fx: 1, y: toPx(v) },
219
+ ]),
220
+ ]
221
+ /** Per label, the line ranges (px) its x-window runs into: fixed, so computed once. */
222
+ const windowRanges = spans.map(([x0, x1]) =>
223
+ obstacles.flatMap((pts) => {
224
+ const range = yRangeWithin(pts, x0, x1)
225
+ return range === null ? [] : [range]
226
+ }),
138
227
  )
139
- return last.map(({ series, row }, i) => ({
140
- series,
141
- row,
142
- anchor: (placed[i] as (typeof placed)[number]).anchor,
143
- side: (placed[i] as (typeof placed)[number]).side,
144
- nudge: 0 - (shifts[i] as number),
145
- }))
228
+ const ranges = (i: number): readonly [number, number][] => windowRanges[i] as [number, number][]
229
+ /** Where the centre goes to clear the lines on `side` of `start`, stepping past each one it meets. */
230
+ const cleared = (i: number, side: EndLabelSide, start: number): number => {
231
+ const blockers = ranges(i)
232
+ let c = start
233
+ for (let n = 0; n <= blockers.length; n++) {
234
+ const hit = blockers.find(([a, b]) => b + LINE_AIR > c - half && a - LINE_AIR < c + half)
235
+ if (hit === undefined) break
236
+ c = side === 'above' ? hit[1] + LINE_AIR + half : hit[0] - LINE_AIR - half
237
+ }
238
+ return c
239
+ }
240
+ const airOf = (i: number, c: number): number =>
241
+ Math.min(
242
+ Number.POSITIVE_INFINITY,
243
+ ...ranges(i).map(([a, b]) => Math.max(a - (c + half), c - half - b)),
244
+ )
245
+ const place = (sides: readonly EndLabelSide[]) => {
246
+ const start = last.map((_, i) =>
247
+ cleared(i, sides[i] as EndLabelSide, nominal(i, sides[i] as EndLabelSide)),
248
+ )
249
+ const shifts = spreadLabels(start, plot.plotHeight)
250
+ const centres = start.map((c, i) => c + (shifts[i] as number))
251
+ // A label across a reference rule from its own line's end reads as the rule's label: worse
252
+ // than any amount of lost air.
253
+ const crossed = centres.filter((c, i) => {
254
+ const end = toPx(lineY(i))
255
+ return rules.some(
256
+ (v) => (toPx(v) - c) * (toPx(v) - end) < 0 && (c - end) * (toPx(v) - end) > 0,
257
+ )
258
+ }).length
259
+ const score =
260
+ centres.reduce((sum, c, i) => sum + Math.min(airOf(i, c), LINE_AIR), 0) -
261
+ CROSSED_PENALTY * crossed
262
+ return { sides, centres, score }
263
+ }
264
+ // Each flipped label may take either side: the assignment that keeps most air from the lines
265
+ // wins, the preferred one on a tie. Level labels (right of
266
+ // their point) keep theirs.
267
+ const options = flipped.length <= MAX_SIDE_SEARCH ? flipped.length : 0
268
+ let best = place(preferred)
269
+ for (let mask = 1; mask < 1 << options; mask++) {
270
+ const sides = preferred.map((side, i): EndLabelSide => {
271
+ const at = flipped.indexOf(i)
272
+ return at >= 0 && (mask >> at) & 1 ? (side === 'above' ? 'below' : 'above') : side
273
+ })
274
+ const trial = place(sides)
275
+ if (trial.score > best.score + 0.01) best = trial
276
+ }
277
+ return last.map(({ series, row }, i) => {
278
+ const side = best.sides[i] as EndLabelSide
279
+ const centre = best.centres[i] as number
280
+ const [x0, x1] = spans[i] as readonly [number, number]
281
+ const perPx = (hi - lo) / (plot.plotHeight || 1)
282
+ // Two pixels of margin each way: the caption keeps its own gap from the claimed box.
283
+ return {
284
+ series,
285
+ row,
286
+ anchor: anchors[i] as EndLabelAnchor,
287
+ side,
288
+ nudge: nominal(i, side) - centre,
289
+ box: {
290
+ fx0: x0 - 2 / plotW,
291
+ fx1: x1 + 2 / plotW,
292
+ y0: lo + (centre - half - 2) * perPx,
293
+ y1: lo + (centre + half + 2) * perPx,
294
+ },
295
+ }
296
+ })
146
297
  }
147
298
 
148
299
  /** The offset of a label from its point: right of it, level; or left of it, above or below. */
@@ -115,7 +115,7 @@ export type SignedSeries<T> = ChartSeries<T> & { colorNegative?: string | undefi
115
115
 
116
116
  type SwatchStyle = Pick<
117
117
  SignedSeries<never>,
118
- 'color' | 'colorNegative' | 'mark' | 'dash' | 'strokeOpacity' | 'fillOpacity'
118
+ 'color' | 'colorNegative' | 'mark' | 'dash' | 'strokeOpacity' | 'fillOpacity' | 'role'
119
119
  >
120
120
 
121
121
  /** A swatch fill: the series colour, or a hard-stop split for a sign-coloured bar. */
@@ -149,7 +149,8 @@ function LineSwatch({ s, compact }: { s: SwatchStyle; compact: boolean }): React
149
149
  y1={4}
150
150
  y2={4}
151
151
  stroke={s.color}
152
- strokeWidth={2}
152
+ // A reference line is drawn as a 1px rule (`refLineMarks`), a series from 2px: the key keeps that difference.
153
+ strokeWidth={s.role === 'reference' ? 1 : 2}
153
154
  strokeLinecap={s.dash === 'dashed' ? 'butt' : 'round'}
154
155
  strokeOpacity={s.strokeOpacity ?? 1}
155
156
  {...(s.dash === 'dashed' && { strokeDasharray: dash })}
@@ -15,6 +15,12 @@ const GLYPH_EM = 0.62
15
15
  const EDGE_INSET = 6
16
16
  const LINE_GAP = 3
17
17
 
18
+ /**
19
+ * A box another label has claimed in the same plot: a share of the plot width and a span of values
20
+ * on the left axis. A caption never takes a spot that overlaps one.
21
+ */
22
+ export type LabelBox = { fx0: number; fx1: number; y0: number; y1: number }
23
+
18
24
  const ORDER_END: readonly RefLabelPosition[] = [
19
25
  'end-above',
20
26
  'end-below',
@@ -29,7 +35,7 @@ const ORDER_START: readonly RefLabelPosition[] = [
29
35
  ]
30
36
 
31
37
  /** The y range of the polyline `points` (sorted by `fx`) between `fx0` and `fx1`, or `null`. */
32
- function yRangeWithin(
38
+ export function yRangeWithin(
33
39
  points: readonly RefLabelPoint[],
34
40
  fx0: number,
35
41
  fx1: number,
@@ -70,8 +76,11 @@ export function refLabelPosition(opts: {
70
76
  fontSize: number
71
77
  /** One polyline per visible series on the line's axis. */
72
78
  series: readonly (readonly RefLabelPoint[])[]
79
+ /** Labels already placed on this axis (end labels): a caption keeps clear of them too. */
80
+ claimed?: readonly LabelBox[] | undefined
73
81
  }): RefLabelPosition {
74
82
  const { pinned, startFirst, value, plotWidth, plotHeight, label, fontSize, series } = opts
83
+ const { claimed = [] } = opts
75
84
  const order = startFirst ? ORDER_START : ORDER_END
76
85
  if (pinned !== undefined) return pinned
77
86
  const [lo, hi] = [Math.min(...opts.domain), Math.max(...opts.domain)]
@@ -92,10 +101,12 @@ export function refLabelPosition(opts: {
92
101
  const [fx0, fx1] = position.startsWith('end')
93
102
  ? [1 - widthFx, 1 - EDGE_INSET / plotWidth]
94
103
  : [EDGE_INSET / plotWidth, widthFx]
95
- return series.every((points) => {
96
- const range = yRangeWithin(points, fx0, fx1)
97
- return range === null || range[1] < from || range[0] > to
98
- })
104
+ return (
105
+ series.every((points) => {
106
+ const range = yRangeWithin(points, fx0, fx1)
107
+ return range === null || range[1] < from || range[0] > to
108
+ }) && claimed.every((b) => b.fx1 < fx0 || b.fx0 > fx1 || b.y1 < from || b.y0 > to)
109
+ )
99
110
  }
100
111
  // A caption that cannot avoid the data still stays inside the plot.
101
112
  return (
@@ -105,27 +116,85 @@ export function refLabelPosition(opts: {
105
116
 
106
117
  /** The gap between a sparkline reference label's box and its line, px. */
107
118
  const SPARK_LABEL_GAP = 2
119
+ /** Air between a sparkline label's box and the data line (stroke and end dot included), px. */
120
+ const SPARK_LINE_AIR = 5
121
+
122
+ export type SparkLabelPlacement = {
123
+ side: 'above' | 'below'
124
+ end: 'start' | 'end'
125
+ /** The engine centres the text on its y: the offset that puts the box on `side` of the line. */
126
+ dy: number
127
+ }
108
128
 
109
129
  /**
110
- * Which side of its line a sparkline's reference label sits on, and its `dy` (the engine centres
111
- * the text on its y): above by default, below when the line is too near the plot top for the label
112
- * box to fit above it. The box clears the line by {@link SPARK_LABEL_GAP}, so the dashed rule never
113
- * strikes the text, and stays inside the plot (`height` less the line inset each side).
130
+ * Where a sparkline's reference label sits: of start-above, start-below, end-above, end-below, the
131
+ * one whose box stays inside the plot (`height` in full) and moves least off the reference to clear
132
+ * the data line by {@link SPARK_LINE_AIR} (the first of the four on a tie). The box clears the
133
+ * reference by at least {@link SPARK_LABEL_GAP}, so the dashed rule never strikes the text.
134
+ * `line` is the plotted polyline as `{ fx, y }` (a share of the plot width, a value on `domain`);
135
+ * without it (a bar sparkline, or no `width`/`label`) only the plot bounds decide.
114
136
  */
115
137
  export function referenceLabelPlacement(opts: {
116
138
  value: number
117
139
  domain: readonly [number, number]
118
140
  height: number
141
+ /** Plot width, px. */
142
+ width?: number | undefined
119
143
  /** The plot's margin inside the svg box, px. */
120
144
  inset: number
121
145
  fontSize: number
122
- }): { side: 'above' | 'below'; dy: number } {
123
- const { value, domain, height, inset, fontSize } = opts
146
+ label?: string | undefined
147
+ line?: readonly RefLabelPoint[] | undefined
148
+ }): SparkLabelPlacement {
149
+ const { value, domain, height, inset, fontSize, line } = opts
150
+ const { width = 0, label = '' } = opts
124
151
  const [lo, hi] = [Math.min(...domain), Math.max(...domain)]
125
- const share = hi === lo ? 0 : (value - lo) / (hi - lo)
126
- const line = inset + (height - 2 * inset) * (1 - share)
127
- const reach = SPARK_LABEL_GAP + fontSize + 1
128
- const side = line - reach >= 0 || line + reach > height ? 'above' : 'below'
129
- const dy = (SPARK_LABEL_GAP + (fontSize + 1) / 2) * (side === 'above' ? -1 : 1)
130
- return { side, dy }
152
+ const plotH = height - 2 * inset
153
+ const plotW = width - 2 * inset
154
+ const px = (y: number): number => inset + plotH * (1 - (hi === lo ? 0 : (y - lo) / (hi - lo)))
155
+ const at = px(value)
156
+ const boxH = fontSize
157
+ const boxW = label.length * fontSize * GLYPH_EM + EDGE_INSET
158
+ const pxLine = line?.map((p) => ({ fx: p.fx, y: px(p.y) }))
159
+ const candidates = (['start', 'end'] as const).flatMap((end) =>
160
+ (['above', 'below'] as const).map((side) => {
161
+ const nominal = side === 'above' ? at - SPARK_LABEL_GAP - boxH : at + SPARK_LABEL_GAP
162
+ const [fx0, fx1] = end === 'start' ? [0, boxW / plotW] : [1 - boxW / plotW, 1]
163
+ // The curve between two points stays between their values but not on the chord, so the
164
+ // window reaches out to the vertices that bound it.
165
+ const lead = pxLine?.find((p) => p.fx >= fx1)?.fx ?? fx1
166
+ const trail = pxLine?.findLast((p) => p.fx <= fx0)?.fx ?? fx0
167
+ const range =
168
+ pxLine === undefined || plotW <= 0
169
+ ? null
170
+ : yRangeWithin(pxLine, Math.min(trail, fx0), Math.max(lead, fx1))
171
+ // A box the line comes within the air of slides away from the reference, along its side, until it has air.
172
+ const near =
173
+ range !== null &&
174
+ range[1] + SPARK_LINE_AIR > nominal &&
175
+ range[0] - SPARK_LINE_AIR < nominal + boxH
176
+ const top = !near
177
+ ? nominal
178
+ : side === 'above'
179
+ ? range[0] - SPARK_LINE_AIR - boxH
180
+ : range[1] + SPARK_LINE_AIR
181
+ return {
182
+ side,
183
+ end,
184
+ top,
185
+ shift: Math.abs(top - nominal),
186
+ fits: top >= 0 && top + boxH <= height,
187
+ }
188
+ }),
189
+ )
190
+ const fitting = candidates.filter((c) => c.fits)
191
+ // The fitting spot that stays nearest the reference (the first of the four on a tie), else the
192
+ // first: the plot bounds alone decide when nothing fits.
193
+ const found = fitting.reduce<(typeof candidates)[number] | undefined>(
194
+ (best, c) => (best === undefined || c.shift < best.shift ? c : best),
195
+ undefined,
196
+ )
197
+ const { side, end, top } = found ?? (candidates[0] as (typeof candidates)[number])
198
+ const dy = top + boxH / 2 - at
199
+ return { side, end, dy }
131
200
  }
@@ -114,3 +114,9 @@
114
114
  border-radius: 50%;
115
115
  transform: translate(-50%, -50%);
116
116
  }
117
+
118
+ /* The reference caption steps aside while a readout shows: the chip can land on it (a long caption
119
+ * reaches the chip's line), and the readout is what the pointer asked for. */
120
+ .root[data-basalt-readout] [data-ts-key='reference-label'] {
121
+ visibility: hidden;
122
+ }
@@ -3,6 +3,7 @@
3
3
  * the chart chrome and `ChartCard`, so a pending card and a pending chart read the same.
4
4
  */
5
5
  import type { CSSProperties, ReactElement, ReactNode } from 'react'
6
+ import { splitEmptyCopy } from '../common/empty-copy'
6
7
  import { toErrorMessage } from '../common/errors'
7
8
  import { RetryButton } from '../common/retry-button'
8
9
  import { VX } from '../tokens'
@@ -42,10 +43,10 @@ const pendingStyle: CSSProperties = {
42
43
  const GHOST_BARS = [38, 52, 44, 66, 58, 74, 62, 48] as const
43
44
 
44
45
  /**
45
- * The shape a pending plot draws: `'bars'` (default), `'line'` for a line or area chart, or a bar
46
- * count when the loaded chart's data length is known (a 6-month chart loads 6 bars).
46
+ * The shape a pending plot draws: `'bars'` (default), `'line'` for a line or area chart, `'band'`
47
+ * for a `BandStrip` (one row), or a bar count when the loaded chart's data length is known (a 6-month chart loads 6 bars).
47
48
  */
48
- export type PlotGhostShape = 'bars' | 'line' | number
49
+ export type PlotGhostShape = 'bars' | 'line' | 'band' | number
49
50
 
50
51
  /** A mild rise across the width, in a 100 x 40 box stretched to the plot. */
51
52
  const GHOST_LINE = '0,30 12,26 25,28 38,18 50,22 62,12 75,16 88,8 100,12'
@@ -60,6 +61,12 @@ function PlotGhost({ shape = 'bars' }: { shape?: PlotGhostShape }): ReactElement
60
61
  </svg>
61
62
  </div>
62
63
  )
64
+ if (shape === 'band')
65
+ return (
66
+ <div aria-hidden="true" data-basalt-plot-ghost="band" className={boxClasses.ghost}>
67
+ <span className={boxClasses.ghostBand} />
68
+ </div>
69
+ )
63
70
  const count =
64
71
  typeof shape === 'number' ? Math.max(1, Math.min(Math.floor(shape), 60)) : GHOST_BARS.length
65
72
  return (
@@ -97,6 +104,11 @@ const errorStyle: CSSProperties = {
97
104
  fontSize: VX.text.sm,
98
105
  gap: VX.space.controlGap,
99
106
  }
107
+ // The empty tile's one ring: an inset shadow, never a border, as `EmptyState`'s section tier draws it.
108
+ const tileStyle: CSSProperties = {
109
+ // theme-allow surface-shadow-override — an inset ring on a tile inside a surface, not a card's depth
110
+ boxShadow: `var(--basalt-state-ring, inset 0 0 0 1px ${VX.surface.hairline})`,
111
+ }
100
112
  const errorHeadStyle: CSSProperties = {
101
113
  display: 'inline-flex',
102
114
  alignItems: 'center',
@@ -152,7 +164,8 @@ export function ChartPlaceholder(props: {
152
164
  if (kind === 'pending')
153
165
  return fit(
154
166
  <output aria-label="Loading chart" aria-busy="true" className={stateClass} style={style}>
155
- {!props.compact && <PlotGhost {...(props.ghost !== undefined && { shape: props.ghost })} />}
167
+ {/* A sparkline's box is too small for bars: it ghosts a line, in the same tone as every ghost. */}
168
+ <PlotGhost shape={props.ghost ?? (props.compact ? 'line' : 'bars')} />
156
169
  </output>,
157
170
  )
158
171
  const label =
@@ -189,9 +202,25 @@ export function ChartPlaceholder(props: {
189
202
  {body}
190
203
  </div>,
191
204
  )
205
+ if (props.compact)
206
+ return fit(
207
+ <output data-basalt-chart-state={kind} className={stateClass} style={style} {...name}>
208
+ {body}
209
+ </output>,
210
+ )
211
+ // The § 6.11 empty tile inside the chart's own box: one ring, card radius, the title at `sm` 500
212
+ // and a muted line, centred. `EmptyState` is Mantine-coupled, so the anatomy is plain HTML here.
213
+ // The live region holds the copy only; the action is its sibling, so a button is not announced.
214
+ const { title, description } = splitEmptyCopy(label)
192
215
  return fit(
193
- <output data-basalt-chart-state={kind} className={stateClass} style={style} {...name}>
194
- {body}
195
- </output>,
216
+ <div data-basalt-chart-state={kind} className={stateClass} style={boxStyle}>
217
+ <div data-basalt-empty-tile="" className={boxClasses.emptyTile} style={tileStyle}>
218
+ <output className={boxClasses.emptyCopy}>
219
+ <span className={boxClasses.emptyTitle}>{title}</span>
220
+ {description !== undefined && <span className={boxClasses.emptyText}>{description}</span>}
221
+ </output>
222
+ {props.action !== undefined && <div>{props.action}</div>}
223
+ </div>
224
+ </div>,
196
225
  )
197
226
  }