basalt-ui 2.17.0 → 2.19.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 +58 -0
- package/MIGRATING.md +108 -12
- package/agent/agents/basalt-design-reviewer.md +2 -2
- package/agent/skills/basalt-design/SKILL.md +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 +12 -1
- package/dist/cli/shot/capture.d.ts.map +1 -1
- package/dist/cli/shot/capture.js +72 -10
- 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/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/commands/notifications/center.d.ts.map +1 -1
- package/dist/commands/notifications/center.js +18 -26
- package/dist/content/callout.d.ts +1 -1
- package/dist/content/callout.d.ts.map +1 -1
- package/dist/content/callout.js +10 -1
- package/dist/content/callout.module.css +15 -3
- package/dist/content/callout.module.css.d.ts +1 -0
- 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 +65 -0
- package/dist/dashboard/bar-list.module.css +91 -0
- package/dist/dashboard/bar-list.module.css.d.ts +17 -0
- 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 +126 -0
- package/dist/dashboard/meter-model.d.ts.map +1 -0
- package/dist/dashboard/meter-model.js +135 -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 +297 -0
- package/dist/dashboard/meter.d.ts +123 -0
- package/dist/dashboard/meter.d.ts.map +1 -0
- package/dist/dashboard/meter.js +43 -0
- package/dist/dashboard/meter.module.css +268 -0
- package/dist/dashboard/meter.module.css.d.ts +39 -0
- 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 +3 -1
- package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
- package/dist/dashboard/stat-card-parts.js +18 -3
- package/dist/dashboard/stat-card.d.ts +6 -5
- package/dist/dashboard/stat-card.d.ts.map +1 -1
- package/dist/dashboard/stat-card.js +48 -77
- package/dist/dashboard/stat-card.module.css +27 -7
- package/dist/dashboard/widget-header.d.ts.map +1 -1
- package/dist/dashboard/widget-header.js +2 -1
- package/dist/dashboard/widget-header.module.css +28 -1
- 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 +29 -17
- package/dist/theme/chrome-vars.d.ts.map +1 -1
- package/dist/theme/chrome-vars.js +18 -8
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +9 -5
- package/dist/theme/status-dot.d.ts +6 -0
- package/dist/theme/status-dot.d.ts.map +1 -0
- package/dist/theme/status-dot.js +9 -0
- package/dist/theme/status-dot.module.css +9 -0
- package/dist/theme/status-dot.module.css.d.ts +8 -0
- package/dist/theme/status-verdict.d.ts +9 -0
- package/dist/theme/status-verdict.d.ts.map +1 -0
- package/dist/theme/status-verdict.js +13 -0
- package/dist/theme/status-verdict.module.css +23 -0
- package/dist/theme/status-verdict.module.css.d.ts +9 -0
- 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 +32 -3
- package/package.json +1 -1
- package/src/cli/help.ts +22 -2
- package/src/cli/shot/capture.ts +98 -9
- package/src/cli/shot/index.ts +50 -4
- package/src/cli/shot/real-data.ts +165 -0
- package/src/cli/shot/serve.ts +28 -3
- package/src/commands/notifications/center.tsx +29 -32
- package/src/content/callout.module.css +15 -3
- package/src/content/callout.module.css.d.ts +1 -0
- package/src/content/callout.tsx +15 -2
- package/src/dashboard/bar-list.module.css +91 -0
- package/src/dashboard/bar-list.module.css.d.ts +17 -0
- package/src/dashboard/bar-list.tsx +125 -0
- package/src/dashboard/index.ts +2 -0
- package/src/dashboard/meter-model.ts +254 -0
- package/src/dashboard/meter-parts.tsx +407 -0
- package/src/dashboard/meter.module.css +268 -0
- package/src/dashboard/meter.module.css.d.ts +39 -0
- package/src/dashboard/meter.tsx +173 -0
- package/src/dashboard/scale.ts +13 -0
- package/src/dashboard/stat-card-parts.tsx +22 -2
- package/src/dashboard/stat-card.module.css +27 -7
- package/src/dashboard/stat-card.tsx +18 -52
- package/src/dashboard/widget-header.module.css +28 -1
- package/src/dashboard/widget-header.tsx +4 -1
- package/src/index.ts +4 -0
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +88 -16
- package/src/theme/chrome-vars.ts +48 -23
- package/src/theme/index.ts +10 -6
- package/src/theme/status-dot.module.css +9 -0
- package/src/theme/status-dot.module.css.d.ts +8 -0
- package/src/theme/status-dot.tsx +19 -0
- package/src/theme/status-verdict.module.css +23 -0
- package/src/theme/status-verdict.module.css.d.ts +9 -0
- package/src/theme/status-verdict.tsx +27 -0
- package/src/tokens/derive.ts +19 -31
- package/src/tokens/status-text.ts +85 -0
|
@@ -30,28 +30,20 @@ import {
|
|
|
30
30
|
import type { BasaltProps } from '../../common/props'
|
|
31
31
|
import { cx } from '../../common/props'
|
|
32
32
|
import { relativeTime } from '../../format'
|
|
33
|
+
import { StatusVerdict } from '../../theme/status-verdict'
|
|
33
34
|
import { VX } from '../../tokens'
|
|
34
35
|
import { resolveAction } from './define-notifications'
|
|
35
36
|
import { useNotificationHistory } from './store'
|
|
36
37
|
import type { NotificationHistoryItem, NotificationIntent } from './store'
|
|
37
38
|
|
|
38
|
-
// ── Intent →
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// ── Intent → status token (docs/DESIGN-SPEC.md §5: "severity accents via status tokens") — the
|
|
48
|
-
// exact spec success/warning/danger hex, used for the item's left-border accent. `info` has no
|
|
49
|
-
// status tone, so it reads the neutral accent instead.
|
|
50
|
-
const INTENT_STATUS: Record<NotificationIntent, string> = {
|
|
51
|
-
success: VX.status.good,
|
|
52
|
-
error: VX.status.bad,
|
|
53
|
-
warning: VX.status.warn,
|
|
54
|
-
info: VX.accent,
|
|
39
|
+
// ── Intent → the action Button's Mantine tint, the status dot colour and the visible word beside
|
|
40
|
+
// it (docs/DESIGN-SPEC.md §5: severity via status tokens). `info` has no status tone, so its dot is
|
|
41
|
+
// the neutral muted ink, not the accent.
|
|
42
|
+
const INTENT_META: Record<NotificationIntent, { color: string; dot: string; word: string }> = {
|
|
43
|
+
success: { color: 'green', dot: VX.status.good, word: 'Success' },
|
|
44
|
+
error: { color: 'red', dot: VX.status.bad, word: 'Error' },
|
|
45
|
+
warning: { color: 'yellow', dot: VX.status.warn, word: 'Warning' },
|
|
46
|
+
info: { color: 'blue', dot: VX.muted, word: 'Info' },
|
|
55
47
|
}
|
|
56
48
|
|
|
57
49
|
type CenterView = 'inbox' | 'all'
|
|
@@ -78,8 +70,7 @@ function NotificationItem({
|
|
|
78
70
|
}) {
|
|
79
71
|
const isUnread = item.readAt === undefined
|
|
80
72
|
const isDismissed = item.dismissedAt !== undefined
|
|
81
|
-
const color =
|
|
82
|
-
const statusAccent = INTENT_STATUS[item.intent]
|
|
73
|
+
const { color, dot, word } = INTENT_META[item.intent]
|
|
83
74
|
const resolved = item.action !== undefined ? resolveAction(item.action) : undefined
|
|
84
75
|
|
|
85
76
|
function handleAction(e: MouseEvent): void {
|
|
@@ -101,7 +92,6 @@ function NotificationItem({
|
|
|
101
92
|
onClick={isUnread ? () => onMarkRead(item.id) : undefined}
|
|
102
93
|
style={{
|
|
103
94
|
cursor: isUnread ? 'pointer' : 'default',
|
|
104
|
-
borderInlineStart: `3px solid ${statusAccent}`,
|
|
105
95
|
borderRadius: 'var(--mantine-radius-sm)',
|
|
106
96
|
background: isUnread ? 'var(--mantine-color-default-hover)' : 'transparent',
|
|
107
97
|
opacity: isDismissed ? 0.55 : 1,
|
|
@@ -109,19 +99,26 @@ function NotificationItem({
|
|
|
109
99
|
>
|
|
110
100
|
<Group gap="xs" align="flex-start" wrap="nowrap">
|
|
111
101
|
<Stack gap={2} style={{ minWidth: 0, flex: 1 }}>
|
|
112
|
-
{item.title !== undefined
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
102
|
+
{item.title !== undefined ? (
|
|
103
|
+
<>
|
|
104
|
+
<Group gap="xs" align="center" wrap="nowrap">
|
|
105
|
+
<StatusVerdict color={dot} word={word} />
|
|
106
|
+
<Text size="sm" fw={isUnread ? 600 : 500} lh={1.2} miw={0} truncate="end">
|
|
107
|
+
{item.title}
|
|
108
|
+
</Text>
|
|
109
|
+
</Group>
|
|
110
|
+
<Text size="sm" lh={1.3} c="dimmed" style={{ wordBreak: 'break-word' }}>
|
|
111
|
+
{item.message}
|
|
112
|
+
</Text>
|
|
113
|
+
</>
|
|
114
|
+
) : (
|
|
115
|
+
<Group gap="xs" align="flex-start" wrap="nowrap">
|
|
116
|
+
<StatusVerdict color={dot} word={word} />
|
|
117
|
+
<Text size="sm" lh={1.3} miw={0} style={{ wordBreak: 'break-word' }}>
|
|
118
|
+
{item.message}
|
|
119
|
+
</Text>
|
|
120
|
+
</Group>
|
|
116
121
|
)}
|
|
117
|
-
<Text
|
|
118
|
-
size="sm"
|
|
119
|
-
lh={1.3}
|
|
120
|
-
{...(item.title !== undefined && { c: 'dimmed' })}
|
|
121
|
-
style={{ wordBreak: 'break-word' }}
|
|
122
|
-
>
|
|
123
|
-
{item.message}
|
|
124
|
-
</Text>
|
|
125
122
|
<Group gap="xs" align="center" mt={2} wrap="nowrap">
|
|
126
123
|
<Text ff="monospace" c="var(--vx-faint)" style={{ fontSize: VX.text.micro }}>
|
|
127
124
|
{relativeTime(item.createdAt, { style: 'short' })}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Callout — a soft tinted panel
|
|
3
|
-
* `--callout-ink` is set per kind below and consumed by both the
|
|
2
|
+
* Callout — a soft tinted panel, keyed to one of the four semantic tokens.
|
|
3
|
+
* `--callout-ink` is set per kind below and consumed by both the bg tint and the title/icon
|
|
4
4
|
* color — one custom property, no per-kind duplication.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
.root {
|
|
8
8
|
border-radius: var(--vx-radius-card);
|
|
9
9
|
padding: var(--vx-space-callout-inset-y) var(--vx-space-callout-inset-x);
|
|
10
|
-
border-left: 2px solid var(--callout-ink);
|
|
11
10
|
background: color-mix(in srgb, var(--callout-ink) 8%, transparent);
|
|
12
11
|
}
|
|
13
12
|
|
|
@@ -56,6 +55,7 @@
|
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
.body {
|
|
58
|
+
position: relative;
|
|
59
59
|
font-size: var(--vx-text-md);
|
|
60
60
|
color: var(--vx-ink2);
|
|
61
61
|
}
|
|
@@ -75,3 +75,15 @@
|
|
|
75
75
|
.body p {
|
|
76
76
|
margin: 0 0 var(--vx-space-stack-sm);
|
|
77
77
|
}
|
|
78
|
+
|
|
79
|
+
/* The kind as a word for assistive tech; visually hidden. */
|
|
80
|
+
.kindWord {
|
|
81
|
+
position: absolute;
|
|
82
|
+
width: 1px;
|
|
83
|
+
height: 1px;
|
|
84
|
+
margin: -1px;
|
|
85
|
+
padding: 0;
|
|
86
|
+
overflow: hidden;
|
|
87
|
+
clip-path: inset(50%);
|
|
88
|
+
white-space: nowrap;
|
|
89
|
+
}
|
package/src/content/callout.tsx
CHANGED
|
@@ -21,7 +21,7 @@ import classes from './callout.module.css'
|
|
|
21
21
|
export type CalloutKind = 'info' | 'good' | 'warn' | 'bad'
|
|
22
22
|
|
|
23
23
|
export type CalloutProps = BasaltProps & {
|
|
24
|
-
/** Semantic kind — drives the
|
|
24
|
+
/** Semantic kind — drives the tint color and the default icon. Default `'info'`. */
|
|
25
25
|
readonly kind?: CalloutKind
|
|
26
26
|
readonly title?: string
|
|
27
27
|
/** Overrides the default per-kind icon. Only rendered alongside a `title`. */
|
|
@@ -77,6 +77,14 @@ function BadGlyph() {
|
|
|
77
77
|
)
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
// The kind as a word for assistive tech, so a title-less callout is never tint-only.
|
|
81
|
+
const KIND_WORD: Record<CalloutKind, string> = {
|
|
82
|
+
info: 'Info',
|
|
83
|
+
good: 'Success',
|
|
84
|
+
warn: 'Warning',
|
|
85
|
+
bad: 'Error',
|
|
86
|
+
}
|
|
87
|
+
|
|
80
88
|
const DEFAULT_ICON: Record<CalloutKind, ReactNode> = {
|
|
81
89
|
info: <InfoGlyph />,
|
|
82
90
|
good: <GoodGlyph />,
|
|
@@ -96,7 +104,12 @@ export function Callout({ kind = 'info', title, icon, children, className, style
|
|
|
96
104
|
<span className={classes.title}>{title}</span>
|
|
97
105
|
</div>
|
|
98
106
|
)}
|
|
99
|
-
<div className={classes.body}>
|
|
107
|
+
<div className={classes.body}>
|
|
108
|
+
<span className={classes.kindWord} data-callout-kind-word="">
|
|
109
|
+
{KIND_WORD[kind]}
|
|
110
|
+
</span>
|
|
111
|
+
{children}
|
|
112
|
+
</div>
|
|
100
113
|
</div>
|
|
101
114
|
)
|
|
102
115
|
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* BarList — ranked rows on one shared scale (docs/waves/design-parity/SPEC.md § 6.4). The bar track
|
|
3
|
+
* is the LABEL column only; the value (and secondary) columns sit outside it, so a full-scale bar ends
|
|
4
|
+
* before the value. The columns are shared by every row (subgrid), which is what keeps one scale. The
|
|
5
|
+
* row never wraps: the label truncates, the value is nowrap.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
.root {
|
|
9
|
+
display: grid;
|
|
10
|
+
grid-template-columns: minmax(0, 1fr) auto;
|
|
11
|
+
column-gap: var(--vx-space-control-gap, 0.375rem);
|
|
12
|
+
row-gap: var(--vx-space-stack-xs, 0.25rem);
|
|
13
|
+
margin: 0;
|
|
14
|
+
padding: 0;
|
|
15
|
+
list-style: none;
|
|
16
|
+
min-width: 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.root[data-secondary] {
|
|
20
|
+
grid-template-columns: minmax(0, 1fr) auto auto;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.row {
|
|
24
|
+
display: grid;
|
|
25
|
+
grid-column: 1 / -1;
|
|
26
|
+
grid-template-columns: subgrid;
|
|
27
|
+
align-items: center;
|
|
28
|
+
min-width: 0;
|
|
29
|
+
min-height: calc(var(--vx-space-control-height-widget, 1.5rem) * var(--mantine-scale, 1));
|
|
30
|
+
font-size: var(--vx-text-sm);
|
|
31
|
+
color: var(--vx-ink);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* The bar's scale: `.bar` is a percentage of THIS box, never of the row. */
|
|
35
|
+
.track {
|
|
36
|
+
position: relative;
|
|
37
|
+
display: flex;
|
|
38
|
+
align-items: center;
|
|
39
|
+
align-self: stretch;
|
|
40
|
+
min-width: 0;
|
|
41
|
+
padding-inline: var(--vx-space-stack-sm);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.bar {
|
|
45
|
+
position: absolute;
|
|
46
|
+
inset-block: 0;
|
|
47
|
+
inset-inline-start: 0;
|
|
48
|
+
border-radius: var(--vx-radius-fine);
|
|
49
|
+
background: color-mix(in srgb, var(--vx-line) 30%, transparent);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.leader {
|
|
53
|
+
background: color-mix(in srgb, var(--vx-accent) 28%, transparent);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.label {
|
|
57
|
+
position: relative;
|
|
58
|
+
min-width: 0;
|
|
59
|
+
overflow: hidden;
|
|
60
|
+
text-overflow: ellipsis;
|
|
61
|
+
white-space: nowrap;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.value {
|
|
65
|
+
font-family: var(--basalt-font-mono);
|
|
66
|
+
text-align: end;
|
|
67
|
+
white-space: nowrap;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.value:last-child,
|
|
71
|
+
.secondary:last-child {
|
|
72
|
+
padding-inline-end: var(--vx-space-stack-sm);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.secondary {
|
|
76
|
+
font-family: var(--basalt-font-mono);
|
|
77
|
+
font-size: var(--vx-text-sm);
|
|
78
|
+
color: var(--vx-muted);
|
|
79
|
+
white-space: nowrap;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.empty {
|
|
83
|
+
grid-column: 1 / -1;
|
|
84
|
+
font-size: var(--vx-text-sm);
|
|
85
|
+
color: var(--vx-muted);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.skeletonRow {
|
|
89
|
+
grid-column: 1 / -1;
|
|
90
|
+
height: calc(var(--vx-space-control-height-widget, 1.5rem) * var(--mantine-scale, 1));
|
|
91
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 track: string
|
|
9
|
+
readonly bar: string
|
|
10
|
+
readonly leader: string
|
|
11
|
+
readonly label: string
|
|
12
|
+
readonly value: string
|
|
13
|
+
readonly secondary: string
|
|
14
|
+
readonly empty: string
|
|
15
|
+
readonly skeletonRow: string
|
|
16
|
+
}
|
|
17
|
+
export default classes
|
|
@@ -0,0 +1,125 @@
|
|
|
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). The bar spans the label column only — values sit outside it, so a full-scale bar ends
|
|
5
|
+
* before its value. Intended for 5–10 rows; past 10 use a table with an inline `Meter` cell. An
|
|
6
|
+
* HTML/CSS primitive, not a chart kind.
|
|
7
|
+
*
|
|
8
|
+
* Colour law: bars are neutral. `accentLeader` (opt-in) lets the top row take the accent — the
|
|
9
|
+
* leader bar, and nothing else. Each value is rendered as text, never only a bar length.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* import { BarList } from 'basalt-ui'
|
|
13
|
+
*
|
|
14
|
+
* // Recovery contributors on a fixed 0–100 scale, the qualitative level as the secondary value:
|
|
15
|
+
* <BarList label="Recovery contributors" max={100} items={[{ label: 'Sleep', value: 82, secondary: 'Good' }]} />
|
|
16
|
+
*
|
|
17
|
+
* <BarList
|
|
18
|
+
* label="Top projects by tokens"
|
|
19
|
+
* items={[{ label: 'argo', value: 1840, secondary: '41%' }, { label: 'rb', value: 920 }]}
|
|
20
|
+
* format={compact}
|
|
21
|
+
* accentLeader
|
|
22
|
+
* />
|
|
23
|
+
*/
|
|
24
|
+
import { Skeleton, VisuallyHidden } from '@mantine/core'
|
|
25
|
+
import type { ReactNode } from 'react'
|
|
26
|
+
import { cx } from '../common/props'
|
|
27
|
+
import type { BasaltProps } from '../common/props'
|
|
28
|
+
import classes from './bar-list.module.css'
|
|
29
|
+
import { asText, barPercent } from './scale'
|
|
30
|
+
|
|
31
|
+
type BarListItem = { label: string; value: number; secondary?: ReactNode; key?: string }
|
|
32
|
+
|
|
33
|
+
export type BarListProps = BasaltProps & {
|
|
34
|
+
/** Accessible name of the list. Required. */
|
|
35
|
+
label: string
|
|
36
|
+
/** Rows in display order (sort them yourself; the list does not reorder). */
|
|
37
|
+
items: readonly BarListItem[]
|
|
38
|
+
/** Formats each value. Default: the number as written. */
|
|
39
|
+
format?: (value: number) => string
|
|
40
|
+
/** Scale end shared by every bar. Default: the largest value. */
|
|
41
|
+
max?: number
|
|
42
|
+
/** The first row's bar takes the accent. Off by default. */
|
|
43
|
+
accentLeader?: boolean
|
|
44
|
+
/** Renders row-shaped skeletons. */
|
|
45
|
+
loading?: boolean
|
|
46
|
+
/** Skeleton rows while loading. Default 5. */
|
|
47
|
+
loadingRows?: number
|
|
48
|
+
/** Muted line shown when `items` is empty. */
|
|
49
|
+
emptyText?: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** `null`, `undefined`, `false` and `true` render nothing, so they are not a secondary value. */
|
|
53
|
+
const hasContent = (node: ReactNode): boolean =>
|
|
54
|
+
node !== undefined && node !== null && typeof node !== 'boolean'
|
|
55
|
+
|
|
56
|
+
export function BarList({
|
|
57
|
+
label,
|
|
58
|
+
items,
|
|
59
|
+
format = asText,
|
|
60
|
+
max,
|
|
61
|
+
accentLeader = false,
|
|
62
|
+
loading = false,
|
|
63
|
+
loadingRows = 5,
|
|
64
|
+
emptyText = 'No data',
|
|
65
|
+
className,
|
|
66
|
+
style,
|
|
67
|
+
}: BarListProps) {
|
|
68
|
+
if (loading) {
|
|
69
|
+
return (
|
|
70
|
+
<div className={cx(classes.root, className)} style={style} aria-busy="true">
|
|
71
|
+
<VisuallyHidden>{`Loading ${label}`}</VisuallyHidden>
|
|
72
|
+
{Array.from({ length: loadingRows }, (_, i) => (
|
|
73
|
+
<Skeleton key={i} className={classes.skeletonRow} data-bar-skeleton />
|
|
74
|
+
))}
|
|
75
|
+
</div>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
if (items.length === 0) {
|
|
79
|
+
return (
|
|
80
|
+
<output className={cx(classes.root, className)} style={style} aria-label={label}>
|
|
81
|
+
<span className={classes.empty}>{emptyText}</span>
|
|
82
|
+
</output>
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const scale =
|
|
87
|
+
max ?? items.reduce((top, i) => (Number.isFinite(i.value) ? Math.max(top, i.value) : top), 0)
|
|
88
|
+
const hasSecondary = items.some((i) => hasContent(i.secondary))
|
|
89
|
+
return (
|
|
90
|
+
// The explicit role is deliberate: `list-style: none` makes Safari/VoiceOver drop the implicit one.
|
|
91
|
+
<ul
|
|
92
|
+
className={cx(classes.root, className)}
|
|
93
|
+
style={style}
|
|
94
|
+
// oxlint-disable-next-line jsx-a11y/no-redundant-roles
|
|
95
|
+
role="list"
|
|
96
|
+
aria-label={label}
|
|
97
|
+
data-secondary={hasSecondary ? '' : undefined}
|
|
98
|
+
>
|
|
99
|
+
{items.map((item, i) => {
|
|
100
|
+
const isLeader = accentLeader && i === 0
|
|
101
|
+
return (
|
|
102
|
+
<li key={item.key ?? `${item.label}-${i}`} className={classes.row}>
|
|
103
|
+
<span className={classes.track} data-bar-track>
|
|
104
|
+
{/* theme-allow hidden-inline-style — the width is the row's data (a % of the shared scale), not styling */}
|
|
105
|
+
<span
|
|
106
|
+
className={cx(classes.bar, isLeader && classes.leader)}
|
|
107
|
+
style={{ width: `${barPercent(item.value, scale)}%` }}
|
|
108
|
+
data-bar-leader={isLeader ? '' : undefined}
|
|
109
|
+
aria-hidden
|
|
110
|
+
/>
|
|
111
|
+
<span className={classes.label} title={item.label}>
|
|
112
|
+
{item.label}
|
|
113
|
+
</span>
|
|
114
|
+
</span>
|
|
115
|
+
<span className={classes.value}>
|
|
116
|
+
{Number.isFinite(item.value) ? format(item.value) : '—'}
|
|
117
|
+
</span>
|
|
118
|
+
{/* Kept empty on a row without one: the secondary column is shared, so every row has the cell. */}
|
|
119
|
+
{hasSecondary && <span className={classes.secondary}>{item.secondary}</span>}
|
|
120
|
+
</li>
|
|
121
|
+
)
|
|
122
|
+
})}
|
|
123
|
+
</ul>
|
|
124
|
+
)
|
|
125
|
+
}
|
package/src/dashboard/index.ts
CHANGED
|
@@ -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,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Meter's pure model: everything the component shows, computed from numbers (no DOM, no React runtime).
|
|
3
|
+
* Internal — `meter.tsx` and `meter-parts.tsx` render what this returns.
|
|
4
|
+
*/
|
|
5
|
+
import type { ReactNode } from 'react'
|
|
6
|
+
import { clamp01, toPct } from './scale'
|
|
7
|
+
|
|
8
|
+
export type MeterSegment = { value: number; label: string; color?: string; forecast?: boolean }
|
|
9
|
+
export type MeterZone = { from: number; to: number; label: string; color?: string }
|
|
10
|
+
|
|
11
|
+
/** A fill on the track, as fractions (0–1) of the span, with the segment it was laid out from. */
|
|
12
|
+
export type Part = {
|
|
13
|
+
readonly start: number
|
|
14
|
+
readonly size: number
|
|
15
|
+
readonly color: string | undefined
|
|
16
|
+
readonly forecast: boolean
|
|
17
|
+
readonly label: string
|
|
18
|
+
/** The segment's own (normalised) amount, in the reading's units — what its caption states. */
|
|
19
|
+
readonly amount: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** One categorical part's caption: its position key, name, formatted amount and fill colour (`undefined` = neutral). */
|
|
23
|
+
export type PartCaption = { key: string; label: string; text: string; color: string | undefined }
|
|
24
|
+
|
|
25
|
+
/** One zone's name on the band: where its band starts (percent) and whether it holds the reading. */
|
|
26
|
+
export type ZoneLabel = { key: string; label: string; startPct: number; holding: boolean }
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* What the caption row says. `reading`: the formatted reading; `parts`: categorical segments name
|
|
30
|
+
* every part instead; `forecast`: committed/forecast keeps the reading plus the forecast caption.
|
|
31
|
+
*/
|
|
32
|
+
export type CaptionMode = 'reading' | 'parts' | 'forecast'
|
|
33
|
+
|
|
34
|
+
type Format = (value: number) => string
|
|
35
|
+
|
|
36
|
+
export const isReading = (n: number | null | undefined): n is number =>
|
|
37
|
+
typeof n === 'number' && Number.isFinite(n)
|
|
38
|
+
|
|
39
|
+
/** The track's span. A track that ends at or before its start falls back to 1 and is `valid: false`. */
|
|
40
|
+
export function spanOf(min: number, max: number): { span: number; valid: boolean } {
|
|
41
|
+
return max > min ? { span: max - min, valid: true } : { span: 1, valid: false }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function ariaValues(reading: number, min: number, max: number, format: Format) {
|
|
45
|
+
return {
|
|
46
|
+
role: 'meter',
|
|
47
|
+
'aria-valuenow': Math.min(max, Math.max(min, reading)),
|
|
48
|
+
'aria-valuemin': min,
|
|
49
|
+
'aria-valuemax': max,
|
|
50
|
+
'aria-valuetext': format(reading),
|
|
51
|
+
} as const
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The ONE place segment amounts are sanitised: a negative or non-finite amount is 0. The painted
|
|
56
|
+
* track, the captions and `aria-valuenow` all derive from what this returns.
|
|
57
|
+
*/
|
|
58
|
+
export function normalizeSegments(segments: readonly MeterSegment[]): MeterSegment[] {
|
|
59
|
+
return segments.map((s) => ({ ...s, value: Number.isFinite(s.value) ? Math.max(0, s.value) : 0 }))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Segment amounts (already normalised) accumulate from `min`; a part never passes the track end. */
|
|
63
|
+
export function layoutSegments(segments: readonly MeterSegment[], span: number): Part[] {
|
|
64
|
+
let cursor = 0
|
|
65
|
+
return segments.map((s) => {
|
|
66
|
+
const size = Math.max(0, Math.min(clamp01(s.value / span), 1 - cursor))
|
|
67
|
+
const forecast = s.forecast === true
|
|
68
|
+
// A forecast stays the lighter neutral: a colour would turn the committed/forecast pair into hue.
|
|
69
|
+
const part = {
|
|
70
|
+
start: cursor,
|
|
71
|
+
size,
|
|
72
|
+
color: forecast ? undefined : s.color,
|
|
73
|
+
forecast,
|
|
74
|
+
label: s.label,
|
|
75
|
+
amount: s.value,
|
|
76
|
+
}
|
|
77
|
+
cursor += size
|
|
78
|
+
return part
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** `from` is inclusive; a value on the last band's `to` belongs to that band. */
|
|
83
|
+
export function findHoldingZone(zones: readonly MeterZone[], value: number): MeterZone | undefined {
|
|
84
|
+
const end = Math.max(...zones.map((z) => z.to))
|
|
85
|
+
return zones.find((z) => value >= z.from && (value < z.to || (value === z.to && z.to === end)))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Sub-pixel rounding in measured boxes: two labels touching within this do not collide. */
|
|
89
|
+
export const LABEL_SUBPIXEL = 0.5
|
|
90
|
+
|
|
91
|
+
/** A measured label box on one axis (any shared origin), in CSS pixels. */
|
|
92
|
+
export type LabelBox = { left: number; right: number }
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether every zone name fits on the band: inside `[0, rowWidth]` and clear of every other name
|
|
96
|
+
* (pairwise, so a long name cannot hide behind a short one between it and a distant neighbour).
|
|
97
|
+
* Boxes are relative to the row's own start edge.
|
|
98
|
+
*/
|
|
99
|
+
export function zoneLabelsFit(boxes: readonly LabelBox[], rowWidth: number): boolean {
|
|
100
|
+
const sorted = boxes.toSorted((a, b) => a.left - b.left)
|
|
101
|
+
return sorted.every(
|
|
102
|
+
(a, i) =>
|
|
103
|
+
a.left >= -LABEL_SUBPIXEL &&
|
|
104
|
+
a.right <= rowWidth + LABEL_SUBPIXEL &&
|
|
105
|
+
sorted.slice(i + 1).every((b) => a.right <= b.left + LABEL_SUBPIXEL),
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A zone band as left edge + width in percent, clipped to the track. */
|
|
110
|
+
export function zoneBand(zone: MeterZone, min: number, span: number) {
|
|
111
|
+
const lo = clamp01((zone.from - min) / span)
|
|
112
|
+
const hi = clamp01((zone.to - min) / span)
|
|
113
|
+
return { start: toPct(lo), width: toPct(hi - lo) }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const segmentText = (s: MeterSegment, format: Format): string =>
|
|
117
|
+
`${s.label} ${format(s.value)}${s.forecast === true ? ' (forecast)' : ''}`
|
|
118
|
+
|
|
119
|
+
export type BarInput = {
|
|
120
|
+
reading: number
|
|
121
|
+
min: number
|
|
122
|
+
max: number
|
|
123
|
+
format: Format
|
|
124
|
+
segments?: readonly MeterSegment[] | undefined
|
|
125
|
+
zones?: readonly MeterZone[] | undefined
|
|
126
|
+
target?: number | undefined
|
|
127
|
+
color?: string | undefined
|
|
128
|
+
forecastCaption?: string | undefined
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type BarModel = {
|
|
132
|
+
span: number
|
|
133
|
+
/** Fills, or none when the track is drawn as zones. */
|
|
134
|
+
parts: readonly Part[]
|
|
135
|
+
/** Percent where the first forecast part starts; absent when the forecast opens the track. */
|
|
136
|
+
boundaryPct: number | undefined
|
|
137
|
+
forecastText: string | null
|
|
138
|
+
captionMode: CaptionMode
|
|
139
|
+
/** Only in `parts` mode: one caption per part, in order. Empty otherwise. */
|
|
140
|
+
partCaptions: readonly PartCaption[]
|
|
141
|
+
/** Only on a zoned track: every zone's name, in zone order. Empty otherwise. */
|
|
142
|
+
zoneLabels: readonly ZoneLabel[]
|
|
143
|
+
markerPct: number | undefined
|
|
144
|
+
targetPct: number | undefined
|
|
145
|
+
/** The default start caption (the zone is named on the band, so it is the bare reading); `startCaption` decides whether it shows. */
|
|
146
|
+
startText: string
|
|
147
|
+
endText: string | null
|
|
148
|
+
describedByText: string | null
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function endCaptionText(reading: number, target: number | undefined, format: Format) {
|
|
152
|
+
if (target === undefined) return null
|
|
153
|
+
if (reading > target) return `Target ${format(target)} · ${format(reading - target)} over`
|
|
154
|
+
return `Target ${format(target)}`
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function fillParts(input: BarInput, span: number, zoned: boolean): Part[] {
|
|
158
|
+
if (zoned) return []
|
|
159
|
+
if (input.segments !== undefined) return layoutSegments(input.segments, span)
|
|
160
|
+
const fill = { value: input.reading - input.min, label: '' }
|
|
161
|
+
return layoutSegments([input.color === undefined ? fill : { ...fill, color: input.color }], span)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Segments are amounts accumulated from `min`, so the reading is `min` plus their sum. */
|
|
165
|
+
export const segmentReading = (segments: readonly MeterSegment[], min: number): number =>
|
|
166
|
+
segments.reduce((sum, s) => sum + s.value, min)
|
|
167
|
+
|
|
168
|
+
/** Keyed by index: segments are an ordered list, and labels are the consumer's, so they may repeat. */
|
|
169
|
+
function partCaptionsOf(parts: readonly Part[], format: Format): PartCaption[] {
|
|
170
|
+
return parts.map((p, i) => ({
|
|
171
|
+
key: String(i),
|
|
172
|
+
label: p.label,
|
|
173
|
+
text: format(p.amount),
|
|
174
|
+
color: p.color,
|
|
175
|
+
}))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Zones win over segments (the union forbids both, a JS caller can still pass them): a zoned track
|
|
180
|
+
* has no fills to caption. Empty segments are the empty state, never categorical.
|
|
181
|
+
*/
|
|
182
|
+
function captionModeOf(
|
|
183
|
+
segments: readonly MeterSegment[] | undefined,
|
|
184
|
+
zoned: boolean,
|
|
185
|
+
firstForecast: number,
|
|
186
|
+
): CaptionMode {
|
|
187
|
+
if (zoned) return 'reading'
|
|
188
|
+
if (segments === undefined || segments.length === 0) return 'reading'
|
|
189
|
+
return firstForecast < 0 ? 'parts' : 'forecast'
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The start caption: the consumer's override, else the model's default — absent in `parts` mode. */
|
|
193
|
+
export function startCaptionText(model: BarModel, override: ReactNode | undefined): ReactNode {
|
|
194
|
+
if (override !== undefined) return override
|
|
195
|
+
return model.captionMode === 'parts' ? null : model.startText
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function modelBar(input: BarInput): BarModel {
|
|
199
|
+
const { reading, min, max, format, segments, zones, target } = input
|
|
200
|
+
const { span } = spanOf(min, max)
|
|
201
|
+
const zoned = zones !== undefined && zones.length > 0
|
|
202
|
+
const holding = zoned ? findHoldingZone(zones, reading) : undefined
|
|
203
|
+
|
|
204
|
+
const parts = fillParts(input, span, zoned)
|
|
205
|
+
const firstForecast = parts.findIndex((p) => p.forecast)
|
|
206
|
+
const forecastLabels =
|
|
207
|
+
segments?.filter((s) => s.forecast === true).map((s) => `${s.label} ${format(s.value)}`) ?? []
|
|
208
|
+
|
|
209
|
+
const captionMode = captionModeOf(segments, zoned, firstForecast)
|
|
210
|
+
const partCaptions = captionMode === 'parts' ? partCaptionsOf(parts, format) : []
|
|
211
|
+
|
|
212
|
+
const zoneText = !zoned
|
|
213
|
+
? []
|
|
214
|
+
: [
|
|
215
|
+
...zones.map((z) => `${z.label} ${format(z.from)} to ${format(z.to)}`),
|
|
216
|
+
holding === undefined
|
|
217
|
+
? `${format(reading)} is outside every zone`
|
|
218
|
+
: `${format(reading)} is in ${holding.label}`,
|
|
219
|
+
]
|
|
220
|
+
const describedBy = [
|
|
221
|
+
...(segments?.map((s) => segmentText(s, format)) ?? []),
|
|
222
|
+
...zoneText,
|
|
223
|
+
...(target === undefined ? [] : [`Target ${format(target)}`]),
|
|
224
|
+
]
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
span,
|
|
228
|
+
parts,
|
|
229
|
+
boundaryPct: firstForecast > 0 ? toPct(parts[firstForecast]?.start ?? 0) : undefined,
|
|
230
|
+
forecastText:
|
|
231
|
+
forecastLabels.length === 0
|
|
232
|
+
? null
|
|
233
|
+
: (input.forecastCaption ?? `Forecast: ${forecastLabels.join(', ')}`),
|
|
234
|
+
captionMode,
|
|
235
|
+
partCaptions,
|
|
236
|
+
zoneLabels: !zoned
|
|
237
|
+
? []
|
|
238
|
+
: zones.map((z, i) => ({
|
|
239
|
+
key: String(i),
|
|
240
|
+
label: z.label,
|
|
241
|
+
startPct: zoneBand(z, min, span).start,
|
|
242
|
+
holding: z === holding,
|
|
243
|
+
})),
|
|
244
|
+
markerPct: zoned ? toPct((reading - min) / span) : undefined,
|
|
245
|
+
targetPct: target === undefined ? undefined : toPct((target - min) / span),
|
|
246
|
+
startText: format(reading),
|
|
247
|
+
endText: endCaptionText(reading, target, format),
|
|
248
|
+
describedByText: describedBy.length > 0 ? describedBy.join('; ') : null,
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** The ring's one arc, as a fraction of the circumference. */
|
|
253
|
+
export const ringFraction = (reading: number, min: number, max: number): number =>
|
|
254
|
+
clamp01((reading - min) / spanOf(min, max).span)
|