@brightlocal/ui-components 3.0.0 → 3.2.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.
Files changed (235) hide show
  1. package/README.md +7 -1
  2. package/component-meta.json +749 -53
  3. package/deprecations.json +114 -4
  4. package/dist/accordion.cjs +1 -1
  5. package/dist/accordion.d.ts +36 -0
  6. package/dist/accordion.d.ts.map +1 -1
  7. package/dist/accordion.js +74 -61
  8. package/dist/alert-dialog.cjs +1 -1
  9. package/dist/alert-dialog.d.ts +32 -3
  10. package/dist/alert-dialog.d.ts.map +1 -1
  11. package/dist/alert-dialog.js +61 -58
  12. package/dist/alert.cjs +1 -1
  13. package/dist/alert.d.ts.map +1 -1
  14. package/dist/alert.js +58 -57
  15. package/dist/badge.cjs +1 -1
  16. package/dist/badge.d.ts +9 -0
  17. package/dist/badge.d.ts.map +1 -1
  18. package/dist/badge.js +50 -30
  19. package/dist/banner.cjs +1 -0
  20. package/dist/banner.d.ts +81 -0
  21. package/dist/banner.d.ts.map +1 -0
  22. package/dist/banner.js +176 -0
  23. package/dist/blocks/empty-state.cjs +1 -1
  24. package/dist/blocks/empty-state.d.ts +4 -3
  25. package/dist/blocks/empty-state.d.ts.map +1 -1
  26. package/dist/blocks/empty-state.js +50 -57
  27. package/dist/blocks/progress-state.d.ts +2 -2
  28. package/dist/blocks/sidebar.cjs +1 -1
  29. package/dist/blocks/sidebar.d.ts +17 -0
  30. package/dist/blocks/sidebar.d.ts.map +1 -1
  31. package/dist/blocks/sidebar.js +8 -2
  32. package/dist/brand-illustration-motif.d.ts +1 -1
  33. package/dist/button-group.cjs +1 -0
  34. package/dist/button-group.d.ts +36 -0
  35. package/dist/button-group.d.ts.map +1 -0
  36. package/dist/button-group.js +47 -0
  37. package/dist/button.cjs +1 -1
  38. package/dist/button.js +1 -1
  39. package/dist/card.cjs +1 -1
  40. package/dist/card.d.ts +105 -7
  41. package/dist/card.d.ts.map +1 -1
  42. package/dist/card.js +198 -101
  43. package/dist/chart.cjs +3 -3
  44. package/dist/chart.d.ts +270 -24
  45. package/dist/chart.d.ts.map +1 -1
  46. package/dist/chart.js +497 -322
  47. package/dist/checkbox.cjs +1 -1
  48. package/dist/checkbox.d.ts +1 -1
  49. package/dist/checkbox.d.ts.map +1 -1
  50. package/dist/checkbox.js +2 -1
  51. package/dist/chip.cjs +1 -1
  52. package/dist/chip.d.ts +38 -4
  53. package/dist/chip.d.ts.map +1 -1
  54. package/dist/chip.js +95 -81
  55. package/dist/combobox.cjs +1 -1
  56. package/dist/combobox.js +2 -2
  57. package/dist/command.cjs +1 -1
  58. package/dist/command.d.ts +23 -1
  59. package/dist/command.d.ts.map +1 -1
  60. package/dist/command.js +251 -225
  61. package/dist/data-table.cjs +1 -1
  62. package/dist/data-table.d.ts +201 -23
  63. package/dist/data-table.d.ts.map +1 -1
  64. package/dist/data-table.js +712 -568
  65. package/dist/date-picker.cjs +1 -1
  66. package/dist/date-picker.d.ts +13 -5
  67. package/dist/date-picker.d.ts.map +1 -1
  68. package/dist/date-picker.js +54 -53
  69. package/dist/field.cjs +1 -1
  70. package/dist/field.d.ts +8 -1
  71. package/dist/field.d.ts.map +1 -1
  72. package/dist/field.js +79 -52
  73. package/dist/global-layout.cjs +1 -1
  74. package/dist/global-layout.d.ts +105 -13
  75. package/dist/global-layout.d.ts.map +1 -1
  76. package/dist/global-layout.js +146 -129
  77. package/dist/grid.d.ts +26 -3
  78. package/dist/grid.d.ts.map +1 -1
  79. package/dist/hooks/use-breakpoint.cjs +1 -1
  80. package/dist/hooks/use-breakpoint.d.ts +1 -1
  81. package/dist/hooks/use-breakpoint.d.ts.map +1 -1
  82. package/dist/hooks/use-breakpoint.js +1 -0
  83. package/dist/hooks/use-mobile.cjs +1 -0
  84. package/dist/hooks/use-mobile.js +8 -0
  85. package/dist/index.cjs +1 -1
  86. package/dist/index.d.ts +6 -0
  87. package/dist/index.d.ts.map +1 -1
  88. package/dist/index.js +594 -553
  89. package/dist/info-tooltip.cjs +1 -0
  90. package/dist/info-tooltip.d.ts +39 -0
  91. package/dist/info-tooltip.d.ts.map +1 -0
  92. package/dist/info-tooltip.js +41 -0
  93. package/dist/input-chip.cjs +1 -1
  94. package/dist/input-chip.js +1 -1
  95. package/dist/input-group.cjs +1 -1
  96. package/dist/input-group.d.ts +33 -2
  97. package/dist/input-group.d.ts.map +1 -1
  98. package/dist/input-group.js +167 -54
  99. package/dist/input-otp.cjs +1 -1
  100. package/dist/input-otp.js +9 -9
  101. package/dist/input-password.cjs +1 -1
  102. package/dist/input-password.d.ts +45 -15
  103. package/dist/input-password.d.ts.map +1 -1
  104. package/dist/input-password.js +31 -30
  105. package/dist/input.cjs +1 -1
  106. package/dist/input.js +10 -10
  107. package/dist/lib/block-illustration.cjs +1 -1
  108. package/dist/lib/block-illustration.d.ts +4 -1
  109. package/dist/lib/block-illustration.d.ts.map +1 -1
  110. package/dist/lib/block-illustration.js +12 -11
  111. package/dist/lib/empty-chip.cjs +1 -0
  112. package/dist/lib/empty-chip.d.ts +4 -0
  113. package/dist/lib/empty-chip.d.ts.map +1 -0
  114. package/dist/lib/empty-chip.js +13 -0
  115. package/dist/lib/utils.cjs +1 -1
  116. package/dist/lib/utils.d.ts.map +1 -1
  117. package/dist/lib/utils.js +4 -3
  118. package/dist/list.cjs +1 -1
  119. package/dist/list.js +1 -1
  120. package/dist/multi-select-filter.cjs +1 -0
  121. package/dist/multi-select-filter.d.ts +178 -0
  122. package/dist/multi-select-filter.d.ts.map +1 -0
  123. package/dist/multi-select-filter.js +463 -0
  124. package/dist/pagination.cjs +1 -1
  125. package/dist/pagination.d.ts +3 -0
  126. package/dist/pagination.d.ts.map +1 -1
  127. package/dist/pagination.js +57 -57
  128. package/dist/progress.cjs +1 -1
  129. package/dist/progress.d.ts +123 -16
  130. package/dist/progress.d.ts.map +1 -1
  131. package/dist/progress.js +89 -75
  132. package/dist/rating.cjs +1 -1
  133. package/dist/rating.js +18 -18
  134. package/dist/relative-date.cjs +1 -0
  135. package/dist/relative-date.d.ts +65 -0
  136. package/dist/relative-date.d.ts.map +1 -0
  137. package/dist/relative-date.js +128 -0
  138. package/dist/scroll-area.cjs +1 -1
  139. package/dist/scroll-area.d.ts +15 -6
  140. package/dist/scroll-area.d.ts.map +1 -1
  141. package/dist/scroll-area.js +38 -36
  142. package/dist/select.cjs +2 -2
  143. package/dist/select.d.ts +24 -3
  144. package/dist/select.d.ts.map +1 -1
  145. package/dist/select.js +130 -110
  146. package/dist/sheet.cjs +1 -1
  147. package/dist/sheet.d.ts +78 -2
  148. package/dist/sheet.d.ts.map +1 -1
  149. package/dist/sheet.js +192 -85
  150. package/dist/single-select-filter.cjs +1 -0
  151. package/dist/single-select-filter.d.ts +81 -0
  152. package/dist/single-select-filter.d.ts.map +1 -0
  153. package/dist/single-select-filter.js +226 -0
  154. package/dist/sonner.cjs +1 -1
  155. package/dist/sonner.d.ts.map +1 -1
  156. package/dist/sonner.js +22 -22
  157. package/dist/split-layout.cjs +1 -1
  158. package/dist/split-layout.d.ts +1 -1
  159. package/dist/split-layout.js +13 -13
  160. package/dist/table.cjs +1 -1
  161. package/dist/table.d.ts +45 -4
  162. package/dist/table.d.ts.map +1 -1
  163. package/dist/table.js +90 -73
  164. package/dist/tabs.cjs +1 -1
  165. package/dist/tabs.d.ts +12 -1
  166. package/dist/tabs.d.ts.map +1 -1
  167. package/dist/tabs.js +148 -126
  168. package/dist/textarea.cjs +1 -1
  169. package/dist/textarea.js +1 -1
  170. package/dist/tooltip.cjs +1 -1
  171. package/dist/tooltip.d.ts +38 -36
  172. package/dist/tooltip.d.ts.map +1 -1
  173. package/dist/tooltip.js +262 -73
  174. package/dist/typography-heading.cjs +1 -1
  175. package/dist/typography-heading.d.ts +13 -0
  176. package/dist/typography-heading.d.ts.map +1 -1
  177. package/dist/typography-heading.js +32 -21
  178. package/dist/typography-text.cjs +1 -1
  179. package/dist/typography-text.d.ts +15 -2
  180. package/dist/typography-text.d.ts.map +1 -1
  181. package/dist/typography-text.js +35 -24
  182. package/dist/typography.d.ts +1 -1
  183. package/package.json +5 -6
  184. package/src/blocks/empty-state.tsx +17 -24
  185. package/src/blocks/progress-state.tsx +2 -2
  186. package/src/blocks/sidebar.tsx +26 -2
  187. package/src/components/accordion.tsx +82 -27
  188. package/src/components/alert-dialog.tsx +50 -5
  189. package/src/components/alert.tsx +16 -8
  190. package/src/components/badge.tsx +40 -3
  191. package/src/components/banner.tsx +288 -0
  192. package/src/components/brand-illustration-motif.tsx +1 -1
  193. package/src/components/button-group.tsx +112 -0
  194. package/src/components/button.tsx +1 -1
  195. package/src/components/card.tsx +249 -27
  196. package/src/components/chart.tsx +586 -53
  197. package/src/components/checkbox.tsx +1 -1
  198. package/src/components/chip.tsx +83 -10
  199. package/src/components/combobox.tsx +2 -2
  200. package/src/components/command.tsx +56 -4
  201. package/src/components/data-table.tsx +842 -199
  202. package/src/components/date-picker.tsx +18 -7
  203. package/src/components/field.tsx +81 -12
  204. package/src/components/global-layout.tsx +176 -38
  205. package/src/components/grid.tsx +34 -3
  206. package/src/components/info-tooltip.tsx +94 -0
  207. package/src/components/input-chip.tsx +1 -1
  208. package/src/components/input-group.tsx +246 -11
  209. package/src/components/input-otp.tsx +1 -1
  210. package/src/components/input-password.tsx +56 -22
  211. package/src/components/input.tsx +1 -1
  212. package/src/components/list.tsx +1 -1
  213. package/src/components/multi-select-filter.tsx +912 -0
  214. package/src/components/pagination.tsx +10 -3
  215. package/src/components/progress.tsx +190 -31
  216. package/src/components/rating.tsx +1 -1
  217. package/src/components/relative-date.tsx +230 -0
  218. package/src/components/scroll-area.tsx +23 -10
  219. package/src/components/select.tsx +65 -11
  220. package/src/components/sheet.tsx +240 -4
  221. package/src/components/single-select-filter.tsx +443 -0
  222. package/src/components/sonner.tsx +16 -5
  223. package/src/components/split-layout.tsx +2 -2
  224. package/src/components/table.tsx +71 -11
  225. package/src/components/tabs.tsx +93 -46
  226. package/src/components/textarea.tsx +1 -1
  227. package/src/components/tooltip.tsx +440 -68
  228. package/src/components/typography-heading.tsx +24 -0
  229. package/src/components/typography-text.tsx +27 -3
  230. package/src/components/typography.tsx +1 -1
  231. package/src/hooks/use-breakpoint.ts +2 -1
  232. package/src/index.ts +6 -0
  233. package/src/lib/block-illustration.tsx +5 -1
  234. package/src/lib/empty-chip.ts +24 -0
  235. package/src/lib/utils.ts +1 -0
