basalt-ui 2.16.0 → 2.18.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 (146) hide show
  1. package/CHANGELOG.md +165 -124
  2. package/MIGRATING.md +133 -15
  3. package/agent/agents/basalt-design-reviewer.md +2 -2
  4. package/agent/skills/basalt-design/SKILL.md +3 -3
  5. package/configs/oxlint-plugin.js +2 -2
  6. package/dist/charts/Donut.d.ts.map +1 -1
  7. package/dist/charts/Donut.js +2 -2
  8. package/dist/charts/DualPanel.js +2 -2
  9. package/dist/charts/Heatmap.d.ts.map +1 -1
  10. package/dist/charts/Heatmap.js +2 -2
  11. package/dist/charts/MirroredBars.js +2 -2
  12. package/dist/charts/Sparkline.d.ts +4 -2
  13. package/dist/charts/Sparkline.d.ts.map +1 -1
  14. package/dist/charts/Sparkline.js +3 -3
  15. package/dist/charts/compile.d.ts +8 -3
  16. package/dist/charts/compile.d.ts.map +1 -1
  17. package/dist/charts/compile.js +19 -9
  18. package/dist/charts/cursor.d.ts +14 -1
  19. package/dist/charts/cursor.d.ts.map +1 -1
  20. package/dist/charts/cursor.js +29 -3
  21. package/dist/charts/date-tick-format.d.ts +17 -0
  22. package/dist/charts/date-tick-format.d.ts.map +1 -0
  23. package/dist/charts/date-tick-format.js +81 -0
  24. package/dist/charts/panel.d.ts +2 -1
  25. package/dist/charts/panel.d.ts.map +1 -1
  26. package/dist/charts/panel.js +3 -2
  27. package/dist/charts/types.d.ts +5 -1
  28. package/dist/charts/types.d.ts.map +1 -1
  29. package/dist/cli/help.d.ts +1 -0
  30. package/dist/cli/help.d.ts.map +1 -1
  31. package/dist/cli/help.js +8 -2
  32. package/dist/cli/shot/capture.d.ts +28 -1
  33. package/dist/cli/shot/capture.d.ts.map +1 -1
  34. package/dist/cli/shot/capture.js +96 -13
  35. package/dist/cli/shot/index.d.ts +10 -1
  36. package/dist/cli/shot/index.d.ts.map +1 -1
  37. package/dist/cli/shot/index.js +40 -5
  38. package/dist/cli/shot/playwright.d.ts +4 -0
  39. package/dist/cli/shot/playwright.d.ts.map +1 -1
  40. package/dist/cli/shot/real-data.d.ts +35 -0
  41. package/dist/cli/shot/real-data.d.ts.map +1 -0
  42. package/dist/cli/shot/real-data.js +120 -0
  43. package/dist/cli/shot/serve.d.ts +2 -2
  44. package/dist/cli/shot/serve.d.ts.map +1 -1
  45. package/dist/cli/shot/serve.js +16 -2
  46. package/dist/dashboard/bar-list.d.ts +29 -0
  47. package/dist/dashboard/bar-list.d.ts.map +1 -0
  48. package/dist/dashboard/bar-list.js +52 -0
  49. package/dist/dashboard/bar-list.module.css +73 -0
  50. package/dist/dashboard/bar-list.module.css.d.ts +16 -0
  51. package/dist/dashboard/delta-badge.d.ts +2 -2
  52. package/dist/dashboard/delta-badge.d.ts.map +1 -1
  53. package/dist/dashboard/delta-badge.js +12 -14
  54. package/dist/dashboard/index.d.ts +2 -0
  55. package/dist/dashboard/index.d.ts.map +1 -1
  56. package/dist/dashboard/index.js +4 -0
  57. package/dist/dashboard/meter-model.d.ts +78 -0
  58. package/dist/dashboard/meter-model.d.ts.map +1 -0
  59. package/dist/dashboard/meter-model.js +91 -0
  60. package/dist/dashboard/meter-parts.d.ts +42 -0
  61. package/dist/dashboard/meter-parts.d.ts.map +1 -0
  62. package/dist/dashboard/meter-parts.js +163 -0
  63. package/dist/dashboard/meter.d.ts +112 -0
  64. package/dist/dashboard/meter.d.ts.map +1 -0
  65. package/dist/dashboard/meter.js +43 -0
  66. package/dist/dashboard/meter.module.css +150 -0
  67. package/dist/dashboard/meter.module.css.d.ts +28 -0
  68. package/dist/dashboard/query-state.d.ts +35 -0
  69. package/dist/dashboard/query-state.d.ts.map +1 -1
  70. package/dist/dashboard/query-state.js +12 -1
  71. package/dist/dashboard/scale.d.ts +9 -0
  72. package/dist/dashboard/scale.d.ts.map +1 -0
  73. package/dist/dashboard/scale.js +10 -0
  74. package/dist/dashboard/stat-card-parts.d.ts +40 -0
  75. package/dist/dashboard/stat-card-parts.d.ts.map +1 -0
  76. package/dist/dashboard/stat-card-parts.js +55 -0
  77. package/dist/dashboard/stat-card.d.ts +46 -34
  78. package/dist/dashboard/stat-card.d.ts.map +1 -1
  79. package/dist/dashboard/stat-card.js +148 -79
  80. package/dist/dashboard/stat-card.module.css +220 -7
  81. package/dist/dashboard/stat-card.module.css.d.ts +20 -0
  82. package/dist/dashboard/widget-header.d.ts.map +1 -1
  83. package/dist/dashboard/widget-header.js +73 -9
  84. package/dist/dashboard/widget-header.module.css +25 -17
  85. package/dist/guard/index.js +2 -2
  86. package/dist/index.d.ts +1 -1
  87. package/dist/index.d.ts.map +1 -1
  88. package/dist/index.js +5 -1
  89. package/dist/theme/chrome-vars.d.ts +82 -0
  90. package/dist/theme/chrome-vars.d.ts.map +1 -0
  91. package/dist/theme/chrome-vars.js +36 -0
  92. package/dist/theme/controls.module.css +20 -1
  93. package/dist/theme/controls.module.css.d.ts +1 -0
  94. package/dist/theme/index.d.ts.map +1 -1
  95. package/dist/theme/index.js +30 -33
  96. package/dist/tokens/derive.d.ts +11 -1
  97. package/dist/tokens/derive.d.ts.map +1 -1
  98. package/dist/tokens/derive.js +9 -24
  99. package/dist/tokens/status-text.d.ts +26 -0
  100. package/dist/tokens/status-text.d.ts.map +1 -0
  101. package/dist/tokens/status-text.js +68 -0
  102. package/llms.txt +36 -4
  103. package/package.json +1 -1
  104. package/src/charts/Donut.tsx +2 -2
  105. package/src/charts/DualPanel.tsx +2 -2
  106. package/src/charts/Heatmap.tsx +2 -2
  107. package/src/charts/MirroredBars.tsx +2 -2
  108. package/src/charts/Sparkline.tsx +7 -5
  109. package/src/charts/compile.ts +31 -11
  110. package/src/charts/cursor.ts +48 -3
  111. package/src/charts/date-tick-format.ts +132 -0
  112. package/src/charts/panel.tsx +5 -3
  113. package/src/charts/types.ts +5 -1
  114. package/src/cli/help.ts +22 -2
  115. package/src/cli/shot/capture.ts +133 -12
  116. package/src/cli/shot/index.ts +50 -4
  117. package/src/cli/shot/playwright.ts +1 -1
  118. package/src/cli/shot/real-data.ts +165 -0
  119. package/src/cli/shot/serve.ts +28 -3
  120. package/src/dashboard/bar-list.module.css +73 -0
  121. package/src/dashboard/bar-list.module.css.d.ts +16 -0
  122. package/src/dashboard/bar-list.tsx +112 -0
  123. package/src/dashboard/delta-badge.tsx +24 -18
  124. package/src/dashboard/index.ts +2 -0
  125. package/src/dashboard/meter-model.ts +161 -0
  126. package/src/dashboard/meter-parts.tsx +223 -0
  127. package/src/dashboard/meter.module.css +150 -0
  128. package/src/dashboard/meter.module.css.d.ts +28 -0
  129. package/src/dashboard/meter.tsx +162 -0
  130. package/src/dashboard/query-state.tsx +38 -0
  131. package/src/dashboard/scale.ts +13 -0
  132. package/src/dashboard/stat-card-parts.tsx +108 -0
  133. package/src/dashboard/stat-card.module.css +220 -7
  134. package/src/dashboard/stat-card.module.css.d.ts +20 -0
  135. package/src/dashboard/stat-card.tsx +282 -174
  136. package/src/dashboard/widget-header.module.css +25 -17
  137. package/src/dashboard/widget-header.tsx +103 -7
  138. package/src/guard/index.ts +2 -2
  139. package/src/index.ts +4 -0
  140. package/src/theme/__snapshots__/plain-theme.test.ts.snap +128 -40
  141. package/src/theme/chrome-vars.ts +111 -0
  142. package/src/theme/controls.module.css +20 -1
  143. package/src/theme/controls.module.css.d.ts +1 -0
  144. package/src/theme/index.ts +39 -101
  145. package/src/tokens/derive.ts +19 -26
  146. package/src/tokens/status-text.ts +85 -0
