@contui/ui 0.0.0-stage → 0.1.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 (427) hide show
  1. package/LICENSE +30 -0
  2. package/README.md +150 -2
  3. package/cli/contui.mjs +250 -0
  4. package/dist/charts/area-chart.d.ts +8 -0
  5. package/dist/charts/area-chart.js +9 -0
  6. package/dist/charts/bar-chart.d.ts +9 -0
  7. package/dist/charts/bar-chart.js +10 -0
  8. package/dist/charts/bubble-chart.d.ts +60 -0
  9. package/dist/charts/bubble-chart.js +392 -0
  10. package/dist/charts/bullet-chart.d.ts +53 -0
  11. package/dist/charts/bullet-chart.js +123 -0
  12. package/dist/charts/candlestick-chart.d.ts +77 -0
  13. package/dist/charts/candlestick-chart.js +558 -0
  14. package/dist/charts/cartesian.d.ts +121 -0
  15. package/dist/charts/cartesian.js +625 -0
  16. package/dist/charts/chart-card.d.ts +80 -0
  17. package/dist/charts/chart-card.js +114 -0
  18. package/dist/charts/donut-chart.d.ts +40 -0
  19. package/dist/charts/donut-chart.js +88 -0
  20. package/dist/charts/funnel-chart.d.ts +16 -0
  21. package/dist/charts/funnel-chart.js +164 -0
  22. package/dist/charts/horizontal-bar-chart.d.ts +67 -0
  23. package/dist/charts/horizontal-bar-chart.js +481 -0
  24. package/dist/charts/index.d.ts +21 -0
  25. package/dist/charts/index.js +32 -0
  26. package/dist/charts/legend.d.ts +23 -0
  27. package/dist/charts/legend.js +22 -0
  28. package/dist/charts/lib/candlestick-window.d.ts +15 -0
  29. package/dist/charts/lib/candlestick-window.js +23 -0
  30. package/dist/charts/lib/color.d.ts +39 -0
  31. package/dist/charts/lib/color.js +83 -0
  32. package/dist/charts/lib/format.d.ts +11 -0
  33. package/dist/charts/lib/format.js +33 -0
  34. package/dist/charts/lib/frame.d.ts +6 -0
  35. package/dist/charts/lib/frame.js +6 -0
  36. package/dist/charts/lib/layout.d.ts +11 -0
  37. package/dist/charts/lib/layout.js +71 -0
  38. package/dist/charts/lib/path.d.ts +35 -0
  39. package/dist/charts/lib/path.js +203 -0
  40. package/dist/charts/lib/scale.d.ts +41 -0
  41. package/dist/charts/lib/scale.js +80 -0
  42. package/dist/charts/lib/use-active.d.ts +18 -0
  43. package/dist/charts/lib/use-active.js +71 -0
  44. package/dist/charts/lib/use-size.d.ts +9 -0
  45. package/dist/charts/lib/use-size.js +40 -0
  46. package/dist/charts/line-chart.d.ts +7 -0
  47. package/dist/charts/line-chart.js +8 -0
  48. package/dist/charts/radar-chart.d.ts +27 -0
  49. package/dist/charts/radar-chart.js +165 -0
  50. package/dist/charts/rose-chart.d.ts +35 -0
  51. package/dist/charts/rose-chart.js +242 -0
  52. package/dist/charts/sparkline.d.ts +35 -0
  53. package/dist/charts/sparkline.js +110 -0
  54. package/dist/charts/tooltip.d.ts +35 -0
  55. package/dist/charts/tooltip.js +22 -0
  56. package/dist/charts/treemap.d.ts +28 -0
  57. package/dist/charts/treemap.js +103 -0
  58. package/dist/charts/uptime-strip.d.ts +40 -0
  59. package/dist/charts/uptime-strip.js +178 -0
  60. package/dist/components/accordion.d.ts +31 -0
  61. package/dist/components/accordion.js +34 -0
  62. package/dist/components/activity-item.d.ts +12 -0
  63. package/dist/components/activity-item.js +6 -0
  64. package/dist/components/alert.d.ts +21 -0
  65. package/dist/components/alert.js +58 -0
  66. package/dist/components/assistant-answer.d.ts +52 -0
  67. package/dist/components/assistant-answer.js +40 -0
  68. package/dist/components/assistant-prompt.d.ts +63 -0
  69. package/dist/components/assistant-prompt.js +122 -0
  70. package/dist/components/avatar.d.ts +28 -0
  71. package/dist/components/avatar.js +75 -0
  72. package/dist/components/badge.d.ts +16 -0
  73. package/dist/components/badge.js +33 -0
  74. package/dist/components/bar-list.d.ts +65 -0
  75. package/dist/components/bar-list.js +119 -0
  76. package/dist/components/block-slider.d.ts +43 -0
  77. package/dist/components/block-slider.js +63 -0
  78. package/dist/components/branch-graph.d.ts +87 -0
  79. package/dist/components/branch-graph.js +231 -0
  80. package/dist/components/breadcrumb.d.ts +41 -0
  81. package/dist/components/breadcrumb.js +53 -0
  82. package/dist/components/button.d.ts +23 -0
  83. package/dist/components/button.js +108 -0
  84. package/dist/components/calendar.d.ts +5 -0
  85. package/dist/components/calendar.js +90 -0
  86. package/dist/components/card.d.ts +65 -0
  87. package/dist/components/card.js +104 -0
  88. package/dist/components/chat-attachment.d.ts +47 -0
  89. package/dist/components/chat-attachment.js +63 -0
  90. package/dist/components/chat-event.d.ts +7 -0
  91. package/dist/components/chat-event.js +7 -0
  92. package/dist/components/chat-message.d.ts +59 -0
  93. package/dist/components/chat-message.js +91 -0
  94. package/dist/components/chat-reference.d.ts +20 -0
  95. package/dist/components/chat-reference.js +41 -0
  96. package/dist/components/chat-shared.d.ts +21 -0
  97. package/dist/components/chat-shared.js +23 -0
  98. package/dist/components/chat-suggestions.d.ts +59 -0
  99. package/dist/components/chat-suggestions.js +88 -0
  100. package/dist/components/chat-thread.d.ts +39 -0
  101. package/dist/components/chat-thread.js +172 -0
  102. package/dist/components/chat-typing.d.ts +35 -0
  103. package/dist/components/chat-typing.js +66 -0
  104. package/dist/components/checkbox.d.ts +18 -0
  105. package/dist/components/checkbox.js +33 -0
  106. package/dist/components/checklist.d.ts +26 -0
  107. package/dist/components/checklist.js +18 -0
  108. package/dist/components/chip.d.ts +29 -0
  109. package/dist/components/chip.js +61 -0
  110. package/dist/components/code-input.d.ts +40 -0
  111. package/dist/components/code-input.js +285 -0
  112. package/dist/components/color-picker.d.ts +38 -0
  113. package/dist/components/color-picker.js +855 -0
  114. package/dist/components/combobox.d.ts +75 -0
  115. package/dist/components/combobox.js +112 -0
  116. package/dist/components/command-menu.d.ts +58 -0
  117. package/dist/components/command-menu.js +166 -0
  118. package/dist/components/copy-button.d.ts +16 -0
  119. package/dist/components/copy-button.js +31 -0
  120. package/dist/components/data-grid.d.ts +50 -0
  121. package/dist/components/data-grid.js +599 -0
  122. package/dist/components/date-picker.d.ts +109 -0
  123. package/dist/components/date-picker.js +268 -0
  124. package/dist/components/description-list.d.ts +12 -0
  125. package/dist/components/description-list.js +10 -0
  126. package/dist/components/dialog.d.ts +57 -0
  127. package/dist/components/dialog.js +140 -0
  128. package/dist/components/dock.d.ts +45 -0
  129. package/dist/components/dock.js +79 -0
  130. package/dist/components/drop-zone.d.ts +46 -0
  131. package/dist/components/drop-zone.js +101 -0
  132. package/dist/components/dropdown-menu.d.ts +50 -0
  133. package/dist/components/dropdown-menu.js +78 -0
  134. package/dist/components/editable-cell.d.ts +34 -0
  135. package/dist/components/editable-cell.js +89 -0
  136. package/dist/components/empty-state.d.ts +17 -0
  137. package/dist/components/empty-state.js +9 -0
  138. package/dist/components/field.d.ts +43 -0
  139. package/dist/components/field.js +75 -0
  140. package/dist/components/filter-condition.d.ts +188 -0
  141. package/dist/components/filter-condition.js +113 -0
  142. package/dist/components/filter-menu.d.ts +57 -0
  143. package/dist/components/filter-menu.js +46 -0
  144. package/dist/components/header-bar.d.ts +84 -0
  145. package/dist/components/header-bar.js +94 -0
  146. package/dist/components/hold-button.d.ts +15 -0
  147. package/dist/components/hold-button.js +88 -0
  148. package/dist/components/icon-tile.d.ts +12 -0
  149. package/dist/components/icon-tile.js +29 -0
  150. package/dist/components/info-tip.d.ts +13 -0
  151. package/dist/components/info-tip.js +26 -0
  152. package/dist/components/input.d.ts +17 -0
  153. package/dist/components/input.js +83 -0
  154. package/dist/components/kbd.d.ts +6 -0
  155. package/dist/components/kbd.js +7 -0
  156. package/dist/components/menu-item.d.ts +56 -0
  157. package/dist/components/menu-item.js +94 -0
  158. package/dist/components/message-composer.d.ts +75 -0
  159. package/dist/components/message-composer.js +199 -0
  160. package/dist/components/nav-rail.d.ts +31 -0
  161. package/dist/components/nav-rail.js +85 -0
  162. package/dist/components/notice.d.ts +45 -0
  163. package/dist/components/notice.js +66 -0
  164. package/dist/components/optimistic-undo.d.ts +28 -0
  165. package/dist/components/optimistic-undo.js +43 -0
  166. package/dist/components/option-row.d.ts +15 -0
  167. package/dist/components/option-row.js +14 -0
  168. package/dist/components/page-content.d.ts +11 -0
  169. package/dist/components/page-content.js +31 -0
  170. package/dist/components/pagination.d.ts +34 -0
  171. package/dist/components/pagination.js +41 -0
  172. package/dist/components/popover.d.ts +27 -0
  173. package/dist/components/popover.js +25 -0
  174. package/dist/components/progress.d.ts +33 -0
  175. package/dist/components/progress.js +75 -0
  176. package/dist/components/radio-group.d.ts +7 -0
  177. package/dist/components/radio-group.js +33 -0
  178. package/dist/components/readonly-field.d.ts +33 -0
  179. package/dist/components/readonly-field.js +80 -0
  180. package/dist/components/segmented-control.d.ts +21 -0
  181. package/dist/components/segmented-control.js +108 -0
  182. package/dist/components/select.d.ts +42 -0
  183. package/dist/components/select.js +86 -0
  184. package/dist/components/separator.d.ts +10 -0
  185. package/dist/components/separator.js +7 -0
  186. package/dist/components/setting-row.d.ts +53 -0
  187. package/dist/components/setting-row.js +93 -0
  188. package/dist/components/sidebar-switcher.d.ts +47 -0
  189. package/dist/components/sidebar-switcher.js +20 -0
  190. package/dist/components/sidebar.d.ts +220 -0
  191. package/dist/components/sidebar.js +327 -0
  192. package/dist/components/skeleton.d.ts +18 -0
  193. package/dist/components/skeleton.js +23 -0
  194. package/dist/components/slider.d.ts +15 -0
  195. package/dist/components/slider.js +28 -0
  196. package/dist/components/spinner.d.ts +14 -0
  197. package/dist/components/spinner.js +19 -0
  198. package/dist/components/status-dot.d.ts +24 -0
  199. package/dist/components/status-dot.js +35 -0
  200. package/dist/components/stepper.d.ts +60 -0
  201. package/dist/components/stepper.js +197 -0
  202. package/dist/components/switch.d.ts +9 -0
  203. package/dist/components/switch.js +26 -0
  204. package/dist/components/table-cell.d.ts +38 -0
  205. package/dist/components/table-cell.js +107 -0
  206. package/dist/components/table-context.d.ts +48 -0
  207. package/dist/components/table-context.js +69 -0
  208. package/dist/components/table-head.d.ts +42 -0
  209. package/dist/components/table-head.js +188 -0
  210. package/dist/components/table-tree.d.ts +96 -0
  211. package/dist/components/table-tree.js +137 -0
  212. package/dist/components/table.d.ts +78 -0
  213. package/dist/components/table.js +119 -0
  214. package/dist/components/tabs.d.ts +27 -0
  215. package/dist/components/tabs.js +79 -0
  216. package/dist/components/textarea.d.ts +3 -0
  217. package/dist/components/textarea.js +20 -0
  218. package/dist/components/theme.d.ts +41 -0
  219. package/dist/components/theme.js +60 -0
  220. package/dist/components/thinking.d.ts +68 -0
  221. package/dist/components/thinking.js +94 -0
  222. package/dist/components/toast.d.ts +39 -0
  223. package/dist/components/toast.js +110 -0
  224. package/dist/components/toolbar.d.ts +25 -0
  225. package/dist/components/toolbar.js +92 -0
  226. package/dist/components/tooltip.d.ts +23 -0
  227. package/dist/components/tooltip.js +27 -0
  228. package/dist/components/top-nav.d.ts +129 -0
  229. package/dist/components/top-nav.js +211 -0
  230. package/dist/components/truncate.d.ts +29 -0
  231. package/dist/components/truncate.js +138 -0
  232. package/dist/deprecated.d.ts +43 -0
  233. package/dist/deprecated.js +47 -0
  234. package/dist/index.d.ts +93 -0
  235. package/dist/index.js +93 -0
  236. package/dist/lib/cn.d.ts +5 -0
  237. package/dist/lib/cn.js +14 -0
  238. package/dist/lib/color.d.ts +136 -0
  239. package/dist/lib/color.js +307 -0
  240. package/dist/lib/contrast.d.ts +53 -0
  241. package/dist/lib/contrast.js +169 -0
  242. package/dist/lib/create-theme.d.ts +240 -0
  243. package/dist/lib/create-theme.js +461 -0
  244. package/dist/lib/harmony.d.ts +102 -0
  245. package/dist/lib/harmony.js +426 -0
  246. package/dist/lib/import-theme.d.ts +22 -0
  247. package/dist/lib/import-theme.js +221 -0
  248. package/dist/lib/overflow-scroll.d.ts +25 -0
  249. package/dist/lib/overflow-scroll.js +66 -0
  250. package/dist/lib/radio-toggle-render.d.ts +3 -0
  251. package/dist/lib/radio-toggle-render.js +9 -0
  252. package/dist/lib/reorder.d.ts +38 -0
  253. package/dist/lib/reorder.js +193 -0
  254. package/dist/lib/spacing.d.ts +101 -0
  255. package/dist/lib/spacing.js +118 -0
  256. package/dist/lib/tailwind-palette.d.ts +5 -0
  257. package/dist/lib/tailwind-palette.js +351 -0
  258. package/dist/lib/truncate.d.ts +18 -0
  259. package/dist/lib/truncate.js +69 -0
  260. package/dist/lib/use-single-value.d.ts +5 -0
  261. package/dist/lib/use-single-value.js +17 -0
  262. package/dist/styles.css +7988 -0
  263. package/mcp/server.mjs +87590 -0
  264. package/package.json +58 -5
  265. package/skills/continuum-fix-padding/SKILL.md +69 -0
  266. package/skills/continuum-fix-padding/scripts/compute.ts +155 -0
  267. package/skills/continuum-fix-padding/scripts/spacing.ts +189 -0
  268. package/skills/continuum-taste/SKILL.md +186 -0
  269. package/skills/continuum-taste/assets/tokens.css +647 -0
  270. package/skills/continuum-taste/references/charts.md +375 -0
  271. package/skills/continuum-taste/references/components.md +1369 -0
  272. package/skills/continuum-taste/references/foundations.md +539 -0
  273. package/skills/continuum-taste/references/media/canvas.md +75 -0
  274. package/skills/continuum-taste/references/media/css.md +61 -0
  275. package/skills/continuum-taste/references/media/react.md +161 -0
  276. package/skills/continuum-taste/references/method.md +87 -0
  277. package/skills/continuum-taste/references/pages.md +391 -0
  278. package/skills/continuum-taste/references/patterns.md +77 -0
  279. package/skills/continuum-taste/references/screens/agent.tsx +450 -0
  280. package/skills/continuum-taste/references/screens/analytics-overview.tsx +377 -0
  281. package/skills/continuum-taste/references/screens/checkout-funnel.tsx +193 -0
  282. package/skills/continuum-taste/references/screens/customer.tsx +475 -0
  283. package/skills/continuum-taste/references/screens/customers.tsx +608 -0
  284. package/skills/continuum-taste/references/screens/index.tsx +72 -0
  285. package/skills/continuum-taste/references/screens/invoice-editor.tsx +558 -0
  286. package/skills/continuum-taste/references/screens/new-customer.tsx +205 -0
  287. package/skills/continuum-taste/references/screens/overview.tsx +196 -0
  288. package/skills/continuum-taste/references/screens/report-generator.tsx +329 -0
  289. package/skills/continuum-taste/references/screens/risk-report.tsx +577 -0
  290. package/skills/continuum-taste/references/screens/sequence-editor.tsx +455 -0
  291. package/skills/continuum-taste/references/screens/settings.tsx +288 -0
  292. package/skills/continuum-taste/references/screens/shared.tsx +204 -0
  293. package/skills/continuum-taste/references/screens/sign-in.tsx +104 -0
  294. package/skills/continuum-taste/references/screens/workspace.tsx +596 -0
  295. package/skills/continuum-taste/references/theming.md +95 -0
  296. package/src/charts/area-chart.tsx +17 -0
  297. package/src/charts/bar-chart.tsx +15 -0
  298. package/src/charts/bubble-chart.tsx +913 -0
  299. package/src/charts/bullet-chart.tsx +419 -0
  300. package/src/charts/candlestick-chart.tsx +1242 -0
  301. package/src/charts/cartesian.tsx +1437 -0
  302. package/src/charts/chart-card.tsx +394 -0
  303. package/src/charts/donut-chart.tsx +299 -0
  304. package/src/charts/funnel-chart.tsx +373 -0
  305. package/src/charts/horizontal-bar-chart.tsx +991 -0
  306. package/src/charts/index.ts +76 -0
  307. package/src/charts/legend.tsx +59 -0
  308. package/src/charts/lib/candlestick-window.ts +42 -0
  309. package/src/charts/lib/color.ts +110 -0
  310. package/src/charts/lib/format.ts +42 -0
  311. package/src/charts/lib/frame.ts +6 -0
  312. package/src/charts/lib/layout.ts +78 -0
  313. package/src/charts/lib/path.ts +225 -0
  314. package/src/charts/lib/scale.ts +91 -0
  315. package/src/charts/lib/use-active.ts +87 -0
  316. package/src/charts/lib/use-size.ts +45 -0
  317. package/src/charts/line-chart.tsx +16 -0
  318. package/src/charts/radar-chart.tsx +485 -0
  319. package/src/charts/rose-chart.tsx +553 -0
  320. package/src/charts/sparkline.tsx +293 -0
  321. package/src/charts/tooltip.tsx +105 -0
  322. package/src/charts/treemap.tsx +271 -0
  323. package/src/charts/uptime-strip.tsx +469 -0
  324. package/src/components/accordion.tsx +136 -0
  325. package/src/components/activity-item.tsx +43 -0
  326. package/src/components/alert.tsx +135 -0
  327. package/src/components/assistant-answer.tsx +185 -0
  328. package/src/components/assistant-prompt.tsx +352 -0
  329. package/src/components/avatar.tsx +157 -0
  330. package/src/components/badge.tsx +55 -0
  331. package/src/components/bar-list.tsx +350 -0
  332. package/src/components/block-slider.tsx +189 -0
  333. package/src/components/branch-graph.tsx +580 -0
  334. package/src/components/breadcrumb.tsx +173 -0
  335. package/src/components/button.tsx +157 -0
  336. package/src/components/calendar.tsx +154 -0
  337. package/src/components/card.tsx +296 -0
  338. package/src/components/chat-attachment.tsx +255 -0
  339. package/src/components/chat-event.tsx +34 -0
  340. package/src/components/chat-message.tsx +332 -0
  341. package/src/components/chat-reference.tsx +100 -0
  342. package/src/components/chat-shared.ts +41 -0
  343. package/src/components/chat-suggestions.tsx +188 -0
  344. package/src/components/chat-thread.tsx +348 -0
  345. package/src/components/chat-typing.tsx +155 -0
  346. package/src/components/checkbox.tsx +83 -0
  347. package/src/components/checklist.tsx +146 -0
  348. package/src/components/chip.tsx +152 -0
  349. package/src/components/code-input.tsx +406 -0
  350. package/src/components/color-picker.tsx +1385 -0
  351. package/src/components/combobox.tsx +420 -0
  352. package/src/components/command-menu.tsx +405 -0
  353. package/src/components/copy-button.tsx +87 -0
  354. package/src/components/data-grid.tsx +965 -0
  355. package/src/components/date-picker.tsx +652 -0
  356. package/src/components/description-list.tsx +39 -0
  357. package/src/components/dialog.tsx +367 -0
  358. package/src/components/dock.tsx +192 -0
  359. package/src/components/drop-zone.tsx +283 -0
  360. package/src/components/dropdown-menu.tsx +323 -0
  361. package/src/components/editable-cell.tsx +219 -0
  362. package/src/components/empty-state.tsx +50 -0
  363. package/src/components/field.tsx +148 -0
  364. package/src/components/filter-condition.tsx +463 -0
  365. package/src/components/filter-menu.tsx +196 -0
  366. package/src/components/header-bar.tsx +220 -0
  367. package/src/components/hold-button.tsx +146 -0
  368. package/src/components/icon-tile.tsx +43 -0
  369. package/src/components/info-tip.tsx +87 -0
  370. package/src/components/input.tsx +136 -0
  371. package/src/components/kbd.tsx +17 -0
  372. package/src/components/menu-item.tsx +216 -0
  373. package/src/components/message-composer.tsx +458 -0
  374. package/src/components/nav-rail.tsx +235 -0
  375. package/src/components/notice.tsx +133 -0
  376. package/src/components/optimistic-undo.ts +68 -0
  377. package/src/components/option-row.tsx +54 -0
  378. package/src/components/page-content.tsx +44 -0
  379. package/src/components/pagination.tsx +182 -0
  380. package/src/components/popover.tsx +113 -0
  381. package/src/components/progress.tsx +222 -0
  382. package/src/components/radio-group.tsx +80 -0
  383. package/src/components/readonly-field.tsx +251 -0
  384. package/src/components/segmented-control.tsx +205 -0
  385. package/src/components/select.tsx +259 -0
  386. package/src/components/separator.tsx +30 -0
  387. package/src/components/setting-row.tsx +277 -0
  388. package/src/components/sidebar-switcher.tsx +134 -0
  389. package/src/components/sidebar.tsx +804 -0
  390. package/src/components/skeleton.tsx +57 -0
  391. package/src/components/slider.tsx +106 -0
  392. package/src/components/spinner.tsx +43 -0
  393. package/src/components/status-dot.tsx +100 -0
  394. package/src/components/stepper.tsx +459 -0
  395. package/src/components/switch.tsx +54 -0
  396. package/src/components/table-cell.tsx +239 -0
  397. package/src/components/table-context.ts +135 -0
  398. package/src/components/table-head.tsx +393 -0
  399. package/src/components/table-tree.tsx +357 -0
  400. package/src/components/table.tsx +295 -0
  401. package/src/components/tabs.tsx +191 -0
  402. package/src/components/textarea.tsx +44 -0
  403. package/src/components/theme.tsx +120 -0
  404. package/src/components/thinking.tsx +295 -0
  405. package/src/components/toast.tsx +308 -0
  406. package/src/components/toolbar.tsx +166 -0
  407. package/src/components/tooltip.tsx +84 -0
  408. package/src/components/top-nav.tsx +660 -0
  409. package/src/components/truncate.tsx +234 -0
  410. package/src/deprecated.ts +61 -0
  411. package/src/index.ts +184 -0
  412. package/src/lib/cn.ts +22 -0
  413. package/src/lib/color.ts +469 -0
  414. package/src/lib/contrast.ts +220 -0
  415. package/src/lib/create-theme.ts +659 -0
  416. package/src/lib/harmony.ts +609 -0
  417. package/src/lib/import-theme.ts +272 -0
  418. package/src/lib/overflow-scroll.tsx +108 -0
  419. package/src/lib/radio-toggle-render.tsx +15 -0
  420. package/src/lib/reorder.ts +222 -0
  421. package/src/lib/spacing.ts +189 -0
  422. package/src/lib/tailwind-palette.ts +355 -0
  423. package/src/lib/truncate.ts +79 -0
  424. package/src/lib/use-single-value.ts +23 -0
  425. package/src/styles/continuum.css +3 -0
  426. package/src/styles/theme.css +524 -0
  427. package/src/styles/tokens.css +647 -0
