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.
- package/CHANGELOG.md +165 -124
- package/MIGRATING.md +133 -15
- package/agent/agents/basalt-design-reviewer.md +2 -2
- package/agent/skills/basalt-design/SKILL.md +3 -3
- package/configs/oxlint-plugin.js +2 -2
- package/dist/charts/Donut.d.ts.map +1 -1
- package/dist/charts/Donut.js +2 -2
- package/dist/charts/DualPanel.js +2 -2
- package/dist/charts/Heatmap.d.ts.map +1 -1
- package/dist/charts/Heatmap.js +2 -2
- package/dist/charts/MirroredBars.js +2 -2
- package/dist/charts/Sparkline.d.ts +4 -2
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +3 -3
- package/dist/charts/compile.d.ts +8 -3
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +19 -9
- package/dist/charts/cursor.d.ts +14 -1
- package/dist/charts/cursor.d.ts.map +1 -1
- package/dist/charts/cursor.js +29 -3
- package/dist/charts/date-tick-format.d.ts +17 -0
- package/dist/charts/date-tick-format.d.ts.map +1 -0
- package/dist/charts/date-tick-format.js +81 -0
- package/dist/charts/panel.d.ts +2 -1
- package/dist/charts/panel.d.ts.map +1 -1
- package/dist/charts/panel.js +3 -2
- package/dist/charts/types.d.ts +5 -1
- package/dist/charts/types.d.ts.map +1 -1
- package/dist/cli/help.d.ts +1 -0
- package/dist/cli/help.d.ts.map +1 -1
- package/dist/cli/help.js +8 -2
- package/dist/cli/shot/capture.d.ts +28 -1
- package/dist/cli/shot/capture.d.ts.map +1 -1
- package/dist/cli/shot/capture.js +96 -13
- package/dist/cli/shot/index.d.ts +10 -1
- package/dist/cli/shot/index.d.ts.map +1 -1
- package/dist/cli/shot/index.js +40 -5
- package/dist/cli/shot/playwright.d.ts +4 -0
- package/dist/cli/shot/playwright.d.ts.map +1 -1
- package/dist/cli/shot/real-data.d.ts +35 -0
- package/dist/cli/shot/real-data.d.ts.map +1 -0
- package/dist/cli/shot/real-data.js +120 -0
- package/dist/cli/shot/serve.d.ts +2 -2
- package/dist/cli/shot/serve.d.ts.map +1 -1
- package/dist/cli/shot/serve.js +16 -2
- package/dist/dashboard/bar-list.d.ts +29 -0
- package/dist/dashboard/bar-list.d.ts.map +1 -0
- package/dist/dashboard/bar-list.js +52 -0
- package/dist/dashboard/bar-list.module.css +73 -0
- package/dist/dashboard/bar-list.module.css.d.ts +16 -0
- package/dist/dashboard/delta-badge.d.ts +2 -2
- package/dist/dashboard/delta-badge.d.ts.map +1 -1
- package/dist/dashboard/delta-badge.js +12 -14
- package/dist/dashboard/index.d.ts +2 -0
- package/dist/dashboard/index.d.ts.map +1 -1
- package/dist/dashboard/index.js +4 -0
- package/dist/dashboard/meter-model.d.ts +78 -0
- package/dist/dashboard/meter-model.d.ts.map +1 -0
- package/dist/dashboard/meter-model.js +91 -0
- package/dist/dashboard/meter-parts.d.ts +42 -0
- package/dist/dashboard/meter-parts.d.ts.map +1 -0
- package/dist/dashboard/meter-parts.js +163 -0
- package/dist/dashboard/meter.d.ts +112 -0
- package/dist/dashboard/meter.d.ts.map +1 -0
- package/dist/dashboard/meter.js +43 -0
- package/dist/dashboard/meter.module.css +150 -0
- package/dist/dashboard/meter.module.css.d.ts +28 -0
- package/dist/dashboard/query-state.d.ts +35 -0
- package/dist/dashboard/query-state.d.ts.map +1 -1
- package/dist/dashboard/query-state.js +12 -1
- package/dist/dashboard/scale.d.ts +9 -0
- package/dist/dashboard/scale.d.ts.map +1 -0
- package/dist/dashboard/scale.js +10 -0
- package/dist/dashboard/stat-card-parts.d.ts +40 -0
- package/dist/dashboard/stat-card-parts.d.ts.map +1 -0
- package/dist/dashboard/stat-card-parts.js +55 -0
- package/dist/dashboard/stat-card.d.ts +46 -34
- package/dist/dashboard/stat-card.d.ts.map +1 -1
- package/dist/dashboard/stat-card.js +148 -79
- package/dist/dashboard/stat-card.module.css +220 -7
- package/dist/dashboard/stat-card.module.css.d.ts +20 -0
- package/dist/dashboard/widget-header.d.ts.map +1 -1
- package/dist/dashboard/widget-header.js +73 -9
- package/dist/dashboard/widget-header.module.css +25 -17
- package/dist/guard/index.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/theme/chrome-vars.d.ts +82 -0
- package/dist/theme/chrome-vars.d.ts.map +1 -0
- package/dist/theme/chrome-vars.js +36 -0
- package/dist/theme/controls.module.css +20 -1
- package/dist/theme/controls.module.css.d.ts +1 -0
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +30 -33
- package/dist/tokens/derive.d.ts +11 -1
- package/dist/tokens/derive.d.ts.map +1 -1
- package/dist/tokens/derive.js +9 -24
- package/dist/tokens/status-text.d.ts +26 -0
- package/dist/tokens/status-text.d.ts.map +1 -0
- package/dist/tokens/status-text.js +68 -0
- package/llms.txt +36 -4
- package/package.json +1 -1
- package/src/charts/Donut.tsx +2 -2
- package/src/charts/DualPanel.tsx +2 -2
- package/src/charts/Heatmap.tsx +2 -2
- package/src/charts/MirroredBars.tsx +2 -2
- package/src/charts/Sparkline.tsx +7 -5
- package/src/charts/compile.ts +31 -11
- package/src/charts/cursor.ts +48 -3
- package/src/charts/date-tick-format.ts +132 -0
- package/src/charts/panel.tsx +5 -3
- package/src/charts/types.ts +5 -1
- package/src/cli/help.ts +22 -2
- package/src/cli/shot/capture.ts +133 -12
- package/src/cli/shot/index.ts +50 -4
- package/src/cli/shot/playwright.ts +1 -1
- package/src/cli/shot/real-data.ts +165 -0
- package/src/cli/shot/serve.ts +28 -3
- package/src/dashboard/bar-list.module.css +73 -0
- package/src/dashboard/bar-list.module.css.d.ts +16 -0
- package/src/dashboard/bar-list.tsx +112 -0
- package/src/dashboard/delta-badge.tsx +24 -18
- package/src/dashboard/index.ts +2 -0
- package/src/dashboard/meter-model.ts +161 -0
- package/src/dashboard/meter-parts.tsx +223 -0
- package/src/dashboard/meter.module.css +150 -0
- package/src/dashboard/meter.module.css.d.ts +28 -0
- package/src/dashboard/meter.tsx +162 -0
- package/src/dashboard/query-state.tsx +38 -0
- package/src/dashboard/scale.ts +13 -0
- package/src/dashboard/stat-card-parts.tsx +108 -0
- package/src/dashboard/stat-card.module.css +220 -7
- package/src/dashboard/stat-card.module.css.d.ts +20 -0
- package/src/dashboard/stat-card.tsx +282 -174
- package/src/dashboard/widget-header.module.css +25 -17
- package/src/dashboard/widget-header.tsx +103 -7
- package/src/guard/index.ts +2 -2
- package/src/index.ts +4 -0
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +128 -40
- package/src/theme/chrome-vars.ts +111 -0
- package/src/theme/controls.module.css +20 -1
- package/src/theme/controls.module.css.d.ts +1 -0
- package/src/theme/index.ts +39 -101
- package/src/tokens/derive.ts +19 -26
- package/src/tokens/status-text.ts +85 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Meter — one neutral track, ordered fills, optional target tick (docs/waves/design-parity/SPEC.md
|
|
3
|
+
* § 6.3). Flat fills, no hatching. Bar height 6 is the measured reference; the radius is the `fine`
|
|
4
|
+
* step so the track follows the radius knob.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
.root {
|
|
8
|
+
display: flex;
|
|
9
|
+
flex-direction: column;
|
|
10
|
+
gap: var(--vx-space-stack-xs, 0.25rem);
|
|
11
|
+
min-width: 0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.track {
|
|
15
|
+
position: relative;
|
|
16
|
+
display: flex;
|
|
17
|
+
height: 0.375rem;
|
|
18
|
+
overflow: hidden;
|
|
19
|
+
border-radius: var(--vx-radius-fine);
|
|
20
|
+
background: var(--vx-surface-subtle);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/* The tick is drawn OVER the fills and outside the clipped track, so it can stand 2px proud of it. */
|
|
24
|
+
.trackBox {
|
|
25
|
+
position: relative;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.fill {
|
|
29
|
+
flex: none;
|
|
30
|
+
height: 100%;
|
|
31
|
+
background: var(--vx-line);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* The committed/forecast pair: the same neutral at two strengths. Flat, never hatched. */
|
|
35
|
+
.forecastFill {
|
|
36
|
+
background: color-mix(in srgb, var(--vx-line) 45%, transparent);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.zone {
|
|
40
|
+
position: absolute;
|
|
41
|
+
inset-block: 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/* The reading on a zone track: a notch standing proud of the bands, in ink. */
|
|
45
|
+
.marker {
|
|
46
|
+
position: absolute;
|
|
47
|
+
top: -3px;
|
|
48
|
+
bottom: -3px;
|
|
49
|
+
width: 3px;
|
|
50
|
+
margin-inline-start: -1.5px;
|
|
51
|
+
border-radius: 1px;
|
|
52
|
+
background: var(--vx-ink);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.tick {
|
|
56
|
+
position: absolute;
|
|
57
|
+
top: -2px;
|
|
58
|
+
bottom: -2px;
|
|
59
|
+
width: 2px;
|
|
60
|
+
margin-inline-start: -1px;
|
|
61
|
+
border-radius: 1px;
|
|
62
|
+
background: var(--vx-ink);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.boundaryTick {
|
|
66
|
+
composes: tick;
|
|
67
|
+
background: var(--vx-muted);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.captions {
|
|
71
|
+
display: flex;
|
|
72
|
+
align-items: baseline;
|
|
73
|
+
justify-content: space-between;
|
|
74
|
+
gap: var(--vx-space-control-gap, 0.375rem);
|
|
75
|
+
font-family: var(--basalt-font-mono);
|
|
76
|
+
font-size: var(--vx-text-micro);
|
|
77
|
+
color: var(--vx-muted);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.caption {
|
|
81
|
+
white-space: nowrap;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.captionEnd {
|
|
85
|
+
composes: caption;
|
|
86
|
+
text-align: end;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/* Alone in the row, the end caption still sits at the end. */
|
|
90
|
+
.captionEnd:first-child {
|
|
91
|
+
margin-inline-start: auto;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.empty {
|
|
95
|
+
font-size: var(--vx-text-sm);
|
|
96
|
+
color: var(--vx-muted);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* The skeleton holds the track + one caption line: 6 + 4 + a micro line. */
|
|
100
|
+
.skeletonTrack {
|
|
101
|
+
height: 0.375rem;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
.skeletonCaption {
|
|
105
|
+
width: 40%;
|
|
106
|
+
height: 0.75rem;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* ── ring ────────────────────────────────────────────────────────────────────────────────────── */
|
|
110
|
+
|
|
111
|
+
.ringBox {
|
|
112
|
+
position: relative;
|
|
113
|
+
display: inline-flex;
|
|
114
|
+
align-items: center;
|
|
115
|
+
justify-content: center;
|
|
116
|
+
align-self: flex-start;
|
|
117
|
+
width: 5rem;
|
|
118
|
+
height: 5rem;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
.ring {
|
|
122
|
+
position: absolute;
|
|
123
|
+
inset: 0;
|
|
124
|
+
width: 100%;
|
|
125
|
+
height: 100%;
|
|
126
|
+
transform: rotate(-90deg);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
.ringTrack {
|
|
130
|
+
fill: none;
|
|
131
|
+
stroke: var(--vx-surface-subtle);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.ringArc {
|
|
135
|
+
fill: none;
|
|
136
|
+
stroke: var(--vx-line);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.ringValue {
|
|
140
|
+
position: relative;
|
|
141
|
+
font-family: var(--basalt-font-mono);
|
|
142
|
+
font-size: var(--vx-text-md);
|
|
143
|
+
font-weight: 500;
|
|
144
|
+
color: var(--vx-ink);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.skeletonRing {
|
|
148
|
+
width: 5rem;
|
|
149
|
+
height: 5rem;
|
|
150
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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 track: string
|
|
8
|
+
readonly trackBox: string
|
|
9
|
+
readonly fill: string
|
|
10
|
+
readonly forecastFill: string
|
|
11
|
+
readonly zone: string
|
|
12
|
+
readonly marker: string
|
|
13
|
+
readonly tick: string
|
|
14
|
+
readonly boundaryTick: string
|
|
15
|
+
readonly captions: string
|
|
16
|
+
readonly caption: string
|
|
17
|
+
readonly captionEnd: string
|
|
18
|
+
readonly empty: string
|
|
19
|
+
readonly skeletonTrack: string
|
|
20
|
+
readonly skeletonCaption: string
|
|
21
|
+
readonly ringBox: string
|
|
22
|
+
readonly ring: string
|
|
23
|
+
readonly ringTrack: string
|
|
24
|
+
readonly ringArc: string
|
|
25
|
+
readonly ringValue: string
|
|
26
|
+
readonly skeletonRing: string
|
|
27
|
+
}
|
|
28
|
+
export default classes
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Meter — one bounded quantity on a track: progress to a target, a committed/forecast split, a
|
|
3
|
+
* part-of-a-whole strip (`segments`), or a single headline ratio as a `ring`. Choose it for "how far
|
|
4
|
+
* along is this against a limit"; choose `BarList` for a ranking and a chart for a trend. An HTML/CSS
|
|
5
|
+
* primitive, not a chart kind (no scale, axis or tooltip).
|
|
6
|
+
*
|
|
7
|
+
* Colour law: the fill is neutral (`VX.line`). `color` is for a VERDICT only (over budget, below
|
|
8
|
+
* target) and is a status colour the consumer passes; categorical `segments` take the consumer's
|
|
9
|
+
* series colours. Committed vs forecast is the same neutral at two strengths — never colour alone: a
|
|
10
|
+
* tick marks the boundary and a caption names the forecast. A value past `max` clamps the fill at the
|
|
11
|
+
* track end; the caption still states the real value.
|
|
12
|
+
*
|
|
13
|
+
* `zones` (bar only) draws the track as contiguous bands (neutral at stepped strengths unless a zone
|
|
14
|
+
* passes a status colour) and the reading becomes a MARKER on the track, not a fill; the caption names
|
|
15
|
+
* the zone that holds the value (`from` inclusive). Use it for a reading judged against ranges.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* import { Meter } from 'basalt-ui'
|
|
19
|
+
*
|
|
20
|
+
* // Spend against a budget: past the target the end caption states the overage.
|
|
21
|
+
* <Meter label="Monthly spend" value={512} max={600} target={450} color="var(--vx-status-bad)" format={usd} />
|
|
22
|
+
* // Training load as a zone band with a marker: caption reads "Optimal · 612".
|
|
23
|
+
* <Meter
|
|
24
|
+
* label="Training load"
|
|
25
|
+
* max={1000}
|
|
26
|
+
* value={612}
|
|
27
|
+
* zones={[
|
|
28
|
+
* { from: 0, to: 300, label: 'Low' },
|
|
29
|
+
* { from: 300, to: 750, label: 'Optimal' },
|
|
30
|
+
* { from: 750, to: 1000, label: 'High' },
|
|
31
|
+
* ]}
|
|
32
|
+
* />
|
|
33
|
+
* // A readiness score on its 0–100 scale (its contributors: <BarList max={100} … secondary="Good" />).
|
|
34
|
+
* <Meter label="Recovery" value={72} max={100} />
|
|
35
|
+
* <Meter
|
|
36
|
+
* label="Course progress"
|
|
37
|
+
* max={120}
|
|
38
|
+
* segments={[{ value: 70, label: 'Done' }, { value: 25, label: 'Planned', forecast: true }]}
|
|
39
|
+
* />
|
|
40
|
+
* <Meter shape="ring" label="Cache hit rate" value={87} max={100} format={(v) => `${v}%`} />
|
|
41
|
+
*/
|
|
42
|
+
import type { ReactNode } from 'react'
|
|
43
|
+
import type { BasaltProps } from '../common/props'
|
|
44
|
+
import { isReading, normalizeSegments, segmentReading, spanOf } from './meter-model'
|
|
45
|
+
import type { MeterSegment, MeterZone } from './meter-model'
|
|
46
|
+
import { MeterBar, MeterEmpty, MeterLoading, MeterRing } from './meter-parts'
|
|
47
|
+
import { asText } from './scale'
|
|
48
|
+
|
|
49
|
+
type Common = BasaltProps & {
|
|
50
|
+
/** Accessible name (`aria-label`). Required: a meter without one is announced as a bare number. */
|
|
51
|
+
label: string
|
|
52
|
+
/** Track start. Default 0. */
|
|
53
|
+
min?: number
|
|
54
|
+
/** Track end. Default 100. */
|
|
55
|
+
max?: number
|
|
56
|
+
/** Formats values in captions and the accessible text. Default: the number as written. */
|
|
57
|
+
format?: (value: number) => string
|
|
58
|
+
/** Renders a skeleton with the meter's footprint. */
|
|
59
|
+
loading?: boolean
|
|
60
|
+
/** Muted line shown when there is no reading. The track stays so the footprint holds. */
|
|
61
|
+
emptyText?: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type BarCommon = Common & {
|
|
65
|
+
shape?: 'bar'
|
|
66
|
+
/** Target position; draws a tick and states it in the end caption (with the overage past it). */
|
|
67
|
+
target?: number
|
|
68
|
+
/** Start caption (default: the formatted reading, or `Zone · reading`). `null` drops it. */
|
|
69
|
+
startCaption?: ReactNode
|
|
70
|
+
/** End caption (default: `Target <target>`). `null` drops it. */
|
|
71
|
+
endCaption?: ReactNode
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* One of four forms; mixing them is a compile error.
|
|
76
|
+
* - Bar `value` (+ `target`, `color`): one reading on a neutral track.
|
|
77
|
+
* - Bar `segments`: ordered parts that sum to the reading (committed/forecast, part of a whole).
|
|
78
|
+
* - Bar `value` + `zones`: the reading is a marker against contiguous bands.
|
|
79
|
+
* - Ring (`shape: 'ring'`) `value` (+ `color`): one headline ratio; no target, captions, segments or zones.
|
|
80
|
+
*/
|
|
81
|
+
export type MeterProps =
|
|
82
|
+
| (BarCommon & {
|
|
83
|
+
/** The reading. `null`/`undefined` (or non-finite) renders the empty state. */
|
|
84
|
+
value?: number | null | undefined
|
|
85
|
+
/** A status colour, for a verdict only. Omit for the neutral fill. */
|
|
86
|
+
color?: string
|
|
87
|
+
segments?: never
|
|
88
|
+
zones?: never
|
|
89
|
+
})
|
|
90
|
+
| (BarCommon & {
|
|
91
|
+
/** Ordered parts, accumulated from `min`; their sum is the reading. `forecast` parts render lighter. */
|
|
92
|
+
segments: readonly MeterSegment[]
|
|
93
|
+
/** Names the forecast beside the boundary tick (default: `Forecast: <labels>`). */
|
|
94
|
+
forecastCaption?: string
|
|
95
|
+
value?: never
|
|
96
|
+
color?: never
|
|
97
|
+
zones?: never
|
|
98
|
+
})
|
|
99
|
+
| (BarCommon & {
|
|
100
|
+
value?: number | null | undefined
|
|
101
|
+
/**
|
|
102
|
+
* Contiguous bands drawn as the track, positioned from each `from`. The reading becomes a marker
|
|
103
|
+
* and the caption names the band that holds it.
|
|
104
|
+
*/
|
|
105
|
+
zones: readonly MeterZone[]
|
|
106
|
+
segments?: never
|
|
107
|
+
color?: never
|
|
108
|
+
})
|
|
109
|
+
| (Common & {
|
|
110
|
+
shape: 'ring'
|
|
111
|
+
value?: number | null | undefined
|
|
112
|
+
/** A status colour, for a verdict only. */
|
|
113
|
+
color?: string
|
|
114
|
+
segments?: never
|
|
115
|
+
zones?: never
|
|
116
|
+
target?: never
|
|
117
|
+
startCaption?: never
|
|
118
|
+
endCaption?: never
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
let warnedTrack = false
|
|
122
|
+
/** One dev warning per page load for a track that ends at or before its start. */
|
|
123
|
+
function warnInvalidTrack(min: number, max: number) {
|
|
124
|
+
if (warnedTrack || spanOf(min, max).valid || process.env.NODE_ENV === 'production') return
|
|
125
|
+
warnedTrack = true
|
|
126
|
+
console.warn(
|
|
127
|
+
`[basalt] Meter: max (${max}) must be greater than min (${min}); drawing an empty track`,
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function Meter(props: MeterProps) {
|
|
132
|
+
const { label, min = 0, max = 100, format = asText, className, style } = props
|
|
133
|
+
const shell = { className, style }
|
|
134
|
+
const ring = props.shape === 'ring'
|
|
135
|
+
warnInvalidTrack(min, max)
|
|
136
|
+
|
|
137
|
+
if (props.loading === true) return <MeterLoading label={label} ring={ring} shell={shell} />
|
|
138
|
+
|
|
139
|
+
const segments = props.segments === undefined ? undefined : normalizeSegments(props.segments)
|
|
140
|
+
const reading = segments === undefined ? props.value : segmentReading(segments, min)
|
|
141
|
+
if (!isReading(reading) || segments?.length === 0) {
|
|
142
|
+
return (
|
|
143
|
+
<MeterEmpty label={label} text={props.emptyText ?? 'No data'} ring={ring} shell={shell} />
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (props.shape === 'ring') {
|
|
148
|
+
return <MeterRing {...{ label, reading, min, max, format, shell }} color={props.color} />
|
|
149
|
+
}
|
|
150
|
+
return (
|
|
151
|
+
<MeterBar
|
|
152
|
+
{...{ label, reading, min, max, format, shell }}
|
|
153
|
+
color={props.color}
|
|
154
|
+
segments={segments}
|
|
155
|
+
zones={props.zones}
|
|
156
|
+
target={isReading(props.target) ? props.target : undefined}
|
|
157
|
+
forecastCaption={props.segments === undefined ? undefined : props.forecastCaption}
|
|
158
|
+
startCaption={props.startCaption}
|
|
159
|
+
endCaption={props.endCaption}
|
|
160
|
+
/>
|
|
161
|
+
)
|
|
162
|
+
}
|
|
@@ -173,6 +173,44 @@ function defaultIsEmpty(data: unknown): boolean {
|
|
|
173
173
|
return false
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
/**
|
|
177
|
+
* The body a card-sized consumer should show for `query`, resolved by ONE precedence:
|
|
178
|
+
* error > pending > empty > ready.
|
|
179
|
+
*
|
|
180
|
+
* - `error`: the fetch failed and there is nothing to show — no data, or data that is empty (an
|
|
181
|
+
* errored refetch over a cached `[]` is a failure, never "no data");
|
|
182
|
+
* - `pending`: no data, not failed, and a fetch is in flight or paused;
|
|
183
|
+
* - `empty`: no data and nothing in flight (an `enabled: false` query), or data that is empty by
|
|
184
|
+
* the same rule as {@link QueryState} (`null`, `[]`, `{ data: [] }`);
|
|
185
|
+
* - `ready`: data worth showing; `refreshFailed` is true when a background refetch failed over it
|
|
186
|
+
* (the "showing cached data" banner case).
|
|
187
|
+
*
|
|
188
|
+
* `QueryState` renders a whole region and keeps its own branch order; this is for a consumer that
|
|
189
|
+
* swaps one region of a larger surface (`StatCard`'s figure) and needs the same words for the same
|
|
190
|
+
* states. Switch over `kind` and end with `default: return branch satisfies never`.
|
|
191
|
+
*/
|
|
192
|
+
export type QueryBranch<TData> =
|
|
193
|
+
| { kind: 'error'; query: QueryStateLike<TData>; error: unknown }
|
|
194
|
+
| { kind: 'pending'; query: QueryStateLike<TData> }
|
|
195
|
+
| { kind: 'empty'; query: QueryStateLike<TData> }
|
|
196
|
+
| { kind: 'ready'; query: QueryStateLike<TData>; data: TData; refreshFailed: boolean }
|
|
197
|
+
|
|
198
|
+
export function resolveQueryBranch<TData>(
|
|
199
|
+
query: QueryStateLike<TData>,
|
|
200
|
+
/** Names the container in the throw on a malformed `query` (`'StatCard'`). */
|
|
201
|
+
caller: string,
|
|
202
|
+
isEmpty: (data: TData) => boolean = defaultIsEmpty,
|
|
203
|
+
): QueryBranch<TData> {
|
|
204
|
+
assertQueryStateLike(caller, query)
|
|
205
|
+
const { data, isError, error, fetchStatus } = query
|
|
206
|
+
if (data === undefined) {
|
|
207
|
+
if (isError) return { kind: 'error', query, error }
|
|
208
|
+
return fetchStatus === 'idle' ? { kind: 'empty', query } : { kind: 'pending', query }
|
|
209
|
+
}
|
|
210
|
+
if (isEmpty(data)) return isError ? { kind: 'error', query, error } : { kind: 'empty', query }
|
|
211
|
+
return { kind: 'ready', query, data, refreshFailed: isError }
|
|
212
|
+
}
|
|
213
|
+
|
|
176
214
|
/**
|
|
177
215
|
* ONE slot, and the single-member union is the statement (`common/props.ts` lists this among the
|
|
178
216
|
* composites): `root` is whichever branch is live — the error alert's stack, the spinner's centre,
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Shared by `Meter` and `BarList`: the one place a value becomes a track percentage. Internal. */
|
|
2
|
+
|
|
3
|
+
/** Clamps to 0–1; a non-finite fraction is 0. */
|
|
4
|
+
export const clamp01 = (n: number): number => (Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : 0)
|
|
5
|
+
|
|
6
|
+
/** A 0–1 fraction as a percent, rounded so float noise never reaches the DOM. */
|
|
7
|
+
export const toPct = (n: number): number => Math.round(clamp01(n) * 10000) / 100
|
|
8
|
+
|
|
9
|
+
/** A value on a track that ends at `scale`; a non-finite value or an empty scale draws nothing. */
|
|
10
|
+
export const barPercent = (value: number, scale: number): number =>
|
|
11
|
+
Number.isFinite(value) && Number.isFinite(scale) && scale > 0 ? toPct(value / scale) : 0
|
|
12
|
+
|
|
13
|
+
export const asText = (n: number): string => String(n)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pieces `StatCard` lays out around its value row: the caption row, the foot, and the
|
|
3
|
+
* skeletons that hold their footprint while a reading is pending. Split out so `stat-card.tsx`
|
|
4
|
+
* orchestrates (which branch, which placement) and these draw. Internal: not exported from any barrel.
|
|
5
|
+
*/
|
|
6
|
+
import { Skeleton } from '@mantine/core'
|
|
7
|
+
import type { CSSProperties, ReactNode } from 'react'
|
|
8
|
+
import { IconSlot } from '../theme/icon-slot'
|
|
9
|
+
import { CtlSlot } from '../theme'
|
|
10
|
+
import { InfoGlyph } from './widget-header'
|
|
11
|
+
import classes from './stat-card.module.css'
|
|
12
|
+
|
|
13
|
+
export type CaptionRowProps = {
|
|
14
|
+
title: string
|
|
15
|
+
icon?: ReactNode
|
|
16
|
+
info?: string
|
|
17
|
+
qualifier?: string
|
|
18
|
+
actions?: ReactNode
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Title, `info` glyph and `qualifier` in one wrapping group, `actions` pinned at the end. When
|
|
23
|
+
* caption + info + qualifier do not fit, the qualifier wraps UNDER the caption instead of truncating
|
|
24
|
+
* either: the container's width decides, never the viewport's.
|
|
25
|
+
*/
|
|
26
|
+
export function CaptionRow({ title, icon, info, qualifier, actions }: CaptionRowProps) {
|
|
27
|
+
return (
|
|
28
|
+
<div className={classes.captionRow}>
|
|
29
|
+
<div className={classes.captionMain}>
|
|
30
|
+
<div className={classes.lead}>
|
|
31
|
+
<h3 className={classes.caption}>
|
|
32
|
+
{icon !== undefined && <IconSlot className={classes.icon}>{icon}</IconSlot>}
|
|
33
|
+
<span className={classes.captionText}>{title}</span>
|
|
34
|
+
</h3>
|
|
35
|
+
{/* OUTSIDE the heading on purpose: `info` would otherwise become part of its name. */}
|
|
36
|
+
{info !== undefined && <InfoGlyph text={info} />}
|
|
37
|
+
</div>
|
|
38
|
+
{qualifier !== undefined && <span className={classes.qualifier}>{qualifier}</span>}
|
|
39
|
+
</div>
|
|
40
|
+
{/* `tier="widget"`: the caption row is 24px (`--vx-space-control-height-widget`), so a card
|
|
41
|
+
with a kebab is the same height as one without (`theme/ctl-theme.tsx`). */}
|
|
42
|
+
{actions !== undefined && (
|
|
43
|
+
<span className={classes.actions} data-basalt-hit-scope>
|
|
44
|
+
<CtlSlot tier="widget">{actions}</CtlSlot>
|
|
45
|
+
</span>
|
|
46
|
+
)}
|
|
47
|
+
</div>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type FootProps = {
|
|
52
|
+
/** Reserve the foot's height (a visual AND a footer line) even when a branch shows neither. */
|
|
53
|
+
reserve: boolean
|
|
54
|
+
/** The visual band is part of the reservation only for the `bleed` placement; `right` keeps its
|
|
55
|
+
* slot beside the breakdown, so a footer there reserves one line, not a band. */
|
|
56
|
+
visualBand: boolean
|
|
57
|
+
visualNode: ReactNode
|
|
58
|
+
footerNode: ReactNode
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The visual and its footer line, pinned to the card's bottom. It reserves visual + gap + footer
|
|
63
|
+
* whenever `reserve` is set, so a card with a sparkline and no footer, one with both, and one in a
|
|
64
|
+
* pending or failed branch are the same height (SPEC § 6.1 footprint).
|
|
65
|
+
*/
|
|
66
|
+
export function Foot({ reserve, visualBand, visualNode, footerNode }: FootProps) {
|
|
67
|
+
if (!reserve) return null
|
|
68
|
+
return (
|
|
69
|
+
<div className={classes.foot} data-band={visualBand}>
|
|
70
|
+
{visualNode}
|
|
71
|
+
{footerNode}
|
|
72
|
+
</div>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The `'right'` slot's size, handed to CSS as custom properties (an inline `flex-basis` would also
|
|
77
|
+
* apply in a narrow card, where the row is a column and the basis becomes the HEIGHT). */
|
|
78
|
+
export function rightSlotStyle(width: number, height: number): CSSProperties {
|
|
79
|
+
return {
|
|
80
|
+
'--basalt-stat-sparkline-w': `${width}px`,
|
|
81
|
+
'--basalt-stat-sparkline-h': `${height}px`,
|
|
82
|
+
} as CSSProperties
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function ValueSkeleton() {
|
|
86
|
+
return <Skeleton className={classes.skeletonValue} radius="sm" />
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function VisualSkeleton() {
|
|
90
|
+
return <Skeleton className={classes.skeletonVisual} />
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function FooterSkeleton() {
|
|
94
|
+
return <Skeleton className={classes.skeletonFooter} radius="sm" />
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** One placeholder row per `breakdown` row, each at the real row's height. */
|
|
98
|
+
export function BreakdownSkeleton({ rows }: { rows: number }) {
|
|
99
|
+
return (
|
|
100
|
+
<div className={classes.breakdown} aria-hidden="true">
|
|
101
|
+
{Array.from({ length: rows }, (_, i) => (
|
|
102
|
+
<div key={i} className={classes.skeletonRowBox}>
|
|
103
|
+
<Skeleton className={classes.skeletonRow} radius="sm" />
|
|
104
|
+
</div>
|
|
105
|
+
))}
|
|
106
|
+
</div>
|
|
107
|
+
)
|
|
108
|
+
}
|