@@ -2,6 +2,8 @@
2
2
 
3
3
  import * as React from "react";
4
4
  import * as RechartsPrimitive from "recharts";
5
+ import { Slot, Slottable } from "@radix-ui/react-slot";
6
+ import { cva } from "class-variance-authority";
5
7
  import { cn } from "@brightlocal/ui-components/lib/utils";
6
8
 
7
9
  // ============================================================================
@@ -22,7 +24,13 @@ type ChartContextProps = {
22
24
  config: ChartConfig;
23
25
  };
24
26
 
25
- const ChartContext = React.createContext<ChartContextProps | null>(null);
27
+ /** What the context carries — the public props plus internal provenance. */
28
+ type ChartContextValue = ChartContextProps & {
29
+ /** True when the config comes from a `ChartLegendList` rather than a `ChartContainer` — no `ChartStyle` variables exist there. */
30
+ standalone?: boolean;
31
+ };
32
+
33
+ const ChartContext = React.createContext<ChartContextValue | null>(null);
26
34
 
27
35
  function useChart() {
28
36
  const context = React.useContext(ChartContext);
@@ -34,6 +42,15 @@ function useChart() {
34
42
  return context;
35
43
  }
36
44
 
45
+ /**
46
+ * The chart config when rendered inside a `ChartContainer` (or a
47
+ * `ChartLegendList` given a `config`), otherwise `null`. Lets legend parts
48
+ * render beside a chart as well as inside one.
49
+ */
50
+ function useChartOptional() {
51
+ return React.useContext(ChartContext);
52
+ }
53
+
37
54
  // ============================================================================
38
55
  // Chart Container
39
56
  // ============================================================================
@@ -200,13 +217,37 @@ function ChartStyle({ id, config }: { id: string; config: ChartConfig }) {
200
217
  // Chart Tooltip
201
218
  // ============================================================================
202
219
 
220
+ /**
221
+ * A single entry of the Recharts tooltip payload, as `ChartTooltipContent`
222
+ * receives it. This is the `item` argument given to `formatter` and the
223
+ * element type of the `payload` arrays given to `formatter` and
224
+ * `labelFormatter`, so consumers can read e.g. `item.color` or
225
+ * `item.payload` without hand-rolled narrowing.
226
+ */
227
+ interface ChartTooltipPayloadItem {
228
+ /** The series `dataKey` this row belongs to. */
229
+ dataKey?: string | number;
230
+ /** The row's value. `undefined` when the datum carries no value. */
231
+ value?: number | string;
232
+ /** The series name Recharts resolves for this row. */
233
+ name?: string;
234
+ /** The series stroke colour Recharts injects. */
235
+ color?: string;
236
+ /** The series fill colour Recharts injects (bars, pie slices). */
237
+ fill?: string;
238
+ /** The underlying data point for this row. */
239
+ payload: Record<string, unknown>;
240
+ }
241
+
203
242
  /**
204
243
  * Whether a tooltip row has a value worth printing. Zero is a real value and
205
244
  * must render; NaN/Infinity (a dataKey over a non-numeric field, a ratio with
206
245
  * a zero denominator) and blank strings have nothing to show, so the value
207
246
  * slot stays empty rather than reading "NaN".
208
247
  */
209
- function isRenderableValue(value: unknown): boolean {
248
+ function isRenderableValue(
249
+ value: number | string | null | undefined
250
+ ): value is number | string {
210
251
  if (value == null || value === "") {
211
252
  return false;
212
253
  }
@@ -250,17 +291,33 @@ interface ChartTooltipContentProps
250
291
  ChartTooltipProps,
251
292
  "hideLabel" | "hideIndicator" | "indicator" | "nameKey" | "labelKey"
252
293
  > {
253
- /** Custom label formatter function */
254
- labelFormatter?: (value: unknown, payload: unknown[]) => React.ReactNode;
294
+ /**
295
+ * Custom label formatter function.
296
+ * @param value - The resolved tooltip label (the config label when one
297
+ * matches, otherwise the raw axis label).
298
+ * @param payload - The tooltip payload entries for the hovered datum.
299
+ */
300
+ labelFormatter?: (
301
+ value: React.ReactNode,
302
+ payload: ChartTooltipPayloadItem[]
303
+ ) => React.ReactNode;
255
304
  /** Additional CSS class for the label */
256
305
  labelClassName?: string;
257
- /** Custom value formatter function */
306
+ /**
307
+ * Custom value formatter function. Replaces the default row (indicator,
308
+ * name, and value) for every entry with a renderable value.
309
+ * @param value - The entry's value (`0` is real data and is passed through).
310
+ * @param name - The entry's series name.
311
+ * @param item - The full payload entry (colour, dataKey, data point, …).
312
+ * @param index - The entry's index within the tooltip payload.
313
+ * @param payload - All payload entries for the hovered datum.
314
+ */
258
315
  formatter?: (
259
- value: unknown,
316
+ value: number | string,
260
317
  name: string,
261
- item: unknown,
318
+ item: ChartTooltipPayloadItem,
262
319
  index: number,
263
- payload: unknown[]
320
+ payload: ChartTooltipPayloadItem[]
264
321
  ) => React.ReactNode;
265
322
  }
266
323
 
@@ -283,13 +340,7 @@ function ChartTooltipContent({
283
340
  labelKey,
284
341
  }: ChartTooltipContentProps & {
285
342
  active?: boolean;
286
- payload?: Array<{
287
- dataKey: string;
288
- value: number | string;
289
- name?: string;
290
- color?: string;
291
- payload: Record<string, unknown>;
292
- }>;
343
+ payload?: ChartTooltipPayloadItem[];
293
344
  label?: string;
294
345
  color?: string;
295
346
  }) {
@@ -363,8 +414,9 @@ function ChartTooltipContent({
363
414
  "[&>svg]:text-muted-foreground flex w-full flex-wrap items-stretch gap-2",
364
415
  indicator === "dot" && "items-center"
365
416
  )}
417
+ data-slot="chart-tooltip-item"
366
418
  >
367
- {formatter && isRenderableValue(item?.value) && item.name ? (
419
+ {formatter && isRenderableValue(item.value) && item.name ? (
368
420
  formatter(item.value, item.name, item, index, payload)
369
421
  ) : (
370
422
  <>
@@ -392,20 +444,29 @@ function ChartTooltipContent({
392
444
  />
393
445
  )
394
446
  )}
447
+ {/* gap-2 keeps the name and value apart once the row grows
448
+ wider than the tooltip min-width, where justify-between
449
+ alone would render them flush together (DS-897). */}
395
450
  <div
396
451
  className={cn(
397
- "flex flex-1 justify-between leading-none",
452
+ "flex flex-1 justify-between gap-2 leading-none",
398
453
  nestLabel ? "items-end" : "items-center"
399
454
  )}
400
455
  >
401
456
  <div className="grid gap-1.5">
402
457
  {nestLabel ? tooltipLabel : null}
403
- <span className="text-muted-foreground">
458
+ <span
459
+ className="text-muted-foreground"
460
+ data-slot="chart-tooltip-item-name"
461
+ >
404
462
  {itemConfig?.label || item.name}
405
463
  </span>
406
464
  </div>
407
465
  {isRenderableValue(item.value) && (
408
- <span className="text-foreground tabular-nums">
466
+ <span
467
+ className="text-foreground tabular-nums"
468
+ data-slot="chart-tooltip-item-value"
469
+ >
409
470
  {typeof item.value === "number"
410
471
  ? item.value.toLocaleString()
411
472
  : item.value}
@@ -439,7 +500,12 @@ interface ChartLegendProps extends React.ComponentProps<
439
500
  }
440
501
 
441
502
  /**
442
- * ChartLegend displays a legend for the chart series.
503
+ * ChartLegend positions a legend inside a Recharts chart. Pass
504
+ * `content={<ChartLegendContent />}` to render it with DS styling.
505
+ *
506
+ * For a legend that lives *beside* a chart (next to a donut, or shared by
507
+ * several charts) compose {@link ChartLegendList} and {@link ChartLegendItem}
508
+ * directly instead — Recharts' Legend takes its room from the plot area.
443
509
  */
444
510
  function ChartLegend({ ...props }: ChartLegendProps) {
445
511
  return <RechartsPrimitive.Legend {...props} />;
@@ -447,36 +513,456 @@ function ChartLegend({ ...props }: ChartLegendProps) {
447
513
 
448
514
  ChartLegend.displayName = "ChartLegend";
449
515
 
516
+ // ----------------------------------------------------------------------------
517
+ // Chart Legend Item
518
+ // ----------------------------------------------------------------------------
519
+
520
+ /**
521
+ * Layout direction of a {@link ChartLegendList}.
522
+ */
523
+ type ChartLegendOrientation = "horizontal" | "vertical";
524
+
525
+ /**
526
+ * Scale of a {@link ChartLegendList} and its items.
527
+ * - `sm` — 12/16 text, 8px swatch: chart chrome, next to axis ticks and the tooltip.
528
+ * - `md` — 14/20 text, 10px swatch: a key read as content, beside a donut or under a report chart.
529
+ */
530
+ type ChartLegendSize = "sm" | "md";
531
+
532
+ /**
533
+ * Props for the ChartLegendItem component.
534
+ */
535
+ interface ChartLegendItemProps extends React.ComponentPropsWithoutRef<"div"> {
536
+ /**
537
+ * Data hook for automated testing (required)
538
+ * @example "sources-legend-google"
539
+ */
540
+ dataHook: string;
541
+ /**
542
+ * Key into the chart config. Resolves the swatch colour, the custom icon and
543
+ * the default label. Reads the config from the nearest `ChartContainer` or
544
+ * `ChartLegendList config={…}`; outside both, pass `color` and a label.
545
+ */
546
+ seriesKey?: string;
547
+ /**
548
+ * Swatch colour. Wins over the config colour — the way to colour an item
549
+ * that renders beside a chart rather than inside one.
550
+ * @example "var(--chart-blue)"
551
+ */
552
+ color?: string;
553
+ /**
554
+ * Hide the series icon from the config and draw the colour swatch instead.
555
+ * @default false
556
+ */
557
+ hideIcon?: boolean;
558
+ /**
559
+ * Trailing value (a count, a percentage), rendered in tabular figures and
560
+ * pushed to the far edge in a vertical list.
561
+ */
562
+ value?: React.ReactNode;
563
+ /**
564
+ * De-emphasised: the swatch dims and the text turns muted. The series is
565
+ * still drawn and still counts — it is just not what the reader is looking
566
+ * at (e.g. sources a filter has not chosen). Figma `Visibility=Muted`.
567
+ * @default false
568
+ */
569
+ muted?: boolean;
570
+ /**
571
+ * Switched off: the muted look plus a strike through the label (the value
572
+ * stays legible), so a hidden series reads as hidden in greyscale and in a
573
+ * screenshot — never by colour alone. The strike also reaches a nested
574
+ * button or link used as the label. Use it for a toggled-off series in an
575
+ * interactive legend. Wins over `muted`. Figma `Visibility=Hidden`.
576
+ * @default false
577
+ */
578
+ hidden?: boolean;
579
+ /**
580
+ * Render the child element instead of a `div`, merging the item's props
581
+ * into it — so the item can be a `PopoverTrigger` or a `<button
582
+ * aria-pressed>`. The child's own content becomes the label; the swatch
583
+ * and value are placed around it.
584
+ * @default false
585
+ */
586
+ asChild?: boolean;
587
+ /** Analytics element identifier */
588
+ trackingEl?: string;
589
+ /** Analytics label */
590
+ trackingLabel?: string;
591
+ /**
592
+ * The label. Defaults to the config label for `seriesKey`, then to the key
593
+ * itself. Required when `asChild` is set (it is the element to render).
594
+ */
595
+ children?: React.ReactNode;
596
+ }
597
+
598
+ /**
599
+ * One legend entry: a colour swatch (or the series icon), a label and an
600
+ * optional value. Composable — it renders inside a chart via
601
+ * {@link ChartLegendContent}, or anywhere else inside a
602
+ * {@link ChartLegendList}.
603
+ *
604
+ * @example
605
+ * ```tsx
606
+ * <ChartLegendList dataHook="sources-legend" orientation="vertical" config={config}>
607
+ * <ChartLegendItem dataHook="sources-legend-google" seriesKey="google" value={1240} />
608
+ * <ChartLegendItem dataHook="sources-legend-other" seriesKey="other" value={88} asChild>
609
+ * <PopoverTrigger>Other</PopoverTrigger>
610
+ * </ChartLegendItem>
611
+ * </ChartLegendList>
612
+ * ```
613
+ */
614
+ const ChartLegendItem = React.forwardRef<HTMLDivElement, ChartLegendItemProps>(
615
+ (
616
+ {
617
+ asChild = false,
618
+ children,
619
+ className,
620
+ color,
621
+ dataHook,
622
+ hidden = false,
623
+ hideIcon = false,
624
+ muted = false,
625
+ seriesKey,
626
+ trackingEl,
627
+ trackingLabel,
628
+ value,
629
+ ...props
630
+ },
631
+ ref
632
+ ) => {
633
+ const chart = useChartOptional();
634
+ const itemConfig = seriesKey ? chart?.config[seriesKey] : undefined;
635
+ /* Colour order: explicit prop → config colour → the `--color-<key>`
636
+ variable ChartStyle defines inside a ChartContainer (the only place a
637
+ `theme` config resolves). */
638
+ const swatchColor =
639
+ color ??
640
+ itemConfig?.color ??
641
+ (seriesKey ? `var(--color-${seriesKey})` : undefined);
642
+
643
+ const unresolved =
644
+ !!seriesKey &&
645
+ !color &&
646
+ (!chart || (!!chart.standalone && !itemConfig?.color));
647
+ React.useEffect(() => {
648
+ if (process.env.NODE_ENV !== "production" && unresolved) {
649
+ console.warn(
650
+ `ChartLegendItem: seriesKey="${seriesKey}" has no colour to resolve — render it inside a ChartContainer, give the ChartLegendList config a \`color\` for this key (a \`theme\` only resolves inside a container), or set \`color\`.`
651
+ );
652
+ }
653
+ }, [seriesKey, unresolved]);
654
+
655
+ const Comp = asChild ? Slot : "div";
656
+ const label = children ?? itemConfig?.label ?? seriesKey;
657
+
658
+ return (
659
+ <Comp
660
+ ref={ref}
661
+ className={cn(
662
+ /* The focus ring is here rather than at each call site, so an
663
+ `asChild` button gets it without remembering to. */
664
+ "[&>svg]:text-muted-foreground focus-visible:shadow-default flex items-center gap-1.5 rounded-sm outline-none group-data-[size=md]/legend:gap-2 [&>svg]:size-3 group-data-[size=md]/legend:[&>svg]:size-3.5",
665
+ (muted || hidden) && "text-muted-foreground",
666
+ /* text-decoration does not propagate into a nested <button>/<a>
667
+ (atomic inline-level boxes), so a trigger used as the label gets
668
+ the strike explicitly. */
669
+ hidden && "line-through [&_:is(button,a)]:line-through",
670
+ className
671
+ )}
672
+ data-hidden={hidden || undefined}
673
+ data-hook={dataHook}
674
+ data-muted={muted || hidden || undefined}
675
+ data-slot="chart-legend-item"
676
+ data-tracking-el={trackingEl}
677
+ data-tracking-label={trackingLabel}
678
+ {...props}
679
+ >
680
+ {itemConfig?.icon && !hideIcon ? (
681
+ <itemConfig.icon size={12} />
682
+ ) : (
683
+ /* A span, not a div: with `asChild` the item is often a button,
684
+ which only allows phrasing content. */
685
+ <span
686
+ aria-hidden="true"
687
+ className={cn(
688
+ "duration-fast block size-2 shrink-0 rounded-[2px] transition-opacity ease-out group-data-[size=md]/legend:size-2.5",
689
+ (muted || hidden) && "opacity-40"
690
+ )}
691
+ data-slot="chart-legend-item-swatch"
692
+ style={{ backgroundColor: swatchColor }}
693
+ />
694
+ )}
695
+ <Slottable>{label}</Slottable>
696
+ {value != null && (
697
+ <span
698
+ className="ml-auto pl-0.5 tabular-nums"
699
+ data-slot="chart-legend-item-value"
700
+ >
701
+ {/* inline-block stops the hidden strike propagating from the item:
702
+ the label is struck, the value stays legible — as in Figma and
703
+ the Chart Legend pattern spec. */}
704
+ <span className="inline-block">{value}</span>
705
+ </span>
706
+ )}
707
+ </Comp>
708
+ );
709
+ }
710
+ );
711
+
712
+ ChartLegendItem.displayName = "ChartLegendItem";
713
+
714
+ // ----------------------------------------------------------------------------
715
+ // Chart Legend List
716
+ // ----------------------------------------------------------------------------
717
+
718
+ const chartLegendListVariants = cva("group/legend flex", {
719
+ variants: {
720
+ size: {
721
+ sm: "text-body-xs",
722
+ md: "text-body-sm",
723
+ },
724
+ orientation: {
725
+ /* Wraps: six or seven series do not fit one line on a phone, and
726
+ without wrapping the tail is simply clipped. */
727
+ horizontal: "flex-wrap items-center justify-center gap-x-4 gap-y-1",
728
+ /* Items stretch to the list's width so a value can sit at the far edge. */
729
+ vertical: "flex-col items-stretch gap-3",
730
+ },
731
+ },
732
+ defaultVariants: {
733
+ orientation: "horizontal",
734
+ size: "sm",
735
+ },
736
+ });
737
+
738
+ /**
739
+ * Props for the ChartLegendList component.
740
+ */
741
+ interface ChartLegendListProps extends React.ComponentPropsWithoutRef<"div"> {
742
+ /**
743
+ * Data hook for automated testing (required)
744
+ * @example "sources-legend"
745
+ */
746
+ dataHook: string;
747
+ /**
748
+ * Layout direction. Horizontal wraps under or above a plot; vertical stacks
749
+ * beside a donut, label left and value right.
750
+ * @default "horizontal"
751
+ */
752
+ orientation?: ChartLegendOrientation;
753
+ /**
754
+ * Scale of the list and its items. `sm` matches the chart's axis ticks and
755
+ * tooltip; `md` is for a key read as content beside a donut or under a
756
+ * report chart (14px text, 10px swatch).
757
+ * @default "sm"
758
+ */
759
+ size?: ChartLegendSize;
760
+ /**
761
+ * Accessible name of the legend group.
762
+ * @default "Chart legend"
763
+ */
764
+ ariaLabel?: string;
765
+ /**
766
+ * Chart config for the items, when the list renders outside a
767
+ * `ChartContainer`. Replaces (does not merge with) a surrounding
768
+ * container's config. Give each key a `color` — a `theme` only resolves
769
+ * inside a container.
770
+ */
771
+ config?: ChartConfig;
772
+ /** Analytics element identifier */
773
+ trackingEl?: string;
774
+ /** Analytics label */
775
+ trackingLabel?: string;
776
+ }
777
+
778
+ /**
779
+ * A legend as a standalone list of {@link ChartLegendItem}s — the key beside
780
+ * a donut, a shared key under several charts, or a row of series toggles.
781
+ * {@link ChartLegendContent} renders one of these inside a chart.
782
+ *
783
+ * It is a labelled `role="group"`, not a list: the same container holds
784
+ * static keys and rows of toggle buttons, and a group with a name reads
785
+ * correctly for both ("Chart legend, group"). A static key therefore no
786
+ * longer announces its item count — accepted trade-off (DS-1026 review).
787
+ */
788
+ const ChartLegendList = React.forwardRef<HTMLDivElement, ChartLegendListProps>(
789
+ (
790
+ {
791
+ ariaLabel,
792
+ children,
793
+ className,
794
+ config,
795
+ dataHook,
796
+ orientation = "horizontal",
797
+ size = "sm",
798
+ trackingEl,
799
+ trackingLabel,
800
+ ...props
801
+ },
802
+ ref
803
+ ) => {
804
+ const contextValue = React.useMemo<ChartContextValue | null>(
805
+ () => (config ? { config, standalone: true } : null),
806
+ [config]
807
+ );
808
+ const list = (
809
+ <div
810
+ ref={ref}
811
+ aria-label={ariaLabel ?? "Chart legend"}
812
+ className={cn(
813
+ chartLegendListVariants({ orientation, size }),
814
+ className
815
+ )}
816
+ data-hook={dataHook}
817
+ data-orientation={orientation}
818
+ data-size={size}
819
+ data-slot="chart-legend-list"
820
+ data-tracking-el={trackingEl}
821
+ data-tracking-label={trackingLabel}
822
+ role="group"
823
+ {...props}
824
+ >
825
+ {children}
826
+ </div>
827
+ );
828
+
829
+ return contextValue ? (
830
+ <ChartContext.Provider value={contextValue}>{list}</ChartContext.Provider>
831
+ ) : (
832
+ list
833
+ );
834
+ }
835
+ );
836
+
837
+ ChartLegendList.displayName = "ChartLegendList";
838
+
839
+ // ----------------------------------------------------------------------------
840
+ // Chart Legend Content (Recharts adapter)
841
+ // ----------------------------------------------------------------------------
842
+
843
+ /** `data-hook` segment from an entry key: lower-case, non-alphanumerics to `-`. */
844
+ const slug = (key: string) =>
845
+ key
846
+ .toLowerCase()
847
+ .replace(/[^a-z0-9]+/g, "-")
848
+ .replace(/^-|-$/g, "");
849
+
850
+ /**
851
+ * One entry of the legend payload Recharts hands to `ChartLegend`'s content.
852
+ */
853
+ interface ChartLegendPayloadItem {
854
+ /** Display name — the series name for Bar/Line, the sector name for Pie. */
855
+ value: string;
856
+ /** Series key. Present for Bar/Line/Area series; Pie entries have none. */
857
+ dataKey?: string;
858
+ color?: string;
859
+ /** The series' or sector's raw datum. */
860
+ payload: Record<string, unknown>;
861
+ }
862
+
450
863
  /**
451
864
  * Props for the ChartLegendContent component.
452
865
  */
453
866
  interface ChartLegendContentProps
454
867
  extends React.ComponentProps<"div">, Pick<ChartLegendProps, "nameKey"> {
868
+ /**
869
+ * Data hook for automated testing. Items derive theirs from it
870
+ * (`<dataHook>-<entry key>`, where the entry key is the series `dataKey`,
871
+ * or the Pie entry's `nameKey` value, slugified).
872
+ * @default "chart-legend"
873
+ */
874
+ dataHook?: string;
455
875
  /** Hide the series icon in the legend */
456
876
  hideIcon?: boolean;
457
877
  /** Legend payload from Recharts (automatically provided) */
458
- payload?: Array<{
459
- value: string;
460
- dataKey: string;
461
- color?: string;
462
- payload: Record<string, unknown>;
463
- }>;
878
+ payload?: Array<ChartLegendPayloadItem>;
464
879
  /**
465
- * Vertical alignment of the legend relative to the chart
880
+ * Vertical alignment of the legend relative to the chart — sets the gap
881
+ * to the plot. Pass the same value to `ChartLegend`.
466
882
  * @default "bottom"
467
883
  */
468
- verticalAlign?: "top" | "bottom";
884
+ verticalAlign?: "top" | "middle" | "bottom";
885
+ /**
886
+ * Layout direction. Pair `vertical` with
887
+ * `<ChartLegend layout="vertical" align="right" verticalAlign="middle" />`.
888
+ * @default "horizontal"
889
+ */
890
+ orientation?: ChartLegendOrientation;
891
+ /**
892
+ * Accessible name of the legend group.
893
+ * @default "Chart legend"
894
+ */
895
+ ariaLabel?: string;
896
+ /**
897
+ * Scale of the legend. Keep `sm` inside a chart — it matches the axis
898
+ * ticks and the tooltip; `md` is for a key read as content.
899
+ * @default "sm"
900
+ */
901
+ size?: ChartLegendSize;
902
+ /**
903
+ * Show each entry's value after its label, read from `payload[valueKey]`.
904
+ * Meaningful for Pie/Donut, whose payload is the sector's datum — for Bar
905
+ * and Line series the payload carries no total, so render a
906
+ * `ChartLegendList` beside the chart with your own values instead.
907
+ * @default false
908
+ */
909
+ showValue?: boolean;
910
+ /**
911
+ * Field of each entry's datum that holds its value — for a Pie, its
912
+ * `dataKey` (e.g. `"visitors"`). Defaults to the series `dataKey`, then
913
+ * `"value"`.
914
+ */
915
+ valueKey?: string;
916
+ /**
917
+ * Formats the value shown by `showValue`. Receives the payload entry;
918
+ * read the number from `item.payload`.
919
+ * @example (item) => `${item.payload.visitors} visitors`
920
+ */
921
+ valueFormatter?: (item: ChartLegendPayloadItem) => React.ReactNode;
922
+ /**
923
+ * Order of the entries. Recharts sorts the payload alphabetically by
924
+ * default; pass the entry keys (series `dataKey`s, or Pie `nameKey`
925
+ * values) in the order the chart declares them, or a comparator. Keys not
926
+ * listed go last. Legend order should match series order and tooltip
927
+ * order.
928
+ * @example ["google", "facebook", "other"]
929
+ */
930
+ sortBy?:
931
+ | string[]
932
+ | ((a: ChartLegendPayloadItem, b: ChartLegendPayloadItem) => number);
933
+ /**
934
+ * Replace the rendering of one entry — e.g. wrap "Other" in a popover
935
+ * trigger. Receives the entry and the default item. Recharts positions the
936
+ * legend inside the chart's box, so floating content opened from here can
937
+ * be clipped by it; for an interactive legend prefer a `ChartLegendList`
938
+ * beside the chart.
939
+ */
940
+ renderItem?: (
941
+ item: ChartLegendPayloadItem,
942
+ defaultNode: React.ReactNode
943
+ ) => React.ReactNode;
469
944
  }
470
945
 
471
946
  /**
472
- * ChartLegendContent renders the content inside the legend.
947
+ * ChartLegendContent renders Recharts' legend payload as a
948
+ * {@link ChartLegendList} of {@link ChartLegendItem}s. Use as
949
+ * `<ChartLegend content={<ChartLegendContent />} />`.
473
950
  */
474
951
  function ChartLegendContent({
952
+ ariaLabel,
475
953
  className,
954
+ dataHook = "chart-legend",
476
955
  hideIcon = false,
956
+ nameKey,
957
+ orientation = "horizontal",
477
958
  payload,
959
+ renderItem,
960
+ showValue = false,
961
+ size = "sm",
962
+ sortBy,
963
+ valueFormatter,
964
+ valueKey,
478
965
  verticalAlign = "bottom",
479
- nameKey,
480
966
  }: ChartLegendContentProps) {
481
967
  const { config } = useChart();
482
968
 
@@ -484,41 +970,80 @@ function ChartLegendContent({
484
970
  return null;
485
971
  }
486
972
 
973
+ /* The key an entry is known by: the series dataKey (Bar/Line/Area), or
974
+ for a Pie — whose entries carry no dataKey — the datum's nameKey value,
975
+ then the display name. Drives the config lookup, sortBy and the hook. */
976
+ const entryKey = (item: ChartLegendPayloadItem) =>
977
+ String(
978
+ item.dataKey ??
979
+ (nameKey ? item.payload?.[nameKey] : undefined) ??
980
+ item.value
981
+ );
982
+ /* Keys absent from a `sortBy` array go last, in their incoming order. */
983
+ const rank = (item: ChartLegendPayloadItem) => {
984
+ const index = Array.isArray(sortBy) ? sortBy.indexOf(entryKey(item)) : -1;
985
+ return index === -1 ? Number.POSITIVE_INFINITY : index;
986
+ };
987
+ const sorted = sortBy
988
+ ? [...payload].sort(
989
+ Array.isArray(sortBy)
990
+ ? (a, b) => {
991
+ const ra = rank(a);
992
+ const rb = rank(b);
993
+ return ra === rb ? 0 : ra - rb;
994
+ }
995
+ : sortBy
996
+ )
997
+ : payload;
998
+
487
999
  return (
488
- <div
1000
+ <ChartLegendList
1001
+ ariaLabel={ariaLabel}
489
1002
  className={cn(
490
- "flex items-center justify-center gap-4",
491
- verticalAlign === "top" ? "pb-4" : "pt-4",
1003
+ verticalAlign === "top" && "pb-4",
1004
+ verticalAlign === "bottom" && "pt-4",
492
1005
  className
493
1006
  )}
1007
+ /* Intentional: inside a chart the root keeps the slot name consumers
1008
+ have selected since before the list existed. */
494
1009
  data-slot="chart-legend-content"
1010
+ dataHook={dataHook}
1011
+ orientation={orientation}
1012
+ size={size}
495
1013
  >
496
- {payload.map((item) => {
497
- const key = `${nameKey || item.dataKey || "value"}`;
1014
+ {sorted.map((item) => {
1015
+ const key = entryKey(item);
498
1016
  const itemConfig = config[key];
499
-
500
- return (
501
- <div
1017
+ const value = showValue
1018
+ ? valueFormatter
1019
+ ? valueFormatter(item)
1020
+ : (item.payload?.[valueKey ?? item.dataKey ?? "value"] as
1021
+ | React.ReactNode
1022
+ | undefined)
1023
+ : undefined;
1024
+
1025
+ const node = (
1026
+ <ChartLegendItem
502
1027
  key={item.value}
503
- className={cn(
504
- "[&>svg]:text-muted-foreground flex items-center gap-1.5"
505
- )}
1028
+ color={item.color}
1029
+ dataHook={`${dataHook}-${slug(key)}`}
1030
+ hideIcon={hideIcon}
1031
+ seriesKey={key}
1032
+ value={value}
506
1033
  >
507
- {itemConfig?.icon && !hideIcon ? (
508
- <itemConfig.icon size={12} />
509
- ) : (
510
- <div
511
- className="h-2 w-2 shrink-0 rounded-[2px]"
512
- style={{
513
- backgroundColor: item.color,
514
- }}
515
- />
516
- )}
517
1034
  {itemConfig?.label || item.value}
518
- </div>
1035
+ </ChartLegendItem>
1036
+ );
1037
+
1038
+ return renderItem ? (
1039
+ <React.Fragment key={item.value}>
1040
+ {renderItem(item, node)}
1041
+ </React.Fragment>
1042
+ ) : (
1043
+ node
519
1044
  );
520
1045
  })}
521
- </div>
1046
+ </ChartLegendList>
522
1047
  );
523
1048
  }
524
1049
 
@@ -1150,6 +1675,8 @@ export {
1150
1675
  ChartTooltipContent,
1151
1676
  ChartLegend,
1152
1677
  ChartLegendContent,
1678
+ ChartLegendList,
1679
+ ChartLegendItem,
1153
1680
  ChartStyle,
1154
1681
  ChartDot,
1155
1682
  ChartDotOnStroke,
@@ -1164,8 +1691,14 @@ export type {
1164
1691
  ChartContainerProps,
1165
1692
  ChartTooltipProps,
1166
1693
  ChartTooltipContentProps,
1694
+ ChartTooltipPayloadItem,
1167
1695
  ChartLegendProps,
1168
1696
  ChartLegendContentProps,
1697
+ ChartLegendListProps,
1698
+ ChartLegendItemProps,
1699
+ ChartLegendOrientation,
1700
+ ChartLegendPayloadItem,
1701
+ ChartLegendSize,
1169
1702
  ChartDotProps,
1170
1703
  ChartDotOnStrokeProps,
1171
1704
  ChartDotSize,