@@ -0,0 +1,1437 @@
1
+ 'use client';
2
+
3
+ import {
4
+ type CSSProperties,
5
+ createContext,
6
+ type ReactNode,
7
+ useCallback,
8
+ useContext,
9
+ useEffect,
10
+ useId,
11
+ useMemo,
12
+ } from 'react';
13
+ import { cn } from '../lib/cn';
14
+ import { type ChartColor, chartColor, seriesColor } from './lib/color';
15
+ import { formatCompact, formatNumber, formatX as formatXValue } from './lib/format';
16
+ import { PLOT_FRAME } from './lib/frame';
17
+ import {
18
+ areaPath,
19
+ curvePath,
20
+ lineAreaPath,
21
+ linePath,
22
+ type Point,
23
+ stepAreaPath,
24
+ stepPath,
25
+ } from './lib/path';
26
+ import { extent, niceTicks, scalePoint, scaleY, slotScale } from './lib/scale';
27
+ import { useActive } from './lib/use-active';
28
+ import { useLabelWidth, useWidth } from './lib/use-size';
29
+ import { ChartTooltip } from './tooltip';
30
+
31
+ /*
32
+ * The frame every plotted chart shares: the scales, the grid, the labels, the marks and
33
+ * the layer that reads a point out. An area, a line and a bar chart differ only in their
34
+ * marks and in how they count across the x axis, so they are this with a `kind` rather
35
+ * than three charts.
36
+ *
37
+ * The lines and the chrome are SVG; the bars are HTML. A bar's corner follows the theme's
38
+ * corner scale, and only a class can say min(4px, var(--radius-chip)).
39
+ */
40
+
41
+ export type ChartKind = 'area' | 'line' | 'bar';
42
+
43
+ export type ChartRow = Record<string, unknown>;
44
+
45
+ export type ChartSeries = {
46
+ /** The field of each row this series plots. */
47
+ key: string;
48
+ /** What the legend and the tooltip call it; the key unless given. */
49
+ name?: string;
50
+ /** `series-1` to `series-5` by position unless given. */
51
+ color?: ChartColor;
52
+ /** Drawn grey and under the rest: the period before, or a series switched off. */
53
+ muted?: boolean;
54
+ /**
55
+ * Bars: this series alone drawn as a tint with a solid edge, the chart's `fill` unless
56
+ * given. A tint says something about one series (a forecast, a plan still in trial); on
57
+ * every series it says nothing.
58
+ */
59
+ fill?: 'solid' | 'tint';
60
+ /** This series' texture, the chart's `texture` unless given. */
61
+ texture?: 'none' | 'stripes' | 'dots';
62
+ };
63
+
64
+ /** What a chart is reading out: the title of the point, and each series' value as written. */
65
+ export type ChartReading = { title: string; values: string[] };
66
+
67
+ export type ChartMarker = {
68
+ /** The row it marks. */
69
+ index: number;
70
+ label: string;
71
+ /** A small icon; without one the marker is a bulb in its tone. */
72
+ icon?: ReactNode;
73
+ tone?: 'neutral' | 'success' | 'warning' | 'error';
74
+ };
75
+
76
+ /** Room for the labels, and the sizes of the marks. Everything else follows the theme. */
77
+ const GUTTER_GAP = 8;
78
+ /** The row of x labels under the plot: the gap to it and one line of labels, no more. */
79
+ const AXIS_HEIGHT = 20;
80
+ /** The room between the plot's frame and the marks in it: none, the frame holds the data tight. */
81
+ const PAD = 0;
82
+ const TOP = 8;
83
+ /** A marker's label: its inset above the plot's foot, and its gap from the rule. */
84
+ const LABEL_INSET = 4;
85
+ const LABEL_GAP = 4;
86
+ /** Labels that would touch stack upward a row at a time, 4 apart, never over each other. */
87
+ const LABEL_ROW = 20;
88
+ const LABEL_SPACE = 4;
89
+ const X_LABEL_GAP = 16;
90
+ const TOOLTIP_OFFSET = 12;
91
+ const DOT_RADIUS = 4;
92
+ const BAR_GAP = 2;
93
+ const BAR_MAX = 28;
94
+ const BAR_MIN = 2;
95
+ /** A bar takes half its slot, and a group of bars seven tenths of it. */
96
+ const BAR_SHARE = 0.5;
97
+ const GROUP_SHARE = 0.7;
98
+ /** A date axis is read in five places at most: a reader counts dates, not ticks. */
99
+ const DATE_TICKS = 5;
100
+ /**
101
+ * Past this many points the marks are drawn finer. A 2px stroke is right for a week, where
102
+ * the line is the subject; over a year it is a rope, and the shape of the data is lost inside
103
+ * its own weight. The hairline the rest of the system is built from is right at that density.
104
+ */
105
+ const DENSE = 120;
106
+ /**
107
+ * The most marks a plot draws. Past this the readings are gathered into even periods and the
108
+ * period's average is drawn, because a year of daily readings has a weekly rhythm in it and
109
+ * that rhythm is all a reader sees: the trend, the slump and the bad week it is made of are
110
+ * lost inside a sawtooth. The spread each average came from is drawn behind it, so nothing
111
+ * is hidden — the shape of the data is the band, not a line through the middle of it.
112
+ */
113
+ const MARKS = 60;
114
+ /**
115
+ * The least a mark may be given. Sixty readings are comfortable across a card and a crowd on a
116
+ * phone, so how many are drawn follows the room there is: the same data gathers into longer
117
+ * periods on a narrow plot rather than turning to hatching.
118
+ */
119
+ const MARK_GAP = 8;
120
+ const LEAST_MARKS = 12;
121
+
122
+ /** The row a period is drawn as: its average per series, and where it came from. */
123
+ type Period = { from: number; to: number };
124
+
125
+ /** Nothing to gather while the readings are few enough for the room to draw one by one. */
126
+ const periodsFor = (count: number, plotWidth: number): Period[] | null => {
127
+ const marks = Math.max(LEAST_MARKS, Math.min(MARKS, Math.floor(plotWidth / MARK_GAP)));
128
+ // Gathering two readings into one is not worth the lie; it starts once there are half again.
129
+ if (count <= marks * 1.5) return null;
130
+ const size = count / marks;
131
+ return Array.from({ length: marks }, (_, i) => ({
132
+ from: Math.round(i * size),
133
+ to: Math.max(Math.round(i * size) + 1, Math.round((i + 1) * size)),
134
+ }));
135
+ };
136
+
137
+ const mean = (values: readonly number[], { from, to }: Period) => {
138
+ let total = 0;
139
+ for (let i = from; i < to; i++) total += values[i] ?? 0;
140
+ return total / Math.max(1, to - from);
141
+ };
142
+
143
+ type ChartContextValue = {
144
+ data: readonly ChartRow[];
145
+ width: number;
146
+ height: number;
147
+ /** The drawing area inside the labels, in the SVG's own coordinates. */
148
+ plot: { left: number; top: number; width: number; height: number };
149
+ xOf: (index: number) => number;
150
+ yOf: (value: number) => number;
151
+ activeIndex: number | null;
152
+ };
153
+
154
+ const ChartContext = createContext<ChartContextValue | null>(null);
155
+
156
+ /** The plot's coordinates, for an overlay drawn as a chart's child. */
157
+ export const useChart = () => {
158
+ const value = useContext(ChartContext);
159
+ if (!value) throw new Error('useChart() is only for a chart’s own children.');
160
+ return value;
161
+ };
162
+
163
+ export type CartesianProps<Row extends ChartRow, X = unknown> = {
164
+ /** Says what the chart shows; it names the chart for a screen reader. */
165
+ 'aria-label': string;
166
+ data: readonly Row[];
167
+ /** The field holding the x value: a Date or a number is a measure, a string a category. */
168
+ x: string;
169
+ series: readonly ChartSeries[];
170
+ kind: ChartKind;
171
+ /** Bars: stack the series rather than standing them side by side. */
172
+ stacked?: boolean;
173
+ /** Including the x labels. */
174
+ height?: number;
175
+ formatX?: (value: X, row: Row, index: number) => string;
176
+ /** The tooltip's title; the long date, or `formatX`, unless given. */
177
+ formatTitle?: (value: X, row: Row, index: number) => string;
178
+ /** The y labels; compact en-GB unless given. It writes the tooltip's values too. */
179
+ formatY?: (value: number) => string;
180
+ /** The tooltip's values alone, where they want more room than an axis label. */
181
+ formatValue?: (value: number) => string;
182
+ /** Holds the axis down, for a measure with a ceiling such as a rate. */
183
+ yMax?: number;
184
+ /** How many x labels to aim for; fewer are drawn where they would not fit. */
185
+ xTicks?: number;
186
+ yTicks?: number;
187
+ markers?: readonly ChartMarker[];
188
+ /**
189
+ * How a reading is joined to the next. `smooth` is a curve that never overshoots a
190
+ * neighbour, `linear` the straight run between them, and `step` holds each value across
191
+ * its own period, for a measure that was only ever true of that period. A chart of many
192
+ * readings draws straight whatever is asked, since at two pixels a span a curve says
193
+ * nothing and only costs the slope maths.
194
+ */
195
+ curve?: 'smooth' | 'linear' | 'step';
196
+ /**
197
+ * A texture in the tint under a line: diagonal stripes or a field of dots. Left out, the
198
+ * chart follows the theme, which may ask for one (`--chart-texture-stripes: 1` or
199
+ * `--chart-texture-dots: 1`) and asks for none by default; `none` holds it plain.
200
+ */
201
+ texture?: 'none' | 'stripes' | 'dots';
202
+ /**
203
+ * Bars: `solid` fills each bar; `tint` draws it as every other mark in the kit is drawn, a
204
+ * tint of its colour with a solid edge at its data end, and takes the texture.
205
+ */
206
+ fill?: 'solid' | 'tint';
207
+ /** A tick at each reading, for a chart whose readings are few and worth pointing out. */
208
+ points?: boolean;
209
+ /** From this row on the line is dashed: the period is not over. */
210
+ incompleteFrom?: number;
211
+ /** False for a chart too small to read out; a function writes its own readout. */
212
+ tooltip?: boolean | ((row: Row, index: number) => ReactNode);
213
+ grid?: boolean;
214
+ xAxis?: boolean;
215
+ yAxis?: boolean;
216
+ onSelect?: (row: Row, index: number) => void;
217
+ /**
218
+ * Told what is being read, as it is written in the readout, and null when nothing is. A card
219
+ * that holds the chart shows it in its own header instead of in a panel over the plot.
220
+ */
221
+ onRead?: (reading: ChartReading | null) => void;
222
+ /** With no data, the plot is a skeleton; with data, the marks wait at half strength. */
223
+ loading?: boolean;
224
+ emptyLabel?: string;
225
+ className?: string;
226
+ style?: CSSProperties;
227
+ /** Overlays drawn in the plot's coordinates, with `useChart()`. */
228
+ children?: ReactNode;
229
+ };
230
+
231
+ const toNumber = (value: unknown) =>
232
+ typeof value === 'number' && Number.isFinite(value) ? value : 0;
233
+
234
+ const colorOf = (line: ChartSeries, index: number) =>
235
+ line.muted ? chartColor('neutral') : chartColor(line.color ?? seriesColor(index));
236
+
237
+ /** The indices to label, spread evenly, the first and last always among them. */
238
+ const labelIndices = (count: number, wanted: number) => {
239
+ if (count === 0) return [];
240
+ const take = Math.max(1, Math.min(wanted, count));
241
+ if (take === 1) return [0];
242
+ const indices = new Set<number>();
243
+ for (let i = 0; i < take; i++) indices.add(Math.round((i * (count - 1)) / (take - 1)));
244
+ return [...indices].sort((a, b) => a - b);
245
+ };
246
+
247
+ export const Cartesian = <Row extends ChartRow, X = unknown>({
248
+ 'aria-label': label,
249
+ data: rows,
250
+ x: xKey,
251
+ series,
252
+ kind,
253
+ stacked = false,
254
+ height = 240,
255
+ formatX,
256
+ formatTitle,
257
+ formatY,
258
+ formatValue,
259
+ yMax,
260
+ xTicks,
261
+ yTicks = 4,
262
+ markers,
263
+ curve = 'smooth',
264
+ points: showPoints = false,
265
+ texture,
266
+ fill = 'solid',
267
+ incompleteFrom,
268
+ tooltip = true,
269
+ grid = true,
270
+ xAxis = true,
271
+ yAxis = true,
272
+ onSelect,
273
+ onRead,
274
+ loading = false,
275
+ emptyLabel = 'No data yet',
276
+ className,
277
+ style,
278
+ children,
279
+ }: CartesianProps<Row, X>) => {
280
+ // React's own ids carry characters a URL fragment should not, so they are stripped.
281
+ const gradientId = useId().replace(/[^a-zA-Z0-9_-]/g, '');
282
+ const [containerRef, width] = useWidth<HTMLDivElement>();
283
+ const [yProbeRef, yLabelWidth] = useLabelWidth();
284
+ const [xProbeRef, xLabelWidth] = useLabelWidth();
285
+
286
+ /*
287
+ * While it loads a chart draws its skeleton and nothing else. Holding the last rows at half
288
+ * strength is not a loading state: it is the old answer looking unwell, and a reader cannot
289
+ * tell whether to trust it.
290
+ */
291
+ const data: readonly Row[] = loading ? [] : rows;
292
+ const empty = data.length === 0;
293
+ const writeAxis = formatY ?? formatCompact;
294
+ const writeValue = formatValue ?? formatY ?? formatNumber;
295
+ const bars = kind === 'bar';
296
+
297
+ // The plot is what the labels leave. It is known before the marks, because how many marks
298
+ // there is room for decides how the readings are gathered.
299
+ // While loading, the value axis keeps its column, so the plot does not widen and then narrow
300
+ // when the data lands: measured from the rows' labels when there are rows, else a short one.
301
+ const left = (yAxis && (!empty || loading) ? yLabelWidth + GUTTER_GAP : 0) + PAD;
302
+ const top = TOP;
303
+ const bottom = (xAxis ? AXIS_HEIGHT : 0) + PAD;
304
+ const plotWidth = Math.max(0, width - left - PAD);
305
+ const plotHeight = Math.max(0, height - top - bottom);
306
+
307
+ // Every series' numbers as given, before any gathering.
308
+ const readings = useMemo(
309
+ () => series.map(line => data.map(row => toNumber(row[line.key]))),
310
+ [data, series],
311
+ );
312
+
313
+ /*
314
+ * What is actually drawn. Few enough readings and it is the readings themselves; past that
315
+ * it is one mark per period, holding the period's average and the spread it came from.
316
+ */
317
+ const periods = useMemo(
318
+ () => (bars ? null : periodsFor(data.length, plotWidth)),
319
+ [bars, data.length, plotWidth],
320
+ );
321
+ const columns = useMemo(
322
+ () =>
323
+ periods
324
+ ? readings.map(values => periods.map(period => mean(values, period)))
325
+ : readings,
326
+ [periods, readings],
327
+ );
328
+ /** The row each mark stands for: the reading itself, or the period's first. */
329
+ const rowAt = useCallback(
330
+ (index: number) => data[periods ? (periods[index]?.from ?? 0) : index],
331
+ [data, periods],
332
+ );
333
+ const marksCount = periods ? periods.length : data.length;
334
+ /**
335
+ * A row of the data as the mark that now carries it. Anything a caller points at — a marker,
336
+ * the row a period stops being measured at — names a row, and must follow it into its period.
337
+ */
338
+ const markOf = (row: number) => {
339
+ if (!periods) return Math.max(0, Math.min(marksCount - 1, row));
340
+ const found = periods.findIndex(period => row < period.to);
341
+ return found === -1 ? periods.length - 1 : found;
342
+ };
343
+ const cutAt = incompleteFrom === undefined ? undefined : markOf(incompleteFrom);
344
+ const sums = useMemo(
345
+ () =>
346
+ stacked && bars
347
+ ? data.map((_, i) => columns.reduce((total, column) => total + (column[i] ?? 0), 0))
348
+ : [],
349
+ [bars, columns, data, stacked],
350
+ );
351
+
352
+ const ticks = useMemo(() => {
353
+ const span = extent(stacked && bars ? sums : columns.flat());
354
+ return niceTicks(span.min, span.max, yTicks, yMax);
355
+ }, [bars, columns, stacked, sums, yMax, yTicks]);
356
+
357
+ const yLabels = useMemo(() => ticks.values.map(writeAxis), [ticks, writeAxis]);
358
+ // The labels the value axis is measured from while loading: the ones the given rows will have,
359
+ // or a short stand-in when there are none yet. They are measured, never drawn.
360
+ const heldLabels = useMemo(() => {
361
+ if (!loading) return yLabels;
362
+ if (rows.length === 0) return [writeAxis(1000)];
363
+ const values = rows.map(row => series.map(line => toNumber(row[line.key])));
364
+ const span = extent(
365
+ stacked && bars
366
+ ? values.map(row => row.reduce((total, value) => total + value, 0))
367
+ : values.flat(),
368
+ );
369
+ return niceTicks(span.min, span.max, yTicks, yMax).values.map(writeAxis);
370
+ }, [bars, loading, rows, series, stacked, writeAxis, yLabels, yMax, yTicks]);
371
+ const labelOf = useCallback(
372
+ (row: Row | undefined, i: number) => {
373
+ const value = row?.[xKey];
374
+ return row && formatX ? formatX(value as X, row, i) : formatXValue(value);
375
+ },
376
+ [formatX, xKey],
377
+ );
378
+ // One label per mark; the table below keeps a label for every reading.
379
+ const xLabels = useMemo(
380
+ () => Array.from({ length: marksCount }, (_, i) => labelOf(rowAt(i), i)),
381
+ [labelOf, marksCount, rowAt],
382
+ );
383
+ const rawLabels = useMemo(() => data.map(labelOf), [data, labelOf]);
384
+
385
+ const slots = useMemo(() => slotScale(data.length, plotWidth), [data.length, plotWidth]);
386
+ const xOf = useMemo(
387
+ () => (index: number) =>
388
+ left + (bars ? slots.center(index) : scalePoint(index, marksCount, plotWidth)),
389
+ [bars, marksCount, left, plotWidth, slots],
390
+ );
391
+ const yOf = useMemo(
392
+ () => (value: number) => top + scaleY(value, ticks, plotHeight),
393
+ [plotHeight, ticks],
394
+ );
395
+ const baseline = yOf(Math.max(ticks.min, 0));
396
+
397
+ const { active, handlers } = useActive(marksCount, xOf);
398
+
399
+ const readable = tooltip !== false && !empty;
400
+ // The plot still answers the pointer when a card reads it out in place of the panel.
401
+ const hoverable = (readable || !!onRead) && !empty;
402
+ const activeRow = active !== null ? rowAt(active) : undefined;
403
+
404
+ // Only as many x labels as fit without touching, however many were asked for.
405
+ const shown = useMemo(() => {
406
+ if (!xAxis || empty) return [];
407
+ const room = Math.max(
408
+ 1,
409
+ Math.floor((plotWidth + X_LABEL_GAP) / Math.max(1, xLabelWidth + X_LABEL_GAP)),
410
+ );
411
+ const dates = data[0]?.[xKey] instanceof Date;
412
+ const wanted = xTicks ?? (dates ? DATE_TICKS : marksCount);
413
+ return labelIndices(marksCount, Math.min(wanted, room));
414
+ }, [data, empty, marksCount, plotWidth, xAxis, xKey, xLabelWidth, xTicks]);
415
+
416
+ // The marks do not depend on the pointer, so moving it never redraws them.
417
+ const marks = useMemo(() => {
418
+ if (empty || bars || plotWidth <= 0 || plotHeight <= 0) return null;
419
+ // A muted series sits under the rest, whatever order it was given in.
420
+ const order = series.map((line, index) => ({ line, index }));
421
+ order.sort((a, b) => Number(b.line.muted ?? false) - Number(a.line.muted ?? false));
422
+
423
+ return order.map(({ line, index }) => {
424
+ const column = columns[index] ?? [];
425
+ const color = colorOf(line, index);
426
+ const points: Point[] = Array.from({ length: marksCount }, (_, i) => ({
427
+ x: xOf(i),
428
+ y: yOf(column[i] ?? 0),
429
+ }));
430
+ const solid = cutAt === undefined ? points : points.slice(0, cutAt + 1);
431
+ const dashed = cutAt === undefined ? [] : points.slice(cutAt);
432
+ const dense = marksCount > DENSE;
433
+ const stroke = dense ? 1 : line.muted ? 1.5 : 2;
434
+ // Many readings are drawn straight whatever was asked: see the curve prop.
435
+ const shape = dense ? 'linear' : curve;
436
+ const draw = shape === 'step' ? stepPath : shape === 'linear' ? linePath : curvePath;
437
+ const fillUnder =
438
+ shape === 'step' ? stepAreaPath : shape === 'linear' ? lineAreaPath : areaPath;
439
+ /*
440
+ * A fill reads under one or two series; past that they are lines. Dense, it reads
441
+ * under one alone: a year of daily readings has a weekly rhythm in it, and two
442
+ * sawtooth washes over each other are mud whatever colours they are.
443
+ */
444
+ const filled = kind === 'area' && !line.muted && series.length <= (dense ? 1 : 2);
445
+
446
+ return (
447
+ <g data-slot="chart-series" key={line.key}>
448
+ {filled && (
449
+ /*
450
+ * The tint holds all the way down rather than fading out, and stands
451
+ * on a rounded foot: the fill's own edge is the baseline.
452
+ */
453
+ <g
454
+ style={{
455
+ clipPath:
456
+ 'inset(0 round 0 0 var(--radius-item) var(--radius-item))',
457
+ }}
458
+ >
459
+ <defs>
460
+ <linearGradient
461
+ gradientUnits="userSpaceOnUse"
462
+ id={`${gradientId}-${index}`}
463
+ x1={0}
464
+ x2={0}
465
+ y1={yOf(extent(column).max)}
466
+ y2={baseline}
467
+ >
468
+ <stop offset="0%" stopColor={color} stopOpacity={0.2} />
469
+ <stop offset="100%" stopColor={color} stopOpacity={0.08} />
470
+ </linearGradient>
471
+ <pattern
472
+ height={7}
473
+ id={`${gradientId}-${index}-stripes`}
474
+ patternTransform="rotate(45)"
475
+ patternUnits="userSpaceOnUse"
476
+ width={7}
477
+ >
478
+ <line
479
+ stroke={color}
480
+ strokeWidth={1.2}
481
+ x1={0}
482
+ x2={0}
483
+ y1={0}
484
+ y2={7}
485
+ />
486
+ </pattern>
487
+ <pattern
488
+ height={6}
489
+ id={`${gradientId}-${index}-dots`}
490
+ patternUnits="userSpaceOnUse"
491
+ width={6}
492
+ >
493
+ <circle cx={3} cy={3} fill={color} r={1.1} />
494
+ </pattern>
495
+ <linearGradient
496
+ id={`${gradientId}-fade`}
497
+ x1="0"
498
+ x2="0"
499
+ y1="0"
500
+ y2="1"
501
+ >
502
+ <stop offset="0%" stopColor="white" stopOpacity={0.55} />
503
+ <stop offset="100%" stopColor="white" stopOpacity={0.12} />
504
+ </linearGradient>
505
+ <mask
506
+ id={`${gradientId}-${index}-thin`}
507
+ maskContentUnits="objectBoundingBox"
508
+ >
509
+ <rect fill={`url(#${gradientId}-fade)`} height={1} width={1} />
510
+ </mask>
511
+ </defs>
512
+ <path
513
+ d={fillUnder(points, baseline)}
514
+ fill={`url(#${gradientId}-${index})`}
515
+ />
516
+ {(line.texture ?? texture) !== 'none' &&
517
+ (['stripes', 'dots'] as const)
518
+ .filter(
519
+ kindOf =>
520
+ !(line.texture ?? texture) ||
521
+ (line.texture ?? texture) === kindOf,
522
+ )
523
+ .map(kindOf => (
524
+ <path
525
+ d={fillUnder(points, baseline)}
526
+ fill={`url(#${gradientId}-${index}-${kindOf})`}
527
+ key={kindOf}
528
+ mask={`url(#${gradientId}-${index}-thin)`}
529
+ // Asked for outright it shows; otherwise the theme says.
530
+ style={
531
+ (line.texture ?? texture)
532
+ ? undefined
533
+ : {
534
+ opacity: `var(--chart-texture-${kindOf}, 0)`,
535
+ }
536
+ }
537
+ />
538
+ ))}
539
+ </g>
540
+ )}
541
+ {points.length === 1 && points[0] ? (
542
+ // One reading has no line in it; drawn as a line it would be nothing at all.
543
+ <circle cx={points[0].x} cy={points[0].y} fill={color} r={3} />
544
+ ) : (
545
+ <path
546
+ d={draw(solid)}
547
+ fill="none"
548
+ stroke={color}
549
+ strokeLinecap="round"
550
+ strokeLinejoin="round"
551
+ strokeWidth={stroke}
552
+ />
553
+ )}
554
+ {showPoints &&
555
+ !dense &&
556
+ points.map(point => (
557
+ // Where a reading was actually taken, for a chart of few enough of them.
558
+ <circle
559
+ cx={point.x}
560
+ cy={point.y}
561
+ fill="var(--color-chart-background)"
562
+ key={`${point.x},${point.y}`}
563
+ r={2.5}
564
+ stroke={color}
565
+ strokeWidth={1.5}
566
+ />
567
+ ))}
568
+ {dashed.length > 1 && (
569
+ // The period is not over, so its last stretch is not a measured value.
570
+ <path
571
+ d={draw(dashed)}
572
+ fill="none"
573
+ stroke={color}
574
+ strokeDasharray="6 4"
575
+ strokeLinecap="round"
576
+ strokeWidth={stroke}
577
+ />
578
+ )}
579
+ </g>
580
+ );
581
+ });
582
+ }, [
583
+ bars,
584
+ baseline,
585
+ columns,
586
+ curve,
587
+ cutAt,
588
+ empty,
589
+ showPoints,
590
+ texture,
591
+ gradientId,
592
+ kind,
593
+ marksCount,
594
+ plotHeight,
595
+ plotWidth,
596
+ series,
597
+ xOf,
598
+ yOf,
599
+ ]);
600
+
601
+ const barLayout = useMemo(() => {
602
+ if (!bars || empty || plotWidth <= 0) return [];
603
+ const count = series.length;
604
+ const grouped = !stacked && count > 1;
605
+ const groupWidth = grouped
606
+ ? Math.min(slots.slot * GROUP_SHARE, BAR_MAX * count + BAR_GAP * (count - 1))
607
+ : Math.min(slots.slot * BAR_SHARE, BAR_MAX);
608
+ const barWidth = Math.max(
609
+ BAR_MIN,
610
+ grouped ? (groupWidth - BAR_GAP * (count - 1)) / count : groupWidth,
611
+ );
612
+
613
+ return data.flatMap((_, row) => {
614
+ let up = baseline;
615
+ let down = baseline;
616
+ return series.map((line, index) => {
617
+ const value = columns[index]?.[row] ?? 0;
618
+ const positive = value >= 0;
619
+ const size = Math.abs(yOf(value) - baseline);
620
+ const center = xOf(row);
621
+ const x = grouped
622
+ ? center - groupWidth / 2 + index * (barWidth + BAR_GAP)
623
+ : center - barWidth / 2;
624
+ const shared = {
625
+ key: `${row}-${line.key}`,
626
+ color: colorOf(line, index),
627
+ tinted: (line.fill ?? fill) === 'tint',
628
+ texture: line.texture ?? texture,
629
+ width: barWidth,
630
+ positive,
631
+ x,
632
+ };
633
+
634
+ if (!stacked)
635
+ return {
636
+ ...shared,
637
+ height: size,
638
+ head: true,
639
+ y: positive ? yOf(value) : baseline,
640
+ };
641
+
642
+ // A stacked segment starts where the one under it stopped, two pixels clear of it.
643
+ const from = positive ? up : down;
644
+ const gap = from === baseline ? 0 : BAR_GAP;
645
+ if (positive) up = from - size;
646
+ else down = from + size;
647
+ return {
648
+ ...shared,
649
+ height: Math.max(0, size - gap),
650
+ // Only the stack's head is rounded; where two segments meet it is square.
651
+ head: index === count - 1,
652
+ y: positive ? from - size : from + gap,
653
+ };
654
+ });
655
+ });
656
+ }, [
657
+ bars,
658
+ baseline,
659
+ columns,
660
+ data,
661
+ empty,
662
+ fill,
663
+ plotWidth,
664
+ series,
665
+ slots,
666
+ stacked,
667
+ texture,
668
+ xOf,
669
+ yOf,
670
+ ]);
671
+
672
+ const context: ChartContextValue = {
673
+ data,
674
+ width,
675
+ height,
676
+ plot: { left, top, width: plotWidth, height: plotHeight },
677
+ xOf,
678
+ yOf,
679
+ activeIndex: active,
680
+ };
681
+
682
+ const tooltipRows = (index: number) =>
683
+ series.map((line, i) => ({
684
+ color: (line.muted ? 'neutral' : (line.color ?? seriesColor(i))) as ChartColor,
685
+ name: line.name ?? line.key,
686
+ value: writeValue(columns[i]?.[index] ?? 0),
687
+ }));
688
+
689
+ /**
690
+ * A gathered chart says so: the readout names the period it covers and the low to high the
691
+ * average was taken from, which is the only place the band behind the line is explained.
692
+ */
693
+ const periodFooter = (index: number) => {
694
+ const period = periods?.[index];
695
+ if (!period || series.length !== 1) return undefined;
696
+ const low = Math.min(...(readings[0] ?? []).slice(period.from, period.to));
697
+ const high = Math.max(...(readings[0] ?? []).slice(period.from, period.to));
698
+ const days = period.to - period.from;
699
+ return (
700
+ <span className="flex items-center justify-between gap-4">
701
+ <span className="text-neutral-600 text-xs">{days} days, low to high</span>
702
+ <span className="font-medium text-neutral-950 text-xs tabular-nums">
703
+ {writeValue(low)}–{writeValue(high)}
704
+ </span>
705
+ </span>
706
+ );
707
+ };
708
+
709
+ const titleOf = (index: number) => {
710
+ const row = rowAt(index);
711
+ if (!row) return '';
712
+ const value = row[xKey];
713
+ if (formatTitle) return formatTitle(value as X, row, index);
714
+ const period = periods?.[index];
715
+ if (period) {
716
+ const last = data[period.to - 1];
717
+ const from = labelOf(row, index);
718
+ const to = last ? labelOf(last, period.to - 1) : from;
719
+ return from === to ? from : `${from} – ${to}`;
720
+ }
721
+ if (formatX) return formatX(value as X, row, index);
722
+ return formatXValue(value, true);
723
+ };
724
+
725
+ // biome-ignore lint/correctness/useExhaustiveDependencies: a reading is told when the point changes, not on every render
726
+ useEffect(() => {
727
+ if (!onRead) return;
728
+ onRead(
729
+ active === null || !rowAt(active)
730
+ ? null
731
+ : {
732
+ title: titleOf(active),
733
+ values: series.map((_, i) => writeValue(columns[i]?.[active] ?? 0)),
734
+ },
735
+ );
736
+ }, [active, columns]);
737
+
738
+ const readoutOf = (index: number) =>
739
+ `${titleOf(index)}: ${series
740
+ .map((line, i) => `${line.name ?? line.key} ${writeValue(columns[i]?.[index] ?? 0)}`)
741
+ .join(', ')}`;
742
+
743
+ return (
744
+ <ChartContext.Provider value={context}>
745
+ <figure
746
+ aria-busy={loading || undefined}
747
+ aria-label={label}
748
+ className={cn('relative m-0 w-full', className)}
749
+ data-slot="chart"
750
+ style={{ height, ...style }}
751
+ >
752
+ {/*
753
+ * The labels, measured in the theme's own font before they are placed: each
754
+ * box is as wide as its widest line, so the gutter follows the font on its own.
755
+ */}
756
+ <div
757
+ aria-hidden
758
+ className="pointer-events-none invisible absolute top-0 left-0 w-max text-xxs tabular-nums"
759
+ ref={yProbeRef}
760
+ >
761
+ {heldLabels.map(text => (
762
+ <div key={text}>{text}</div>
763
+ ))}
764
+ </div>
765
+ <div
766
+ aria-hidden
767
+ className="pointer-events-none invisible absolute top-0 left-0 w-max text-xxs tabular-nums"
768
+ ref={xProbeRef}
769
+ >
770
+ {xLabels.map((text, i) => (
771
+ <div key={`${i}-${text}`}>{text}</div>
772
+ ))}
773
+ </div>
774
+
775
+ <div
776
+ className={cn(
777
+ 'relative size-full transition-opacity duration-200 motion-reduce:transition-none',
778
+ // It appears once it knows how wide it is, rather than jumping into place.
779
+ width > 0 ? 'opacity-100' : 'opacity-0',
780
+ )}
781
+ ref={containerRef}
782
+ >
783
+ {width > 0 && (
784
+ <>
785
+ {/* The marks are drawn for the eye; the table below carries the figures. */}
786
+ <svg
787
+ aria-hidden="true"
788
+ className="block overflow-visible"
789
+ height={height}
790
+ width={width}
791
+ >
792
+ {/*
793
+ * The frame's own rect and corner: the marks and their grid are
794
+ * held inside it, so nothing runs past its hairline or its round.
795
+ */}
796
+ <defs>
797
+ <clipPath id={`${gradientId}-plot`}>
798
+ <rect
799
+ height={plotHeight}
800
+ style={{
801
+ rx: 'var(--radius-item)',
802
+ ry: 'var(--radius-item)',
803
+ }}
804
+ width={plotWidth}
805
+ x={left}
806
+ y={top}
807
+ />
808
+ </clipPath>
809
+ </defs>
810
+ <g clipPath={`url(#${gradientId}-plot)`}>
811
+ {grid &&
812
+ !loading &&
813
+ ticks.values
814
+ .filter(
815
+ tick =>
816
+ !(
817
+ kind === 'area' &&
818
+ series.length <= 2 &&
819
+ tick === Math.max(ticks.min, 0)
820
+ ),
821
+ )
822
+ .map(tick => (
823
+ // Half a pixel off the whole one, so a 1px rule lands crisp.
824
+ <line
825
+ key={tick}
826
+ shapeRendering="crispEdges"
827
+ stroke="var(--color-chart-grid)"
828
+ x1={left}
829
+ x2={left + plotWidth}
830
+ y1={Math.round(yOf(tick)) + 0.5}
831
+ y2={Math.round(yOf(tick)) + 0.5}
832
+ />
833
+ ))}
834
+ </g>
835
+
836
+ {yAxis &&
837
+ !empty &&
838
+ ticks.values.map((tick, i) => (
839
+ <text
840
+ className="text-xxs tabular-nums"
841
+ dominantBaseline="middle"
842
+ fill="var(--color-chart-label)"
843
+ key={tick}
844
+ textAnchor="end"
845
+ x={left - PAD - GUTTER_GAP}
846
+ y={yOf(tick)}
847
+ >
848
+ {yLabels[i]}
849
+ </text>
850
+ ))}
851
+
852
+ {shown.map(index => (
853
+ <text
854
+ className="text-xxs tabular-nums"
855
+ fill="var(--color-chart-label)"
856
+ key={index}
857
+ textAnchor={
858
+ bars || (index > 0 && index < data.length - 1)
859
+ ? 'middle'
860
+ : index === 0
861
+ ? 'start'
862
+ : 'end'
863
+ }
864
+ x={xOf(index)}
865
+ y={top + plotHeight + PAD + 16}
866
+ >
867
+ {xLabels[index]}
868
+ </text>
869
+ ))}
870
+
871
+ {/* A hovered bar is marked by its slot, which is easier to aim at. */}
872
+
873
+ {/*
874
+ * The marks wipe in from the left once, in the time a reader
875
+ * takes to look at the card: it says which way the x axis runs
876
+ * before a label is read. A keyframe, not a state flipped on the
877
+ * next frame: a chart in a background tab, a print or a picture
878
+ * of the page never gets that frame, and would stay blank.
879
+ */}
880
+ <g clipPath={`url(#${gradientId}-plot)`}>
881
+ <g className="motion-safe:animate-reveal">{marks}</g>
882
+ </g>
883
+
884
+ {!bars && active !== null && (
885
+ <g data-slot="chart-crosshair">
886
+ {/*
887
+ * The rule fades out at both ends: solid from edge to
888
+ * edge it reads as heavy as the data it is pointing at.
889
+ */}
890
+ <defs>
891
+ <linearGradient
892
+ id={`${gradientId}-crosshair`}
893
+ x1="0"
894
+ x2="0"
895
+ y1="0"
896
+ y2="1"
897
+ >
898
+ <stop
899
+ offset="0%"
900
+ stopColor="var(--color-chart-crosshair)"
901
+ stopOpacity="0"
902
+ />
903
+ <stop
904
+ offset="14%"
905
+ stopColor="var(--color-chart-crosshair)"
906
+ stopOpacity="1"
907
+ />
908
+ <stop
909
+ offset="86%"
910
+ stopColor="var(--color-chart-crosshair)"
911
+ stopOpacity="1"
912
+ />
913
+ <stop
914
+ offset="100%"
915
+ stopColor="var(--color-chart-crosshair)"
916
+ stopOpacity="0"
917
+ />
918
+ </linearGradient>
919
+ </defs>
920
+ <rect
921
+ fill={`url(#${gradientId}-crosshair)`}
922
+ height={plotHeight}
923
+ shapeRendering="crispEdges"
924
+ width={1}
925
+ x={Math.round(xOf(active))}
926
+ y={top}
927
+ />
928
+ {series.map((line, index) => (
929
+ <circle
930
+ cx={xOf(active)}
931
+ cy={yOf(columns[index]?.[active] ?? 0)}
932
+ fill={colorOf(line, index)}
933
+ key={line.key}
934
+ r={DOT_RADIUS}
935
+ stroke="var(--color-chart-background)"
936
+ strokeWidth={2}
937
+ />
938
+ ))}
939
+ </g>
940
+ )}
941
+
942
+ {markers?.map(marker => {
943
+ const at = markOf(marker.index);
944
+ const cx = xOf(at);
945
+ const foot = top + plotHeight;
946
+
947
+ return (
948
+ // A quiet rule over the plot's full height: a note about the
949
+ // data, never louder than the series it sits on.
950
+ <line
951
+ key={`${marker.index}-${marker.label}`}
952
+ shapeRendering="crispEdges"
953
+ stroke="var(--color-neutral-400)"
954
+ strokeDasharray="3 3"
955
+ x1={Math.round(cx) + 0.5}
956
+ x2={Math.round(cx) + 0.5}
957
+ y1={top}
958
+ y2={foot}
959
+ />
960
+ );
961
+ })}
962
+
963
+ {children}
964
+ </svg>
965
+
966
+ {bars && (
967
+ // Held to the frame's rect and corner, as the SVG marks are.
968
+ <div
969
+ className="pointer-events-none absolute overflow-hidden rounded-item"
970
+ data-slot="chart-bars"
971
+ style={{ height: plotHeight, left, top, width: plotWidth }}
972
+ >
973
+ {active !== null && (
974
+ // The hovered slot, on the item corner as a hovered row is.
975
+ <div
976
+ className="absolute rounded-item"
977
+ style={{
978
+ background: 'var(--color-chart-band)',
979
+ height: plotHeight,
980
+ left: slots.slot * active + 2,
981
+ top: 0,
982
+ width: Math.max(0, slots.slot - 4),
983
+ }}
984
+ />
985
+ )}
986
+ {barLayout.map(bar => {
987
+ /*
988
+ * A bar stands flat on the baseline and is rounded at its
989
+ * head, on the item corner: rounded at one end, it never
990
+ * turns into a pill.
991
+ */
992
+ const head = bar.head
993
+ ? `min(var(--radius-item), ${bar.width / 2}px)`
994
+ : '0px';
995
+ const { tinted, texture: grain } = bar;
996
+
997
+ return (
998
+ <div
999
+ className="absolute overflow-hidden"
1000
+ key={bar.key}
1001
+ style={{
1002
+ background: tinted
1003
+ ? `color-mix(in oklab, ${bar.color} 20%, transparent)`
1004
+ : bar.color,
1005
+ borderRadius: bar.positive
1006
+ ? `${head} ${head} 0 0`
1007
+ : `0 0 ${head} ${head}`,
1008
+ // The bar's own line at its data end, as a
1009
+ // line stands over the tint under it.
1010
+ boxShadow: tinted
1011
+ ? `inset 0 ${bar.positive ? 2 : -2}px 0 ${bar.color}`
1012
+ : undefined,
1013
+ height: bar.height,
1014
+ left: bar.x - left,
1015
+ top: bar.y - top,
1016
+ width: bar.width,
1017
+ }}
1018
+ >
1019
+ {tinted &&
1020
+ grain !== 'none' &&
1021
+ (['stripes', 'dots'] as const)
1022
+ .filter(
1023
+ kindOf => !grain || grain === kindOf,
1024
+ )
1025
+ .map(kindOf => (
1026
+ <span
1027
+ aria-hidden
1028
+ className="absolute inset-0"
1029
+ key={kindOf}
1030
+ style={{
1031
+ background: TEXTURES[kindOf](
1032
+ bar.color,
1033
+ ),
1034
+ // Asked for outright it shows; otherwise the theme says.
1035
+ opacity: grain
1036
+ ? 1
1037
+ : `var(--chart-texture-${kindOf}, 0)`,
1038
+ }}
1039
+ />
1040
+ ))}
1041
+ </div>
1042
+ );
1043
+ })}
1044
+ </div>
1045
+ )}
1046
+
1047
+ {markers &&
1048
+ placeMarkerLabels(
1049
+ markers.map(marker => xOf(markOf(marker.index))),
1050
+ markers,
1051
+ left,
1052
+ left + plotWidth,
1053
+ ).map(({ marker, x, before, row }) => (
1054
+ <ChartMarkerLabel
1055
+ before={before}
1056
+ key={`${marker.index}-${marker.label}`}
1057
+ marker={marker}
1058
+ x={x}
1059
+ y={top + plotHeight - LABEL_INSET - row * LABEL_ROW}
1060
+ />
1061
+ ))}
1062
+
1063
+ {(!empty || loading) && (
1064
+ // The plot's own place in the card: the marks and their grid inside, the labels out.
1065
+ <div
1066
+ aria-hidden
1067
+ className={PLOT_FRAME}
1068
+ data-slot="chart-plot"
1069
+ style={{
1070
+ height: plotHeight + PAD * 2,
1071
+ left: left - PAD,
1072
+ top: top - PAD,
1073
+ width: plotWidth + PAD * 2,
1074
+ }}
1075
+ />
1076
+ )}
1077
+
1078
+ {loading && (
1079
+ <ChartSkeleton
1080
+ height={plotHeight}
1081
+ kind={kind}
1082
+ left={left}
1083
+ top={top}
1084
+ width={plotWidth}
1085
+ />
1086
+ )}
1087
+
1088
+ {empty && !loading && (
1089
+ <p
1090
+ className="absolute flex items-center justify-center text-neutral-600 text-xs"
1091
+ style={{ height: plotHeight, left, top, width: plotWidth }}
1092
+ >
1093
+ {emptyLabel}
1094
+ </p>
1095
+ )}
1096
+
1097
+ {readable && active !== null && activeRow && (
1098
+ /*
1099
+ * The readout sits at the plot's top, on the side of the
1100
+ * crosshair with more room. It holds that height whatever the
1101
+ * pointer does: with two series there is no empty half to duck
1102
+ * into, and a panel that jumps is worse than one that covers a
1103
+ * little of a line the reader is not looking at.
1104
+ */
1105
+ <div
1106
+ className="pointer-events-none absolute z-10"
1107
+ style={{
1108
+ left:
1109
+ xOf(active) > width / 2
1110
+ ? undefined
1111
+ : xOf(active) + TOOLTIP_OFFSET,
1112
+ right:
1113
+ xOf(active) > width / 2
1114
+ ? width - (xOf(active) - TOOLTIP_OFFSET)
1115
+ : undefined,
1116
+ top,
1117
+ }}
1118
+ >
1119
+ {typeof tooltip === 'function' ? (
1120
+ tooltip(activeRow, active)
1121
+ ) : (
1122
+ <ChartTooltip
1123
+ footer={
1124
+ stacked && series.length > 1 ? (
1125
+ <ChartTooltipTotal
1126
+ value={writeValue(sums[active] ?? 0)}
1127
+ />
1128
+ ) : (
1129
+ periodFooter(active)
1130
+ )
1131
+ }
1132
+ rows={tooltipRows(active)}
1133
+ // A bubble only where there is a rule to say it from.
1134
+ tail={
1135
+ bars
1136
+ ? 'none'
1137
+ : xOf(active) > width / 2
1138
+ ? 'right'
1139
+ : 'left'
1140
+ }
1141
+ title={titleOf(active)}
1142
+ />
1143
+ )}
1144
+ </div>
1145
+ )}
1146
+
1147
+ {hoverable && (
1148
+ // One tab stop over the plot: the arrow keys walk the points, and
1149
+ // Enter picks the one being read, as a click on it would.
1150
+ <button
1151
+ aria-label={`${label}. Read each point with the arrow keys.`}
1152
+ className={cn(
1153
+ 'absolute rounded-xs outline-none focus-visible:outline-2 focus-visible:outline-focus focus-visible:outline-solid focus-visible:outline-offset-2',
1154
+ onSelect ? 'cursor-pointer' : 'cursor-default',
1155
+ )}
1156
+ data-slot="chart-hover-layer"
1157
+ onClick={() => {
1158
+ const row = active !== null ? data[active] : undefined;
1159
+ if (row && active !== null) onSelect?.(row, active);
1160
+ }}
1161
+ style={{
1162
+ height: plotHeight,
1163
+ left,
1164
+ top,
1165
+ touchAction: 'pan-y',
1166
+ width: plotWidth,
1167
+ }}
1168
+ type="button"
1169
+ {...handlers}
1170
+ />
1171
+ )}
1172
+ </>
1173
+ )}
1174
+ </div>
1175
+
1176
+ <p aria-live="polite" className="sr-only">
1177
+ {active !== null && activeRow ? readoutOf(active) : ''}
1178
+ </p>
1179
+
1180
+ {/* The figures themselves, for a reader who cannot see the marks. */}
1181
+ <div className="sr-only">
1182
+ <table>
1183
+ <caption>{label}</caption>
1184
+ <thead>
1185
+ <tr>
1186
+ <th scope="col">{xKey}</th>
1187
+ {series.map(line => (
1188
+ <th key={line.key} scope="col">
1189
+ {line.name ?? line.key}
1190
+ </th>
1191
+ ))}
1192
+ </tr>
1193
+ </thead>
1194
+ <tbody>
1195
+ {rawLabels.map((text, index) => (
1196
+ <tr key={`${index}-${text}`}>
1197
+ <th scope="row">{text}</th>
1198
+ {series.map((line, i) => (
1199
+ <td key={line.key}>
1200
+ {writeValue(readings[i]?.[index] ?? 0)}
1201
+ </td>
1202
+ ))}
1203
+ </tr>
1204
+ ))}
1205
+ </tbody>
1206
+ </table>
1207
+ </div>
1208
+ </figure>
1209
+ </ChartContext.Provider>
1210
+ );
1211
+ };
1212
+
1213
+ /**
1214
+ * The stand-in's heights, as shares of the plot: a week that climbs, peaks late and falls back,
1215
+ * as a measure over time tends to, so the shape reads as data waiting rather than a wave.
1216
+ */
1217
+ const SKELETON_LEVELS = [0.5, 0.62, 0.58, 0.82, 0.76, 0.42, 0.5];
1218
+
1219
+ /** Where the stand-in's grid sits, as shares of the plot's height: the rules a loaded chart draws. */
1220
+ const SKELETON_RULES = [0.08, 0.38, 0.68];
1221
+
1222
+ /** The edge of the stand-in's curve, a step stronger than its wash, as a line is over its tint. */
1223
+ const SKELETON_EDGE = 'color-mix(in oklab, var(--color-neutral-950) 14%, transparent)';
1224
+
1225
+ /**
1226
+ * A chart that is loading, with the depth of a loaded one: its frame and grid stay, and the
1227
+ * shape of its kind stands in the skeleton wash with the light crossing it. An area is a curve
1228
+ * with a firm edge over a wash that fades towards the baseline, as the real one does; a line is
1229
+ * the edge alone; a bar chart its bars. It takes the plot's room, so nothing moves when the data
1230
+ * lands.
1231
+ */
1232
+ const ChartSkeleton = ({
1233
+ kind,
1234
+ left,
1235
+ top,
1236
+ width,
1237
+ height,
1238
+ }: {
1239
+ kind: ChartKind;
1240
+ left: number;
1241
+ top: number;
1242
+ width: number;
1243
+ height: number;
1244
+ }) => {
1245
+ if (width <= 0 || height <= 0) return null;
1246
+ const step = width / (SKELETON_LEVELS.length - 1);
1247
+ const points = SKELETON_LEVELS.map((level, i) => ({ x: i * step, y: height * (1 - level) }));
1248
+ // The edge: a 2px ribbon along the curve.
1249
+ const edge = `${curvePath(points)} L${width},${(points.at(-1)?.y ?? 0) + 2} ${curvePath(
1250
+ points.map(point => ({ ...point, y: point.y + 2 })).reverse(),
1251
+ ).replace(/^M/, 'L')} Z`;
1252
+ // About one bar per 48px, never fewer than five.
1253
+ const bars = Math.max(5, Math.round(width / 48));
1254
+ const slot = width / bars;
1255
+
1256
+ return (
1257
+ <div
1258
+ aria-hidden
1259
+ className="absolute"
1260
+ data-slot="chart-skeleton"
1261
+ style={{ height, left, top, width }}
1262
+ >
1263
+ {SKELETON_RULES.map(share => (
1264
+ <div
1265
+ className="absolute inset-x-0 h-px"
1266
+ key={share}
1267
+ style={{
1268
+ backgroundColor: 'var(--color-chart-grid)',
1269
+ top: Math.round(height * share),
1270
+ }}
1271
+ />
1272
+ ))}
1273
+ {kind === 'bar' ? (
1274
+ Array.from({ length: bars }, (_, i) => {
1275
+ const at = Math.round(
1276
+ (i / Math.max(1, bars - 1)) * (SKELETON_LEVELS.length - 1),
1277
+ );
1278
+ return (
1279
+ <div
1280
+ className="skeleton absolute bottom-0 rounded-t-chip"
1281
+ // biome-ignore lint/suspicious/noArrayIndexKey: the bars never reorder
1282
+ key={i}
1283
+ style={{
1284
+ height: height * (SKELETON_LEVELS[at] ?? 0.5),
1285
+ left: i * slot + slot * 0.2,
1286
+ width: slot * 0.6,
1287
+ }}
1288
+ />
1289
+ );
1290
+ })
1291
+ ) : (
1292
+ <>
1293
+ {kind === 'area' && (
1294
+ <div
1295
+ className="skeleton absolute inset-0"
1296
+ style={{
1297
+ clipPath: `path('${areaPath(points, height)}')`,
1298
+ // The wash fades towards the baseline, as an area's tint does.
1299
+ maskImage:
1300
+ 'linear-gradient(to bottom, black 20%, transparent 100%)',
1301
+ }}
1302
+ />
1303
+ )}
1304
+ <div
1305
+ className="skeleton absolute inset-0"
1306
+ style={{ backgroundColor: SKELETON_EDGE, clipPath: `path('${edge}')` }}
1307
+ />
1308
+ </>
1309
+ )}
1310
+ </div>
1311
+ );
1312
+ };
1313
+
1314
+ /** The two textures as CSS, for marks drawn in HTML: the same stripes and dots as the SVG fills. */
1315
+ const TEXTURES = {
1316
+ stripes: (color: string) =>
1317
+ `repeating-linear-gradient(135deg, color-mix(in oklab, ${color} 50%, transparent) 0 1.2px, transparent 1.2px 6px)`,
1318
+ dots: (color: string) =>
1319
+ `radial-gradient(color-mix(in oklab, ${color} 55%, transparent) 1.1px, transparent 1.4px) 0 0 / 6px 6px`,
1320
+ };
1321
+
1322
+ const ChartTooltipTotal = ({ value }: { value: string }) => (
1323
+ <span className="flex items-center justify-between gap-4">
1324
+ <span className="text-neutral-600 text-xs">Total</span>
1325
+ <span className="font-medium text-neutral-950 text-xs tabular-nums">{value}</span>
1326
+ </span>
1327
+ );
1328
+
1329
+ const toneFill: Record<string, string> = {
1330
+ success: 'bg-success-500',
1331
+ warning: 'bg-warning-500',
1332
+ error: 'bg-error-500',
1333
+ neutral: 'bg-neutral-400',
1334
+ };
1335
+
1336
+ /** A label's width, near enough to keep two apart: 11 type, its dot or icon, its padding. */
1337
+ const labelWidth = (marker: ChartMarker) =>
1338
+ Math.ceil(marker.label.length * 6.2) + (marker.icon ? 12 : 6) + 4 + 8;
1339
+
1340
+ /**
1341
+ * Where each marker's label goes. Beside its rule at the plot's foot, on the rule's right before the
1342
+ * middle and its left past it, so it never runs off the frame. A label that would touch one already
1343
+ * placed takes the rule's other side, and when neither side is clear it steps up a row: two labels
1344
+ * never sit over each other, however close their markers are. A label also keeps off the other
1345
+ * markers' rules when a side lets it.
1346
+ */
1347
+ const placeMarkerLabels = (
1348
+ xs: number[],
1349
+ markers: readonly ChartMarker[],
1350
+ start: number,
1351
+ end: number,
1352
+ ) => {
1353
+ const placed: { from: number; to: number; row: number }[] = [];
1354
+ const order = markers.map((_, index) => index).sort((a, b) => (xs[a] ?? 0) - (xs[b] ?? 0));
1355
+ const result: { marker: ChartMarker; x: number; before: boolean; row: number }[] = [];
1356
+ for (const index of order) {
1357
+ const marker = markers[index] as ChartMarker;
1358
+ const x = xs[index] ?? 0;
1359
+ const width = labelWidth(marker);
1360
+ const span = (before: boolean) =>
1361
+ before
1362
+ ? { from: x - LABEL_GAP - width, to: x - LABEL_GAP }
1363
+ : { from: x + LABEL_GAP, to: x + LABEL_GAP + width };
1364
+ const preferred = x > start + (end - start) / 2;
1365
+ // Another marker's rule runs the plot's full height, so no row clears it: a side whose label
1366
+ // would cross one comes last, and is taken only when both sides do.
1367
+ const crossesRule = (before: boolean) => {
1368
+ const { from, to } = span(before);
1369
+ return xs.some(
1370
+ (other, at) =>
1371
+ at !== index && other >= from - LABEL_SPACE && other <= to + LABEL_SPACE,
1372
+ );
1373
+ };
1374
+ const sides = [preferred, !preferred]
1375
+ .filter(before => {
1376
+ const { from, to } = span(before);
1377
+ return from >= start && to <= end;
1378
+ })
1379
+ .sort((a, b) => Number(crossesRule(a)) - Number(crossesRule(b)));
1380
+ if (sides.length === 0) sides.push(preferred);
1381
+ const clear = (before: boolean, row: number) => {
1382
+ const { from, to } = span(before);
1383
+ return placed.every(
1384
+ other =>
1385
+ other.row !== row ||
1386
+ to + LABEL_SPACE <= other.from ||
1387
+ from >= other.to + LABEL_SPACE,
1388
+ );
1389
+ };
1390
+ let choice = { before: preferred, row: 0 };
1391
+ search: for (let row = 0; row < markers.length; row += 1) {
1392
+ for (const before of sides) {
1393
+ if (clear(before, row)) {
1394
+ choice = { before, row };
1395
+ break search;
1396
+ }
1397
+ }
1398
+ }
1399
+ placed.push({ ...span(choice.before), row: choice.row });
1400
+ result.push({ marker, x, ...choice });
1401
+ }
1402
+ return result;
1403
+ };
1404
+
1405
+ /**
1406
+ * A marker's note, always shown: 11 type beside its rule at the plot's foot, a 6 dot of its tone in
1407
+ * front (or its icon at 12), on the chart's own background so it reads over the series. No pin, no
1408
+ * shadow: a note about the data, never louder than the data.
1409
+ */
1410
+ const ChartMarkerLabel = ({
1411
+ marker,
1412
+ x,
1413
+ y,
1414
+ before,
1415
+ }: {
1416
+ marker: ChartMarker;
1417
+ x: number;
1418
+ y: number;
1419
+ before: boolean;
1420
+ }) => (
1421
+ <span
1422
+ className={cn(
1423
+ 'pointer-events-none absolute z-10 flex -translate-y-full items-center gap-1 whitespace-nowrap rounded-chip bg-chart-background px-1 py-0.5 text-neutral-600 text-xxs [&_svg]:size-3',
1424
+ before ? '-translate-x-full' : '',
1425
+ )}
1426
+ data-slot="chart-marker"
1427
+ style={{ left: before ? x - LABEL_GAP : x + LABEL_GAP, top: y }}
1428
+ >
1429
+ {marker.icon ?? (
1430
+ <span
1431
+ aria-hidden
1432
+ className={cn('size-1.5 shrink-0 rounded-full', toneFill[marker.tone ?? 'neutral'])}
1433
+ />
1434
+ )}
1435
+ {marker.label}
1436
+ </span>
1437
+ );