@arolariu/components 0.0.39 → 0.0.40

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