@arolariu/components 0.0.31 → 0.0.32
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/LICENSE +21 -0
- package/dist/cjs/components/ui/alert-dialog.cjs.map +1 -1
- package/dist/cjs/components/ui/alert.cjs +1 -1
- package/dist/cjs/components/ui/alert.cjs.map +1 -1
- package/dist/cjs/components/ui/aspect-ratio.cjs.map +1 -1
- package/dist/cjs/components/ui/avatar.cjs.map +1 -1
- package/dist/cjs/components/ui/badge.cjs.map +1 -1
- package/dist/cjs/components/ui/breadcrumb.cjs.map +1 -1
- package/dist/cjs/components/ui/button.cjs.map +1 -1
- package/dist/cjs/components/ui/calendar.cjs.map +1 -1
- package/dist/cjs/components/ui/card.cjs.map +1 -1
- package/dist/cjs/components/ui/carousel.cjs.map +1 -1
- package/dist/cjs/components/ui/chart.cjs.map +1 -1
- package/dist/cjs/components/ui/checkbox.cjs.map +1 -1
- package/dist/cjs/components/ui/collapsible.cjs.map +1 -1
- package/dist/cjs/components/ui/command.cjs.map +1 -1
- package/dist/cjs/components/ui/context-menu.cjs.map +1 -1
- package/dist/cjs/components/ui/drawer.cjs.map +1 -1
- package/dist/cjs/components/ui/dropdown-menu.cjs.map +1 -1
- package/dist/cjs/components/ui/form.cjs.map +1 -1
- package/dist/cjs/components/ui/hover-card.cjs.map +1 -1
- package/dist/cjs/components/ui/input-otp.cjs.map +1 -1
- package/dist/cjs/components/ui/input.cjs.map +1 -1
- package/dist/cjs/components/ui/label.cjs.map +1 -1
- package/dist/cjs/components/ui/menubar.cjs.map +1 -1
- package/dist/cjs/components/ui/navigation-menu.cjs.map +1 -1
- package/dist/cjs/components/ui/pagination.cjs.map +1 -1
- package/dist/cjs/components/ui/popover.cjs.map +1 -1
- package/dist/cjs/components/ui/progress.cjs.map +1 -1
- package/dist/cjs/components/ui/radio-group.cjs.map +1 -1
- package/dist/cjs/components/ui/resizable.cjs.map +1 -1
- package/dist/cjs/components/ui/scroll-area.cjs.map +1 -1
- package/dist/cjs/components/ui/select.cjs.map +1 -1
- package/dist/cjs/components/ui/separator.cjs.map +1 -1
- package/dist/cjs/components/ui/sheet.cjs.map +1 -1
- package/dist/cjs/components/ui/sidebar.cjs.map +1 -1
- package/dist/cjs/components/ui/skeleton.cjs.map +1 -1
- package/dist/cjs/components/ui/slider.cjs.map +1 -1
- package/dist/cjs/components/ui/sonner.cjs.map +1 -1
- package/dist/cjs/components/ui/switch.cjs.map +1 -1
- package/dist/cjs/components/ui/table.cjs.map +1 -1
- package/dist/cjs/components/ui/tabs.cjs.map +1 -1
- package/dist/cjs/components/ui/textarea.cjs.map +1 -1
- package/dist/cjs/components/ui/toggle-group.cjs.map +1 -1
- package/dist/cjs/components/ui/toggle.cjs.map +1 -1
- package/dist/cjs/components/ui/tooltip.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.css +1 -0
- package/dist/esm/components/ui/alert-dialog.js.map +1 -1
- package/dist/esm/components/ui/alert.js +8 -8
- package/dist/esm/components/ui/alert.js.map +1 -1
- package/dist/esm/components/ui/aspect-ratio.js.map +1 -1
- package/dist/esm/components/ui/avatar.js.map +1 -1
- package/dist/esm/components/ui/badge.js.map +1 -1
- package/dist/esm/components/ui/breadcrumb.js.map +1 -1
- package/dist/esm/components/ui/button.js.map +1 -1
- package/dist/esm/components/ui/calendar.js.map +1 -1
- package/dist/esm/components/ui/card.js.map +1 -1
- package/dist/esm/components/ui/carousel.js.map +1 -1
- package/dist/esm/components/ui/chart.js.map +1 -1
- package/dist/esm/components/ui/checkbox.js.map +1 -1
- package/dist/esm/components/ui/collapsible.js.map +1 -1
- package/dist/esm/components/ui/command.js.map +1 -1
- package/dist/esm/components/ui/context-menu.js.map +1 -1
- package/dist/esm/components/ui/drawer.js.map +1 -1
- package/dist/esm/components/ui/dropdown-menu.js.map +1 -1
- package/dist/esm/components/ui/form.js.map +1 -1
- package/dist/esm/components/ui/hover-card.js.map +1 -1
- package/dist/esm/components/ui/input-otp.js.map +1 -1
- package/dist/esm/components/ui/input.js.map +1 -1
- package/dist/esm/components/ui/label.js.map +1 -1
- package/dist/esm/components/ui/menubar.js.map +1 -1
- package/dist/esm/components/ui/navigation-menu.js.map +1 -1
- package/dist/esm/components/ui/pagination.js.map +1 -1
- package/dist/esm/components/ui/popover.js.map +1 -1
- package/dist/esm/components/ui/progress.js.map +1 -1
- package/dist/esm/components/ui/radio-group.js.map +1 -1
- package/dist/esm/components/ui/resizable.js.map +1 -1
- package/dist/esm/components/ui/scroll-area.js.map +1 -1
- package/dist/esm/components/ui/select.js.map +1 -1
- package/dist/esm/components/ui/separator.js.map +1 -1
- package/dist/esm/components/ui/sheet.js.map +1 -1
- package/dist/esm/components/ui/sidebar.js.map +1 -1
- package/dist/esm/components/ui/skeleton.js.map +1 -1
- package/dist/esm/components/ui/slider.js.map +1 -1
- package/dist/esm/components/ui/sonner.js.map +1 -1
- package/dist/esm/components/ui/switch.js.map +1 -1
- package/dist/esm/components/ui/table.js.map +1 -1
- package/dist/esm/components/ui/tabs.js.map +1 -1
- package/dist/esm/components/ui/textarea.js.map +1 -1
- package/dist/esm/components/ui/toggle-group.js.map +1 -1
- package/dist/esm/components/ui/toggle.js.map +1 -1
- package/dist/esm/components/ui/tooltip.js.map +1 -1
- package/dist/esm/index.css +1 -0
- package/dist/esm/index.js +294 -293
- package/dist/esm/index.js.map +1 -1
- package/dist/index.css +1 -0
- package/dist/index.js +298 -0
- package/dist/types/components/ui/alert.d.ts.map +1 -1
- package/dist/types/components/ui/form.d.ts.map +1 -1
- package/package.json +76 -45
- package/src/components/ui/alert-dialog.tsx +160 -160
- package/src/components/ui/alert.tsx +2 -0
- package/src/components/ui/aspect-ratio.tsx +12 -12
- package/src/components/ui/avatar.tsx +52 -52
- package/src/components/ui/badge.tsx +47 -47
- package/src/components/ui/breadcrumb.tsx +116 -116
- package/src/components/ui/button.tsx +60 -60
- package/src/components/ui/calendar.tsx +75 -75
- package/src/components/ui/card.tsx +79 -79
- package/src/components/ui/carousel.tsx +240 -240
- package/src/components/ui/chart.tsx +352 -352
- package/src/components/ui/checkbox.tsx +31 -31
- package/src/components/ui/collapsible.tsx +34 -34
- package/src/components/ui/command.tsx +176 -176
- package/src/components/ui/context-menu.tsx +254 -254
- package/src/components/ui/drawer.tsx +137 -137
- package/src/components/ui/dropdown-menu.tsx +259 -259
- package/src/components/ui/form.tsx +173 -173
- package/src/components/ui/hover-card.tsx +41 -41
- package/src/components/ui/input-otp.tsx +77 -77
- package/src/components/ui/input.tsx +22 -22
- package/src/components/ui/label.tsx +23 -23
- package/src/components/ui/menubar.tsx +278 -278
- package/src/components/ui/navigation-menu.tsx +170 -170
- package/src/components/ui/pagination.tsx +128 -128
- package/src/components/ui/popover.tsx +47 -47
- package/src/components/ui/progress.tsx +30 -30
- package/src/components/ui/radio-group.tsx +44 -44
- package/src/components/ui/resizable.tsx +55 -55
- package/src/components/ui/scroll-area.tsx +57 -57
- package/src/components/ui/select.tsx +183 -183
- package/src/components/ui/separator.tsx +27 -27
- package/src/components/ui/sheet.tsx +144 -144
- package/src/components/ui/sidebar.tsx +725 -725
- package/src/components/ui/skeleton.tsx +17 -17
- package/src/components/ui/slider.tsx +62 -62
- package/src/components/ui/sonner.tsx +30 -30
- package/src/components/ui/switch.tsx +30 -30
- package/src/components/ui/table.tsx +118 -118
- package/src/components/ui/tabs.tsx +65 -65
- package/src/components/ui/textarea.tsx +19 -19
- package/src/components/ui/toggle-group.tsx +72 -72
- package/src/components/ui/toggle.tsx +46 -46
- package/src/components/ui/tooltip.tsx +60 -60
- package/src/index.css +3 -107
- package/src/index.ts +2 -0
- package/tailwind.config.mjs +67 -0
- package/tsconfig.json +4 -1
|
@@ -1,352 +1,352 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import * as React from "react";
|
|
4
|
-
import * as RechartsPrimitive from "recharts";
|
|
5
|
-
import { cn } from "./../../lib/utils";
|
|
6
|
-
|
|
7
|
-
// Format: { THEME_NAME: CSS_SELECTOR }
|
|
8
|
-
const THEMES = { light: "", dark: ".dark" } as const;
|
|
9
|
-
|
|
10
|
-
export type ChartConfig = {
|
|
11
|
-
[k in string]: {
|
|
12
|
-
label?: React.ReactNode;
|
|
13
|
-
icon?: React.ComponentType;
|
|
14
|
-
} & (
|
|
15
|
-
| { color?: string; theme?: never }
|
|
16
|
-
| { color?: never; theme: Record<keyof typeof THEMES, string> }
|
|
17
|
-
);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
type ChartContextProps = {
|
|
21
|
-
config: ChartConfig;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const ChartContext = React.createContext<ChartContextProps | null>(null);
|
|
25
|
-
|
|
26
|
-
function useChart() {
|
|
27
|
-
const context = React.useContext(ChartContext);
|
|
28
|
-
|
|
29
|
-
if (!context) {
|
|
30
|
-
throw new Error("useChart must be used within a <ChartContainer />");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return context;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function ChartContainer({
|
|
37
|
-
id,
|
|
38
|
-
className,
|
|
39
|
-
children,
|
|
40
|
-
config,
|
|
41
|
-
...props
|
|
42
|
-
}: React.ComponentProps<"div"> & {
|
|
43
|
-
config: ChartConfig;
|
|
44
|
-
children: React.ComponentProps<
|
|
45
|
-
typeof RechartsPrimitive.ResponsiveContainer
|
|
46
|
-
>["children"];
|
|
47
|
-
}) {
|
|
48
|
-
const uniqueId = React.useId();
|
|
49
|
-
const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`;
|
|
50
|
-
|
|
51
|
-
return (
|
|
52
|
-
<ChartContext.Provider value={{ config }}>
|
|
53
|
-
<div
|
|
54
|
-
data-slot="chart"
|
|
55
|
-
data-chart={chartId}
|
|
56
|
-
className={cn(
|
|
57
|
-
"[&_.recharts-cartesian-axis-tick_text]:fill-muted-foreground [&_.recharts-cartesian-grid_line[stroke='#ccc']]:stroke-border/50 [&_.recharts-curve.recharts-tooltip-cursor]:stroke-border [&_.recharts-polar-grid_[stroke='#ccc']]:stroke-border [&_.recharts-radial-bar-background-sector]:fill-muted [&_.recharts-rectangle.recharts-tooltip-cursor]:fill-muted [&_.recharts-reference-line_[stroke='#ccc']]:stroke-border flex aspect-video justify-center text-xs [&_.recharts-dot[stroke='#fff']]:stroke-transparent [&_.recharts-layer]:outline-hidden [&_.recharts-sector]:outline-hidden [&_.recharts-sector[stroke='#fff']]:stroke-transparent [&_.recharts-surface]:outline-hidden",
|
|
58
|
-
className
|
|
59
|
-
)}
|
|
60
|
-
{...props}
|
|
61
|
-
>
|
|
62
|
-
<ChartStyle id={chartId} config={config} />
|
|
63
|
-
<RechartsPrimitive.ResponsiveContainer>
|
|
64
|
-
{children}
|
|
65
|
-
</RechartsPrimitive.ResponsiveContainer>
|
|
66
|
-
</div>
|
|
67
|
-
</ChartContext.Provider>
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
|
|
72
|
-
const colorConfig = Object.entries(config).filter(
|
|
73
|
-
([, config]) => config.theme || config.color
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
if (!colorConfig.length) {
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return (
|
|
81
|
-
<style
|
|
82
|
-
dangerouslySetInnerHTML={{
|
|
83
|
-
__html: Object.entries(THEMES)
|
|
84
|
-
.map(
|
|
85
|
-
([theme, prefix]) => `
|
|
86
|
-
${prefix} [data-chart=${id}] {
|
|
87
|
-
${colorConfig
|
|
88
|
-
.map(([key, itemConfig]) => {
|
|
89
|
-
const color =
|
|
90
|
-
itemConfig.theme?.[theme as keyof typeof itemConfig.theme] ||
|
|
91
|
-
itemConfig.color;
|
|
92
|
-
return color ? ` --color-${key}: ${color};` : null;
|
|
93
|
-
})
|
|
94
|
-
.join("\n")}
|
|
95
|
-
}
|
|
96
|
-
`
|
|
97
|
-
)
|
|
98
|
-
.join("\n"),
|
|
99
|
-
}}
|
|
100
|
-
/>
|
|
101
|
-
);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const ChartTooltip = RechartsPrimitive.Tooltip;
|
|
105
|
-
|
|
106
|
-
function ChartTooltipContent({
|
|
107
|
-
active,
|
|
108
|
-
payload,
|
|
109
|
-
className,
|
|
110
|
-
indicator = "dot",
|
|
111
|
-
hideLabel = false,
|
|
112
|
-
hideIndicator = false,
|
|
113
|
-
label,
|
|
114
|
-
labelFormatter,
|
|
115
|
-
labelClassName,
|
|
116
|
-
formatter,
|
|
117
|
-
color,
|
|
118
|
-
nameKey,
|
|
119
|
-
labelKey,
|
|
120
|
-
}: React.ComponentProps<typeof RechartsPrimitive.Tooltip> &
|
|
121
|
-
React.ComponentProps<"div"> & {
|
|
122
|
-
hideLabel?: boolean;
|
|
123
|
-
hideIndicator?: boolean;
|
|
124
|
-
indicator?: "line" | "dot" | "dashed";
|
|
125
|
-
nameKey?: string;
|
|
126
|
-
labelKey?: string;
|
|
127
|
-
}) {
|
|
128
|
-
const { config } = useChart();
|
|
129
|
-
|
|
130
|
-
const tooltipLabel = React.useMemo(() => {
|
|
131
|
-
if (hideLabel || !payload?.length) {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const [item] = payload;
|
|
136
|
-
const key = `${labelKey || item?.dataKey || item?.name || "value"}`;
|
|
137
|
-
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
138
|
-
const value =
|
|
139
|
-
!labelKey && typeof label === "string"
|
|
140
|
-
? config[label as keyof typeof config]?.label || label
|
|
141
|
-
: itemConfig?.label;
|
|
142
|
-
|
|
143
|
-
if (labelFormatter) {
|
|
144
|
-
return (
|
|
145
|
-
<div className={cn("font-medium", labelClassName)}>
|
|
146
|
-
{labelFormatter(value, payload)}
|
|
147
|
-
</div>
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (!value) {
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return <div className={cn("font-medium", labelClassName)}>{value}</div>;
|
|
156
|
-
}, [
|
|
157
|
-
label,
|
|
158
|
-
labelFormatter,
|
|
159
|
-
payload,
|
|
160
|
-
hideLabel,
|
|
161
|
-
labelClassName,
|
|
162
|
-
config,
|
|
163
|
-
labelKey,
|
|
164
|
-
]);
|
|
165
|
-
|
|
166
|
-
if (!active || !payload?.length) {
|
|
167
|
-
return null;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const nestLabel = payload.length === 1 && indicator !== "dot";
|
|
171
|
-
|
|
172
|
-
return (
|
|
173
|
-
<div
|
|
174
|
-
className={cn(
|
|
175
|
-
"border-neutral-200/50 bg-white grid min-w-[8rem] items-start gap-1.5 rounded-lg border border-neutral-200 px-2.5 py-1.5 text-xs shadow-xl dark:border-neutral-800/50 dark:bg-neutral-950 dark:border-neutral-800",
|
|
176
|
-
className
|
|
177
|
-
)}
|
|
178
|
-
>
|
|
179
|
-
{!nestLabel ? tooltipLabel : null}
|
|
180
|
-
<div className="grid gap-1.5">
|
|
181
|
-
{payload.map((item, index) => {
|
|
182
|
-
const key = `${nameKey || item.name || item.dataKey || "value"}`;
|
|
183
|
-
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
184
|
-
const indicatorColor = color || item.payload.fill || item.color;
|
|
185
|
-
|
|
186
|
-
return (
|
|
187
|
-
<div
|
|
188
|
-
key={item.dataKey}
|
|
189
|
-
className={cn(
|
|
190
|
-
"[&>svg]:text-neutral-500 flex w-full flex-wrap items-stretch gap-2 [&>svg]:h-2.5 [&>svg]:w-2.5 dark:[&>svg]:text-neutral-400",
|
|
191
|
-
indicator === "dot" && "items-center"
|
|
192
|
-
)}
|
|
193
|
-
>
|
|
194
|
-
{formatter && item?.value !== undefined && item.name ? (
|
|
195
|
-
formatter(item.value, item.name, item, index, item.payload)
|
|
196
|
-
) : (
|
|
197
|
-
<>
|
|
198
|
-
{itemConfig?.icon ? (
|
|
199
|
-
<itemConfig.icon />
|
|
200
|
-
) : (
|
|
201
|
-
!hideIndicator && (
|
|
202
|
-
<div
|
|
203
|
-
className={cn(
|
|
204
|
-
"shrink-0 rounded-[2px] border-(--color-border) bg-(--color-bg)",
|
|
205
|
-
{
|
|
206
|
-
"h-2.5 w-2.5": indicator === "dot",
|
|
207
|
-
"w-1": indicator === "line",
|
|
208
|
-
"w-0 border-[1.5px] border-dashed bg-transparent":
|
|
209
|
-
indicator === "dashed",
|
|
210
|
-
"my-0.5": nestLabel && indicator === "dashed",
|
|
211
|
-
}
|
|
212
|
-
)}
|
|
213
|
-
style={
|
|
214
|
-
{
|
|
215
|
-
"--color-bg": indicatorColor,
|
|
216
|
-
"--color-border": indicatorColor,
|
|
217
|
-
} as React.CSSProperties
|
|
218
|
-
}
|
|
219
|
-
/>
|
|
220
|
-
)
|
|
221
|
-
)}
|
|
222
|
-
<div
|
|
223
|
-
className={cn(
|
|
224
|
-
"flex flex-1 justify-between leading-none",
|
|
225
|
-
nestLabel ? "items-end" : "items-center"
|
|
226
|
-
)}
|
|
227
|
-
>
|
|
228
|
-
<div className="grid gap-1.5">
|
|
229
|
-
{nestLabel ? tooltipLabel : null}
|
|
230
|
-
<span className="text-neutral-500 dark:text-neutral-400">
|
|
231
|
-
{itemConfig?.label || item.name}
|
|
232
|
-
</span>
|
|
233
|
-
</div>
|
|
234
|
-
{item.value && (
|
|
235
|
-
<span className="text-neutral-950 font-mono font-medium tabular-nums dark:text-neutral-50">
|
|
236
|
-
{item.value.toLocaleString()}
|
|
237
|
-
</span>
|
|
238
|
-
)}
|
|
239
|
-
</div>
|
|
240
|
-
</>
|
|
241
|
-
)}
|
|
242
|
-
</div>
|
|
243
|
-
);
|
|
244
|
-
})}
|
|
245
|
-
</div>
|
|
246
|
-
</div>
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const ChartLegend = RechartsPrimitive.Legend;
|
|
251
|
-
|
|
252
|
-
function ChartLegendContent({
|
|
253
|
-
className,
|
|
254
|
-
hideIcon = false,
|
|
255
|
-
payload,
|
|
256
|
-
verticalAlign = "bottom",
|
|
257
|
-
nameKey,
|
|
258
|
-
}: React.ComponentProps<"div"> &
|
|
259
|
-
Pick<RechartsPrimitive.LegendProps, "payload" | "verticalAlign"> & {
|
|
260
|
-
hideIcon?: boolean;
|
|
261
|
-
nameKey?: string;
|
|
262
|
-
}) {
|
|
263
|
-
const { config } = useChart();
|
|
264
|
-
|
|
265
|
-
if (!payload?.length) {
|
|
266
|
-
return null;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
return (
|
|
270
|
-
<div
|
|
271
|
-
className={cn(
|
|
272
|
-
"flex items-center justify-center gap-4",
|
|
273
|
-
verticalAlign === "top" ? "pb-3" : "pt-3",
|
|
274
|
-
className
|
|
275
|
-
)}
|
|
276
|
-
>
|
|
277
|
-
{payload.map((item) => {
|
|
278
|
-
const key = `${nameKey || item.dataKey || "value"}`;
|
|
279
|
-
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
280
|
-
|
|
281
|
-
return (
|
|
282
|
-
<div
|
|
283
|
-
key={item.value}
|
|
284
|
-
className={cn(
|
|
285
|
-
"[&>svg]:text-neutral-500 flex items-center gap-1.5 [&>svg]:h-3 [&>svg]:w-3 dark:[&>svg]:text-neutral-400"
|
|
286
|
-
)}
|
|
287
|
-
>
|
|
288
|
-
{itemConfig?.icon && !hideIcon ? (
|
|
289
|
-
<itemConfig.icon />
|
|
290
|
-
) : (
|
|
291
|
-
<div
|
|
292
|
-
className="h-2 w-2 shrink-0 rounded-[2px]"
|
|
293
|
-
style={{
|
|
294
|
-
backgroundColor: item.color,
|
|
295
|
-
}}
|
|
296
|
-
/>
|
|
297
|
-
)}
|
|
298
|
-
{itemConfig?.label}
|
|
299
|
-
</div>
|
|
300
|
-
);
|
|
301
|
-
})}
|
|
302
|
-
</div>
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// Helper to extract item config from a payload.
|
|
307
|
-
function getPayloadConfigFromPayload(
|
|
308
|
-
config: ChartConfig,
|
|
309
|
-
payload: unknown,
|
|
310
|
-
key: string
|
|
311
|
-
) {
|
|
312
|
-
if (typeof payload !== "object" || payload === null) {
|
|
313
|
-
return undefined;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const payloadPayload =
|
|
317
|
-
"payload" in payload &&
|
|
318
|
-
typeof payload.payload === "object" &&
|
|
319
|
-
payload.payload !== null
|
|
320
|
-
? payload.payload
|
|
321
|
-
: undefined;
|
|
322
|
-
|
|
323
|
-
let configLabelKey: string = key;
|
|
324
|
-
|
|
325
|
-
if (
|
|
326
|
-
key in payload &&
|
|
327
|
-
typeof payload[key as keyof typeof payload] === "string"
|
|
328
|
-
) {
|
|
329
|
-
configLabelKey = payload[key as keyof typeof payload] as string;
|
|
330
|
-
} else if (
|
|
331
|
-
payloadPayload &&
|
|
332
|
-
key in payloadPayload &&
|
|
333
|
-
typeof payloadPayload[key as keyof typeof payloadPayload] === "string"
|
|
334
|
-
) {
|
|
335
|
-
configLabelKey = payloadPayload[
|
|
336
|
-
key as keyof typeof payloadPayload
|
|
337
|
-
] as string;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
return configLabelKey in config
|
|
341
|
-
? config[configLabelKey]
|
|
342
|
-
: config[key as keyof typeof config];
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
export {
|
|
346
|
-
ChartContainer,
|
|
347
|
-
ChartTooltip,
|
|
348
|
-
ChartTooltipContent,
|
|
349
|
-
ChartLegend,
|
|
350
|
-
ChartLegendContent,
|
|
351
|
-
ChartStyle,
|
|
352
|
-
};
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import * as RechartsPrimitive from "recharts";
|
|
5
|
+
import { cn } from "./../../lib/utils";
|
|
6
|
+
|
|
7
|
+
// Format: { THEME_NAME: CSS_SELECTOR }
|
|
8
|
+
const THEMES = { light: "", dark: ".dark" } as const;
|
|
9
|
+
|
|
10
|
+
export type ChartConfig = {
|
|
11
|
+
[k in string]: {
|
|
12
|
+
label?: React.ReactNode;
|
|
13
|
+
icon?: React.ComponentType;
|
|
14
|
+
} & (
|
|
15
|
+
| { color?: string; theme?: never }
|
|
16
|
+
| { color?: never; theme: Record<keyof typeof THEMES, string> }
|
|
17
|
+
);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type ChartContextProps = {
|
|
21
|
+
config: ChartConfig;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const ChartContext = React.createContext<ChartContextProps | null>(null);
|
|
25
|
+
|
|
26
|
+
function useChart() {
|
|
27
|
+
const context = React.useContext(ChartContext);
|
|
28
|
+
|
|
29
|
+
if (!context) {
|
|
30
|
+
throw new Error("useChart must be used within a <ChartContainer />");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return context;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function ChartContainer({
|
|
37
|
+
id,
|
|
38
|
+
className,
|
|
39
|
+
children,
|
|
40
|
+
config,
|
|
41
|
+
...props
|
|
42
|
+
}: React.ComponentProps<"div"> & {
|
|
43
|
+
config: ChartConfig;
|
|
44
|
+
children: React.ComponentProps<
|
|
45
|
+
typeof RechartsPrimitive.ResponsiveContainer
|
|
46
|
+
>["children"];
|
|
47
|
+
}) {
|
|
48
|
+
const uniqueId = React.useId();
|
|
49
|
+
const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`;
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<ChartContext.Provider value={{ config }}>
|
|
53
|
+
<div
|
|
54
|
+
data-slot="chart"
|
|
55
|
+
data-chart={chartId}
|
|
56
|
+
className={cn(
|
|
57
|
+
"[&_.recharts-cartesian-axis-tick_text]:fill-muted-foreground [&_.recharts-cartesian-grid_line[stroke='#ccc']]:stroke-border/50 [&_.recharts-curve.recharts-tooltip-cursor]:stroke-border [&_.recharts-polar-grid_[stroke='#ccc']]:stroke-border [&_.recharts-radial-bar-background-sector]:fill-muted [&_.recharts-rectangle.recharts-tooltip-cursor]:fill-muted [&_.recharts-reference-line_[stroke='#ccc']]:stroke-border flex aspect-video justify-center text-xs [&_.recharts-dot[stroke='#fff']]:stroke-transparent [&_.recharts-layer]:outline-hidden [&_.recharts-sector]:outline-hidden [&_.recharts-sector[stroke='#fff']]:stroke-transparent [&_.recharts-surface]:outline-hidden",
|
|
58
|
+
className
|
|
59
|
+
)}
|
|
60
|
+
{...props}
|
|
61
|
+
>
|
|
62
|
+
<ChartStyle id={chartId} config={config} />
|
|
63
|
+
<RechartsPrimitive.ResponsiveContainer>
|
|
64
|
+
{children}
|
|
65
|
+
</RechartsPrimitive.ResponsiveContainer>
|
|
66
|
+
</div>
|
|
67
|
+
</ChartContext.Provider>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
|
|
72
|
+
const colorConfig = Object.entries(config).filter(
|
|
73
|
+
([, config]) => config.theme || config.color
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
if (!colorConfig.length) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<style
|
|
82
|
+
dangerouslySetInnerHTML={{
|
|
83
|
+
__html: Object.entries(THEMES)
|
|
84
|
+
.map(
|
|
85
|
+
([theme, prefix]) => `
|
|
86
|
+
${prefix} [data-chart=${id}] {
|
|
87
|
+
${colorConfig
|
|
88
|
+
.map(([key, itemConfig]) => {
|
|
89
|
+
const color =
|
|
90
|
+
itemConfig.theme?.[theme as keyof typeof itemConfig.theme] ||
|
|
91
|
+
itemConfig.color;
|
|
92
|
+
return color ? ` --color-${key}: ${color};` : null;
|
|
93
|
+
})
|
|
94
|
+
.join("\n")}
|
|
95
|
+
}
|
|
96
|
+
`
|
|
97
|
+
)
|
|
98
|
+
.join("\n"),
|
|
99
|
+
}}
|
|
100
|
+
/>
|
|
101
|
+
);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const ChartTooltip = RechartsPrimitive.Tooltip;
|
|
105
|
+
|
|
106
|
+
function ChartTooltipContent({
|
|
107
|
+
active,
|
|
108
|
+
payload,
|
|
109
|
+
className,
|
|
110
|
+
indicator = "dot",
|
|
111
|
+
hideLabel = false,
|
|
112
|
+
hideIndicator = false,
|
|
113
|
+
label,
|
|
114
|
+
labelFormatter,
|
|
115
|
+
labelClassName,
|
|
116
|
+
formatter,
|
|
117
|
+
color,
|
|
118
|
+
nameKey,
|
|
119
|
+
labelKey,
|
|
120
|
+
}: React.ComponentProps<typeof RechartsPrimitive.Tooltip> &
|
|
121
|
+
React.ComponentProps<"div"> & {
|
|
122
|
+
hideLabel?: boolean;
|
|
123
|
+
hideIndicator?: boolean;
|
|
124
|
+
indicator?: "line" | "dot" | "dashed";
|
|
125
|
+
nameKey?: string;
|
|
126
|
+
labelKey?: string;
|
|
127
|
+
}) {
|
|
128
|
+
const { config } = useChart();
|
|
129
|
+
|
|
130
|
+
const tooltipLabel = React.useMemo(() => {
|
|
131
|
+
if (hideLabel || !payload?.length) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const [item] = payload;
|
|
136
|
+
const key = `${labelKey || item?.dataKey || item?.name || "value"}`;
|
|
137
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
138
|
+
const value =
|
|
139
|
+
!labelKey && typeof label === "string"
|
|
140
|
+
? config[label as keyof typeof config]?.label || label
|
|
141
|
+
: itemConfig?.label;
|
|
142
|
+
|
|
143
|
+
if (labelFormatter) {
|
|
144
|
+
return (
|
|
145
|
+
<div className={cn("font-medium", labelClassName)}>
|
|
146
|
+
{labelFormatter(value, payload)}
|
|
147
|
+
</div>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!value) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return <div className={cn("font-medium", labelClassName)}>{value}</div>;
|
|
156
|
+
}, [
|
|
157
|
+
label,
|
|
158
|
+
labelFormatter,
|
|
159
|
+
payload,
|
|
160
|
+
hideLabel,
|
|
161
|
+
labelClassName,
|
|
162
|
+
config,
|
|
163
|
+
labelKey,
|
|
164
|
+
]);
|
|
165
|
+
|
|
166
|
+
if (!active || !payload?.length) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const nestLabel = payload.length === 1 && indicator !== "dot";
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<div
|
|
174
|
+
className={cn(
|
|
175
|
+
"border-neutral-200/50 bg-white grid min-w-[8rem] items-start gap-1.5 rounded-lg border border-neutral-200 px-2.5 py-1.5 text-xs shadow-xl dark:border-neutral-800/50 dark:bg-neutral-950 dark:border-neutral-800",
|
|
176
|
+
className
|
|
177
|
+
)}
|
|
178
|
+
>
|
|
179
|
+
{!nestLabel ? tooltipLabel : null}
|
|
180
|
+
<div className="grid gap-1.5">
|
|
181
|
+
{payload.map((item, index) => {
|
|
182
|
+
const key = `${nameKey || item.name || item.dataKey || "value"}`;
|
|
183
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
184
|
+
const indicatorColor = color || item.payload.fill || item.color;
|
|
185
|
+
|
|
186
|
+
return (
|
|
187
|
+
<div
|
|
188
|
+
key={item.dataKey}
|
|
189
|
+
className={cn(
|
|
190
|
+
"[&>svg]:text-neutral-500 flex w-full flex-wrap items-stretch gap-2 [&>svg]:h-2.5 [&>svg]:w-2.5 dark:[&>svg]:text-neutral-400",
|
|
191
|
+
indicator === "dot" && "items-center"
|
|
192
|
+
)}
|
|
193
|
+
>
|
|
194
|
+
{formatter && item?.value !== undefined && item.name ? (
|
|
195
|
+
formatter(item.value, item.name, item, index, item.payload)
|
|
196
|
+
) : (
|
|
197
|
+
<>
|
|
198
|
+
{itemConfig?.icon ? (
|
|
199
|
+
<itemConfig.icon />
|
|
200
|
+
) : (
|
|
201
|
+
!hideIndicator && (
|
|
202
|
+
<div
|
|
203
|
+
className={cn(
|
|
204
|
+
"shrink-0 rounded-[2px] border-(--color-border) bg-(--color-bg)",
|
|
205
|
+
{
|
|
206
|
+
"h-2.5 w-2.5": indicator === "dot",
|
|
207
|
+
"w-1": indicator === "line",
|
|
208
|
+
"w-0 border-[1.5px] border-dashed bg-transparent":
|
|
209
|
+
indicator === "dashed",
|
|
210
|
+
"my-0.5": nestLabel && indicator === "dashed",
|
|
211
|
+
}
|
|
212
|
+
)}
|
|
213
|
+
style={
|
|
214
|
+
{
|
|
215
|
+
"--color-bg": indicatorColor,
|
|
216
|
+
"--color-border": indicatorColor,
|
|
217
|
+
} as React.CSSProperties
|
|
218
|
+
}
|
|
219
|
+
/>
|
|
220
|
+
)
|
|
221
|
+
)}
|
|
222
|
+
<div
|
|
223
|
+
className={cn(
|
|
224
|
+
"flex flex-1 justify-between leading-none",
|
|
225
|
+
nestLabel ? "items-end" : "items-center"
|
|
226
|
+
)}
|
|
227
|
+
>
|
|
228
|
+
<div className="grid gap-1.5">
|
|
229
|
+
{nestLabel ? tooltipLabel : null}
|
|
230
|
+
<span className="text-neutral-500 dark:text-neutral-400">
|
|
231
|
+
{itemConfig?.label || item.name}
|
|
232
|
+
</span>
|
|
233
|
+
</div>
|
|
234
|
+
{item.value && (
|
|
235
|
+
<span className="text-neutral-950 font-mono font-medium tabular-nums dark:text-neutral-50">
|
|
236
|
+
{item.value.toLocaleString()}
|
|
237
|
+
</span>
|
|
238
|
+
)}
|
|
239
|
+
</div>
|
|
240
|
+
</>
|
|
241
|
+
)}
|
|
242
|
+
</div>
|
|
243
|
+
);
|
|
244
|
+
})}
|
|
245
|
+
</div>
|
|
246
|
+
</div>
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const ChartLegend = RechartsPrimitive.Legend;
|
|
251
|
+
|
|
252
|
+
function ChartLegendContent({
|
|
253
|
+
className,
|
|
254
|
+
hideIcon = false,
|
|
255
|
+
payload,
|
|
256
|
+
verticalAlign = "bottom",
|
|
257
|
+
nameKey,
|
|
258
|
+
}: React.ComponentProps<"div"> &
|
|
259
|
+
Pick<RechartsPrimitive.LegendProps, "payload" | "verticalAlign"> & {
|
|
260
|
+
hideIcon?: boolean;
|
|
261
|
+
nameKey?: string;
|
|
262
|
+
}) {
|
|
263
|
+
const { config } = useChart();
|
|
264
|
+
|
|
265
|
+
if (!payload?.length) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return (
|
|
270
|
+
<div
|
|
271
|
+
className={cn(
|
|
272
|
+
"flex items-center justify-center gap-4",
|
|
273
|
+
verticalAlign === "top" ? "pb-3" : "pt-3",
|
|
274
|
+
className
|
|
275
|
+
)}
|
|
276
|
+
>
|
|
277
|
+
{payload.map((item) => {
|
|
278
|
+
const key = `${nameKey || item.dataKey || "value"}`;
|
|
279
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
280
|
+
|
|
281
|
+
return (
|
|
282
|
+
<div
|
|
283
|
+
key={item.value}
|
|
284
|
+
className={cn(
|
|
285
|
+
"[&>svg]:text-neutral-500 flex items-center gap-1.5 [&>svg]:h-3 [&>svg]:w-3 dark:[&>svg]:text-neutral-400"
|
|
286
|
+
)}
|
|
287
|
+
>
|
|
288
|
+
{itemConfig?.icon && !hideIcon ? (
|
|
289
|
+
<itemConfig.icon />
|
|
290
|
+
) : (
|
|
291
|
+
<div
|
|
292
|
+
className="h-2 w-2 shrink-0 rounded-[2px]"
|
|
293
|
+
style={{
|
|
294
|
+
backgroundColor: item.color,
|
|
295
|
+
}}
|
|
296
|
+
/>
|
|
297
|
+
)}
|
|
298
|
+
{itemConfig?.label}
|
|
299
|
+
</div>
|
|
300
|
+
);
|
|
301
|
+
})}
|
|
302
|
+
</div>
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Helper to extract item config from a payload.
|
|
307
|
+
function getPayloadConfigFromPayload(
|
|
308
|
+
config: ChartConfig,
|
|
309
|
+
payload: unknown,
|
|
310
|
+
key: string
|
|
311
|
+
) {
|
|
312
|
+
if (typeof payload !== "object" || payload === null) {
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const payloadPayload =
|
|
317
|
+
"payload" in payload &&
|
|
318
|
+
typeof payload.payload === "object" &&
|
|
319
|
+
payload.payload !== null
|
|
320
|
+
? payload.payload
|
|
321
|
+
: undefined;
|
|
322
|
+
|
|
323
|
+
let configLabelKey: string = key;
|
|
324
|
+
|
|
325
|
+
if (
|
|
326
|
+
key in payload &&
|
|
327
|
+
typeof payload[key as keyof typeof payload] === "string"
|
|
328
|
+
) {
|
|
329
|
+
configLabelKey = payload[key as keyof typeof payload] as string;
|
|
330
|
+
} else if (
|
|
331
|
+
payloadPayload &&
|
|
332
|
+
key in payloadPayload &&
|
|
333
|
+
typeof payloadPayload[key as keyof typeof payloadPayload] === "string"
|
|
334
|
+
) {
|
|
335
|
+
configLabelKey = payloadPayload[
|
|
336
|
+
key as keyof typeof payloadPayload
|
|
337
|
+
] as string;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return configLabelKey in config
|
|
341
|
+
? config[configLabelKey]
|
|
342
|
+
: config[key as keyof typeof config];
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export {
|
|
346
|
+
ChartContainer,
|
|
347
|
+
ChartTooltip,
|
|
348
|
+
ChartTooltipContent,
|
|
349
|
+
ChartLegend,
|
|
350
|
+
ChartLegendContent,
|
|
351
|
+
ChartStyle,
|
|
352
|
+
};
|