@underpin/ui 0.0.0 → 2.0.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 (259) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +44 -1
  3. package/components/autocomplete.d.ts +25 -0
  4. package/components/autocomplete.js +71 -0
  5. package/components/autocomplete.js.map +1 -0
  6. package/components/avatar.d.ts +5 -0
  7. package/components/avatar.js +26 -0
  8. package/components/avatar.js.map +1 -0
  9. package/components/badge.d.ts +36 -0
  10. package/components/badge.js +140 -0
  11. package/components/badge.js.map +1 -0
  12. package/components/bar-list.d.ts +133 -0
  13. package/components/bar-list.js +204 -0
  14. package/components/bar-list.js.map +1 -0
  15. package/components/breakdown-card.d.ts +30 -0
  16. package/components/breakdown-card.js +43 -0
  17. package/components/breakdown-card.js.map +1 -0
  18. package/components/button-group.d.ts +17 -0
  19. package/components/button-group.js +27 -0
  20. package/components/button-group.js.map +1 -0
  21. package/components/button.d.ts +39 -0
  22. package/components/button.js +128 -0
  23. package/components/button.js.map +1 -0
  24. package/components/calendar.d.ts +27 -0
  25. package/components/calendar.js +95 -0
  26. package/components/calendar.js.map +1 -0
  27. package/components/callout.d.ts +26 -0
  28. package/components/callout.js +71 -0
  29. package/components/callout.js.map +1 -0
  30. package/components/card.d.ts +55 -0
  31. package/components/card.js +96 -0
  32. package/components/card.js.map +1 -0
  33. package/components/chart.d.ts +32 -0
  34. package/components/chart.js +43 -0
  35. package/components/chart.js.map +1 -0
  36. package/components/checkbox.d.ts +15 -0
  37. package/components/checkbox.js +42 -0
  38. package/components/checkbox.js.map +1 -0
  39. package/components/code-block.d.ts +21 -0
  40. package/components/code-block.js +35 -0
  41. package/components/code-block.js.map +1 -0
  42. package/components/collapse.d.ts +14 -0
  43. package/components/collapse.js +25 -0
  44. package/components/collapse.js.map +1 -0
  45. package/components/combobox.d.ts +71 -0
  46. package/components/combobox.js +155 -0
  47. package/components/combobox.js.map +1 -0
  48. package/components/copy-button.d.ts +29 -0
  49. package/components/copy-button.js +100 -0
  50. package/components/copy-button.js.map +1 -0
  51. package/components/copy-input.d.ts +15 -0
  52. package/components/copy-input.js +51 -0
  53. package/components/copy-input.js.map +1 -0
  54. package/components/date-picker-trigger.d.ts +12 -0
  55. package/components/date-picker-trigger.js +30 -0
  56. package/components/date-picker-trigger.js.map +1 -0
  57. package/components/date-picker.d.ts +36 -0
  58. package/components/date-picker.js +70 -0
  59. package/components/date-picker.js.map +1 -0
  60. package/components/detail-row.d.ts +51 -0
  61. package/components/detail-row.js +77 -0
  62. package/components/detail-row.js.map +1 -0
  63. package/components/dialog.d.ts +121 -0
  64. package/components/dialog.js +327 -0
  65. package/components/dialog.js.map +1 -0
  66. package/components/disabled-reason.d.ts +31 -0
  67. package/components/disabled-reason.js +67 -0
  68. package/components/disabled-reason.js.map +1 -0
  69. package/components/empty-state.d.ts +41 -0
  70. package/components/empty-state.js +84 -0
  71. package/components/empty-state.js.map +1 -0
  72. package/components/entity-avatar.d.ts +54 -0
  73. package/components/entity-avatar.js +68 -0
  74. package/components/entity-avatar.js.map +1 -0
  75. package/components/form-section.d.ts +22 -0
  76. package/components/form-section.js +38 -0
  77. package/components/form-section.js.map +1 -0
  78. package/components/form.d.ts +199 -0
  79. package/components/form.js +456 -0
  80. package/components/form.js.map +1 -0
  81. package/components/formatted-number.d.ts +21 -0
  82. package/components/formatted-number.js +13 -0
  83. package/components/formatted-number.js.map +1 -0
  84. package/components/heading.d.ts +23 -0
  85. package/components/heading.js +44 -0
  86. package/components/heading.js.map +1 -0
  87. package/components/icon-swap.d.ts +15 -0
  88. package/components/icon-swap.js +29 -0
  89. package/components/icon-swap.js.map +1 -0
  90. package/components/inline-action.d.ts +21 -0
  91. package/components/inline-action.js +36 -0
  92. package/components/inline-action.js.map +1 -0
  93. package/components/input.d.ts +51 -0
  94. package/components/input.js +52 -0
  95. package/components/input.js.map +1 -0
  96. package/components/kbd.d.ts +17 -0
  97. package/components/kbd.js +34 -0
  98. package/components/kbd.js.map +1 -0
  99. package/components/label.d.ts +10 -0
  100. package/components/label.js +20 -0
  101. package/components/label.js.map +1 -0
  102. package/components/menu.d.ts +77 -0
  103. package/components/menu.js +189 -0
  104. package/components/menu.js.map +1 -0
  105. package/components/nav-button.d.ts +34 -0
  106. package/components/nav-button.js +71 -0
  107. package/components/nav-button.js.map +1 -0
  108. package/components/nav.d.ts +39 -0
  109. package/components/nav.js +68 -0
  110. package/components/nav.js.map +1 -0
  111. package/components/number-input.d.ts +16 -0
  112. package/components/number-input.js +25 -0
  113. package/components/number-input.js.map +1 -0
  114. package/components/page.d.ts +14 -0
  115. package/components/page.js +23 -0
  116. package/components/page.js.map +1 -0
  117. package/components/pagination.d.ts +15 -0
  118. package/components/pagination.js +63 -0
  119. package/components/pagination.js.map +1 -0
  120. package/components/ping.d.ts +38 -0
  121. package/components/ping.js +50 -0
  122. package/components/ping.js.map +1 -0
  123. package/components/popover.d.ts +5 -0
  124. package/components/popover.js +25 -0
  125. package/components/popover.js.map +1 -0
  126. package/components/radio-group.d.ts +35 -0
  127. package/components/radio-group.js +81 -0
  128. package/components/radio-group.js.map +1 -0
  129. package/components/relative-time.d.ts +25 -0
  130. package/components/relative-time.js +51 -0
  131. package/components/relative-time.js.map +1 -0
  132. package/components/saved-mark.d.ts +15 -0
  133. package/components/saved-mark.js +45 -0
  134. package/components/saved-mark.js.map +1 -0
  135. package/components/search-input.d.ts +13 -0
  136. package/components/search-input.js +44 -0
  137. package/components/search-input.js.map +1 -0
  138. package/components/section-header.d.ts +83 -0
  139. package/components/section-header.js +170 -0
  140. package/components/section-header.js.map +1 -0
  141. package/components/segmented-control.d.ts +24 -0
  142. package/components/segmented-control.js +70 -0
  143. package/components/segmented-control.js.map +1 -0
  144. package/components/select.d.ts +28 -0
  145. package/components/select.js +129 -0
  146. package/components/select.js.map +1 -0
  147. package/components/shortcut-keys.d.ts +14 -0
  148. package/components/shortcut-keys.js +32 -0
  149. package/components/shortcut-keys.js.map +1 -0
  150. package/components/shortcuts-dialog.d.ts +10 -0
  151. package/components/shortcuts-dialog.js +92 -0
  152. package/components/shortcuts-dialog.js.map +1 -0
  153. package/components/skeleton.d.ts +8 -0
  154. package/components/skeleton.js +23 -0
  155. package/components/skeleton.js.map +1 -0
  156. package/components/sparkline.d.ts +25 -0
  157. package/components/sparkline.js +70 -0
  158. package/components/sparkline.js.map +1 -0
  159. package/components/stack.d.ts +21 -0
  160. package/components/stack.js +45 -0
  161. package/components/stack.js.map +1 -0
  162. package/components/stat-strip.d.ts +43 -0
  163. package/components/stat-strip.js +151 -0
  164. package/components/stat-strip.js.map +1 -0
  165. package/components/steps.d.ts +19 -0
  166. package/components/steps.js +25 -0
  167. package/components/steps.js.map +1 -0
  168. package/components/switch.d.ts +16 -0
  169. package/components/switch.js +29 -0
  170. package/components/switch.js.map +1 -0
  171. package/components/tabs.d.ts +18 -0
  172. package/components/tabs.js +65 -0
  173. package/components/tabs.js.map +1 -0
  174. package/components/text-link.d.ts +16 -0
  175. package/components/text-link.js +32 -0
  176. package/components/text-link.js.map +1 -0
  177. package/components/textarea.d.ts +10 -0
  178. package/components/textarea.js +23 -0
  179. package/components/textarea.js.map +1 -0
  180. package/components/theme.d.ts +28 -0
  181. package/components/theme.js +158 -0
  182. package/components/theme.js.map +1 -0
  183. package/components/time-series-chart.d.ts +122 -0
  184. package/components/time-series-chart.js +722 -0
  185. package/components/time-series-chart.js.map +1 -0
  186. package/components/toaster.d.ts +3 -0
  187. package/components/toaster.js +44 -0
  188. package/components/toaster.js.map +1 -0
  189. package/components/tooltip.d.ts +64 -0
  190. package/components/tooltip.js +126 -0
  191. package/components/tooltip.js.map +1 -0
  192. package/components/view-picker.d.ts +20 -0
  193. package/components/view-picker.js +24 -0
  194. package/components/view-picker.js.map +1 -0
  195. package/fonts/inter-OFL.txt +93 -0
  196. package/fonts/inter-cyrillic-ext.woff2 +0 -0
  197. package/fonts/inter-cyrillic.woff2 +0 -0
  198. package/fonts/inter-greek-ext.woff2 +0 -0
  199. package/fonts/inter-greek.woff2 +0 -0
  200. package/fonts/inter-latin-ext.woff2 +0 -0
  201. package/fonts/inter-latin.woff2 +0 -0
  202. package/fonts/inter-vietnamese.woff2 +0 -0
  203. package/fonts.css +83 -0
  204. package/lib/animation.d.ts +14 -0
  205. package/lib/animation.js +20 -0
  206. package/lib/animation.js.map +1 -0
  207. package/lib/aria.d.ts +5 -0
  208. package/lib/aria.js +12 -0
  209. package/lib/aria.js.map +1 -0
  210. package/lib/chart-curve.d.ts +13 -0
  211. package/lib/chart-curve.js +79 -0
  212. package/lib/chart-curve.js.map +1 -0
  213. package/lib/classes.d.ts +134 -0
  214. package/lib/classes.js +152 -0
  215. package/lib/classes.js.map +1 -0
  216. package/lib/color.d.ts +25 -0
  217. package/lib/color.js +149 -0
  218. package/lib/color.js.map +1 -0
  219. package/lib/day.d.ts +9 -0
  220. package/lib/day.js +20 -0
  221. package/lib/day.js.map +1 -0
  222. package/lib/design-tokens.d.ts +56 -0
  223. package/lib/design-tokens.js +196 -0
  224. package/lib/design-tokens.js.map +1 -0
  225. package/lib/icon.d.ts +17 -0
  226. package/lib/icon.js +34 -0
  227. package/lib/icon.js.map +1 -0
  228. package/lib/icons.d.ts +23 -0
  229. package/lib/icons.js +28 -0
  230. package/lib/icons.js.map +1 -0
  231. package/lib/interactive.d.ts +21 -0
  232. package/lib/interactive.js +31 -0
  233. package/lib/interactive.js.map +1 -0
  234. package/lib/media.d.ts +58 -0
  235. package/lib/media.js +116 -0
  236. package/lib/media.js.map +1 -0
  237. package/lib/segmented.d.ts +57 -0
  238. package/lib/segmented.js +83 -0
  239. package/lib/segmented.js.map +1 -0
  240. package/lib/shortcuts.d.ts +94 -0
  241. package/lib/shortcuts.js +271 -0
  242. package/lib/shortcuts.js.map +1 -0
  243. package/lib/slot.d.ts +9 -0
  244. package/lib/slot.js +18 -0
  245. package/lib/slot.js.map +1 -0
  246. package/lib/time-series.d.ts +100 -0
  247. package/lib/time-series.js +144 -0
  248. package/lib/time-series.js.map +1 -0
  249. package/lib/tone.d.ts +6 -0
  250. package/lib/tone.js +0 -0
  251. package/lib/variants.d.ts +12 -0
  252. package/lib/variants.js +14 -0
  253. package/lib/variants.js.map +1 -0
  254. package/package.json +110 -3
  255. package/styles.css +619 -0
  256. package/tokens.d.ts +104 -0
  257. package/tokens.js +108 -0
  258. package/tokens.js.map +1 -0
  259. package/publish.log +0 -1
