@uibot/blocks-studio 0.18.1 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/blocks/charts.d.ts +1 -6
- package/dist/catalog.js +1 -1
- package/dist/index.js +2 -2
- package/dist/solution-recipes.d.ts +13 -0
- package/package.json +4 -4
- package/src/blocks/charts.tsx +186 -78
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SolutionLabel } from "@uibot/contracts";
|
|
2
|
+
/** Exact source versions; composition never silently upgrades a member. */
|
|
3
|
+
export declare const INDUSTRY_SOLUTION_RECIPES: Array<{
|
|
4
|
+
id: string;
|
|
5
|
+
title: SolutionLabel;
|
|
6
|
+
industry: string;
|
|
7
|
+
functions: string[];
|
|
8
|
+
scenarios: string[];
|
|
9
|
+
members: Array<{
|
|
10
|
+
resourceId: string;
|
|
11
|
+
version: string;
|
|
12
|
+
}>;
|
|
13
|
+
}>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uibot/blocks-studio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -41,9 +41,9 @@
|
|
|
41
41
|
"react-dom": ">=18"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@uibot/contracts": "^0.
|
|
45
|
-
"@uibot/plugin-sdk": "^0.
|
|
46
|
-
"@uibot/renderer-react": "^0.
|
|
44
|
+
"@uibot/contracts": "^0.20.0",
|
|
45
|
+
"@uibot/plugin-sdk": "^0.20.0",
|
|
46
|
+
"@uibot/renderer-react": "^0.20.0"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "rm -rf dist && bun build --minify ./src/index.ts ./src/registry-source.ts ./src/catalog.ts --outdir ./dist --format esm --target bun --external react --external react/* --external react-dom --external react-dom/* --external @uibot/renderer-react && bun x tsc -p tsconfig.json",
|
package/src/blocks/charts.tsx
CHANGED
|
@@ -1,126 +1,232 @@
|
|
|
1
|
+
import { useId, useState, type KeyboardEvent, type PointerEvent } from "react";
|
|
1
2
|
import type { RendererComponent } from "@uibot/renderer-react";
|
|
2
3
|
|
|
3
4
|
function cn(...cls: (string | false | null | undefined)[]) {
|
|
4
5
|
return cls.filter(Boolean).join(" ");
|
|
5
6
|
}
|
|
6
|
-
|
|
7
7
|
interface Series {
|
|
8
8
|
name?: string;
|
|
9
9
|
points: number[];
|
|
10
10
|
area?: boolean;
|
|
11
11
|
}
|
|
12
|
-
|
|
13
12
|
function parseSeries(raw: unknown): Series[] {
|
|
14
13
|
if (!Array.isArray(raw)) return [];
|
|
15
14
|
return raw
|
|
16
15
|
.map((s) => {
|
|
17
16
|
if (!s || typeof s !== "object") return null;
|
|
18
17
|
const rec = s as Record<string, unknown>;
|
|
19
|
-
const points = Array.isArray(rec
|
|
20
|
-
? rec
|
|
18
|
+
const points = Array.isArray(rec.points)
|
|
19
|
+
? rec.points.map((p) => {
|
|
20
|
+
const value = Number(p);
|
|
21
|
+
return Number.isFinite(value) ? value : 0;
|
|
22
|
+
})
|
|
21
23
|
: [];
|
|
22
24
|
return {
|
|
23
|
-
name: typeof rec
|
|
25
|
+
name: typeof rec.name === "string" ? rec.name : undefined,
|
|
24
26
|
points,
|
|
25
|
-
area: rec
|
|
27
|
+
area: rec.area === true,
|
|
26
28
|
} as Series;
|
|
27
29
|
})
|
|
28
30
|
.filter((s): s is Series => s !== null && s.points.length > 0);
|
|
29
31
|
}
|
|
30
|
-
|
|
31
|
-
// Padding reserved around the plot so strokes are not clipped at the edges.
|
|
32
32
|
const PAD = 8;
|
|
33
|
-
|
|
34
|
-
// Map a series' points into an SVG polyline `points` string. min/max are taken
|
|
35
|
-
// across every series so all lines share one vertical scale.
|
|
36
|
-
function toPolyline(points: number[], w: number, h: number, min: number, max: number): string {
|
|
37
|
-
const range = max - min || 1;
|
|
38
|
-
const innerW = w - PAD * 2;
|
|
39
|
-
const innerH = h - PAD * 2;
|
|
40
|
-
const step = points.length > 1 ? innerW / (points.length - 1) : 0;
|
|
41
|
-
return points
|
|
42
|
-
.map((value, i) => {
|
|
43
|
-
const x = PAD + step * i;
|
|
44
|
-
const y = PAD + innerH - ((value - min) / range) * innerH;
|
|
45
|
-
return `${x.toFixed(1)},${y.toFixed(1)}`;
|
|
46
|
-
})
|
|
47
|
-
.join(" ");
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// A small palette of theme-token colours, cycled per series.
|
|
51
33
|
const STROKE = ["text-primary", "text-sky-500", "text-emerald-500", "text-amber-500"];
|
|
52
34
|
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
|
|
35
|
+
/** Small SVG charts share one time axis and vertical scale. Inspection is local. */
|
|
36
|
+
function TrendChartView({ props }: { props: Record<string, unknown> }) {
|
|
37
|
+
const [inspection, setInspection] = useState<{ index: number; keyboard: boolean } | null>(null);
|
|
38
|
+
const tooltipId = useId();
|
|
39
|
+
const instructionsId = useId();
|
|
40
|
+
const series = parseSeries(props.series);
|
|
41
|
+
const title = typeof props.title === "string" ? props.title : "";
|
|
42
|
+
const w = typeof props.width === "number" ? Math.max(32, props.width) : 480;
|
|
43
|
+
const h = typeof props.height === "number" ? Math.max(32, props.height) : 160;
|
|
44
|
+
const labels = Array.isArray(props.labels) ? props.labels.map(String) : [];
|
|
45
|
+
const plain = props.variant === "plain";
|
|
46
|
+
const count = Math.max(0, ...series.map((s) => s.points.length));
|
|
64
47
|
const all = series.flatMap((s) => s.points);
|
|
65
48
|
const min = all.length ? Math.min(...all) : 0;
|
|
66
49
|
const max = all.length ? Math.max(...all) : 1;
|
|
67
|
-
|
|
50
|
+
const pointX = (i: number) => PAD + (count > 1 ? i / (count - 1) : 0) * (w - PAD * 2);
|
|
51
|
+
const pointY = (value: number) => PAD + (h - PAD * 2) * (1 - (value - min) / (max - min || 1));
|
|
52
|
+
const index = inspection?.index ?? null;
|
|
53
|
+
const inspectPointer = (event: PointerEvent<SVGSVGElement>) => {
|
|
54
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
55
|
+
if (!rect.width) return;
|
|
56
|
+
const position = (((event.clientX - rect.left) / rect.width) * w - PAD) / (w - PAD * 2);
|
|
57
|
+
setInspection({
|
|
58
|
+
index: Math.max(0, Math.min(count - 1, Math.round(position * (count - 1)))),
|
|
59
|
+
keyboard: false,
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
const inspectKey = (event: KeyboardEvent<SVGSVGElement>) => {
|
|
63
|
+
if (event.key === "Escape") {
|
|
64
|
+
setInspection(null);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
let next: number;
|
|
68
|
+
if (event.key === "Home") next = 0;
|
|
69
|
+
else if (event.key === "End") next = count - 1;
|
|
70
|
+
else if (event.key === "ArrowLeft") next = (index ?? 1) - 1;
|
|
71
|
+
else if (event.key === "ArrowRight") next = (index ?? -1) + 1;
|
|
72
|
+
else return;
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
setInspection({ index: Math.max(0, Math.min(count - 1, next)), keyboard: true });
|
|
75
|
+
};
|
|
68
76
|
return (
|
|
69
77
|
<div
|
|
70
|
-
className=
|
|
78
|
+
className={cn(
|
|
79
|
+
"uibot-trendchart min-w-0",
|
|
80
|
+
!plain && "bg-card text-card-foreground rounded-xl border p-4 shadow-sm",
|
|
81
|
+
)}
|
|
71
82
|
data-uibot-type="TrendChart"
|
|
72
83
|
>
|
|
73
|
-
{title && <h3 className="mb-2 text-sm font-medium">{title}</h3>}
|
|
84
|
+
{title && <h3 className={plain ? "sr-only" : "mb-2 text-sm font-medium"}>{title}</h3>}
|
|
74
85
|
{series.length === 0 ? (
|
|
75
86
|
<p className="py-6 text-center text-sm text-muted-foreground">No data</p>
|
|
76
87
|
) : (
|
|
77
|
-
<
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
<div className="relative">
|
|
89
|
+
<span id={instructionsId} className="sr-only">
|
|
90
|
+
Use Left and Right arrows to inspect values, Home and End to jump, and Escape to
|
|
91
|
+
dismiss.
|
|
92
|
+
</span>
|
|
93
|
+
<svg
|
|
94
|
+
viewBox={`0 0 ${w} ${h}`}
|
|
95
|
+
className="h-auto w-full rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
96
|
+
style={plain ? { height: h } : undefined}
|
|
97
|
+
role="img"
|
|
98
|
+
aria-label={title || "Trend chart"}
|
|
99
|
+
preserveAspectRatio="none"
|
|
100
|
+
tabIndex={0}
|
|
101
|
+
onPointerMove={inspectPointer}
|
|
102
|
+
onPointerDown={inspectPointer}
|
|
103
|
+
onPointerLeave={() => {
|
|
104
|
+
if (!inspection?.keyboard) setInspection(null);
|
|
105
|
+
}}
|
|
106
|
+
onPointerCancel={() => setInspection(null)}
|
|
107
|
+
onFocus={() => setInspection((current) => current ?? { index: 0, keyboard: true })}
|
|
108
|
+
onBlur={() => setInspection(null)}
|
|
109
|
+
onKeyDown={inspectKey}
|
|
110
|
+
aria-describedby={`${instructionsId}${index === null ? "" : ` ${tooltipId}`}`}
|
|
111
|
+
>
|
|
112
|
+
{[0, 0.25, 0.5, 0.75, 1].map((t) => (
|
|
88
113
|
<line
|
|
89
114
|
key={t}
|
|
90
115
|
x1={PAD}
|
|
91
116
|
x2={w - PAD}
|
|
92
|
-
y1={
|
|
93
|
-
y2={
|
|
117
|
+
y1={PAD + (h - PAD * 2) * t}
|
|
118
|
+
y2={PAD + (h - PAD * 2) * t}
|
|
94
119
|
className="text-border"
|
|
95
120
|
stroke="currentColor"
|
|
96
121
|
strokeWidth={0.5}
|
|
97
122
|
/>
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
123
|
+
))}
|
|
124
|
+
{series.map((s, i) => {
|
|
125
|
+
const pts = s.points
|
|
126
|
+
.map((value, j) => `${pointX(j).toFixed(1)},${pointY(value).toFixed(1)}`)
|
|
127
|
+
.join(" ");
|
|
128
|
+
return (
|
|
129
|
+
<g key={i} className={STROKE[i % STROKE.length]}>
|
|
130
|
+
{s.area && (
|
|
131
|
+
<polygon
|
|
132
|
+
fill="currentColor"
|
|
133
|
+
fillOpacity={0.12}
|
|
134
|
+
points={`${PAD},${h - PAD} ${pts} ${pointX(s.points.length - 1)},${h - PAD}`}
|
|
135
|
+
/>
|
|
136
|
+
)}
|
|
137
|
+
<polyline
|
|
138
|
+
fill="none"
|
|
139
|
+
stroke="currentColor"
|
|
140
|
+
strokeWidth={2}
|
|
141
|
+
strokeLinecap="round"
|
|
142
|
+
strokeLinejoin="round"
|
|
143
|
+
points={pts}
|
|
110
144
|
/>
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
145
|
+
</g>
|
|
146
|
+
);
|
|
147
|
+
})}
|
|
148
|
+
{index !== null && (
|
|
149
|
+
<g aria-hidden="true" pointerEvents="none">
|
|
150
|
+
<line
|
|
151
|
+
x1={pointX(index)}
|
|
152
|
+
x2={pointX(index)}
|
|
153
|
+
y1={PAD}
|
|
154
|
+
y2={h - PAD}
|
|
155
|
+
className="text-muted-foreground"
|
|
114
156
|
stroke="currentColor"
|
|
115
|
-
strokeWidth={
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
points={pts}
|
|
157
|
+
strokeWidth={1}
|
|
158
|
+
strokeDasharray="4 4"
|
|
159
|
+
vectorEffect="non-scaling-stroke"
|
|
119
160
|
/>
|
|
161
|
+
{series.map((s, i) =>
|
|
162
|
+
s.points[index] === undefined ? null : (
|
|
163
|
+
<circle
|
|
164
|
+
key={i}
|
|
165
|
+
data-chart-point="active"
|
|
166
|
+
cx={pointX(index)}
|
|
167
|
+
cy={pointY(s.points[index] ?? 0)}
|
|
168
|
+
r={4}
|
|
169
|
+
className={STROKE[i % STROKE.length]}
|
|
170
|
+
fill="currentColor"
|
|
171
|
+
stroke="var(--background)"
|
|
172
|
+
strokeWidth={2}
|
|
173
|
+
vectorEffect="non-scaling-stroke"
|
|
174
|
+
/>
|
|
175
|
+
),
|
|
176
|
+
)}
|
|
120
177
|
</g>
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
|
|
178
|
+
)}
|
|
179
|
+
</svg>
|
|
180
|
+
{index !== null && (
|
|
181
|
+
<div
|
|
182
|
+
id={tooltipId}
|
|
183
|
+
role="tooltip"
|
|
184
|
+
aria-live={inspection?.keyboard ? "polite" : "off"}
|
|
185
|
+
className="pointer-events-none absolute top-3 z-10 min-w-36 rounded-lg border bg-popover px-3 py-2.5 text-xs text-popover-foreground shadow-md"
|
|
186
|
+
style={{
|
|
187
|
+
...(index < count / 2 ? { right: 12 } : { left: 12 }),
|
|
188
|
+
maxWidth: "calc(100% - 24px)",
|
|
189
|
+
}}
|
|
190
|
+
>
|
|
191
|
+
<div className="mb-2 font-medium">{labels[index] ?? `Point ${index + 1}`}</div>
|
|
192
|
+
<div className="space-y-1.5">
|
|
193
|
+
{series.map((s, i) =>
|
|
194
|
+
s.points[index] === undefined ? null : (
|
|
195
|
+
<div key={i} className="flex items-center gap-2">
|
|
196
|
+
<span
|
|
197
|
+
className={cn(
|
|
198
|
+
"h-2 w-2 shrink-0 rounded-sm bg-current",
|
|
199
|
+
STROKE[i % STROKE.length],
|
|
200
|
+
)}
|
|
201
|
+
aria-hidden="true"
|
|
202
|
+
/>
|
|
203
|
+
<span className="text-muted-foreground">{s.name ?? `Series ${i + 1}`}</span>
|
|
204
|
+
<span className="ms-auto ps-3 font-medium tabular-nums">
|
|
205
|
+
{s.points[index]?.toLocaleString("en-US")}
|
|
206
|
+
</span>
|
|
207
|
+
</div>
|
|
208
|
+
),
|
|
209
|
+
)}
|
|
210
|
+
</div>
|
|
211
|
+
</div>
|
|
212
|
+
)}
|
|
213
|
+
</div>
|
|
214
|
+
)}
|
|
215
|
+
{labels.length > 0 && series.length > 0 && (
|
|
216
|
+
<div
|
|
217
|
+
className="mt-2 flex justify-between gap-2 text-xs text-muted-foreground"
|
|
218
|
+
aria-hidden="true"
|
|
219
|
+
>
|
|
220
|
+
{labels
|
|
221
|
+
.filter(
|
|
222
|
+
(_, i) =>
|
|
223
|
+
i < count &&
|
|
224
|
+
(i === 0 || i === count - 1 || i % Math.max(1, Math.ceil(count / 5)) === 0),
|
|
225
|
+
)
|
|
226
|
+
.map((label, i) => (
|
|
227
|
+
<span key={i}>{label}</span>
|
|
228
|
+
))}
|
|
229
|
+
</div>
|
|
124
230
|
)}
|
|
125
231
|
{series.some((s) => s.name) && (
|
|
126
232
|
<ul className="mt-3 flex flex-wrap gap-4">
|
|
@@ -134,7 +240,9 @@ export const TrendChart: RendererComponent = (props) => {
|
|
|
134
240
|
)}
|
|
135
241
|
</div>
|
|
136
242
|
);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
243
|
+
}
|
|
244
|
+
export const TrendChart: RendererComponent = (props) => (
|
|
245
|
+
<TrendChartView key={JSON.stringify([props.series, props.labels])} props={props} />
|
|
246
|
+
);
|
|
247
|
+
/** Alias for specs that prefer the LineChart name. */
|
|
140
248
|
export const LineChart = TrendChart;
|