@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,991 @@
1
+ 'use client';
2
+
3
+ import {
4
+ type CSSProperties,
5
+ Fragment,
6
+ type KeyboardEvent,
7
+ type PointerEvent,
8
+ type ReactNode,
9
+ useCallback,
10
+ useId,
11
+ useLayoutEffect,
12
+ useMemo,
13
+ useRef,
14
+ useState,
15
+ } from 'react';
16
+ import { cn } from '../lib/cn';
17
+ import type { ChartRow, ChartSeries } from './cartesian';
18
+ import { type ChartColor, chartColor, seriesColor } from './lib/color';
19
+ import { formatCompact, formatNumber, formatX as formatXValue } from './lib/format';
20
+ import { PLOT_FRAME } from './lib/frame';
21
+ import { extent, niceTicks } from './lib/scale';
22
+ import { useWidth } from './lib/use-size';
23
+ import { ChartTooltip } from './tooltip';
24
+
25
+ /*
26
+ * The bar chart turned on its side: the categories down the left, each bar growing to the
27
+ * right from a flat foot on zero. It takes the bar chart's props under the bar chart's names
28
+ * (`x` is still the category, `formatY` still writes the value axis), so one becomes the other
29
+ * by its name alone. On its side a bar has the room along it for its own figures, so each
30
+ * segment carries its value inside it wherever the value fits.
31
+ */
32
+
33
+ export type HorizontalBarChartProps<Row extends ChartRow, X = unknown> = {
34
+ /** Says what the chart shows; it names the chart for a screen reader. */
35
+ 'aria-label': string;
36
+ data: readonly Row[];
37
+ /**
38
+ * The field naming each row, down the side. Called `x` as in the bar chart this is turned
39
+ * from, so either chart takes the other's props unchanged.
40
+ */
41
+ x: string;
42
+ series: readonly ChartSeries[];
43
+ /**
44
+ * Stack the series into one bar a row, as parts of a total the reader is meant to add up;
45
+ * false stands them side by side, one under the other. Stacked unless turned off.
46
+ */
47
+ stacked?: boolean;
48
+ /**
49
+ * The chart's whole height, the value axis included. Left out, it follows the rows: each is
50
+ * given the room a bar reads in with a figure inside it, so ten rows are twice as tall as five.
51
+ */
52
+ height?: number;
53
+ /** Writes each row's name down the side. */
54
+ formatX?: (value: X, row: Row, index: number) => string;
55
+ /** The readout's title; `formatX` unless given. */
56
+ formatTitle?: (value: X, row: Row, index: number) => string;
57
+ /** The value axis' labels at the foot; compact en-GB unless given. It writes the figures too. */
58
+ formatY?: (value: number) => string;
59
+ /** The figures alone, in the segments, past the bars and in the readout. */
60
+ formatValue?: (value: number) => string;
61
+ /** Holds the value axis down, for a measure with a ceiling such as a rate. */
62
+ yMax?: number;
63
+ /** How many value labels to aim for; as many as fit the width unless given. */
64
+ yTicks?: number;
65
+ /**
66
+ * Each segment's figure inside it. A segment too short to hold its figure goes without
67
+ * rather than spilling it over its neighbour; the readout still has it.
68
+ */
69
+ labels?: boolean;
70
+ /** Each bar's figure past its end: a stack's total, or a lone bar's own value. */
71
+ total?: boolean;
72
+ /**
73
+ * `solid` fills each bar; `tint` draws it as every other mark in the kit is drawn, a tint of
74
+ * its colour with a solid edge at its data end, and takes the texture.
75
+ */
76
+ fill?: 'solid' | 'tint';
77
+ /**
78
+ * A texture in a tinted bar: diagonal stripes or a field of dots. Left out, the chart
79
+ * follows the theme (`--chart-texture-stripes: 1` or `--chart-texture-dots: 1`).
80
+ */
81
+ texture?: 'none' | 'stripes' | 'dots';
82
+ /** False for a chart too small to read out; a function writes its own readout. */
83
+ tooltip?: boolean | ((row: Row, index: number) => ReactNode);
84
+ /** The hairlines up from each value label. */
85
+ grid?: boolean;
86
+ /** The rows' names down the side: the bar chart's x axis, turned. */
87
+ xAxis?: boolean;
88
+ /** The value labels at the foot: the bar chart's y axis, turned. */
89
+ yAxis?: boolean;
90
+ onSelect?: (row: Row, index: number) => void;
91
+ /** With no data, the plot is a skeleton. */
92
+ loading?: boolean;
93
+ emptyLabel?: string;
94
+ className?: string;
95
+ style?: CSSProperties;
96
+ };
97
+
98
+ /** Room for the labels, and the sizes of the marks. Everything else follows the theme. */
99
+ const GUTTER_GAP = 8;
100
+ const AXIS_HEIGHT = 20;
101
+ /** The room a row is given when the chart is not told its height. */
102
+ const ROW = 36;
103
+ /** A grouped row gives each of its bars this much, and keeps the same air round the group. */
104
+ const GROUPED_BAR = 12;
105
+ const BAR_GAP = 2;
106
+ const BAR_MAX = 24;
107
+ const BAR_MIN = 2;
108
+ /** A bar takes this share of its row, a group of bars a little more. */
109
+ const BAR_SHARE = 0.56;
110
+ const GROUP_SHARE = 0.72;
111
+ /** A figure needs this much clear on each side of it inside its segment. */
112
+ const LABEL_PAD = 6;
113
+ /** And a bar at least this thick, or the figure is taller than the bar it sits in. */
114
+ const LABEL_MIN_THICKNESS = 15;
115
+ const TOTAL_GAP = 6;
116
+ /** The value axis is read in about this many px a label. */
117
+ const TICK_ROOM = 100;
118
+ /** Past this share of the width the rows' names are cut short rather than squeeze the bars. */
119
+ const GUTTER_MAX = 0.36;
120
+ const TOOLTIP_GAP = 4;
121
+
122
+ const toNumber = (value: unknown) =>
123
+ typeof value === 'number' && Number.isFinite(value) ? value : 0;
124
+
125
+ const colorNameOf = (line: ChartSeries, index: number): ChartColor =>
126
+ line.muted ? 'neutral' : (line.color ?? (`series-${Math.min(index + 1, 5)}` as ChartColor));
127
+
128
+ const colorOf = (line: ChartSeries, index: number) =>
129
+ line.muted ? chartColor('neutral') : chartColor(line.color ?? seriesColor(index));
130
+
131
+ /**
132
+ * The ink a figure takes on a solid fill: near white on a deep colour, near black on a light
133
+ * one. It is worked out from the fill itself, in CSS, so a theme's own colours and the dark
134
+ * mode's get the right one without the chart knowing what they are.
135
+ */
136
+ const inkOn = (color: string) => `oklch(from ${color} clamp(0.2, (0.66 - l) * 1000, 0.99) 0 0)`;
137
+
138
+ /** The two textures as CSS: the same stripes and dots as the bar chart's. */
139
+ const TEXTURES = {
140
+ stripes: (color: string) =>
141
+ `repeating-linear-gradient(135deg, color-mix(in oklab, ${color} 50%, transparent) 0 1.2px, transparent 1.2px 6px)`,
142
+ dots: (color: string) =>
143
+ `radial-gradient(color-mix(in oklab, ${color} 55%, transparent) 1.1px, transparent 1.4px) 0 0 / 6px 6px`,
144
+ };
145
+
146
+ /**
147
+ * How wide each of a set of labels is, measured in the theme's own font rather than guessed.
148
+ * The probe holds each label once, out of sight; its layout width is read, so a chart drawn in
149
+ * a scaled frame measures what it will draw.
150
+ */
151
+ const useTextWidths = (texts: readonly string[]) => {
152
+ const ref = useRef<HTMLDivElement>(null);
153
+ const [widths, setWidths] = useState<ReadonlyMap<string, number>>(() => new Map());
154
+ const key = texts.join('\u0000');
155
+
156
+ // biome-ignore lint/correctness/useExhaustiveDependencies: the probe is read again when its texts change
157
+ useLayoutEffect(() => {
158
+ const node = ref.current;
159
+ if (!node) return;
160
+ const read = () => {
161
+ const next = new Map<string, number>();
162
+ for (const child of Array.from(node.children)) {
163
+ next.set(child.textContent ?? '', (child as HTMLElement).offsetWidth + 1);
164
+ }
165
+ setWidths(next);
166
+ };
167
+ read();
168
+ // A font that loads late, or a theme's own size, resizes the probe: read it again.
169
+ const observer = new ResizeObserver(read);
170
+ observer.observe(node);
171
+ return () => observer.disconnect();
172
+ }, [key]);
173
+
174
+ return [ref, widths] as const;
175
+ };
176
+
177
+ type Segment = {
178
+ key: string;
179
+ row: number;
180
+ series: number;
181
+ value: number;
182
+ color: string;
183
+ tinted: boolean;
184
+ texture?: 'none' | 'stripes' | 'dots';
185
+ positive: boolean;
186
+ head: boolean;
187
+ x: number;
188
+ y: number;
189
+ width: number;
190
+ thickness: number;
191
+ };
192
+
193
+ export const HorizontalBarChart = <Row extends ChartRow, X = unknown>({
194
+ 'aria-label': label,
195
+ data: rows,
196
+ x: xKey,
197
+ series,
198
+ stacked = true,
199
+ height: givenHeight,
200
+ formatX,
201
+ formatTitle,
202
+ formatY,
203
+ formatValue,
204
+ yMax,
205
+ yTicks,
206
+ labels = true,
207
+ total = false,
208
+ fill = 'solid',
209
+ texture,
210
+ tooltip = true,
211
+ grid = true,
212
+ xAxis = true,
213
+ yAxis = true,
214
+ onSelect,
215
+ loading = false,
216
+ emptyLabel = 'No data yet',
217
+ className,
218
+ style,
219
+ }: HorizontalBarChartProps<Row, X>) => {
220
+ const [containerRef, width] = useWidth<HTMLDivElement>();
221
+ const [active, setActive] = useState<number | null>(null);
222
+ const clipId = `plot-${useId().replace(/:/g, '')}`;
223
+
224
+ const data: readonly Row[] = loading ? [] : rows;
225
+ const empty = data.length === 0;
226
+ const writeAxis = formatY ?? formatCompact;
227
+ const writeValue = formatValue ?? formatY ?? formatNumber;
228
+ const count = series.length;
229
+ const grouped = !stacked && count > 1;
230
+ // A bar of one segment carries its figure past its end when the total is asked for.
231
+ const lone = !stacked || count === 1;
232
+
233
+ // Each row is given room for its bar; a group of bars, room for each of them.
234
+ const rowRoom = grouped ? Math.max(ROW, count * GROUPED_BAR + (count - 1) * BAR_GAP + 16) : ROW;
235
+ const bottom = yAxis ? AXIS_HEIGHT : 0;
236
+ // While loading, the rows the chart was given (or five) keep its height, so nothing moves when they land.
237
+ const rowsShown = loading ? rows.length || 5 : empty ? 5 : data.length;
238
+ const height = givenHeight ?? rowsShown * rowRoom + bottom;
239
+ const plotHeight = Math.max(0, height - bottom);
240
+ const slot = empty ? plotHeight : plotHeight / data.length;
241
+
242
+ const readings = useMemo(
243
+ () => series.map(line => data.map(row => toNumber(row[line.key]))),
244
+ [data, series],
245
+ );
246
+ const sums = useMemo(
247
+ () => data.map((_, i) => readings.reduce((sum, column) => sum + (column[i] ?? 0), 0)),
248
+ [data, readings],
249
+ );
250
+
251
+ const labelOf = useCallback(
252
+ (row: Row | undefined, i: number) => {
253
+ const value = row?.[xKey];
254
+ return row && formatX ? formatX(value as X, row, i) : formatXValue(value);
255
+ },
256
+ [formatX, xKey],
257
+ );
258
+ const names = useMemo(() => data.map(labelOf), [data, labelOf]);
259
+
260
+ // Every figure the chart may draw, measured once so each knows whether it fits.
261
+ const figures = useMemo(() => {
262
+ const all = new Set<string>();
263
+ for (const column of readings) for (const value of column) all.add(writeValue(value));
264
+ if (total) for (const sum of sums) all.add(writeValue(sum));
265
+ return [...all];
266
+ }, [readings, sums, total, writeValue]);
267
+ const [figureProbe, figureWidths] = useTextWidths(figures);
268
+ // While loading, the names of the rows it was given are measured, never drawn, so the plot keeps its place.
269
+ const measured = useMemo(
270
+ () => (loading ? rows.map(labelOf) : names),
271
+ [loading, rows, labelOf, names],
272
+ );
273
+ const [nameProbe, nameWidths] = useTextWidths(measured);
274
+ const widestName = Math.max(
275
+ loading && rows.length === 0 ? 64 : 0,
276
+ ...measured.map(name => nameWidths.get(name) ?? 0),
277
+ );
278
+
279
+ const left =
280
+ xAxis && (!empty || loading)
281
+ ? Math.min(widestName, Math.round(width * GUTTER_MAX)) + GUTTER_GAP
282
+ : 0;
283
+ // The plot, and the frame round it, take the width past the names.
284
+ const plotWidth = Math.max(0, width - left);
285
+
286
+ /*
287
+ * The value axis. The data sets the domain, and the axis is read in as many places as the
288
+ * width has room for. With the totals drawn past the bars, the domain reaches far enough
289
+ * that the longest bar's figure still ends inside the chart.
290
+ */
291
+ const ticks = useMemo(() => {
292
+ const span = extent(stacked ? sums : readings.flat());
293
+ const wanted = yTicks ?? Math.max(2, Math.min(8, Math.floor(plotWidth / TICK_ROOM)));
294
+ const nice = niceTicks(span.min, span.max, wanted, yMax);
295
+ if (!total || plotWidth <= 0) return nice;
296
+ const ends = stacked ? sums : readings.flat();
297
+ let max = nice.max;
298
+ for (const value of ends) {
299
+ if (value <= 0) continue;
300
+ const room = plotWidth - TOTAL_GAP - (figureWidths.get(writeValue(value)) ?? 0);
301
+ if (room > 0) max = Math.max(max, nice.min + ((value - nice.min) * plotWidth) / room);
302
+ }
303
+ return { ...nice, max };
304
+ }, [figureWidths, plotWidth, readings, stacked, sums, total, writeValue, yMax, yTicks]);
305
+
306
+ const xOf = useCallback(
307
+ (value: number) =>
308
+ left +
309
+ (ticks.max === ticks.min
310
+ ? 0
311
+ : ((value - ticks.min) / (ticks.max - ticks.min)) * plotWidth),
312
+ [left, plotWidth, ticks],
313
+ );
314
+ const baseline = xOf(Math.max(ticks.min, 0));
315
+ const axisLabels = useMemo(() => ticks.values.map(writeAxis), [ticks, writeAxis]);
316
+ const [axisProbe, axisWidths] = useTextWidths(axisLabels);
317
+
318
+ const thickness = Math.max(
319
+ BAR_MIN,
320
+ grouped
321
+ ? (Math.min(slot * GROUP_SHARE, BAR_MAX * count + BAR_GAP * (count - 1)) -
322
+ BAR_GAP * (count - 1)) /
323
+ count
324
+ : Math.min(slot * BAR_SHARE, BAR_MAX),
325
+ );
326
+
327
+ const segments = useMemo(() => {
328
+ if (empty || plotWidth <= 0) return [];
329
+ const out: Segment[] = [];
330
+ data.forEach((_, row) => {
331
+ const middle = slot * (row + 0.5);
332
+ const groupThickness = grouped ? thickness * count + BAR_GAP * (count - 1) : thickness;
333
+ let right = baseline;
334
+ let leftEdge = baseline;
335
+ const placed: Segment[] = [];
336
+ series.forEach((line, index) => {
337
+ const value = readings[index]?.[row] ?? 0;
338
+ const positive = value >= 0;
339
+ const size = Math.abs(xOf(value) - xOf(0));
340
+ const shared = {
341
+ key: `${row}-${line.key}`,
342
+ row,
343
+ series: index,
344
+ value,
345
+ color: colorOf(line, index),
346
+ tinted: (line.fill ?? fill) === 'tint',
347
+ texture: line.texture ?? texture,
348
+ positive,
349
+ thickness,
350
+ };
351
+ if (!stacked || count === 1) {
352
+ placed.push({
353
+ ...shared,
354
+ head: size > 0,
355
+ x: positive ? baseline : baseline - size,
356
+ y:
357
+ middle -
358
+ groupThickness / 2 +
359
+ (grouped ? index * (thickness + BAR_GAP) : 0),
360
+ width: size,
361
+ });
362
+ return;
363
+ }
364
+ // A stacked segment starts where the one before it stopped, two pixels clear of it.
365
+ const from = positive ? right : leftEdge;
366
+ const gap = from === baseline ? 0 : BAR_GAP;
367
+ if (positive) right = from + size;
368
+ else leftEdge = from - size;
369
+ placed.push({
370
+ ...shared,
371
+ head: false,
372
+ x: positive ? from + gap : from - size,
373
+ y: middle - thickness / 2,
374
+ width: Math.max(0, size - gap),
375
+ });
376
+ });
377
+ if (stacked && count > 1) {
378
+ // Only the stack's far end is rounded: the last segment drawn on either side.
379
+ const lastRight = placed.findLastIndex(s => s.positive && s.width > 0);
380
+ const lastLeft = placed.findLastIndex(s => !s.positive && s.width > 0);
381
+ for (const at of [lastRight, lastLeft]) {
382
+ const segment = placed[at];
383
+ if (segment) segment.head = true;
384
+ }
385
+ }
386
+ out.push(...placed);
387
+ });
388
+ return out;
389
+ }, [
390
+ baseline,
391
+ count,
392
+ data,
393
+ empty,
394
+ fill,
395
+ grouped,
396
+ plotWidth,
397
+ readings,
398
+ series,
399
+ slot,
400
+ stacked,
401
+ texture,
402
+ thickness,
403
+ xOf,
404
+ ]);
405
+
406
+ // Where each bar ends, for the figure drawn past it.
407
+ const ends = useMemo(() => {
408
+ if (!total || empty) return [];
409
+ if (lone)
410
+ return segments.map(segment => ({
411
+ key: segment.key,
412
+ row: segment.row,
413
+ text: writeValue(segment.value),
414
+ positive: segment.positive,
415
+ at: segment.positive ? segment.x + segment.width : segment.x,
416
+ y: segment.y + segment.thickness / 2,
417
+ }));
418
+ return data.map((_, row) => {
419
+ const sum = sums[row] ?? 0;
420
+ const own = segments.filter(segment => segment.key.startsWith(`${row}-`));
421
+ const reach =
422
+ sum >= 0
423
+ ? Math.max(baseline, ...own.map(s => s.x + s.width))
424
+ : Math.min(baseline, ...own.map(s => s.x));
425
+ return {
426
+ key: String(row),
427
+ row,
428
+ text: writeValue(sum),
429
+ positive: sum >= 0,
430
+ at: reach,
431
+ y: slot * (row + 0.5),
432
+ };
433
+ });
434
+ }, [baseline, data, empty, lone, segments, slot, sums, total, writeValue]);
435
+
436
+ /*
437
+ * Which row is being read: the one under the pointer, or the one the arrow keys walked to.
438
+ * A reader aims at a row, never at a bar, so the pointer's height is snapped to its row.
439
+ */
440
+ const rowAt = (y: number) =>
441
+ Math.max(0, Math.min(data.length - 1, Math.floor(y / Math.max(1, slot))));
442
+ const onPointerMove = (event: PointerEvent<HTMLElement>) => {
443
+ if (empty) return;
444
+ const box = event.currentTarget.getBoundingClientRect();
445
+ // The layer's own height against its drawn one: a chart in a scaled frame still reads true.
446
+ const scale = box.height / Math.max(1, event.currentTarget.offsetHeight);
447
+ setActive(rowAt((event.clientY - box.top) / scale));
448
+ };
449
+ const onKeyDown = (event: KeyboardEvent<HTMLElement>) => {
450
+ if (empty) return;
451
+ const last = data.length - 1;
452
+ const step = (by: number) =>
453
+ setActive(current =>
454
+ Math.max(0, Math.min(last, (current ?? (by > 0 ? -1 : data.length)) + by)),
455
+ );
456
+ switch (event.key) {
457
+ case 'ArrowDown':
458
+ case 'ArrowRight':
459
+ step(1);
460
+ break;
461
+ case 'ArrowUp':
462
+ case 'ArrowLeft':
463
+ step(-1);
464
+ break;
465
+ case 'Home':
466
+ setActive(0);
467
+ break;
468
+ case 'End':
469
+ setActive(last);
470
+ break;
471
+ case 'Escape':
472
+ setActive(null);
473
+ break;
474
+ default:
475
+ return;
476
+ }
477
+ event.preventDefault();
478
+ };
479
+
480
+ const activeRow = active !== null ? data[active] : undefined;
481
+ const readable = tooltip !== false && !empty;
482
+
483
+ const titleOf = (index: number) => {
484
+ const row = data[index];
485
+ if (!row) return '';
486
+ const value = row[xKey];
487
+ if (formatTitle) return formatTitle(value as X, row, index);
488
+ if (formatX) return formatX(value as X, row, index);
489
+ return formatXValue(value, true);
490
+ };
491
+ const readoutOf = (index: number) =>
492
+ `${titleOf(index)}: ${series
493
+ .map((line, i) => `${line.name ?? line.key} ${writeValue(readings[i]?.[index] ?? 0)}`)
494
+ .join(', ')}`;
495
+
496
+ // The readout stands at the far end of the values, beside the row: under it in the top
497
+ // half, over it in the bottom half, and held inside the chart when it is taller than the room.
498
+ const below = active !== null && active < data.length / 2;
499
+ const rowTop = active !== null ? slot * active : 0;
500
+
501
+ return (
502
+ <figure
503
+ aria-busy={loading || undefined}
504
+ aria-label={label}
505
+ className={cn('relative m-0 w-full', className)}
506
+ data-slot="horizontal-bar-chart"
507
+ style={{ height, ...style }}
508
+ >
509
+ {/* The labels, measured in the theme's own font before they are placed. */}
510
+ <div
511
+ aria-hidden
512
+ className="pointer-events-none invisible absolute top-0 left-0 w-max text-xxs"
513
+ ref={nameProbe}
514
+ >
515
+ {[...new Set(measured)].map(text => (
516
+ <div className="w-max whitespace-nowrap" key={text}>
517
+ {text}
518
+ </div>
519
+ ))}
520
+ </div>
521
+ <div
522
+ aria-hidden
523
+ className="pointer-events-none invisible absolute top-0 left-0 w-max font-medium text-xxs tabular-nums"
524
+ ref={figureProbe}
525
+ >
526
+ {figures.map(text => (
527
+ <div className="w-max whitespace-nowrap" key={text}>
528
+ {text}
529
+ </div>
530
+ ))}
531
+ </div>
532
+ <div
533
+ aria-hidden
534
+ className="pointer-events-none invisible absolute top-0 left-0 w-max text-xxs tabular-nums"
535
+ ref={axisProbe}
536
+ >
537
+ {[...new Set(axisLabels)].map(text => (
538
+ <div className="w-max whitespace-nowrap" key={text}>
539
+ {text}
540
+ </div>
541
+ ))}
542
+ </div>
543
+
544
+ <div
545
+ className={cn(
546
+ 'relative size-full transition-opacity duration-200 motion-reduce:transition-none',
547
+ // It appears once it knows how wide it is, rather than jumping into place.
548
+ width > 0 ? 'opacity-100' : 'opacity-0',
549
+ )}
550
+ ref={containerRef}
551
+ >
552
+ {width > 0 && (
553
+ <>
554
+ {(!empty || loading) && (
555
+ // The plot's own place in its card; the names and the axis stand outside it.
556
+ <div
557
+ aria-hidden
558
+ className={PLOT_FRAME}
559
+ data-slot="chart-frame"
560
+ style={{ height: plotHeight, left, top: 0, width: plotWidth }}
561
+ />
562
+ )}
563
+ {/* The marks are drawn for the eye; the table below carries the figures. */}
564
+ <svg
565
+ aria-hidden="true"
566
+ className="absolute inset-0 block overflow-visible"
567
+ height={height}
568
+ width={width}
569
+ >
570
+ {/* Whatever is drawn in the plot stays in its frame, corners and all. */}
571
+ <defs>
572
+ <clipPath id={clipId}>
573
+ <rect
574
+ height={plotHeight}
575
+ style={{ rx: 'var(--radius-item)' }}
576
+ width={plotWidth}
577
+ x={left}
578
+ y={0}
579
+ />
580
+ </clipPath>
581
+ </defs>
582
+ {grid && !loading && !empty && (
583
+ <g clipPath={`url(#${clipId})`}>
584
+ {ticks.values
585
+ // A rule on the frame's own edge is the frame's.
586
+ .filter(tick => {
587
+ const at = Math.round(xOf(tick));
588
+ return at > left + 1 && at < width - 2;
589
+ })
590
+ .map(tick => (
591
+ // Half a pixel off the whole one, so a 1px rule lands crisp.
592
+ <line
593
+ key={tick}
594
+ shapeRendering="crispEdges"
595
+ stroke="var(--color-chart-grid)"
596
+ x1={Math.round(xOf(tick)) + 0.5}
597
+ x2={Math.round(xOf(tick)) + 0.5}
598
+ y1={0}
599
+ y2={plotHeight}
600
+ />
601
+ ))}
602
+ </g>
603
+ )}
604
+ {yAxis &&
605
+ !empty &&
606
+ ticks.values.map((tick, i) => {
607
+ const at = xOf(tick);
608
+ const half = (axisWidths.get(axisLabels[i] ?? '') ?? 0) / 2;
609
+ // Centred under its rule, unless that would run off an edge.
610
+ const anchor =
611
+ at - half < left
612
+ ? 'start'
613
+ : at + half > width
614
+ ? 'end'
615
+ : 'middle';
616
+ return (
617
+ <text
618
+ className="text-xxs tabular-nums"
619
+ fill="var(--color-chart-label)"
620
+ key={tick}
621
+ textAnchor={anchor}
622
+ x={
623
+ anchor === 'start'
624
+ ? left
625
+ : anchor === 'end'
626
+ ? width
627
+ : at
628
+ }
629
+ y={plotHeight + 16}
630
+ >
631
+ {axisLabels[i]}
632
+ </text>
633
+ );
634
+ })}
635
+ </svg>
636
+
637
+ {xAxis &&
638
+ !empty &&
639
+ names.map((name, index) => (
640
+ <div
641
+ className={cn(
642
+ 'absolute flex items-center text-xxs transition-colors duration-150 motion-reduce:transition-none',
643
+ active === index && 'text-neutral-950',
644
+ )}
645
+ key={`${index}-${name}`}
646
+ style={{
647
+ color:
648
+ active === index
649
+ ? undefined
650
+ : 'var(--color-chart-label)',
651
+ height: slot,
652
+ left: 0,
653
+ top: slot * index,
654
+ width: Math.max(0, left - GUTTER_GAP),
655
+ }}
656
+ >
657
+ <span className="truncate">{name}</span>
658
+ </div>
659
+ ))}
660
+
661
+ {/*
662
+ * The bars, clipped to the frame's rect and corner. The layer inside is
663
+ * set back by the gutter, so the marks keep the chart's own coordinates.
664
+ */}
665
+ <div
666
+ className="pointer-events-none absolute overflow-hidden rounded-item"
667
+ data-slot="chart-bars"
668
+ style={{ height: plotHeight, left, top: 0, width: plotWidth }}
669
+ >
670
+ <div className="absolute top-0" style={{ height, left: -left, width }}>
671
+ {active !== null && (
672
+ // The row being read, on the item corner as a hovered row is.
673
+ <div
674
+ className="absolute rounded-item"
675
+ style={{
676
+ background: 'var(--color-chart-band)',
677
+ height: Math.max(0, slot - 2),
678
+ left: left + 3,
679
+ top: rowTop + 1,
680
+ width: Math.max(0, plotWidth - 6),
681
+ }}
682
+ />
683
+ )}
684
+ {/*
685
+ * The bars wipe in from their foot once, as they would grow: a
686
+ * keyframe, so a chart printed or in a background tab is still drawn.
687
+ */}
688
+ <div className="absolute inset-0 motion-safe:animate-reveal">
689
+ {segments.map(segment => {
690
+ if (segment.width <= 0) return null;
691
+ /*
692
+ * A bar stands flat on zero and is rounded at its far end, on
693
+ * the item corner: rounded at one end, it never turns into a pill.
694
+ */
695
+ const head = segment.head
696
+ ? `min(var(--radius-item), ${segment.thickness / 2}px)`
697
+ : '0px';
698
+ const { tinted, texture: grain, color } = segment;
699
+ const text = writeValue(segment.value);
700
+ const textWidth =
701
+ figureWidths.get(text) ?? Number.POSITIVE_INFINITY;
702
+ const labelled =
703
+ labels &&
704
+ !(total && lone) &&
705
+ segment.thickness >= LABEL_MIN_THICKNESS &&
706
+ segment.width >= textWidth + LABEL_PAD * 2;
707
+
708
+ return (
709
+ <div
710
+ className={cn(
711
+ // The row being read is marked by its band, as a column is in the bar chart.
712
+ 'absolute flex items-center justify-center overflow-hidden',
713
+ )}
714
+ key={segment.key}
715
+ style={{
716
+ background: tinted
717
+ ? `color-mix(in oklab, ${color} 20%, transparent)`
718
+ : color,
719
+ borderRadius: segment.positive
720
+ ? `0 ${head} ${head} 0`
721
+ : `${head} 0 0 ${head}`,
722
+ // The bar's own line at its data end, as a line
723
+ // stands over the tint under it.
724
+ boxShadow: tinted
725
+ ? `inset ${segment.positive ? -2 : 2}px 0 0 ${color}`
726
+ : undefined,
727
+ height: segment.thickness,
728
+ left: segment.x,
729
+ top: segment.y,
730
+ width: segment.width,
731
+ }}
732
+ >
733
+ {tinted &&
734
+ grain !== 'none' &&
735
+ (['stripes', 'dots'] as const)
736
+ .filter(
737
+ kindOf => !grain || grain === kindOf,
738
+ )
739
+ .map(kindOf => (
740
+ <span
741
+ aria-hidden
742
+ className="absolute inset-0"
743
+ key={kindOf}
744
+ style={{
745
+ background:
746
+ TEXTURES[kindOf](color),
747
+ // Asked for outright it shows; otherwise the theme says.
748
+ opacity: grain
749
+ ? 1
750
+ : `var(--chart-texture-${kindOf}, 0)`,
751
+ }}
752
+ />
753
+ ))}
754
+ {labelled && (
755
+ <span
756
+ className={cn(
757
+ 'relative font-medium text-xxs tabular-nums',
758
+ tinted && 'text-neutral-950',
759
+ )}
760
+ style={
761
+ tinted
762
+ ? undefined
763
+ : { color: inkOn(color) }
764
+ }
765
+ >
766
+ {text}
767
+ </span>
768
+ )}
769
+ </div>
770
+ );
771
+ })}
772
+ {ends.map(end => (
773
+ <span
774
+ className={cn(
775
+ 'absolute flex -translate-y-1/2 items-center whitespace-nowrap font-medium text-neutral-950 text-xxs tabular-nums',
776
+ )}
777
+ key={end.key}
778
+ style={
779
+ end.positive
780
+ ? { left: end.at + TOTAL_GAP, top: end.y }
781
+ : {
782
+ right: width - end.at + TOTAL_GAP,
783
+ top: end.y,
784
+ }
785
+ }
786
+ >
787
+ {end.text}
788
+ </span>
789
+ ))}
790
+ </div>
791
+ </div>
792
+ </div>
793
+
794
+ {loading && (
795
+ <ChartSkeleton
796
+ height={plotHeight}
797
+ left={left}
798
+ top={0}
799
+ width={plotWidth}
800
+ />
801
+ )}
802
+
803
+ {empty && !loading && (
804
+ <p
805
+ className="absolute flex items-center justify-center rounded-item border border-neutral-200 border-dashed text-neutral-600 text-xs"
806
+ style={{ height: plotHeight, left: 0, top: 0, width }}
807
+ >
808
+ {emptyLabel}
809
+ </p>
810
+ )}
811
+
812
+ {readable && active !== null && activeRow && (
813
+ <div
814
+ className={cn(
815
+ 'pointer-events-none absolute inset-y-0 right-0 z-10 flex flex-col',
816
+ !below && 'justify-end',
817
+ )}
818
+ >
819
+ {below && (
820
+ <div
821
+ className="min-h-0 shrink"
822
+ style={{ height: rowTop + slot + TOOLTIP_GAP }}
823
+ />
824
+ )}
825
+ <div className="shrink-0">
826
+ {typeof tooltip === 'function' ? (
827
+ tooltip(activeRow, active)
828
+ ) : (
829
+ <ChartTooltip
830
+ footer={
831
+ stacked && count > 1 ? (
832
+ <ChartTooltipTotal
833
+ value={writeValue(sums[active] ?? 0)}
834
+ />
835
+ ) : undefined
836
+ }
837
+ rows={series.map((line, i) => ({
838
+ color: colorNameOf(line, i),
839
+ name: line.name ?? line.key,
840
+ value: writeValue(readings[i]?.[active] ?? 0),
841
+ }))}
842
+ title={titleOf(active)}
843
+ />
844
+ )}
845
+ </div>
846
+ {!below && (
847
+ <div
848
+ className="min-h-0 shrink"
849
+ style={{ height: height - rowTop + TOOLTIP_GAP }}
850
+ />
851
+ )}
852
+ </div>
853
+ )}
854
+
855
+ {!empty && (readable || onSelect) && (
856
+ // One tab stop over the plot: the arrow keys walk the rows, and Enter
857
+ // picks the one being read, as a click on it would.
858
+ <button
859
+ aria-label={`${label}. Read each row with the arrow keys.`}
860
+ className={cn(
861
+ 'absolute rounded-xs outline-none focus-visible:outline-2 focus-visible:outline-focus focus-visible:outline-solid focus-visible:outline-offset-2',
862
+ onSelect ? 'cursor-pointer' : 'cursor-default',
863
+ )}
864
+ data-slot="chart-hover-layer"
865
+ onBlur={() => setActive(null)}
866
+ onClick={() => {
867
+ const row = active !== null ? data[active] : undefined;
868
+ if (row && active !== null) onSelect?.(row, active);
869
+ }}
870
+ onKeyDown={onKeyDown}
871
+ onPointerCancel={() => setActive(null)}
872
+ onPointerLeave={() => setActive(null)}
873
+ onPointerMove={onPointerMove}
874
+ style={{
875
+ height: plotHeight,
876
+ left: 0,
877
+ top: 0,
878
+ touchAction: 'pan-y',
879
+ width,
880
+ }}
881
+ type="button"
882
+ />
883
+ )}
884
+ </>
885
+ )}
886
+ </div>
887
+
888
+ <p aria-live="polite" className="sr-only">
889
+ {active !== null && activeRow ? readoutOf(active) : ''}
890
+ </p>
891
+
892
+ {/* The figures themselves, for a reader who cannot see the marks. */}
893
+ <div className="sr-only">
894
+ <table>
895
+ <caption>{label}</caption>
896
+ <thead>
897
+ <tr>
898
+ <th scope="col">{xKey}</th>
899
+ {series.map(line => (
900
+ <th key={line.key} scope="col">
901
+ {line.name ?? line.key}
902
+ </th>
903
+ ))}
904
+ </tr>
905
+ </thead>
906
+ <tbody>
907
+ {names.map((text, index) => (
908
+ <tr key={`${index}-${text}`}>
909
+ <th scope="row">{text}</th>
910
+ {series.map((line, i) => (
911
+ <td key={line.key}>{writeValue(readings[i]?.[index] ?? 0)}</td>
912
+ ))}
913
+ </tr>
914
+ ))}
915
+ </tbody>
916
+ </table>
917
+ </div>
918
+ </figure>
919
+ );
920
+ };
921
+
922
+ /** The stand-in bars' lengths, as shares of the plot: a ranking, longest first. */
923
+ const SKELETON_LENGTHS = [0.86, 0.72, 0.6, 0.47, 0.38, 0.3, 0.24, 0.2];
924
+
925
+ /**
926
+ * A chart that is loading: a ranking of bars in the skeleton wash, longest first, each with a
927
+ * short label before it, and the band of light crossing them. It takes the plot's room, so
928
+ * nothing moves when the data lands.
929
+ */
930
+ const ChartSkeleton = ({
931
+ left,
932
+ top,
933
+ width,
934
+ height,
935
+ }: {
936
+ left: number;
937
+ top: number;
938
+ width: number;
939
+ height: number;
940
+ }) => {
941
+ if (width <= 0 || height <= 0) return null;
942
+ const rows = Math.max(3, Math.min(SKELETON_LENGTHS.length, Math.floor(height / ROW)));
943
+ const row = height / rows;
944
+ const bar = Math.min(BAR_MAX, row * BAR_SHARE);
945
+ // The names' column the chart keeps while loading, and the plot past it, exactly as loaded.
946
+ const gutter = Math.max(0, left - GUTTER_GAP);
947
+ const plotLeft = left;
948
+ const plotWidth = width;
949
+
950
+ return (
951
+ <div
952
+ aria-hidden
953
+ className="absolute inset-x-0"
954
+ data-slot="chart-skeleton"
955
+ style={{ height, top }}
956
+ >
957
+ {Array.from({ length: rows }, (_, i) => {
958
+ const y = i * row + (row - bar) / 2;
959
+ return (
960
+ // biome-ignore lint/suspicious/noArrayIndexKey: the rows never reorder
961
+ <Fragment key={i}>
962
+ <div
963
+ className="skeleton absolute h-2.5 rounded-chip"
964
+ style={{
965
+ left: 0,
966
+ top: y + bar / 2 - 5,
967
+ width: gutter * (i % 2 ? 0.6 : 0.8),
968
+ }}
969
+ />
970
+ <div
971
+ className="skeleton absolute rounded-r-chip"
972
+ style={{
973
+ height: bar,
974
+ left: plotLeft,
975
+ top: y,
976
+ width: plotWidth * (SKELETON_LENGTHS[i] ?? 0.2),
977
+ }}
978
+ />
979
+ </Fragment>
980
+ );
981
+ })}
982
+ </div>
983
+ );
984
+ };
985
+
986
+ const ChartTooltipTotal = ({ value }: { value: string }) => (
987
+ <span className="flex items-center justify-between gap-4">
988
+ <span className="text-neutral-600 text-xs">Total</span>
989
+ <span className="font-medium text-neutral-950 text-xs tabular-nums">{value}</span>
990
+ </span>
991
+ );