@@ -0,0 +1,722 @@
1
+ "use client";
2
+ import { CHART_TRANSITION } from "../lib/animation.js";
3
+ import { splitCurve } from "../lib/chart-curve.js";
4
+ import { tooltipLabelClasses } from "../lib/classes.js";
5
+ import { useElementWidth } from "../lib/media.js";
6
+ import { AREA_FADE, SERIES_COLORS, bucketNotes, countPartialStart, countRunning, domainTicks, niceTicks, pickTicks, seriesTotal } from "../lib/time-series.js";
7
+ import { cn } from "../lib/variants.js";
8
+ import { ChartTooltipContent } from "./chart.js";
9
+ import { jsx, jsxs } from "react/jsx-runtime";
10
+ import { useDeferredValue, useId, useRef, useState } from "react";
11
+ import { curveLinear, curveMonotoneX } from "d3-shape";
12
+ import { areaY, barY, createMark, defineChart, group, lineY } from "@tanstack/charts";
13
+ import { d3Curve } from "@tanstack/charts/d3/shape";
14
+ import { focusGuideX } from "@tanstack/charts/focus/guide";
15
+ import { motion } from "@tanstack/charts/motion";
16
+ import { RendererChart } from "@tanstack/charts/react/tooltip";
17
+ import { scaleBand } from "@tanstack/charts/scales/band";
18
+ import { scaleLinear } from "@tanstack/charts/scales/linear";
19
+ import { tooltip } from "@tanstack/charts/tooltip";
20
+ import { portal } from "@tanstack/charts/tooltip/portal";
21
+ // src/components/time-series-chart.tsx
22
+ const DEFAULT_COLOR = "var(--color-chart-1)";
23
+ const BAR_COLOR = "var(--color-chart-2)";
24
+ const NOTE_COLOR = "var(--color-chart-3)";
25
+ /**
26
+ * The space between a plot edge that carries value labels and the data nearest it. The grid
27
+ * lines run the full width; the line or the outermost bar stops this far short of the labels.
28
+ */
29
+ const PLOT_INSET = 40;
30
+ const PLOT_HEIGHT = 256;
31
+ /** The `text-2xs` size in px, as the chart renderer takes it. */
32
+ const TICK_FONT_SIZE = 11;
33
+ const GRID = {
34
+ stroke: "var(--color-edge)",
35
+ strokeOpacity: 1,
36
+ strokeDasharray: "2 3"
37
+ };
38
+ const BAR_RADIUS = [
39
+ 2,
40
+ 2,
41
+ 0,
42
+ 0
43
+ ];
44
+ const BAR_MAX_WIDTH = 18;
45
+ /** The share of the space between buckets that the renderer gives a bucket's bars. */
46
+ const BAR_SHARE = .8;
47
+ /** The band a lone bucket's bars get, with no neighbour to measure the space against. */
48
+ const LONE_BANDWIDTH = 48;
49
+ /** The gaps around a compared pair's two bars, as a share of the pair's band. */
50
+ const PAIR_PADDING = .08;
51
+ /** Half the diagonal of the 9px square a note marker is, stood on its corner. */
52
+ const NOTE_RADIUS = 6.4;
53
+ /** A chart moves when its numbers change. Its first draw is not a change, so it lands at once. */
54
+ const RENDERER = motion({
55
+ initial: false,
56
+ transition: CHART_TRANSITION
57
+ });
58
+ /**
59
+ * Room for one date label. Ticks are thinned to what the plot can hold at this pitch, so a
60
+ * week shows every day on a wide card and a 180-day range still reads on a narrow one.
61
+ */
62
+ const TICK_PITCH = 80;
63
+ /** A tick label's width per character, as a share of its font size: the average advance. */
64
+ const TICK_CHARACTER_WIDTH = .6;
65
+ /** Notes listed in one tooltip before the rest are summed up. */
66
+ const TOOLTIP_NOTES = 2;
67
+ const DAY_MS = 864e5;
68
+ /**
69
+ * How far a bucket's bars reach either side of its time, given the band the renderer sizes
70
+ * them from: half a bar, or half a compared pair of them. Follows the renderer's own sizing,
71
+ * so the outermost bar can be placed to the pixel.
72
+ */
73
+ function measureBarReach(bandwidth, paired) {
74
+ if (!paired) return Math.min(bandwidth, BAR_MAX_WIDTH) / 2;
75
+ const step = bandwidth / 2.08;
76
+ return (step + Math.min(.92 * step, BAR_MAX_WIDTH)) / 2;
77
+ }
78
+ /** The small line over a note's text: whichever of author and day apply, or nothing at all. */
79
+ function Eyebrow({ parts }) {
80
+ const shown = parts.filter(Boolean);
81
+ if (!shown.length) return null;
82
+ return /* @__PURE__ */ jsx("div", {
83
+ className: cn(tooltipLabelClasses, "truncate text-2xs"),
84
+ children: shown.join(" · ")
85
+ });
86
+ }
87
+ /**
88
+ * The hovered bucket's notes under its figures, each with the marker's colour down its left
89
+ * edge and a small line naming the author and the day. The day is left off only where the
90
+ * tooltip's own heading already says it: a bucket no wider than a day, holding a note on that
91
+ * very instant. Then what a click does, where the chart takes one; with neither notes nor a
92
+ * click to explain it renders nothing at all.
93
+ */
94
+ function NotesFooter({ notes, bucket, end, hint, color, formatDate }) {
95
+ const shown = notes?.slice(0, TOOLTIP_NOTES) ?? [];
96
+ const rest = (notes?.length ?? 0) - shown.length;
97
+ const dayBucket = bucket !== void 0 && end !== void 0 && end - bucket <= DAY_MS;
98
+ if (!shown.length) return hint ? /* @__PURE__ */ jsx("div", {
99
+ className: tooltipLabelClasses,
100
+ children: hint
101
+ }) : null;
102
+ return /* @__PURE__ */ jsxs("div", {
103
+ className: "grid gap-1.5",
104
+ children: [
105
+ shown.map((note, index) => /* @__PURE__ */ jsxs("div", {
106
+ className: "min-w-0 border-l-2 border-(--color-note) pl-2",
107
+ style: { "--color-note": color },
108
+ children: [/* @__PURE__ */ jsx(Eyebrow, { parts: [note.author, !(dayBucket && note.date.getTime() === bucket) && formatDate(note.date)] }), /* @__PURE__ */ jsx("div", {
109
+ className: "truncate font-medium",
110
+ children: note.text
111
+ })]
112
+ }, index)),
113
+ rest > 0 && /* @__PURE__ */ jsxs("div", {
114
+ className: "text-ink-muted",
115
+ children: [
116
+ "and ",
117
+ rest,
118
+ " more ",
119
+ rest === 1 ? "note" : "notes"
120
+ ]
121
+ }),
122
+ hint && /* @__PURE__ */ jsx("div", {
123
+ className: cn("text-ink-muted", "mt-0.5 border-t pt-2", "border-edge"),
124
+ children: hint
125
+ })
126
+ ]
127
+ });
128
+ }
129
+ /**
130
+ * One entry per line of a multi-series plot: the line's swatch, its name and its total over
131
+ * the range. Left-aligned above the plot, so the names are read before the lines they label.
132
+ */
133
+ function SeriesLegend({ series, totals, formatValue }) {
134
+ return /* @__PURE__ */ jsx("div", {
135
+ className: "mb-3 flex flex-wrap gap-x-4 gap-y-1 text-xs",
136
+ children: series.map((entry) => /* @__PURE__ */ jsxs("div", {
137
+ className: "flex items-center gap-1.5",
138
+ children: [
139
+ /* @__PURE__ */ jsx("span", {
140
+ "aria-hidden": true,
141
+ className: "h-0.5 w-2.5 shrink-0 rounded-[1px] bg-(--color-swatch)",
142
+ style: { "--color-swatch": entry.color }
143
+ }),
144
+ /* @__PURE__ */ jsx("span", {
145
+ className: "text-ink-secondary",
146
+ children: entry.label
147
+ }),
148
+ totals && /* @__PURE__ */ jsx("span", {
149
+ className: "font-medium tabular-nums",
150
+ children: formatValue(totals.get(entry.key) ?? 0)
151
+ })
152
+ ]
153
+ }, entry.key))
154
+ });
155
+ }
156
+ /** The dot a line shows at the hovered bucket, ringed in the card's colour. */
157
+ function focusMarker(color) {
158
+ return {
159
+ radius: 4,
160
+ fill: color,
161
+ stroke: "var(--color-surface)",
162
+ strokeWidth: 2
163
+ };
164
+ }
165
+ /** Whether a bucket has an outline segment to stack on its bar. */
166
+ function isOutlined(point) {
167
+ return (point.outline ?? 0) > 0;
168
+ }
169
+ /**
170
+ * A diamond in the note colour on the baseline of each bucket holding a note, the shape
171
+ * Plausible settled on: a dot the size of the line would read as a data point, and one small
172
+ * enough not to would read as nothing. Decorative, so hovering it reports its bucket as usual.
173
+ */
174
+ function noteMarkers(times, baseline, scaleId, color) {
175
+ return createMark(() => ({
176
+ id: "notes",
177
+ channels: {
178
+ x: {
179
+ scale: "x",
180
+ values: times
181
+ },
182
+ y: {
183
+ scale: scaleId,
184
+ values: [baseline]
185
+ }
186
+ },
187
+ render({ scales }) {
188
+ const xScale = scales.x;
189
+ const yScale = scales[scaleId];
190
+ if (!xScale || !yScale) return { nodes: [] };
191
+ const cy = yScale.map(baseline);
192
+ return { nodes: times.map((time) => {
193
+ const cx = xScale.map(time);
194
+ return {
195
+ kind: "area",
196
+ key: `note-${time}`,
197
+ points: [
198
+ [cx, cy - NOTE_RADIUS],
199
+ [cx + NOTE_RADIUS, cy],
200
+ [cx, cy + NOTE_RADIUS],
201
+ [cx - NOTE_RADIUS, cy]
202
+ ],
203
+ style: {
204
+ fill: color,
205
+ stroke: "var(--color-surface)",
206
+ strokeWidth: 1.5
207
+ }
208
+ };
209
+ }) };
210
+ }
211
+ }));
212
+ }
213
+ /**
214
+ * A value tick with no label. The axis itself stays, because the grid lines follow its ticks.
215
+ */
216
+ function formatNoTick() {
217
+ return "";
218
+ }
219
+ /**
220
+ * The single time series: a smooth gradient area with a right-hand value axis on round grid
221
+ * lines and evenly spaced date ticks along the bottom. A compared period draws as a dashed
222
+ * line in the same hue. Notes pinned to dates mark their bucket on the baseline and list
223
+ * themselves in its tooltip. With `series` set it is several plain lines on that same axis,
224
+ * under a legend of totals.
225
+ */
226
+ function TimeSeriesChart({ data, label, color = DEFAULT_COLOR, series, seriesTotals = true, formatTick, formatValue, formatDate, formatNoteDate = formatDate, formatTooltipDate, domain, curve = "smooth", shape = "line", barLabel = "", outlineLabel = "", barColor = BAR_COLOR, formatBarTick = formatTick, formatBarValue = formatValue, notes = [], noteColor = NOTE_COLOR, bucketEnd, onPointClick, pointClickHint, hideValueAxis = false, className, ...props }) {
227
+ const asBars = shape === "bar";
228
+ const previousColor = `color-mix(in oklab, ${barColor} 45%, transparent)`;
229
+ const hatchId = `hatch-${useId()}`;
230
+ const lines = (series ?? []).map((entry, index) => ({
231
+ ...entry,
232
+ color: entry.color ?? SERIES_COLORS[index % SERIES_COLORS.length] ?? DEFAULT_COLOR
233
+ }));
234
+ const multi = lines.length > 0;
235
+ const compared = !multi && data.some((point) => point.previous !== void 0);
236
+ const hasOutline = data.some(isOutlined);
237
+ const hasBars = asBars || hasOutline || data.some((point) => (point.bar ?? 0) > 0);
238
+ const plotted = useDeferredValue(true, false);
239
+ const [element, setElement] = useState(null);
240
+ const plotWidth = useElementWidth(element, { deferred: false }) ?? 0;
241
+ const pairs = asBars && compared;
242
+ const insetLeft = hasBars && !asBars && !hideValueAxis ? PLOT_INSET : 0;
243
+ const insetRight = hideValueAxis ? 0 : PLOT_INSET;
244
+ const times = data.map((point) => point.date.getTime());
245
+ const first = times[0] ?? 0;
246
+ const last = times[times.length - 1] ?? first;
247
+ const closest = Math.min(...times.slice(1).map((time, index) => time - (times[index] ?? 0)));
248
+ function reachAt(width) {
249
+ if (!hasBars) return 0;
250
+ return measureBarReach(last > first ? BAR_SHARE * width * closest / (last - first) : LONE_BANDWIDTH, pairs);
251
+ }
252
+ const edgeSpan = plotWidth - insetLeft - insetRight;
253
+ const reach = reachAt(edgeSpan - reachAt(edgeSpan) * 2);
254
+ const startOffset = insetLeft + reach;
255
+ const endOffset = insetRight + reach;
256
+ const bucketSpan = plotWidth - startOffset - endOffset;
257
+ const firstDate = data[0]?.date;
258
+ const labelWidth = firstDate ? formatDate(firstDate).length * TICK_FONT_SIZE * TICK_CHARACTER_WIDTH : 0;
259
+ const tickPitch = Math.max(TICK_PITCH, labelWidth * 1.5);
260
+ const tickCount = Math.max(2, Math.floor(bucketSpan / tickPitch) + 1);
261
+ const previousLabel = `${label} (previous)`;
262
+ const dismissTooltip = useRef(void 0);
263
+ const touched = useRef(false);
264
+ function dismissAfterTouch() {
265
+ if (touched.current) setTimeout(() => dismissTooltip.current?.(), 0);
266
+ }
267
+ function onLift(event) {
268
+ touched.current = event.pointerType !== "mouse";
269
+ dismissAfterTouch();
270
+ }
271
+ const lastDate = data[data.length - 1]?.date;
272
+ const endOfLast = bucketEnd && lastDate ? bucketEnd(lastDate).getTime() : void 0;
273
+ const bucketed = bucketNotes(times, notes, endOfLast);
274
+ const points = data.map((point, index) => {
275
+ const time = times[index] ?? 0;
276
+ const end = times[index + 1] ?? endOfLast ?? time + (time - (times[index - 1] ?? time));
277
+ return {
278
+ ...point,
279
+ time,
280
+ end,
281
+ notes: bucketed.get(time) ?? []
282
+ };
283
+ });
284
+ const xTicks = pickTicks(times, Math.min(tickCount, points.length));
285
+ const labels = new Map(xTicks.map((tick) => [tick, formatDate(new Date(tick))]));
286
+ const values = asBars ? [0] : multi ? data.flatMap((point) => lines.map((line) => point.values?.[line.key] ?? 0)) : data.flatMap((point) => [point.value, ...point.previous === void 0 ? [] : [point.previous]]);
287
+ const max = Math.max(...values, 0);
288
+ const totals = seriesTotals ? new Map(lines.map((line) => [line.key, seriesTotal(data, line.key)])) : void 0;
289
+ const yTicks = domain ? domainTicks(domain) : niceTicks(max, {
290
+ integer: values.every(Number.isInteger),
291
+ format: formatTick
292
+ });
293
+ const [low, top] = domain ?? [0, yTicks[yTicks.length - 1] ?? 1];
294
+ const bars = data.flatMap((point) => [(point.bar ?? 0) + (point.outline ?? 0), ...asBars && point.previous !== void 0 ? [point.previous] : []]);
295
+ const barMax = Math.max(...bars, 0);
296
+ const barTicks = niceTicks(asBars ? barMax : barMax * 2, {
297
+ integer: bars.every(Number.isInteger),
298
+ format: formatBarTick
299
+ });
300
+ const barTop = barTicks[barTicks.length - 1] ?? 1;
301
+ /**
302
+ * The hovered bucket's rows: the chart's own series first, its compared period under it, the
303
+ * outline last, whatever the draw order. Several lines read largest first, so the tooltip's
304
+ * order is the order the lines stack in at that point. A bucket that took no money and lost
305
+ * none has no bar or outline row to report; a bar chart's own series stays whatever its value.
306
+ */
307
+ function tooltipItems(point) {
308
+ const bar = point.bar ?? 0;
309
+ const outline = point.outline ?? 0;
310
+ const barRow = hasBars && (asBars || bar > 0) ? [{
311
+ key: "bar",
312
+ label: asBars ? label : barLabel,
313
+ value: formatBarValue(bar),
314
+ color: barColor
315
+ }] : [];
316
+ if (multi) return [...lines.map((line) => ({
317
+ line,
318
+ value: point.values?.[line.key] ?? 0
319
+ })).sort((a, b) => b.value - a.value).map(({ line, value }) => ({
320
+ key: line.key,
321
+ label: line.label,
322
+ value: formatValue(value),
323
+ color: line.color
324
+ })), ...barRow];
325
+ const valueRow = asBars ? [] : [{
326
+ key: "value",
327
+ label,
328
+ value: formatValue(point.value),
329
+ color
330
+ }];
331
+ const previousRow = compared && point.previous !== void 0 ? [{
332
+ key: "previous",
333
+ label: previousLabel,
334
+ value: (asBars ? formatBarValue : formatValue)(point.previous),
335
+ color: asBars ? previousColor : color,
336
+ indicator: "outline"
337
+ }] : [];
338
+ const outlineRow = hasOutline && outline > 0 ? [{
339
+ key: "outline",
340
+ label: outlineLabel,
341
+ value: formatBarValue(outline),
342
+ color: barColor,
343
+ indicator: "outline"
344
+ }] : [];
345
+ return asBars ? [
346
+ ...barRow,
347
+ ...previousRow,
348
+ ...outlineRow
349
+ ] : [
350
+ ...valueRow,
351
+ ...previousRow,
352
+ ...barRow,
353
+ ...outlineRow
354
+ ];
355
+ }
356
+ const curveFactory = curve === "smooth" ? curveMonotoneX : curveLinear;
357
+ const lineCurve = d3Curve(curveFactory);
358
+ const running = countRunning(data);
359
+ const leading = countPartialStart(data);
360
+ const split = running + leading > 0 ? splitCurve(curveFactory, running, leading) : void 0;
361
+ const span = Math.max(last - first, 1);
362
+ const msPerPixel = bucketSpan > 0 ? span / bucketSpan : 0;
363
+ const timeDomain = [first - startOffset * msPerPixel, first + span + endOffset * msPerPixel];
364
+ const pairLayout = group({ scale: scaleBand().domain(["previous", "bar"]).padding(PAIR_PADDING) });
365
+ const definition = defineChart({
366
+ marks: [
367
+ ...!asBars && !multi ? [areaY(points, {
368
+ id: "area",
369
+ x: "time",
370
+ y1: low,
371
+ y2: "value",
372
+ fill: "url(#fill)",
373
+ fillOpacity: 1,
374
+ curve: lineCurve
375
+ })] : [],
376
+ ...pairs ? [barY(points, {
377
+ id: "previous",
378
+ x: "time",
379
+ y1: 0,
380
+ y2: (point) => point.previous ?? 0,
381
+ z: () => "previous",
382
+ yScale: "bar",
383
+ fill: `url(#${hatchId})`,
384
+ radius: [...BAR_RADIUS],
385
+ maxThickness: BAR_MAX_WIDTH,
386
+ layout: pairLayout
387
+ })] : [],
388
+ ...hasOutline ? [barY(points, {
389
+ id: "outline",
390
+ x: "time",
391
+ y1: 0,
392
+ y2: (point) => (point.bar ?? 0) + (point.outline ?? 0),
393
+ z: () => "bar",
394
+ yScale: "bar",
395
+ fill: barColor,
396
+ fillOpacity: .15,
397
+ stroke: (point) => isOutlined(point) ? barColor : "transparent",
398
+ strokeOpacity: .7,
399
+ strokeDasharray: "3 2",
400
+ strokeWidth: 1,
401
+ inset: .5,
402
+ radius: [...BAR_RADIUS],
403
+ maxThickness: 17,
404
+ ...pairs && { layout: pairLayout }
405
+ })] : [],
406
+ ...hasBars ? [
407
+ barY(points, {
408
+ id: "bar-mask",
409
+ x: "time",
410
+ y1: 0,
411
+ y2: (point) => point.partial ? 0 : point.bar ?? 0,
412
+ z: () => "bar",
413
+ yScale: "bar",
414
+ fill: "var(--color-surface)",
415
+ radius: (point) => isOutlined(point) ? 0 : [...BAR_RADIUS],
416
+ maxThickness: BAR_MAX_WIDTH,
417
+ ...pairs && { layout: pairLayout }
418
+ }),
419
+ barY(points, {
420
+ id: "bar",
421
+ x: "time",
422
+ y1: 0,
423
+ y2: (point) => point.partial ? 0 : point.bar ?? 0,
424
+ z: () => "bar",
425
+ yScale: "bar",
426
+ fill: barColor,
427
+ fillOpacity: .7,
428
+ radius: (point) => isOutlined(point) ? 0 : [...BAR_RADIUS],
429
+ maxThickness: BAR_MAX_WIDTH,
430
+ ...pairs && { layout: pairLayout }
431
+ }),
432
+ ...running + leading > 0 ? [barY(points, {
433
+ id: "bar-running",
434
+ x: "time",
435
+ y1: 0,
436
+ y2: (point) => point.partial ? point.bar ?? 0 : 0,
437
+ z: () => "bar",
438
+ yScale: "bar",
439
+ fill: barColor,
440
+ fillOpacity: .15,
441
+ stroke: (point) => point.partial && (point.bar ?? 0) > 0 ? barColor : "transparent",
442
+ strokeOpacity: .7,
443
+ strokeDasharray: "3 2",
444
+ strokeWidth: 1,
445
+ inset: .5,
446
+ radius: [...BAR_RADIUS],
447
+ maxThickness: 17,
448
+ ...pairs && { layout: pairLayout }
449
+ })] : []
450
+ ] : [],
451
+ ...compared && !asBars ? [lineY(points, {
452
+ id: "previous-line",
453
+ x: "time",
454
+ y: (point) => point.previous,
455
+ stroke: color,
456
+ strokeWidth: 1.5,
457
+ strokeDasharray: "4 3",
458
+ strokeOpacity: .7,
459
+ curve: lineCurve
460
+ })] : [],
461
+ ...!asBars && !multi ? [lineY(points, {
462
+ id: "value",
463
+ x: "time",
464
+ y: "value",
465
+ stroke: color,
466
+ strokeWidth: 2,
467
+ curve: split?.complete ?? lineCurve
468
+ }), ...split ? [lineY(points, {
469
+ id: "value-running",
470
+ x: "time",
471
+ y: "value",
472
+ stroke: color,
473
+ strokeWidth: 2,
474
+ strokeDasharray: "4 4",
475
+ curve: split.running
476
+ })] : []] : [],
477
+ ...lines.flatMap((line) => [lineY(points, {
478
+ id: `series-${line.key}`,
479
+ x: "time",
480
+ y: (point) => point.values?.[line.key] ?? 0,
481
+ stroke: line.color,
482
+ strokeWidth: 2,
483
+ curve: split?.complete ?? lineCurve
484
+ }), ...split ? [lineY(points, {
485
+ id: `series-${line.key}-running`,
486
+ x: "time",
487
+ y: (point) => point.values?.[line.key] ?? 0,
488
+ stroke: line.color,
489
+ strokeWidth: 2,
490
+ strokeDasharray: "4 4",
491
+ curve: split.running
492
+ })] : []]),
493
+ focusGuideX(points, {
494
+ id: "rule",
495
+ x: "time",
496
+ y: () => low,
497
+ match: "x",
498
+ xRule: {
499
+ stroke: "var(--chart-rule)",
500
+ strokeOpacity: 1,
501
+ strokeDasharray: "none"
502
+ },
503
+ motion: false,
504
+ marker: false
505
+ }),
506
+ ...!asBars && !multi ? [focusGuideX(points, {
507
+ id: "value-marker",
508
+ x: "time",
509
+ y: "value",
510
+ match: "x",
511
+ xRule: false,
512
+ motion: false,
513
+ marker: focusMarker(color)
514
+ })] : [],
515
+ ...compared && !asBars ? [focusGuideX(points, {
516
+ id: "previous-marker",
517
+ x: "time",
518
+ y: (point) => point.previous,
519
+ match: "x",
520
+ xRule: false,
521
+ motion: false,
522
+ marker: focusMarker(color)
523
+ })] : [],
524
+ ...lines.map((line) => focusGuideX(points, {
525
+ id: `series-${line.key}-marker`,
526
+ x: "time",
527
+ y: (point) => point.values?.[line.key] ?? 0,
528
+ match: "x",
529
+ xRule: false,
530
+ motion: false,
531
+ marker: focusMarker(line.color)
532
+ })),
533
+ noteMarkers([...bucketed.keys()], asBars ? 0 : low, asBars ? "bar" : "y", noteColor)
534
+ ],
535
+ scales: {
536
+ x: {
537
+ scale: scaleLinear().domain(timeDomain),
538
+ axis: {
539
+ motion: false,
540
+ line: false,
541
+ ticks: {
542
+ values: xTicks,
543
+ size: 0,
544
+ padding: 12,
545
+ format: (time) => labels.get(time) ?? ""
546
+ },
547
+ tickLabels: {
548
+ fontSize: TICK_FONT_SIZE,
549
+ opacity: 1,
550
+ thin: false,
551
+ anchor: ({ value }) => {
552
+ if (value === first && !insetLeft) return "start";
553
+ return value === last && !insetRight ? "end" : "middle";
554
+ },
555
+ dx: ({ value }) => {
556
+ if (value === first && !insetLeft) return -reach;
557
+ return value === last && !insetRight ? reach : 0;
558
+ }
559
+ }
560
+ }
561
+ },
562
+ y: {
563
+ scale: scaleLinear().domain([low, top > low ? top : low + 1]),
564
+ side: "right",
565
+ grid: asBars ? false : GRID,
566
+ axis: asBars ? false : {
567
+ motion: false,
568
+ line: false,
569
+ ticks: {
570
+ values: yTicks,
571
+ size: 0,
572
+ padding: 0,
573
+ format: hideValueAxis ? formatNoTick : formatTick
574
+ },
575
+ tickLabels: {
576
+ fontSize: TICK_FONT_SIZE,
577
+ opacity: 1,
578
+ anchor: "end",
579
+ dy: -8,
580
+ thin: false
581
+ }
582
+ }
583
+ },
584
+ ...hasBars && { bar: {
585
+ channel: "y",
586
+ scale: scaleLinear().domain([0, barTop]),
587
+ side: asBars ? "right" : "left",
588
+ grid: asBars ? GRID : false,
589
+ axis: {
590
+ motion: false,
591
+ line: false,
592
+ ticks: {
593
+ values: barTicks,
594
+ size: 0,
595
+ padding: 0,
596
+ format: hideValueAxis ? formatNoTick : formatBarTick
597
+ },
598
+ tickLabels: {
599
+ fontSize: TICK_FONT_SIZE,
600
+ opacity: 1,
601
+ anchor: asBars ? "end" : "start",
602
+ dy: -8,
603
+ thin: false
604
+ }
605
+ }
606
+ } }
607
+ },
608
+ gradients: [{
609
+ id: "fill",
610
+ x1: 0,
611
+ y1: 0,
612
+ x2: 0,
613
+ y2: 1,
614
+ stops: AREA_FADE.map((stop) => ({
615
+ ...stop,
616
+ color
617
+ }))
618
+ }],
619
+ theme: {
620
+ foreground: "var(--color-ink)",
621
+ muted: "var(--color-ink-muted)",
622
+ grid: "var(--color-edge)",
623
+ background: "transparent"
624
+ },
625
+ margin: {
626
+ top: 16,
627
+ right: 0,
628
+ left: 0
629
+ },
630
+ focus: "group-x",
631
+ maxFocusDistance: Number.POSITIVE_INFINITY,
632
+ focusRing: false,
633
+ tooltip: {
634
+ use: tooltip,
635
+ portal,
636
+ sticky: false,
637
+ motion: false,
638
+ anchor: {
639
+ x: "value",
640
+ y: "plot-top"
641
+ },
642
+ placement: ["bottom-right", "bottom-left"],
643
+ offset: 12
644
+ }
645
+ });
646
+ return /* @__PURE__ */ jsxs("div", {
647
+ ref: setElement,
648
+ className: cn("flex flex-col [--chart-rule:color-mix(in_oklab,var(--color-ink)_25%,transparent)]", onPointClick && "cursor-pointer active:[--chart-rule:var(--color-ink-secondary)]", className),
649
+ ...props,
650
+ onPointerUp: onLift,
651
+ onPointerCancel: onLift,
652
+ children: [
653
+ multi && /* @__PURE__ */ jsx(SeriesLegend, {
654
+ series: lines,
655
+ totals,
656
+ formatValue
657
+ }),
658
+ /* @__PURE__ */ jsx("svg", {
659
+ "aria-hidden": true,
660
+ className: "absolute size-0",
661
+ children: /* @__PURE__ */ jsx("defs", { children: /* @__PURE__ */ jsxs("pattern", {
662
+ id: hatchId,
663
+ width: "5",
664
+ height: "5",
665
+ patternUnits: "userSpaceOnUse",
666
+ patternTransform: "rotate(45)",
667
+ children: [/* @__PURE__ */ jsx("rect", {
668
+ width: "5",
669
+ height: "5",
670
+ fill: barColor,
671
+ fillOpacity: .12
672
+ }), /* @__PURE__ */ jsx("line", {
673
+ x1: "0",
674
+ y1: "0",
675
+ x2: "0",
676
+ y2: "5",
677
+ stroke: barColor,
678
+ strokeWidth: "1.5",
679
+ strokeOpacity: .55
680
+ })]
681
+ }) })
682
+ }),
683
+ plotted ? /* @__PURE__ */ jsx(RendererChart, {
684
+ definition,
685
+ renderer: RENDERER,
686
+ ariaLabel: label,
687
+ height: PLOT_HEIGHT,
688
+ className: cn("w-full touch-pan-y text-xs [&_svg]:overflow-visible", "[&_svg]:outline-none", "[&_[data-ts-key^=bar-tick-label]]:tabular-nums [&_[data-ts-key^=y-tick-label]]:tabular-nums", "[&_[data-ts-key^=bar-tick-label]]:stroke-surface [&_[data-ts-key^=bar-tick-label]]:stroke-3 [&_[data-ts-key^=bar-tick-label]]:[paint-order:stroke] [&_[data-ts-key^=y-tick-label]]:stroke-surface [&_[data-ts-key^=y-tick-label]]:stroke-3 [&_[data-ts-key^=y-tick-label]]:[paint-order:stroke]", "[--ts-chart-tooltip-background:transparent] [--ts-chart-tooltip-border:none] [--ts-chart-tooltip-max-width:none] [--ts-chart-tooltip-padding:0] [--ts-chart-tooltip-shadow:none]"),
689
+ onSelect: (point) => {
690
+ dismissAfterTouch();
691
+ if (!point || !onPointClick) return;
692
+ onPointClick(point.datum.date, new Date(point.datum.end));
693
+ },
694
+ renderTooltipBody: ({ points: focused, dismiss }) => {
695
+ dismissTooltip.current = dismiss;
696
+ const point = focused[0]?.datum;
697
+ if (!point) return null;
698
+ return /* @__PURE__ */ jsx(ChartTooltipContent, {
699
+ title: formatTooltipDate(point.date),
700
+ items: tooltipItems(point),
701
+ details: point.details,
702
+ className: "min-w-48 max-w-56",
703
+ footer: /* @__PURE__ */ jsx(NotesFooter, {
704
+ notes: point.notes,
705
+ bucket: point.time,
706
+ end: point.end,
707
+ hint: onPointClick ? pointClickHint : void 0,
708
+ color: noteColor,
709
+ formatDate: formatNoteDate
710
+ })
711
+ });
712
+ }
713
+ }) : /* @__PURE__ */ jsx("div", {
714
+ className: "w-full",
715
+ style: { height: PLOT_HEIGHT }
716
+ })
717
+ ]
718
+ });
719
+ }
720
+ export { TimeSeriesChart };
721
+
722
+ //# sourceMappingURL=time-series-chart.js.map