@@ -0,0 +1,73 @@
1
+ /*
2
+ * BarList — ranked rows on one shared scale (docs/waves/design-parity/SPEC.md § 6.4). The bar sits
3
+ * BEHIND the label and value; the row never wraps: the label truncates, the value is nowrap.
4
+ */
5
+
6
+ .root {
7
+ display: flex;
8
+ flex-direction: column;
9
+ gap: var(--vx-space-stack-xs, 0.25rem);
10
+ margin: 0;
11
+ padding: 0;
12
+ list-style: none;
13
+ min-width: 0;
14
+ }
15
+
16
+ .row {
17
+ position: relative;
18
+ display: flex;
19
+ align-items: center;
20
+ gap: var(--vx-space-control-gap, 0.375rem);
21
+ min-width: 0;
22
+ min-height: calc(var(--vx-space-control-height-widget, 1.5rem) * var(--mantine-scale, 1));
23
+ padding-inline: var(--vx-space-stack-sm);
24
+ font-size: var(--vx-text-sm);
25
+ color: var(--vx-ink);
26
+ }
27
+
28
+ .bar {
29
+ position: absolute;
30
+ inset-block: 0;
31
+ inset-inline-start: 0;
32
+ border-radius: var(--vx-radius-fine);
33
+ background: color-mix(in srgb, var(--vx-line) 30%, transparent);
34
+ }
35
+
36
+ .leader {
37
+ background: color-mix(in srgb, var(--vx-accent) 28%, transparent);
38
+ }
39
+
40
+ .label {
41
+ position: relative;
42
+ flex: 1 1 0;
43
+ min-width: 0;
44
+ overflow: hidden;
45
+ text-overflow: ellipsis;
46
+ white-space: nowrap;
47
+ }
48
+
49
+ .value {
50
+ position: relative;
51
+ flex: none;
52
+ font-family: var(--basalt-font-mono);
53
+ text-align: end;
54
+ white-space: nowrap;
55
+ }
56
+
57
+ .secondary {
58
+ position: relative;
59
+ flex: none;
60
+ font-family: var(--basalt-font-mono);
61
+ font-size: var(--vx-text-xs);
62
+ color: var(--vx-muted);
63
+ white-space: nowrap;
64
+ }
65
+
66
+ .empty {
67
+ font-size: var(--vx-text-sm);
68
+ color: var(--vx-muted);
69
+ }
70
+
71
+ .skeletonRow {
72
+ height: calc(var(--vx-space-control-height-widget, 1.5rem) * var(--mantine-scale, 1));
73
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Typed CSS-module declaration (the S4 CSS-modules path). Named string props — not an index
3
+ * signature — so dot access stays verbatim under the package's strict flags.
4
+ */
5
+ declare const classes: {
6
+ readonly root: string
7
+ readonly row: string
8
+ readonly bar: string
9
+ readonly leader: string
10
+ readonly label: string
11
+ readonly value: string
12
+ readonly secondary: string
13
+ readonly empty: string
14
+ readonly skeletonRow: string
15
+ }
16
+ export default classes
@@ -0,0 +1,112 @@
1
+ /**
2
+ * BarList — a ranked list: label, an inline bar on ONE shared scale, a mono right-aligned value and
3
+ * an optional muted secondary value. Choose it for "top N" (the bar ranks by length before anything
4
+ * is read). Intended for 5–10 rows; past 10 use a table with an inline `Meter` cell. An HTML/CSS
5
+ * primitive, not a chart kind.
6
+ *
7
+ * Colour law: bars are neutral. `accentLeader` (opt-in) lets the top row take the accent — the
8
+ * leader bar, and nothing else. Each value is rendered as text, never only a bar length.
9
+ *
10
+ * @example
11
+ * import { BarList } from 'basalt-ui'
12
+ *
13
+ * // Recovery contributors on a fixed 0–100 scale, the qualitative level as the secondary value:
14
+ * <BarList label="Recovery contributors" max={100} items={[{ label: 'Sleep', value: 82, secondary: 'Good' }]} />
15
+ *
16
+ * <BarList
17
+ * label="Top projects by tokens"
18
+ * items={[{ label: 'argo', value: 1840, secondary: '41%' }, { label: 'rb', value: 920 }]}
19
+ * format={compact}
20
+ * accentLeader
21
+ * />
22
+ */
23
+ import { Skeleton, VisuallyHidden } from '@mantine/core'
24
+ import type { ReactNode } from 'react'
25
+ import { cx } from '../common/props'
26
+ import type { BasaltProps } from '../common/props'
27
+ import classes from './bar-list.module.css'
28
+ import { asText, barPercent } from './scale'
29
+
30
+ type BarListItem = { label: string; value: number; secondary?: ReactNode; key?: string }
31
+
32
+ export type BarListProps = BasaltProps & {
33
+ /** Accessible name of the list. Required. */
34
+ label: string
35
+ /** Rows in display order (sort them yourself; the list does not reorder). */
36
+ items: readonly BarListItem[]
37
+ /** Formats each value. Default: the number as written. */
38
+ format?: (value: number) => string
39
+ /** Scale end shared by every bar. Default: the largest value. */
40
+ max?: number
41
+ /** The first row's bar takes the accent. Off by default. */
42
+ accentLeader?: boolean
43
+ /** Renders row-shaped skeletons. */
44
+ loading?: boolean
45
+ /** Skeleton rows while loading. Default 5. */
46
+ loadingRows?: number
47
+ /** Muted line shown when `items` is empty. */
48
+ emptyText?: string
49
+ }
50
+
51
+ export function BarList({
52
+ label,
53
+ items,
54
+ format = asText,
55
+ max,
56
+ accentLeader = false,
57
+ loading = false,
58
+ loadingRows = 5,
59
+ emptyText = 'No data',
60
+ className,
61
+ style,
62
+ }: BarListProps) {
63
+ if (loading) {
64
+ return (
65
+ <div className={cx(classes.root, className)} style={style} aria-busy="true">
66
+ <VisuallyHidden>{`Loading ${label}`}</VisuallyHidden>
67
+ {Array.from({ length: loadingRows }, (_, i) => (
68
+ <Skeleton key={i} className={classes.skeletonRow} data-bar-skeleton />
69
+ ))}
70
+ </div>
71
+ )
72
+ }
73
+ if (items.length === 0) {
74
+ return (
75
+ <output className={cx(classes.root, className)} style={style} aria-label={label}>
76
+ <span className={classes.empty}>{emptyText}</span>
77
+ </output>
78
+ )
79
+ }
80
+
81
+ const scale =
82
+ max ?? items.reduce((top, i) => (Number.isFinite(i.value) ? Math.max(top, i.value) : top), 0)
83
+ return (
84
+ // The explicit role is deliberate: `list-style: none` makes Safari/VoiceOver drop the implicit one.
85
+ // oxlint-disable-next-line jsx-a11y/no-redundant-roles
86
+ <ul className={cx(classes.root, className)} style={style} role="list" aria-label={label}>
87
+ {items.map((item, i) => {
88
+ const isLeader = accentLeader && i === 0
89
+ return (
90
+ <li key={item.key ?? `${item.label}-${i}`} className={classes.row}>
91
+ {/* theme-allow hidden-inline-style — the width is the row's data (a % of the shared scale), not styling */}
92
+ <span
93
+ className={cx(classes.bar, isLeader && classes.leader)}
94
+ style={{ width: `${barPercent(item.value, scale)}%` }}
95
+ data-bar-leader={isLeader ? '' : undefined}
96
+ aria-hidden
97
+ />
98
+ <span className={classes.label} title={item.label}>
99
+ {item.label}
100
+ </span>
101
+ <span className={classes.value}>
102
+ {Number.isFinite(item.value) ? format(item.value) : '—'}
103
+ </span>
104
+ {item.secondary !== undefined && (
105
+ <span className={classes.secondary}>{item.secondary}</span>
106
+ )}
107
+ </li>
108
+ )
109
+ })}
110
+ </ul>
111
+ )
112
+ }
@@ -1,10 +1,12 @@
1
1
  /**
2
- * DeltaBadge — the KPI trend chip (docs/DESIGN-SPEC.md §5): mono 11.5px weight 600, status-color
3
- * text on a status-13% tint, control radius, 2px/7px padding, optional ▲/▼ glyph at 9px.
4
- * `polarity` decides the TONE the sign maps to — `'up-good'` (default) reads a rise as success and
5
- * a fall as danger; `'up-bad'` inverts that for a metric where a rise is the worse reading (a
6
- * cycle-top index, latency, error rate); `'neutral'` renders every sign in a faint, non-verdict
7
- * tint. Zero is always faint with no glyph, under every polarity. The label never carries a
2
+ * DeltaBadge — the KPI trend readout (docs/DESIGN-CORE.md: "up/down deltas are text colour only,
3
+ * never a fill"): a glyph and a signed figure in the trend colour, mono `sm`, with NO fill, border
4
+ * or padding in either scheme. The optional ▲/▼ glyph sits on the `micro` rung and `period` is
5
+ * `micro` muted. `polarity` decides the TONE the sign maps to — `'up-good'` (default) reads a rise
6
+ * as success and a fall as danger; `'up-bad'` inverts that for a metric where a rise is the worse
7
+ * reading (a cycle-top index, latency, error rate); `'neutral'` renders every sign in a faint,
8
+ * non-verdict
9
+ * colour. Zero is always faint with no glyph, under every polarity. The label never carries a
8
10
  * leading "+"; pass `format` to control the magnitude string (defaults to a one-decimal percentage
9
11
  * of the absolute value). The ▲/▼ glyph is DIRECTION, not verdict, and stays polarity-independent —
10
12
  * governed only by `withGlyph` and the sign.
@@ -16,13 +18,13 @@
16
18
  * @example
17
19
  * import { DeltaBadge } from 'basalt-ui'
18
20
  *
19
- * <DeltaBadge value={12.4} /> // ▲ 12.4%, success tint
20
- * <DeltaBadge value={-3.1} /> // ▼ 3.1%, danger tint
21
- * <DeltaBadge value={0} /> // 0.0%, neutral faint tint, no glyph
21
+ * <DeltaBadge value={12.4} /> // ▲ 12.4%, success colour
22
+ * <DeltaBadge value={-3.1} /> // ▼ 3.1%, danger colour
23
+ * <DeltaBadge value={0} /> // 0.0%, faint, no glyph
22
24
  * <DeltaBadge value={182} format={(v) => `${Math.abs(v)}ms`} withGlyph={false} />
23
- * <DeltaBadge value={35.9} polarity="up-bad" /> // ▲ 35.9%, danger tint — a rise is bad here
25
+ * <DeltaBadge value={35.9} polarity="up-bad" /> // ▲ 35.9%, danger — a rise is bad here
24
26
  */
25
- import { alpha, VX } from '../tokens'
27
+ import { VX } from '../tokens'
26
28
  import classes from './widget-header.module.css'
27
29
 
28
30
  export type DeltaPolarity = 'up-good' | 'up-bad' | 'neutral'
@@ -39,8 +41,8 @@ export type DeltaBadgeProps = {
39
41
  format?: (value: number) => string
40
42
  /** Render the ▲/▼ direction glyph. Defaults to `true`; a zero value never shows a glyph. */
41
43
  withGlyph?: boolean
42
- /** Optional comparison timeframe rendered directly after the value in a dimmer shade of the same
43
- * tone (e.g. `MoM`, `WoW`, `YTD`) — states what the delta is measured against without a hover. */
44
+ /** Optional comparison timeframe rendered directly after the value, muted (e.g. `MoM`, `WoW`,
45
+ * `YTD`) — states what the delta is measured against without a hover. */
44
46
  period?: string | undefined
45
47
  }
46
48
 
@@ -53,6 +55,13 @@ function resolveTone(value: number, polarity: DeltaPolarity): string {
53
55
  return good ? VX.status.good : VX.status.bad
54
56
  }
55
57
 
58
+ /** The direction mark for a signed value; zero has none. Direction only, never the verdict. */
59
+ function directionGlyph(value: number): '▲' | '▼' | undefined {
60
+ if (value > 0) return '▲'
61
+ if (value < 0) return '▼'
62
+ return undefined
63
+ }
64
+
56
65
  export function DeltaBadge({
57
66
  value,
58
67
  polarity = 'up-good',
@@ -61,14 +70,11 @@ export function DeltaBadge({
61
70
  period,
62
71
  }: DeltaBadgeProps) {
63
72
  const tone = resolveTone(value, polarity)
64
- const glyph = value > 0 ? '▲' : value < 0 ? '▼' : undefined
73
+ const glyph = directionGlyph(value)
65
74
  const showGlyph = withGlyph && glyph !== undefined
66
75
 
67
76
  return (
68
- <span
69
- className={classes.deltaBadge}
70
- style={{ backgroundColor: alpha(tone, 0.13), color: tone }}
71
- >
77
+ <span className={classes.deltaBadge} style={{ color: tone }}>
72
78
  {showGlyph && <span className={classes.deltaGlyph}>{glyph}</span>}
73
79
  {format(value)}
74
80
  {period !== undefined && <span className={classes.deltaPeriod}>{period}</span>}
@@ -5,3 +5,5 @@ export { StatCard, type StatCardBreakdownRow, type StatCardSparklineSize } from
5
5
  export { StatGroup } from './stat-group'
6
6
  export { EmptyState } from './empty-state'
7
7
  export { SettingsSection, SettingsRow, DangerZone } from './settings-section'
8
+ export { Meter, type MeterProps } from './meter'
9
+ export { BarList, type BarListProps } from './bar-list'
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Meter's pure model: everything the component shows, computed from numbers (no DOM, no React).
3
+ * Internal — `meter.tsx` and `meter-parts.tsx` render what this returns.
4
+ */
5
+ import { clamp01, toPct } from './scale'
6
+
7
+ export type MeterSegment = { value: number; label: string; color?: string; forecast?: boolean }
8
+ export type MeterZone = { from: number; to: number; label: string; color?: string }
9
+
10
+ /** A fill on the track, as fractions (0–1) of the span. */
11
+ export type Part = {
12
+ readonly start: number
13
+ readonly size: number
14
+ readonly color: string | undefined
15
+ readonly forecast: boolean
16
+ }
17
+
18
+ type Format = (value: number) => string
19
+
20
+ export const isReading = (n: number | null | undefined): n is number =>
21
+ typeof n === 'number' && Number.isFinite(n)
22
+
23
+ /** The track's span. A track that ends at or before its start falls back to 1 and is `valid: false`. */
24
+ export function spanOf(min: number, max: number): { span: number; valid: boolean } {
25
+ return max > min ? { span: max - min, valid: true } : { span: 1, valid: false }
26
+ }
27
+
28
+ export function ariaValues(reading: number, min: number, max: number, format: Format) {
29
+ return {
30
+ role: 'meter',
31
+ 'aria-valuenow': Math.min(max, Math.max(min, reading)),
32
+ 'aria-valuemin': min,
33
+ 'aria-valuemax': max,
34
+ 'aria-valuetext': format(reading),
35
+ } as const
36
+ }
37
+
38
+ /**
39
+ * The ONE place segment amounts are sanitised: a negative or non-finite amount is 0. The painted
40
+ * track, the captions and `aria-valuenow` all derive from what this returns.
41
+ */
42
+ export function normalizeSegments(segments: readonly MeterSegment[]): MeterSegment[] {
43
+ return segments.map((s) => ({ ...s, value: Number.isFinite(s.value) ? Math.max(0, s.value) : 0 }))
44
+ }
45
+
46
+ /** Segment amounts (already normalised) accumulate from `min`; a part never passes the track end. */
47
+ export function layoutSegments(segments: readonly MeterSegment[], span: number): Part[] {
48
+ let cursor = 0
49
+ return segments.map((s) => {
50
+ const size = Math.max(0, Math.min(clamp01(s.value / span), 1 - cursor))
51
+ const forecast = s.forecast === true
52
+ // A forecast stays the lighter neutral: a colour would turn the committed/forecast pair into hue.
53
+ const part = { start: cursor, size, color: forecast ? undefined : s.color, forecast }
54
+ cursor += size
55
+ return part
56
+ })
57
+ }
58
+
59
+ /** `from` is inclusive; a value on the last band's `to` belongs to that band. */
60
+ export function findHoldingZone(zones: readonly MeterZone[], value: number): MeterZone | undefined {
61
+ const end = Math.max(...zones.map((z) => z.to))
62
+ return zones.find((z) => value >= z.from && (value < z.to || (value === z.to && z.to === end)))
63
+ }
64
+
65
+ /** A zone band as left edge + width in percent, clipped to the track. */
66
+ export function zoneBand(zone: MeterZone, min: number, span: number) {
67
+ const lo = clamp01((zone.from - min) / span)
68
+ const hi = clamp01((zone.to - min) / span)
69
+ return { start: toPct(lo), width: toPct(hi - lo) }
70
+ }
71
+
72
+ const segmentText = (s: MeterSegment, format: Format): string =>
73
+ `${s.label} ${format(s.value)}${s.forecast === true ? ' (forecast)' : ''}`
74
+
75
+ export type BarInput = {
76
+ reading: number
77
+ min: number
78
+ max: number
79
+ format: Format
80
+ segments?: readonly MeterSegment[] | undefined
81
+ zones?: readonly MeterZone[] | undefined
82
+ target?: number | undefined
83
+ color?: string | undefined
84
+ forecastCaption?: string | undefined
85
+ }
86
+
87
+ export type BarModel = {
88
+ span: number
89
+ /** Fills, or none when the track is drawn as zones. */
90
+ parts: readonly Part[]
91
+ /** Percent where the first forecast part starts; absent when the forecast opens the track. */
92
+ boundaryPct: number | undefined
93
+ forecastText: string | null
94
+ markerPct: number | undefined
95
+ targetPct: number | undefined
96
+ startText: string
97
+ endText: string | null
98
+ describedByText: string | null
99
+ }
100
+
101
+ function endCaptionText(reading: number, target: number | undefined, format: Format) {
102
+ if (target === undefined) return null
103
+ if (reading > target) return `Target ${format(target)} · ${format(reading - target)} over`
104
+ return `Target ${format(target)}`
105
+ }
106
+
107
+ function fillParts(input: BarInput, span: number, zoned: boolean): Part[] {
108
+ if (zoned) return []
109
+ if (input.segments !== undefined) return layoutSegments(input.segments, span)
110
+ const fill = { value: input.reading - input.min, label: '' }
111
+ return layoutSegments([input.color === undefined ? fill : { ...fill, color: input.color }], span)
112
+ }
113
+
114
+ /** Segments are amounts accumulated from `min`, so the reading is `min` plus their sum. */
115
+ export const segmentReading = (segments: readonly MeterSegment[], min: number): number =>
116
+ segments.reduce((sum, s) => sum + s.value, min)
117
+
118
+ export function modelBar(input: BarInput): BarModel {
119
+ const { reading, min, max, format, segments, zones, target } = input
120
+ const { span } = spanOf(min, max)
121
+ const zoned = zones !== undefined && zones.length > 0
122
+ const holding = zoned ? findHoldingZone(zones, reading) : undefined
123
+
124
+ const parts = fillParts(input, span, zoned)
125
+ const firstForecast = parts.findIndex((p) => p.forecast)
126
+ const forecastLabels =
127
+ segments?.filter((s) => s.forecast === true).map((s) => `${s.label} ${format(s.value)}`) ?? []
128
+
129
+ const zoneText = !zoned
130
+ ? []
131
+ : [
132
+ ...zones.map((z) => `${z.label} ${format(z.from)} to ${format(z.to)}`),
133
+ holding === undefined
134
+ ? `${format(reading)} is outside every zone`
135
+ : `${format(reading)} is in ${holding.label}`,
136
+ ]
137
+ const describedBy = [
138
+ ...(segments?.map((s) => segmentText(s, format)) ?? []),
139
+ ...zoneText,
140
+ ...(target === undefined ? [] : [`Target ${format(target)}`]),
141
+ ]
142
+
143
+ return {
144
+ span,
145
+ parts,
146
+ boundaryPct: firstForecast > 0 ? toPct(parts[firstForecast]?.start ?? 0) : undefined,
147
+ forecastText:
148
+ forecastLabels.length === 0
149
+ ? null
150
+ : (input.forecastCaption ?? `Forecast: ${forecastLabels.join(', ')}`),
151
+ markerPct: zoned ? toPct((reading - min) / span) : undefined,
152
+ targetPct: target === undefined ? undefined : toPct((target - min) / span),
153
+ startText: holding === undefined ? format(reading) : `${holding.label} · ${format(reading)}`,
154
+ endText: endCaptionText(reading, target, format),
155
+ describedByText: describedBy.length > 0 ? describedBy.join('; ') : null,
156
+ }
157
+ }
158
+
159
+ /** The ring's one arc, as a fraction of the circumference. */
160
+ export const ringFraction = (reading: number, min: number, max: number): number =>
161
+ clamp01((reading - min) / spanOf(min, max).span)
@@ -0,0 +1,223 @@
1
+ /** Meter's internal pieces: bar, ring, empty and loading. `meter.tsx` dispatches between them. */
2
+ import { Skeleton, VisuallyHidden } from '@mantine/core'
3
+ import { useId } from 'react'
4
+ import type { CSSProperties, ReactNode } from 'react'
5
+ import { cx } from '../common/props'
6
+ import { ariaValues, modelBar, ringFraction, zoneBand } from './meter-model'
7
+ import type { MeterSegment, MeterZone, Part } from './meter-model'
8
+ import classes from './meter.module.css'
9
+ import { toPct } from './scale'
10
+
11
+ export type Shell = { className: string | undefined; style: CSSProperties | undefined }
12
+
13
+ const RING_VIEWBOX = 36
14
+ const RING_STROKE = 4
15
+ const RING_RADIUS = (RING_VIEWBOX - RING_STROKE) / 2
16
+ const RING_LENGTH = 2 * Math.PI * RING_RADIUS
17
+ const RING_CENTER = RING_VIEWBOX / 2
18
+
19
+ /** The one ring drawing: a track and, when there is a reading, its arc (a fraction of the circle). */
20
+ function RingSvg({ fraction, color }: { fraction?: number; color?: string | undefined }) {
21
+ return (
22
+ <svg className={classes.ring} viewBox={`0 0 ${RING_VIEWBOX} ${RING_VIEWBOX}`} aria-hidden>
23
+ <circle
24
+ className={classes.ringTrack}
25
+ cx={RING_CENTER}
26
+ cy={RING_CENTER}
27
+ r={RING_RADIUS}
28
+ strokeWidth={RING_STROKE}
29
+ />
30
+ {fraction !== undefined && (
31
+ <circle
32
+ className={classes.ringArc}
33
+ cx={RING_CENTER}
34
+ cy={RING_CENTER}
35
+ r={RING_RADIUS}
36
+ strokeWidth={RING_STROKE}
37
+ strokeDasharray={`${fraction * RING_LENGTH} ${RING_LENGTH}`}
38
+ {...(color !== undefined && { style: { stroke: color } })}
39
+ />
40
+ )}
41
+ </svg>
42
+ )
43
+ }
44
+
45
+ export function MeterLoading({
46
+ label,
47
+ ring,
48
+ shell,
49
+ }: {
50
+ label: string
51
+ ring: boolean
52
+ shell: Shell
53
+ }) {
54
+ return (
55
+ <div className={cx(classes.root, shell.className)} style={shell.style} aria-busy="true">
56
+ <VisuallyHidden>{`Loading ${label}`}</VisuallyHidden>
57
+ {ring ? (
58
+ <Skeleton className={classes.skeletonRing} circle />
59
+ ) : (
60
+ <>
61
+ <Skeleton className={classes.skeletonTrack} />
62
+ <Skeleton className={classes.skeletonCaption} radius="sm" />
63
+ </>
64
+ )}
65
+ </div>
66
+ )
67
+ }
68
+
69
+ export function MeterEmpty(props: { label: string; text: string; ring: boolean; shell: Shell }) {
70
+ const { label, text, ring, shell } = props
71
+ return (
72
+ <output className={cx(classes.root, shell.className)} style={shell.style} aria-label={label}>
73
+ {ring ? (
74
+ <span className={classes.ringBox}>
75
+ <RingSvg />
76
+ </span>
77
+ ) : (
78
+ <div className={classes.trackBox}>
79
+ <div className={classes.track} data-meter-track />
80
+ </div>
81
+ )}
82
+ <span className={classes.empty}>{text}</span>
83
+ </output>
84
+ )
85
+ }
86
+
87
+ export function MeterRing(props: {
88
+ label: string
89
+ reading: number
90
+ min: number
91
+ max: number
92
+ color: string | undefined
93
+ format: (value: number) => string
94
+ shell: Shell
95
+ }) {
96
+ const { label, reading, min, max, color, format, shell } = props
97
+ return (
98
+ <div className={cx(classes.root, shell.className)} style={shell.style}>
99
+ <span
100
+ className={classes.ringBox}
101
+ aria-label={label}
102
+ {...ariaValues(reading, min, max, format)}
103
+ >
104
+ <RingSvg fraction={ringFraction(reading, min, max)} color={color} />
105
+ <span className={classes.ringValue}>{format(reading)}</span>
106
+ </span>
107
+ </div>
108
+ )
109
+ }
110
+
111
+ function fillStyle(p: Part): CSSProperties {
112
+ return { width: `${toPct(p.size)}%`, ...(p.color !== undefined && { background: p.color }) }
113
+ }
114
+
115
+ /** Uncoloured zones step from `MIN` to `MIN + STEP` percent of the neutral, so order reads without hue. */
116
+ const ZONE_STRENGTH_MIN = 15
117
+ const ZONE_STRENGTH_STEP = 45
118
+ const ZONE_STRENGTH_SOLO = 40
119
+ /** Every band after the first gives up its leading pixel, so a boundary never relies on fill strength. */
120
+ const ZONE_GAP = '1px'
121
+
122
+ function zoneStyle(zone: MeterZone, index: number, count: number, min: number, span: number) {
123
+ const band = zoneBand(zone, min, span)
124
+ const gap = index > 0
125
+ const strength =
126
+ count > 1 ? ZONE_STRENGTH_MIN + (index / (count - 1)) * ZONE_STRENGTH_STEP : ZONE_STRENGTH_SOLO
127
+ return {
128
+ insetInlineStart: gap ? `calc(${band.start}% + ${ZONE_GAP})` : `${band.start}%`,
129
+ width: gap ? `calc(${band.width}% - ${ZONE_GAP})` : `${band.width}%`,
130
+ background: zone.color ?? `color-mix(in srgb, var(--vx-line) ${strength}%, transparent)`,
131
+ } satisfies CSSProperties
132
+ }
133
+
134
+ export function MeterBar(props: {
135
+ label: string
136
+ reading: number
137
+ min: number
138
+ max: number
139
+ format: (value: number) => string
140
+ color: string | undefined
141
+ segments: readonly MeterSegment[] | undefined
142
+ zones: readonly MeterZone[] | undefined
143
+ target: number | undefined
144
+ forecastCaption: string | undefined
145
+ startCaption: ReactNode
146
+ endCaption: ReactNode
147
+ shell: Shell
148
+ }) {
149
+ const { label, reading, min, max, format, zones, shell } = props
150
+ const describedId = useId()
151
+ const model = modelBar(props)
152
+ const start = props.startCaption === undefined ? model.startText : props.startCaption
153
+ const end = props.endCaption === undefined ? model.endText : props.endCaption
154
+ const hasCaptions = start !== null || end !== null || model.forecastText !== null
155
+ const describedBy =
156
+ model.describedByText === null ? {} : ({ 'aria-describedby': describedId } as const)
157
+
158
+ return (
159
+ <div className={cx(classes.root, shell.className)} style={shell.style}>
160
+ <div
161
+ className={classes.trackBox}
162
+ aria-label={label}
163
+ {...ariaValues(reading, min, max, format)}
164
+ {...describedBy}
165
+ >
166
+ <div className={classes.track} data-meter-track>
167
+ {model.markerPct !== undefined &&
168
+ zones?.map((z, i) => (
169
+ <div
170
+ key={i}
171
+ className={classes.zone}
172
+ data-meter-zone={z.label}
173
+ style={zoneStyle(z, i, zones.length, min, model.span)}
174
+ />
175
+ ))}
176
+ {model.parts.map((p, i) => (
177
+ <div
178
+ key={i}
179
+ className={cx(classes.fill, p.forecast && classes.forecastFill)}
180
+ data-meter-fill={p.forecast ? 'forecast' : 'committed'}
181
+ style={fillStyle(p)}
182
+ />
183
+ ))}
184
+ </div>
185
+ <Tick kind="marker" className={classes.marker} pct={model.markerPct} />
186
+ <Tick kind="boundary" className={classes.boundaryTick} pct={model.boundaryPct} />
187
+ <Tick kind="target" className={classes.tick} pct={model.targetPct} />
188
+ </div>
189
+ {model.describedByText !== null && (
190
+ <VisuallyHidden id={describedId}>{model.describedByText}</VisuallyHidden>
191
+ )}
192
+ {hasCaptions && (
193
+ <div className={classes.captions} data-meter-captions>
194
+ {start !== null && <span className={classes.caption}>{start}</span>}
195
+ {model.forecastText !== null && (
196
+ <span className={classes.caption}>{model.forecastText}</span>
197
+ )}
198
+ {end !== null && <span className={classes.captionEnd}>{end}</span>}
199
+ </div>
200
+ )}
201
+ </div>
202
+ )
203
+ }
204
+
205
+ function Tick({
206
+ kind,
207
+ className,
208
+ pct,
209
+ }: {
210
+ kind: 'marker' | 'boundary' | 'target'
211
+ className: string
212
+ pct: number | undefined
213
+ }) {
214
+ if (pct === undefined) return null
215
+ return (
216
+ <div
217
+ className={className}
218
+ data-meter-tick={kind}
219
+ style={{ insetInlineStart: `${pct}%` }}
220
+ aria-hidden
221
+ />
222
+ )
223
+ }