@alpic-ai/ui 0.0.0-dev.g2df19cc → 0.0.0-dev.g2eefcc2
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/components/accordion-card.d.mts +5 -6
- package/dist/components/accordion.d.mts +5 -6
- package/dist/components/alert.d.mts +9 -11
- package/dist/components/area-chart.d.mts +62 -0
- package/dist/components/area-chart.mjs +269 -0
- package/dist/components/attachment-tile.d.mts +1 -3
- package/dist/components/avatar.d.mts +8 -10
- package/dist/components/badge.d.mts +2 -4
- package/dist/components/bar-chart.d.mts +48 -0
- package/dist/components/bar-chart.mjs +256 -0
- package/dist/components/bar-list.d.mts +28 -0
- package/dist/components/bar-list.mjs +98 -0
- package/dist/components/breadcrumb.d.mts +10 -11
- package/dist/components/button.d.mts +6 -8
- package/dist/components/card.d.mts +9 -10
- package/dist/components/chart-card.d.mts +25 -0
- package/dist/components/chart-card.mjs +48 -0
- package/dist/components/chart-container.d.mts +20 -0
- package/dist/components/chart-container.mjs +37 -0
- package/dist/components/chart-legend.d.mts +16 -0
- package/dist/components/chart-legend.mjs +26 -0
- package/dist/components/chart-tooltip.d.mts +33 -0
- package/dist/components/chart-tooltip.mjs +52 -0
- package/dist/components/checkbox.d.mts +2 -3
- package/dist/components/collapsible.d.mts +4 -5
- package/dist/components/combobox.d.mts +12 -11
- package/dist/components/combobox.mjs +7 -4
- package/dist/components/command.d.mts +9 -10
- package/dist/components/copyable.d.mts +2 -3
- package/dist/components/description-list.d.mts +5 -6
- package/dist/components/dialog.d.mts +15 -17
- package/dist/components/donut-chart.d.mts +46 -0
- package/dist/components/donut-chart.mjs +185 -0
- package/dist/components/dropdown-menu.d.mts +18 -20
- package/dist/components/form.d.mts +38 -21
- package/dist/components/form.mjs +6 -6
- package/dist/components/github-button.d.mts +1 -2
- package/dist/components/heatmap-chart.d.mts +40 -0
- package/dist/components/heatmap-chart.mjs +198 -0
- package/dist/components/input-group.d.mts +5 -7
- package/dist/components/input.d.mts +4 -5
- package/dist/components/input.mjs +2 -2
- package/dist/components/label.d.mts +2 -3
- package/dist/components/line-chart.d.mts +55 -0
- package/dist/components/line-chart.mjs +211 -0
- package/dist/components/page-loader.d.mts +1 -3
- package/dist/components/pagination.d.mts +3 -4
- package/dist/components/popover.d.mts +5 -6
- package/dist/components/radio-group.d.mts +3 -4
- package/dist/components/scroll-area.d.mts +3 -4
- package/dist/components/select-trigger-variants.d.mts +1 -3
- package/dist/components/select.d.mts +9 -10
- package/dist/components/separator.d.mts +2 -3
- package/dist/components/sheet.d.mts +11 -12
- package/dist/components/shimmer-text.d.mts +2 -2
- package/dist/components/sidebar.d.mts +34 -36
- package/dist/components/sidebar.mjs +10 -10
- package/dist/components/skeleton.d.mts +2 -4
- package/dist/components/sonner.d.mts +5 -6
- package/dist/components/spinner.d.mts +3 -5
- package/dist/components/stat.d.mts +30 -0
- package/dist/components/stat.mjs +107 -0
- package/dist/components/status-dot.d.mts +2 -4
- package/dist/components/switch.d.mts +2 -3
- package/dist/components/table.d.mts +10 -11
- package/dist/components/tabs.d.mts +12 -14
- package/dist/components/tag.d.mts +3 -5
- package/dist/components/task-progress.d.mts +1 -3
- package/dist/components/textarea.d.mts +3 -4
- package/dist/components/textarea.mjs +2 -2
- package/dist/components/toggle-group.d.mts +4 -6
- package/dist/components/toggle-group.mjs +3 -3
- package/dist/components/tooltip-icon-button.d.mts +1 -2
- package/dist/components/tooltip.d.mts +5 -6
- package/dist/components/typography.d.mts +4 -5
- package/dist/components/wizard.d.mts +4 -23
- package/dist/components/wizard.mjs +1 -19
- package/dist/hooks/use-chart-theme.d.mts +18 -0
- package/dist/hooks/use-chart-theme.mjs +57 -0
- package/dist/hooks/use-mobile.mjs +3 -3
- package/dist/hooks/use-reduced-motion.d.mts +4 -0
- package/dist/hooks/use-reduced-motion.mjs +16 -0
- package/dist/lib/chart-palette.d.mts +4 -0
- package/dist/lib/chart-palette.mjs +95 -0
- package/dist/lib/chart.d.mts +14 -0
- package/dist/lib/chart.mjs +27 -0
- package/package.json +30 -29
- package/src/components/area-chart.tsx +339 -0
- package/src/components/bar-chart.tsx +309 -0
- package/src/components/bar-list.tsx +150 -0
- package/src/components/chart-card.tsx +63 -0
- package/src/components/chart-container.tsx +49 -0
- package/src/components/chart-legend.tsx +41 -0
- package/src/components/chart-tooltip.tsx +93 -0
- package/src/components/combobox.tsx +9 -2
- package/src/components/donut-chart.tsx +217 -0
- package/src/components/heatmap-chart.tsx +287 -0
- package/src/components/line-chart.tsx +264 -0
- package/src/components/stat.tsx +109 -0
- package/src/components/wizard.tsx +1 -35
- package/src/hooks/use-chart-theme.ts +75 -0
- package/src/hooks/use-reduced-motion.ts +17 -0
- package/src/lib/chart-palette.ts +110 -0
- package/src/lib/chart.ts +56 -0
- package/src/stories/area-chart.stories.tsx +198 -0
- package/src/stories/bar-chart.stories.tsx +167 -0
- package/src/stories/bar-list.stories.tsx +83 -0
- package/src/stories/donut-chart.stories.tsx +110 -0
- package/src/stories/heatmap-chart.stories.tsx +105 -0
- package/src/stories/line-chart.stories.tsx +144 -0
- package/src/stories/stat.stories.tsx +64 -0
- package/src/stories/wizard.stories.tsx +22 -4
- package/src/styles/tokens.css +63 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import {
|
|
5
|
+
Bar,
|
|
6
|
+
CartesianGrid,
|
|
7
|
+
LabelList,
|
|
8
|
+
BarChart as RechartsBarChart,
|
|
9
|
+
ReferenceArea,
|
|
10
|
+
ReferenceDot,
|
|
11
|
+
ReferenceLine,
|
|
12
|
+
ResponsiveContainer,
|
|
13
|
+
Tooltip,
|
|
14
|
+
XAxis,
|
|
15
|
+
YAxis,
|
|
16
|
+
} from "recharts";
|
|
17
|
+
|
|
18
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion";
|
|
19
|
+
import { type ChartSeries, orderByLuminance, resolveSeries } from "../lib/chart";
|
|
20
|
+
import type { ChartPaletteName } from "../lib/chart-palette";
|
|
21
|
+
import { cn } from "../lib/cn";
|
|
22
|
+
import type { ChartMarker } from "./area-chart";
|
|
23
|
+
import { useChartContext } from "./chart-container";
|
|
24
|
+
import { ChartLegend } from "./chart-legend";
|
|
25
|
+
import { ChartTooltipContent } from "./chart-tooltip";
|
|
26
|
+
|
|
27
|
+
const BAR_RADIUS = 4;
|
|
28
|
+
const MAX_BAR_SIZE = 48;
|
|
29
|
+
|
|
30
|
+
export interface BarChartProps {
|
|
31
|
+
data: ReadonlyArray<Record<string, string | number | null | undefined>>;
|
|
32
|
+
index: string;
|
|
33
|
+
series: ChartSeries[];
|
|
34
|
+
variant?: "stacked" | "grouped" | "expand";
|
|
35
|
+
legend?: boolean;
|
|
36
|
+
valueLabels?: boolean;
|
|
37
|
+
height?: number;
|
|
38
|
+
yAxisWidth?: number;
|
|
39
|
+
palette?: ChartPaletteName;
|
|
40
|
+
referenceLine?: { y: number; label?: string; band?: boolean };
|
|
41
|
+
markers?: ChartMarker[];
|
|
42
|
+
texture?: boolean;
|
|
43
|
+
loading?: boolean;
|
|
44
|
+
valueFormatter?: (value: number) => string;
|
|
45
|
+
labelFormatter?: (label: string | number) => string;
|
|
46
|
+
className?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function BarChart({
|
|
50
|
+
data,
|
|
51
|
+
index,
|
|
52
|
+
series,
|
|
53
|
+
variant = "stacked",
|
|
54
|
+
legend = false,
|
|
55
|
+
valueLabels = false,
|
|
56
|
+
height = 200,
|
|
57
|
+
yAxisWidth = 48,
|
|
58
|
+
palette,
|
|
59
|
+
referenceLine,
|
|
60
|
+
markers,
|
|
61
|
+
texture = false,
|
|
62
|
+
loading = false,
|
|
63
|
+
valueFormatter = (value) => value.toLocaleString("en-US"),
|
|
64
|
+
labelFormatter,
|
|
65
|
+
className,
|
|
66
|
+
}: BarChartProps) {
|
|
67
|
+
const { palette: paletteColors, theme } = useChartContext(palette);
|
|
68
|
+
const reactId = React.useId().replace(/:/g, "");
|
|
69
|
+
const reducedMotion = useReducedMotion();
|
|
70
|
+
const animated = !reducedMotion;
|
|
71
|
+
|
|
72
|
+
const resolved = resolveSeries(series, paletteColors, theme);
|
|
73
|
+
const stacked = variant === "stacked" || variant === "expand";
|
|
74
|
+
const rendered = stacked ? orderByLuminance(resolved) : resolved;
|
|
75
|
+
const lead = resolved[0];
|
|
76
|
+
const withTotal = stacked && rendered.length > 1;
|
|
77
|
+
|
|
78
|
+
// Stacked bars reach the stack height (sum per x-point), not the tallest single
|
|
79
|
+
// series, so the explicit YAxis domain must not clip a tall stack short.
|
|
80
|
+
const numericMax = React.useMemo(() => {
|
|
81
|
+
let max = 0;
|
|
82
|
+
for (const row of data) {
|
|
83
|
+
let rowTotal = 0;
|
|
84
|
+
for (const entry of series) {
|
|
85
|
+
const value = Number(row[entry.key]);
|
|
86
|
+
if (Number.isFinite(value)) {
|
|
87
|
+
rowTotal += value;
|
|
88
|
+
if (!stacked && value > max) {
|
|
89
|
+
max = value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (stacked && rowTotal > max) {
|
|
94
|
+
max = rowTotal;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return max;
|
|
98
|
+
}, [data, series, stacked]);
|
|
99
|
+
|
|
100
|
+
const margin = { top: markers?.length || valueLabels ? 18 : 8, right: 8, bottom: 2, left: 0 };
|
|
101
|
+
|
|
102
|
+
const axis = {
|
|
103
|
+
stroke: theme.border,
|
|
104
|
+
tick: { fill: theme.axisForeground, fontSize: 10, fontFamily: theme.fontMono },
|
|
105
|
+
tickLine: false as const,
|
|
106
|
+
axisLine: { stroke: theme.border, strokeOpacity: 0.6 },
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const legendItems = resolved.map((entry) => ({ name: entry.name, color: entry.color, dashed: entry.dashed }));
|
|
110
|
+
|
|
111
|
+
const radiusFor = (slot: number): [number, number, number, number] => {
|
|
112
|
+
if (!stacked || slot === rendered.length - 1) {
|
|
113
|
+
return [BAR_RADIUS, BAR_RADIUS, 0, 0];
|
|
114
|
+
}
|
|
115
|
+
return [0, 0, 0, 0];
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const fillFor = (entry: (typeof rendered)[number], slot: number) => {
|
|
119
|
+
if (texture && lead && entry.key === lead.key) {
|
|
120
|
+
return `url(#bar-hatch-${reactId})`;
|
|
121
|
+
}
|
|
122
|
+
// Stacked segments read cleanest as flat solids — a per-segment gradient
|
|
123
|
+
// banding looks like a drop shadow between bands. Grouped bars keep the fade.
|
|
124
|
+
if (stacked) {
|
|
125
|
+
return entry.color;
|
|
126
|
+
}
|
|
127
|
+
return `url(#bar-${reactId}-${slot})`;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const renderValueLabel =
|
|
131
|
+
(color: string) =>
|
|
132
|
+
(props: {
|
|
133
|
+
x?: string | number;
|
|
134
|
+
y?: string | number;
|
|
135
|
+
width?: string | number;
|
|
136
|
+
value?: string | number | boolean | Array<string | number | boolean> | null;
|
|
137
|
+
}) => {
|
|
138
|
+
if (props.x == null || props.y == null) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
return (
|
|
142
|
+
<text
|
|
143
|
+
x={Number(props.x) + Number(props.width ?? 0) / 2}
|
|
144
|
+
y={Number(props.y) - 5}
|
|
145
|
+
textAnchor="middle"
|
|
146
|
+
fill={color}
|
|
147
|
+
fontFamily={theme.fontMono}
|
|
148
|
+
fontSize={10}
|
|
149
|
+
style={{ fontVariantNumeric: "tabular-nums" }}
|
|
150
|
+
>
|
|
151
|
+
{valueFormatter(Number(props.value ?? 0))}
|
|
152
|
+
</text>
|
|
153
|
+
);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const isEmpty = data.length === 0 || rendered.length === 0;
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<div data-slot="bar-chart" className={cn("flex w-full flex-col gap-3", className)}>
|
|
160
|
+
<div className="w-full" style={{ height }}>
|
|
161
|
+
{loading ? (
|
|
162
|
+
<div className="flex h-full items-center justify-center gap-2.5 font-mono text-quaternary-foreground text-text-xs">
|
|
163
|
+
<span className="size-4 animate-spin rounded-full border-2 border-border border-t-foreground" />
|
|
164
|
+
loading…
|
|
165
|
+
</div>
|
|
166
|
+
) : isEmpty ? (
|
|
167
|
+
<div className="flex h-full items-center justify-center rounded-lg border border-border border-dashed font-mono text-quaternary-foreground text-text-xs">
|
|
168
|
+
no data in range
|
|
169
|
+
</div>
|
|
170
|
+
) : (
|
|
171
|
+
<ResponsiveContainer width="100%" height="100%">
|
|
172
|
+
<RechartsBarChart
|
|
173
|
+
data={data as Record<string, string | number>[]}
|
|
174
|
+
stackOffset={variant === "expand" ? "expand" : "none"}
|
|
175
|
+
margin={margin}
|
|
176
|
+
barCategoryGap={stacked ? "20%" : "16%"}
|
|
177
|
+
>
|
|
178
|
+
<defs>
|
|
179
|
+
{!stacked &&
|
|
180
|
+
rendered.map((entry, slot) => (
|
|
181
|
+
<linearGradient key={entry.key} id={`bar-${reactId}-${slot}`} x1="0" y1="0" x2="0" y2="1">
|
|
182
|
+
<stop offset="0%" stopColor={entry.color} stopOpacity={0.95} />
|
|
183
|
+
<stop offset="100%" stopColor={entry.color} stopOpacity={0.5} />
|
|
184
|
+
</linearGradient>
|
|
185
|
+
))}
|
|
186
|
+
{texture && lead && (
|
|
187
|
+
<pattern
|
|
188
|
+
id={`bar-hatch-${reactId}`}
|
|
189
|
+
patternUnits="userSpaceOnUse"
|
|
190
|
+
width={6}
|
|
191
|
+
height={6}
|
|
192
|
+
patternTransform="rotate(45)"
|
|
193
|
+
>
|
|
194
|
+
<rect width={6} height={6} fill={lead.color} fillOpacity={0.22} />
|
|
195
|
+
<line x1={0} y1={0} x2={0} y2={6} stroke={lead.color} strokeWidth={1.2} strokeOpacity={0.65} />
|
|
196
|
+
</pattern>
|
|
197
|
+
)}
|
|
198
|
+
</defs>
|
|
199
|
+
|
|
200
|
+
<CartesianGrid vertical={false} stroke={theme.grid} strokeDasharray="2 4" />
|
|
201
|
+
<XAxis dataKey={index} {...axis} interval="preserveStartEnd" minTickGap={44} />
|
|
202
|
+
<YAxis
|
|
203
|
+
{...axis}
|
|
204
|
+
width={yAxisWidth}
|
|
205
|
+
domain={
|
|
206
|
+
referenceLine && variant !== "expand"
|
|
207
|
+
? [0, Math.ceil(Math.max(numericMax, referenceLine.y) * 1.15)]
|
|
208
|
+
: undefined
|
|
209
|
+
}
|
|
210
|
+
tickFormatter={(value: number) =>
|
|
211
|
+
variant === "expand" ? `${Math.round(value * 100)}%` : valueFormatter(value)
|
|
212
|
+
}
|
|
213
|
+
/>
|
|
214
|
+
<Tooltip
|
|
215
|
+
offset={12}
|
|
216
|
+
allowEscapeViewBox={{ x: false, y: false }}
|
|
217
|
+
cursor={{ fill: lead?.color ?? theme.mutedForeground, fillOpacity: theme.isDark ? 0.1 : 0.06 }}
|
|
218
|
+
content={
|
|
219
|
+
<ChartTooltipContent
|
|
220
|
+
valueFormatter={valueFormatter}
|
|
221
|
+
labelFormatter={labelFormatter}
|
|
222
|
+
showTotal={withTotal}
|
|
223
|
+
/>
|
|
224
|
+
}
|
|
225
|
+
/>
|
|
226
|
+
{referenceLine?.band && (
|
|
227
|
+
<ReferenceArea
|
|
228
|
+
y1={referenceLine.y}
|
|
229
|
+
y2={numericMax}
|
|
230
|
+
fill={theme.warning}
|
|
231
|
+
fillOpacity={0.06}
|
|
232
|
+
ifOverflow="extendDomain"
|
|
233
|
+
/>
|
|
234
|
+
)}
|
|
235
|
+
{referenceLine && (
|
|
236
|
+
<ReferenceLine
|
|
237
|
+
y={referenceLine.y}
|
|
238
|
+
stroke={theme.warning}
|
|
239
|
+
strokeDasharray="4 4"
|
|
240
|
+
strokeOpacity={0.6}
|
|
241
|
+
label={
|
|
242
|
+
referenceLine.label
|
|
243
|
+
? {
|
|
244
|
+
value: referenceLine.label,
|
|
245
|
+
fill: theme.warning,
|
|
246
|
+
fontSize: 9,
|
|
247
|
+
fontFamily: theme.fontMono,
|
|
248
|
+
position: "insideBottomRight",
|
|
249
|
+
}
|
|
250
|
+
: undefined
|
|
251
|
+
}
|
|
252
|
+
/>
|
|
253
|
+
)}
|
|
254
|
+
|
|
255
|
+
{rendered.map((entry, slot) => (
|
|
256
|
+
<Bar
|
|
257
|
+
key={entry.key}
|
|
258
|
+
dataKey={entry.key}
|
|
259
|
+
name={entry.name}
|
|
260
|
+
stackId={stacked ? "stack" : undefined}
|
|
261
|
+
fill={fillFor(entry, slot)}
|
|
262
|
+
stroke={entry.color}
|
|
263
|
+
strokeWidth={0}
|
|
264
|
+
radius={radiusFor(slot)}
|
|
265
|
+
maxBarSize={MAX_BAR_SIZE}
|
|
266
|
+
activeBar={{ fillOpacity: 1, stroke: theme.card, strokeWidth: 1 }}
|
|
267
|
+
isAnimationActive={animated}
|
|
268
|
+
animationDuration={650}
|
|
269
|
+
animationEasing="ease-out"
|
|
270
|
+
>
|
|
271
|
+
{valueLabels && (!stacked || rendered.length === 1) && (
|
|
272
|
+
<LabelList dataKey={entry.key} content={renderValueLabel(entry.color)} />
|
|
273
|
+
)}
|
|
274
|
+
</Bar>
|
|
275
|
+
))}
|
|
276
|
+
|
|
277
|
+
{markers?.map((marker) => (
|
|
278
|
+
<ReferenceDot
|
|
279
|
+
key={`${marker.x}-${marker.y}`}
|
|
280
|
+
x={marker.x}
|
|
281
|
+
y={marker.y}
|
|
282
|
+
r={3.5}
|
|
283
|
+
fill={marker.color ?? theme.foreground}
|
|
284
|
+
stroke={theme.card}
|
|
285
|
+
strokeWidth={2}
|
|
286
|
+
label={
|
|
287
|
+
marker.label
|
|
288
|
+
? {
|
|
289
|
+
value: marker.label,
|
|
290
|
+
fill: marker.color ?? theme.foreground,
|
|
291
|
+
fontSize: 9,
|
|
292
|
+
fontFamily: theme.fontMono,
|
|
293
|
+
position: "top",
|
|
294
|
+
}
|
|
295
|
+
: undefined
|
|
296
|
+
}
|
|
297
|
+
/>
|
|
298
|
+
))}
|
|
299
|
+
</RechartsBarChart>
|
|
300
|
+
</ResponsiveContainer>
|
|
301
|
+
)}
|
|
302
|
+
</div>
|
|
303
|
+
|
|
304
|
+
{legend && !isEmpty && <ChartLegend items={legendItems} style={{ paddingLeft: yAxisWidth }} />}
|
|
305
|
+
</div>
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export { BarChart };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion";
|
|
6
|
+
import { formatShare } from "../lib/chart";
|
|
7
|
+
import type { ChartPaletteName } from "../lib/chart-palette";
|
|
8
|
+
import { rampColor } from "../lib/chart-palette";
|
|
9
|
+
import { cn } from "../lib/cn";
|
|
10
|
+
import { useChartContext } from "./chart-container";
|
|
11
|
+
|
|
12
|
+
export interface BarListProps {
|
|
13
|
+
data: ReadonlyArray<Record<string, string | number | null | undefined>>;
|
|
14
|
+
index: string;
|
|
15
|
+
dataKey?: string;
|
|
16
|
+
maxItems?: number;
|
|
17
|
+
palette?: ChartPaletteName;
|
|
18
|
+
loading?: boolean;
|
|
19
|
+
valueFormatter?: (value: number) => string;
|
|
20
|
+
labelFormatter?: (label: string | number) => string;
|
|
21
|
+
className?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const PLACEHOLDER_HEIGHT = 168;
|
|
25
|
+
// Cap the ramp off its palest end so low-rank bars stay legible on a light card.
|
|
26
|
+
const RAMP_CEILING = 0.8;
|
|
27
|
+
const IN_FILL_SHADOW = "0 1px 2px rgb(0 0 0 / 0.28)";
|
|
28
|
+
|
|
29
|
+
function BarList({
|
|
30
|
+
data,
|
|
31
|
+
index,
|
|
32
|
+
dataKey = "value",
|
|
33
|
+
maxItems,
|
|
34
|
+
palette,
|
|
35
|
+
loading = false,
|
|
36
|
+
valueFormatter = (value) => value.toLocaleString("en-US"),
|
|
37
|
+
labelFormatter,
|
|
38
|
+
className,
|
|
39
|
+
}: BarListProps) {
|
|
40
|
+
const { paletteName } = useChartContext(palette);
|
|
41
|
+
const reducedMotion = useReducedMotion();
|
|
42
|
+
const [mounted, setMounted] = React.useState(false);
|
|
43
|
+
const [active, setActive] = React.useState<number | null>(null);
|
|
44
|
+
|
|
45
|
+
React.useEffect(() => {
|
|
46
|
+
setMounted(true);
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
const total = React.useMemo(() => data.reduce((sum, row) => sum + (Number(row[dataKey]) || 0), 0), [data, dataKey]);
|
|
50
|
+
|
|
51
|
+
const rows = React.useMemo(() => {
|
|
52
|
+
const mapped = data.map((row) => ({
|
|
53
|
+
name: String(row[index] ?? ""),
|
|
54
|
+
value: Number(row[dataKey]) || 0,
|
|
55
|
+
}));
|
|
56
|
+
mapped.sort((lower, upper) => upper.value - lower.value);
|
|
57
|
+
const capped = maxItems ? mapped.slice(0, maxItems) : mapped;
|
|
58
|
+
return capped.map((row, rank) => ({
|
|
59
|
+
...row,
|
|
60
|
+
color: rampColor(paletteName, (capped.length > 1 ? rank / (capped.length - 1) : 0) * RAMP_CEILING),
|
|
61
|
+
}));
|
|
62
|
+
}, [data, index, dataKey, maxItems, paletteName]);
|
|
63
|
+
|
|
64
|
+
const maxValue = rows.reduce((max, row) => (row.value > max ? row.value : max), 0);
|
|
65
|
+
const isEmpty = rows.length === 0;
|
|
66
|
+
const formatName = (name: string) => (labelFormatter ? labelFormatter(name) : name);
|
|
67
|
+
|
|
68
|
+
if (loading) {
|
|
69
|
+
return (
|
|
70
|
+
<div
|
|
71
|
+
className={cn(
|
|
72
|
+
"flex items-center justify-center gap-2.5 font-mono text-quaternary-foreground text-text-xs",
|
|
73
|
+
className,
|
|
74
|
+
)}
|
|
75
|
+
style={{ minHeight: PLACEHOLDER_HEIGHT }}
|
|
76
|
+
>
|
|
77
|
+
<span className="size-4 animate-spin rounded-full border-2 border-border border-t-foreground" />
|
|
78
|
+
loading…
|
|
79
|
+
</div>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (isEmpty) {
|
|
84
|
+
return (
|
|
85
|
+
<div
|
|
86
|
+
className={cn(
|
|
87
|
+
"flex items-center justify-center rounded-lg border border-border border-dashed font-mono text-quaternary-foreground text-text-xs",
|
|
88
|
+
className,
|
|
89
|
+
)}
|
|
90
|
+
style={{ minHeight: PLACEHOLDER_HEIGHT }}
|
|
91
|
+
>
|
|
92
|
+
no data in range
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<div data-slot="bar-list" className={cn("flex w-full flex-col gap-2", className)}>
|
|
99
|
+
{rows.map((row, slot) => {
|
|
100
|
+
const fraction = maxValue > 0 ? row.value / maxValue : 0;
|
|
101
|
+
const fillWidth = !reducedMotion && !mounted ? "0%" : `${fraction * 100}%`;
|
|
102
|
+
const dimmed = active !== null && active !== slot;
|
|
103
|
+
const isActive = active === slot;
|
|
104
|
+
return (
|
|
105
|
+
// biome-ignore lint/a11y/noStaticElementInteractions: decorative hover-sync; all values are visible without it
|
|
106
|
+
<div
|
|
107
|
+
key={row.name}
|
|
108
|
+
onMouseEnter={() => setActive(slot)}
|
|
109
|
+
onMouseLeave={() => setActive(null)}
|
|
110
|
+
className="flex items-center gap-3"
|
|
111
|
+
>
|
|
112
|
+
<div className="relative h-[30px] flex-1 overflow-hidden rounded-md bg-muted">
|
|
113
|
+
<span className="pointer-events-none absolute inset-x-3 top-1/2 z-0 -translate-y-1/2 truncate type-text-xs font-medium text-foreground">
|
|
114
|
+
{formatName(row.name)}
|
|
115
|
+
</span>
|
|
116
|
+
<div
|
|
117
|
+
className="absolute inset-y-0 left-0 z-10 overflow-hidden rounded-md motion-safe:transition-[width,opacity] motion-safe:duration-700 motion-safe:ease-out"
|
|
118
|
+
style={{
|
|
119
|
+
width: fillWidth,
|
|
120
|
+
background: `linear-gradient(90deg, ${row.color}, color-mix(in oklab, ${row.color} 82%, transparent))`,
|
|
121
|
+
boxShadow: `inset 0 0 0 1px ${row.color}`,
|
|
122
|
+
opacity: dimmed ? 0.45 : 1,
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
<span
|
|
126
|
+
className="pointer-events-none absolute top-1/2 -translate-y-1/2 truncate type-text-xs font-medium text-white"
|
|
127
|
+
style={{
|
|
128
|
+
left: 12,
|
|
129
|
+
width: `calc(${100 / Math.max(fraction, 0.0001)}% - 24px)`,
|
|
130
|
+
textShadow: IN_FILL_SHADOW,
|
|
131
|
+
}}
|
|
132
|
+
>
|
|
133
|
+
{formatName(row.name)}
|
|
134
|
+
</span>
|
|
135
|
+
</div>
|
|
136
|
+
</div>
|
|
137
|
+
<span
|
|
138
|
+
className="w-16 shrink-0 text-right font-mono text-text-xs tabular-nums motion-safe:transition-colors"
|
|
139
|
+
style={{ color: isActive ? row.color : undefined }}
|
|
140
|
+
>
|
|
141
|
+
{isActive ? formatShare(total > 0 ? row.value / total : 0) : valueFormatter(row.value)}
|
|
142
|
+
</span>
|
|
143
|
+
</div>
|
|
144
|
+
);
|
|
145
|
+
})}
|
|
146
|
+
</div>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export { BarList };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { CHART_PALETTES, type ChartPaletteName } from "../lib/chart-palette";
|
|
6
|
+
import { cn } from "../lib/cn";
|
|
7
|
+
import { ChartContainer } from "./chart-container";
|
|
8
|
+
|
|
9
|
+
export interface ChartCardProps extends Omit<React.ComponentProps<"section">, "title"> {
|
|
10
|
+
palette?: ChartPaletteName;
|
|
11
|
+
kicker?: React.ReactNode;
|
|
12
|
+
title?: React.ReactNode;
|
|
13
|
+
description?: React.ReactNode;
|
|
14
|
+
action?: React.ReactNode;
|
|
15
|
+
accent?: "top" | "left";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function ChartCard({
|
|
19
|
+
palette = "magenta",
|
|
20
|
+
kicker,
|
|
21
|
+
title,
|
|
22
|
+
description,
|
|
23
|
+
action,
|
|
24
|
+
accent = "top",
|
|
25
|
+
className,
|
|
26
|
+
children,
|
|
27
|
+
...props
|
|
28
|
+
}: ChartCardProps) {
|
|
29
|
+
// biome-ignore lint/style/noNonNullAssertion: palettes are never empty
|
|
30
|
+
const lead = CHART_PALETTES[palette][0]!;
|
|
31
|
+
const isLeft = accent === "left";
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<section
|
|
35
|
+
data-slot="chart-card"
|
|
36
|
+
className={cn(
|
|
37
|
+
"chart-rise relative overflow-hidden rounded-xl border bg-card p-5 text-card-foreground shadow-shadow",
|
|
38
|
+
isLeft && "pl-6",
|
|
39
|
+
className,
|
|
40
|
+
)}
|
|
41
|
+
{...props}
|
|
42
|
+
>
|
|
43
|
+
<span
|
|
44
|
+
aria-hidden
|
|
45
|
+
className={cn("absolute", isLeft ? "inset-y-0 left-0 w-[3px]" : "inset-x-0 top-0 h-[3px]")}
|
|
46
|
+
style={{ background: lead }}
|
|
47
|
+
/>
|
|
48
|
+
{(kicker || title || action) && (
|
|
49
|
+
<header className="mb-4 flex items-start justify-between gap-4">
|
|
50
|
+
<div className="flex flex-col gap-1">
|
|
51
|
+
{kicker && <span className="font-mono text-[10px] text-primary uppercase tracking-[0.18em]">{kicker}</span>}
|
|
52
|
+
{title && <h3 className="type-text-md font-semibold leading-none tracking-tight">{title}</h3>}
|
|
53
|
+
{description && <p className="type-text-xs text-muted-foreground">{description}</p>}
|
|
54
|
+
</div>
|
|
55
|
+
{action}
|
|
56
|
+
</header>
|
|
57
|
+
)}
|
|
58
|
+
<ChartContainer palette={palette}>{children}</ChartContainer>
|
|
59
|
+
</section>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { ChartCard };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { type ChartTheme, useChartTheme } from "../hooks/use-chart-theme";
|
|
6
|
+
import { CHART_PALETTES, type ChartPaletteName } from "../lib/chart-palette";
|
|
7
|
+
import { cn } from "../lib/cn";
|
|
8
|
+
|
|
9
|
+
interface ChartContextValue {
|
|
10
|
+
palette: readonly string[];
|
|
11
|
+
paletteName: ChartPaletteName;
|
|
12
|
+
theme: ChartTheme;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const ChartContext = React.createContext<ChartContextValue | null>(null);
|
|
16
|
+
|
|
17
|
+
function ChartContainer({
|
|
18
|
+
palette = "magenta",
|
|
19
|
+
className,
|
|
20
|
+
...props
|
|
21
|
+
}: React.ComponentProps<"div"> & { palette?: ChartPaletteName }) {
|
|
22
|
+
const theme = useChartTheme();
|
|
23
|
+
const value = React.useMemo<ChartContextValue>(
|
|
24
|
+
() => ({ palette: CHART_PALETTES[palette], paletteName: palette, theme }),
|
|
25
|
+
[palette, theme],
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<ChartContext.Provider value={value}>
|
|
30
|
+
<div data-slot="chart-container" className={cn("flex flex-col gap-4", className)} {...props} />
|
|
31
|
+
</ChartContext.Provider>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function useChartContext(paletteOverride?: ChartPaletteName): ChartContextValue {
|
|
36
|
+
const provided = React.useContext(ChartContext);
|
|
37
|
+
const fallbackTheme = useChartTheme();
|
|
38
|
+
if (provided && !paletteOverride) {
|
|
39
|
+
return provided;
|
|
40
|
+
}
|
|
41
|
+
const paletteName = paletteOverride ?? provided?.paletteName ?? "magenta";
|
|
42
|
+
return {
|
|
43
|
+
palette: CHART_PALETTES[paletteName],
|
|
44
|
+
paletteName,
|
|
45
|
+
theme: provided?.theme ?? fallbackTheme,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { ChartContainer, type ChartContextValue, useChartContext };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { cn } from "../lib/cn";
|
|
4
|
+
|
|
5
|
+
export interface ChartLegendItem {
|
|
6
|
+
name: string;
|
|
7
|
+
color: string;
|
|
8
|
+
dashed?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ChartLegendProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
12
|
+
items: ChartLegendItem[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function Swatch({ color, dashed }: { color: string; dashed?: boolean }) {
|
|
16
|
+
return (
|
|
17
|
+
<span
|
|
18
|
+
aria-hidden
|
|
19
|
+
className="size-2 shrink-0 rounded-full"
|
|
20
|
+
style={dashed ? { border: `1.5px solid ${color}` } : { background: color }}
|
|
21
|
+
/>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function ChartLegend({ items, className, ...props }: ChartLegendProps) {
|
|
26
|
+
return (
|
|
27
|
+
<div className={cn("flex flex-wrap gap-x-4 gap-y-1.5", className)} {...props}>
|
|
28
|
+
{items.map((item) => (
|
|
29
|
+
<span
|
|
30
|
+
key={item.name}
|
|
31
|
+
className="inline-flex items-center gap-1.5 font-mono text-[10px] text-muted-foreground uppercase tracking-[0.12em]"
|
|
32
|
+
>
|
|
33
|
+
<Swatch color={item.color} dashed={item.dashed} />
|
|
34
|
+
{item.name}
|
|
35
|
+
</span>
|
|
36
|
+
))}
|
|
37
|
+
</div>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { ChartLegend };
|