basalt-ui 1.30.2 → 1.32.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 (357) hide show
  1. package/AGENTS.md +1 -1
  2. package/CHANGELOG.md +54 -0
  3. package/MIGRATING.md +256 -12
  4. package/agent/rules/basalt-controls.md +19 -19
  5. package/agent/rules/basalt-mantine.md +5 -5
  6. package/configs/oxlint-plugin.js +371 -4
  7. package/configs/oxlint-plugin.test.ts +327 -2
  8. package/configs/oxlint.json +2 -1
  9. package/dist/agent-chat/composer.d.ts.map +1 -1
  10. package/dist/agent-chat/composer.js +41 -43
  11. package/dist/agent-chat/thread-workspace.d.ts +2 -2
  12. package/dist/agent-chat/thread-workspace.d.ts.map +1 -1
  13. package/dist/agent-chat/thread-workspace.js +3 -4
  14. package/dist/agent-chat/use-keyboard-inset.d.ts +2 -0
  15. package/dist/agent-chat/use-keyboard-inset.d.ts.map +1 -0
  16. package/dist/agent-chat/use-keyboard-inset.js +34 -0
  17. package/dist/charts/cursor/touch-pin.d.ts +45 -0
  18. package/dist/charts/cursor/touch-pin.d.ts.map +1 -0
  19. package/dist/charts/cursor/touch-pin.js +79 -0
  20. package/dist/charts/hooks/use-discrete-cursor.d.ts +66 -0
  21. package/dist/charts/hooks/use-discrete-cursor.d.ts.map +1 -0
  22. package/dist/charts/hooks/use-discrete-cursor.js +198 -0
  23. package/dist/charts/hooks/use-hysteresis.d.ts +18 -0
  24. package/dist/charts/hooks/use-hysteresis.d.ts.map +1 -0
  25. package/dist/charts/hooks/use-hysteresis.js +12 -0
  26. package/dist/charts/hooks/useBandPlot.d.ts +7 -0
  27. package/dist/charts/hooks/useBandPlot.d.ts.map +1 -1
  28. package/dist/charts/hooks/useBandPlot.js +62 -18
  29. package/dist/charts/hooks/useChartCursor.d.ts +40 -11
  30. package/dist/charts/hooks/useChartCursor.d.ts.map +1 -1
  31. package/dist/charts/hooks/useChartCursor.js +102 -11
  32. package/dist/charts/index.d.ts +0 -1
  33. package/dist/charts/index.d.ts.map +1 -1
  34. package/dist/charts/index.js +0 -10
  35. package/dist/charts/kinds/BandStrip.d.ts.map +1 -1
  36. package/dist/charts/kinds/BandStrip.js +47 -82
  37. package/dist/charts/kinds/Donut.d.ts +9 -3
  38. package/dist/charts/kinds/Donut.d.ts.map +1 -1
  39. package/dist/charts/kinds/Donut.js +110 -48
  40. package/dist/charts/kinds/DualPanel.d.ts.map +1 -1
  41. package/dist/charts/kinds/DualPanel.js +138 -145
  42. package/dist/charts/kinds/Heatmap.d.ts +7 -0
  43. package/dist/charts/kinds/Heatmap.d.ts.map +1 -1
  44. package/dist/charts/kinds/Heatmap.js +37 -16
  45. package/dist/charts/kinds/MirroredBars.d.ts.map +1 -1
  46. package/dist/charts/kinds/MirroredBars.js +68 -111
  47. package/dist/charts/kinds/band-chrome.d.ts +31 -0
  48. package/dist/charts/kinds/band-chrome.d.ts.map +1 -0
  49. package/dist/charts/kinds/band-chrome.js +71 -0
  50. package/dist/charts/kinds/donut-layout.d.ts +47 -0
  51. package/dist/charts/kinds/donut-layout.d.ts.map +1 -0
  52. package/dist/charts/kinds/donut-layout.js +52 -0
  53. package/dist/charts/layout/auto-margin.d.ts +7 -2
  54. package/dist/charts/layout/auto-margin.d.ts.map +1 -1
  55. package/dist/charts/layout/auto-margin.js +12 -3
  56. package/dist/charts/layout/end-labels.d.ts +64 -0
  57. package/dist/charts/layout/end-labels.d.ts.map +1 -0
  58. package/dist/charts/layout/end-labels.js +76 -0
  59. package/dist/charts/primitives/Axes.d.ts +32 -4
  60. package/dist/charts/primitives/Axes.d.ts.map +1 -1
  61. package/dist/charts/primitives/Axes.js +62 -16
  62. package/dist/charts/primitives/CartesianChart.d.ts +7 -6
  63. package/dist/charts/primitives/CartesianChart.d.ts.map +1 -1
  64. package/dist/charts/primitives/CartesianChart.js +264 -145
  65. package/dist/charts/primitives/ChartCard.d.ts +0 -14
  66. package/dist/charts/primitives/ChartCard.d.ts.map +1 -1
  67. package/dist/charts/primitives/ChartCard.js +56 -21
  68. package/dist/charts/primitives/ChartFrame.d.ts +28 -22
  69. package/dist/charts/primitives/ChartFrame.d.ts.map +1 -1
  70. package/dist/charts/primitives/ChartFrame.js +116 -34
  71. package/dist/charts/primitives/ChartLegend.d.ts +19 -15
  72. package/dist/charts/primitives/ChartLegend.d.ts.map +1 -1
  73. package/dist/charts/primitives/ChartLegend.js +86 -73
  74. package/dist/charts/primitives/ChartTooltip.d.ts +5 -0
  75. package/dist/charts/primitives/ChartTooltip.d.ts.map +1 -1
  76. package/dist/charts/primitives/ChartTooltip.js +20 -4
  77. package/dist/charts/primitives/Crosshair.js +2 -2
  78. package/dist/charts/primitives/HoverOverlay.d.ts +29 -9
  79. package/dist/charts/primitives/HoverOverlay.d.ts.map +1 -1
  80. package/dist/charts/primitives/HoverOverlay.js +54 -27
  81. package/dist/charts/primitives/HoverOverlay.module.css +13 -0
  82. package/dist/charts/primitives/HoverOverlay.module.css.d.ts +8 -0
  83. package/dist/charts/primitives/chart-card-context.d.ts +33 -0
  84. package/dist/charts/primitives/chart-card-context.d.ts.map +1 -0
  85. package/dist/charts/primitives/chart-card-context.js +18 -0
  86. package/dist/charts/primitives/chart-frame-layout.d.ts +93 -113
  87. package/dist/charts/primitives/chart-frame-layout.d.ts.map +1 -1
  88. package/dist/charts/primitives/chart-frame-layout.js +75 -34
  89. package/dist/charts/primitives/chart-layout.d.ts +112 -0
  90. package/dist/charts/primitives/chart-layout.d.ts.map +1 -0
  91. package/dist/charts/primitives/chart-layout.js +175 -0
  92. package/dist/charts/primitives/chart-tier.d.ts +13 -11
  93. package/dist/charts/primitives/chart-tier.d.ts.map +1 -1
  94. package/dist/charts/primitives/chart-tier.js +42 -13
  95. package/dist/charts/primitives/legend-disclosure.d.ts +27 -0
  96. package/dist/charts/primitives/legend-disclosure.d.ts.map +1 -0
  97. package/dist/charts/primitives/legend-disclosure.js +153 -0
  98. package/dist/charts/series.d.ts +4 -1
  99. package/dist/charts/series.d.ts.map +1 -1
  100. package/dist/charts/utils/ticks.d.ts +27 -8
  101. package/dist/charts/utils/ticks.d.ts.map +1 -1
  102. package/dist/charts/utils/ticks.js +35 -11
  103. package/dist/common/errors.d.ts +11 -1
  104. package/dist/common/errors.d.ts.map +1 -1
  105. package/dist/common/errors.js +16 -2
  106. package/dist/common/hit-floor.d.ts +10 -0
  107. package/dist/common/hit-floor.d.ts.map +1 -0
  108. package/dist/common/hit-floor.js +6 -0
  109. package/dist/{shell → common}/isomorphic-layout-effect.d.ts +4 -3
  110. package/dist/common/isomorphic-layout-effect.d.ts.map +1 -0
  111. package/dist/common/use-measured-widths.d.ts +37 -0
  112. package/dist/common/use-measured-widths.d.ts.map +1 -0
  113. package/dist/common/use-measured-widths.js +51 -0
  114. package/dist/common/use-media-query.d.ts +6 -0
  115. package/dist/common/use-media-query.d.ts.map +1 -0
  116. package/dist/common/use-media-query.js +40 -0
  117. package/dist/common/validate.d.ts +9 -0
  118. package/dist/common/validate.d.ts.map +1 -1
  119. package/dist/common/validate.js +9 -4
  120. package/dist/content/article-layout.d.ts +4 -2
  121. package/dist/content/article-layout.d.ts.map +1 -1
  122. package/dist/content/article-layout.js +43 -3
  123. package/dist/content/article-layout.module.css +45 -4
  124. package/dist/content/article-layout.module.css.d.ts +3 -0
  125. package/dist/content/code-block.d.ts.map +1 -1
  126. package/dist/content/code-block.js +40 -2
  127. package/dist/content/code-block.module.css +32 -2
  128. package/dist/content/prose.module.css +13 -8
  129. package/dist/controls/actions.d.ts +29 -6
  130. package/dist/controls/actions.d.ts.map +1 -1
  131. package/dist/controls/actions.js +158 -27
  132. package/dist/controls/actions.module.css +14 -1
  133. package/dist/controls/actions.module.css.d.ts +1 -0
  134. package/dist/controls/controls.module.css +13 -31
  135. package/dist/controls/enum-filter.js +2 -2
  136. package/dist/controls/filter-context.d.ts +10 -0
  137. package/dist/controls/filter-context.d.ts.map +1 -1
  138. package/dist/controls/filter-context.js +2 -0
  139. package/dist/controls/filter-pill.d.ts.map +1 -1
  140. package/dist/controls/filter-pill.js +1 -0
  141. package/dist/controls/index.d.ts +1 -1
  142. package/dist/controls/index.d.ts.map +1 -1
  143. package/dist/controls/index.js +0 -2
  144. package/dist/controls/multi-select-filter.js +2 -2
  145. package/dist/controls/sync-button.module.css +2 -1
  146. package/dist/dashboard/section.d.ts.map +1 -1
  147. package/dist/dashboard/section.js +1 -0
  148. package/dist/dashboard/section.module.css +1 -26
  149. package/dist/dashboard/stat-card.d.ts +1 -1
  150. package/dist/dashboard/stat-card.d.ts.map +1 -1
  151. package/dist/dashboard/stat-card.js +4 -1
  152. package/dist/dashboard/stat-card.module.css +6 -9
  153. package/dist/dashboard/stat-group.module.css +33 -66
  154. package/dist/dashboard/widget-grid.d.ts.map +1 -1
  155. package/dist/dashboard/widget-grid.module.css +11 -38
  156. package/dist/dashboard/widget-header.d.ts +14 -4
  157. package/dist/dashboard/widget-header.d.ts.map +1 -1
  158. package/dist/dashboard/widget-header.js +23 -6
  159. package/dist/dashboard/widget-header.module.css +145 -133
  160. package/dist/dashboard/widget-header.module.css.d.ts +2 -0
  161. package/dist/data/data-table-state.d.ts +83 -0
  162. package/dist/data/data-table-state.d.ts.map +1 -0
  163. package/dist/data/data-table-state.js +139 -0
  164. package/dist/data/data-table.d.ts +56 -2
  165. package/dist/data/data-table.d.ts.map +1 -1
  166. package/dist/data/data-table.js +436 -336
  167. package/dist/data/data-table.module.css +94 -0
  168. package/dist/data/data-table.module.css.d.ts +11 -0
  169. package/dist/data/virtual-list.d.ts.map +1 -1
  170. package/dist/data/virtual-list.js +10 -0
  171. package/dist/forms/form-layout.d.ts.map +1 -1
  172. package/dist/forms/form-layout.js +3 -3
  173. package/dist/forms/form-layout.module.css +20 -5
  174. package/dist/forms/form-layout.module.css.d.ts +1 -0
  175. package/dist/guard/index.d.ts +8 -2
  176. package/dist/guard/index.d.ts.map +1 -1
  177. package/dist/guard/index.js +68 -2
  178. package/dist/guard/types.d.ts +3 -3
  179. package/dist/guard/types.d.ts.map +1 -1
  180. package/dist/index.d.ts +1 -1
  181. package/dist/index.d.ts.map +1 -1
  182. package/dist/index.js +3 -1
  183. package/dist/notifications/center.d.ts.map +1 -1
  184. package/dist/provider/index.d.ts +20 -1
  185. package/dist/provider/index.d.ts.map +1 -1
  186. package/dist/provider/index.js +30 -8
  187. package/dist/shell/app-brand.d.ts.map +1 -1
  188. package/dist/shell/app-brand.js +1 -0
  189. package/dist/shell/app-brand.module.css +2 -2
  190. package/dist/shell/app-header.module.css +18 -8
  191. package/dist/shell/app-main.module.css +54 -3
  192. package/dist/shell/app-main.module.css.d.ts +1 -0
  193. package/dist/shell/app-mobile-nav.module.css +16 -4
  194. package/dist/shell/app-sidebar.module.css +4 -6
  195. package/dist/shell/aside-docking.d.ts +31 -0
  196. package/dist/shell/aside-docking.d.ts.map +1 -0
  197. package/dist/shell/aside-docking.js +19 -0
  198. package/dist/shell/index.d.ts +1 -0
  199. package/dist/shell/index.d.ts.map +1 -1
  200. package/dist/shell/index.js +56 -16
  201. package/dist/shell/mobile-nav-model.d.ts +1 -1
  202. package/dist/shell/page-aside.d.ts +22 -3
  203. package/dist/shell/page-aside.d.ts.map +1 -1
  204. package/dist/shell/page-aside.js +27 -13
  205. package/dist/shell/page-bar.d.ts.map +1 -1
  206. package/dist/shell/page-bar.js +10 -5
  207. package/dist/shell/page-bar.module.css +10 -34
  208. package/dist/shell/use-breakpoint.d.ts +3 -4
  209. package/dist/shell/use-breakpoint.d.ts.map +1 -1
  210. package/dist/shell/use-breakpoint.js +3 -25
  211. package/dist/shell/use-size-class.d.ts +6 -0
  212. package/dist/shell/use-size-class.d.ts.map +1 -0
  213. package/dist/shell/use-size-class.js +19 -0
  214. package/dist/state/persisted-or-local.d.ts +4 -1
  215. package/dist/state/persisted-or-local.d.ts.map +1 -1
  216. package/dist/state/persisted-or-local.js +4 -3
  217. package/dist/state/persisted.d.ts +10 -0
  218. package/dist/state/persisted.d.ts.map +1 -1
  219. package/dist/state/persisted.js +44 -14
  220. package/dist/styles.css +41 -0
  221. package/dist/surfaces.d.ts +4 -4
  222. package/dist/surfaces.d.ts.map +1 -1
  223. package/dist/surfaces.js +11 -3
  224. package/dist/theme/controls.module.css +7 -0
  225. package/dist/theme/ctl-theme.d.ts +1 -0
  226. package/dist/theme/ctl-theme.d.ts.map +1 -1
  227. package/dist/theme/ctl-theme.js +12 -4
  228. package/dist/theme/floating.module.css +37 -0
  229. package/dist/theme/floating.module.css.d.ts +3 -0
  230. package/dist/theme/index.d.ts +2 -2
  231. package/dist/theme/index.d.ts.map +1 -1
  232. package/dist/theme/index.js +34 -5
  233. package/dist/theme/nav-link.module.css +7 -0
  234. package/dist/theme-lab/derive-controls.d.ts.map +1 -1
  235. package/dist/tokens/index.d.ts +7 -16
  236. package/dist/tokens/index.d.ts.map +1 -1
  237. package/dist/tokens/index.js +23 -28
  238. package/dist/tokens/palette.d.ts +5 -27
  239. package/dist/tokens/palette.d.ts.map +1 -1
  240. package/dist/tokens/palette.js +8 -30
  241. package/dist/tokens/size-classes.d.ts +40 -0
  242. package/dist/tokens/size-classes.d.ts.map +1 -0
  243. package/dist/tokens/size-classes.js +29 -0
  244. package/dist/tokens.css +8 -4
  245. package/llms.txt +3 -3
  246. package/package.json +18 -17
  247. package/src/agent-chat/composer.tsx +74 -47
  248. package/src/agent-chat/thread-workspace.tsx +8 -11
  249. package/src/agent-chat/use-keyboard-inset.ts +68 -0
  250. package/src/charts/cursor/touch-pin.ts +138 -0
  251. package/src/charts/hooks/use-discrete-cursor.ts +335 -0
  252. package/src/charts/hooks/use-hysteresis.ts +26 -0
  253. package/src/charts/hooks/useBandPlot.ts +90 -27
  254. package/src/charts/hooks/useChartCursor.ts +211 -26
  255. package/src/charts/index.ts +0 -8
  256. package/src/charts/kinds/BandStrip.tsx +25 -61
  257. package/src/charts/kinds/Donut.tsx +153 -62
  258. package/src/charts/kinds/DualPanel.tsx +21 -26
  259. package/src/charts/kinds/Heatmap.tsx +61 -22
  260. package/src/charts/kinds/MirroredBars.tsx +23 -65
  261. package/src/charts/kinds/band-chrome.tsx +91 -0
  262. package/src/charts/kinds/donut-layout.ts +82 -0
  263. package/src/charts/layout/auto-margin.ts +24 -5
  264. package/src/charts/layout/end-labels.ts +135 -0
  265. package/src/charts/primitives/Axes.tsx +112 -21
  266. package/src/charts/primitives/CartesianChart.tsx +282 -111
  267. package/src/charts/primitives/ChartCard.tsx +119 -48
  268. package/src/charts/primitives/ChartFrame.tsx +204 -60
  269. package/src/charts/primitives/ChartLegend.tsx +115 -78
  270. package/src/charts/primitives/ChartTooltip.tsx +24 -3
  271. package/src/charts/primitives/Crosshair.tsx +2 -2
  272. package/src/charts/primitives/HoverOverlay.module.css +13 -0
  273. package/src/charts/primitives/HoverOverlay.module.css.d.ts +8 -0
  274. package/src/charts/primitives/HoverOverlay.tsx +62 -20
  275. package/src/charts/primitives/chart-card-context.ts +46 -0
  276. package/src/charts/primitives/chart-frame-layout.ts +207 -182
  277. package/src/charts/primitives/chart-layout.ts +318 -0
  278. package/src/charts/primitives/chart-tier.tsx +66 -21
  279. package/src/charts/primitives/legend-disclosure.tsx +177 -0
  280. package/src/charts/series.ts +4 -1
  281. package/src/charts/utils/ticks.ts +95 -19
  282. package/src/common/errors.ts +25 -1
  283. package/src/common/hit-floor.ts +9 -0
  284. package/src/{shell → common}/isomorphic-layout-effect.ts +4 -3
  285. package/src/common/use-measured-widths.ts +88 -0
  286. package/src/common/use-media-query.ts +56 -0
  287. package/src/common/validate.ts +17 -4
  288. package/src/content/article-layout.module.css +45 -4
  289. package/src/content/article-layout.module.css.d.ts +3 -0
  290. package/src/content/article-layout.tsx +60 -5
  291. package/src/content/code-block.module.css +32 -2
  292. package/src/content/code-block.tsx +47 -3
  293. package/src/content/prose.module.css +13 -8
  294. package/src/controls/actions.module.css +14 -1
  295. package/src/controls/actions.module.css.d.ts +1 -0
  296. package/src/controls/actions.tsx +223 -75
  297. package/src/controls/controls.module.css +13 -31
  298. package/src/controls/enum-filter.tsx +2 -2
  299. package/src/controls/filter-context.tsx +11 -0
  300. package/src/controls/filter-pill.tsx +1 -0
  301. package/src/controls/index.ts +0 -1
  302. package/src/controls/multi-select-filter.tsx +2 -2
  303. package/src/controls/sync-button.module.css +2 -1
  304. package/src/dashboard/section.module.css +1 -26
  305. package/src/dashboard/section.tsx +1 -0
  306. package/src/dashboard/stat-card.module.css +6 -9
  307. package/src/dashboard/stat-card.tsx +6 -3
  308. package/src/dashboard/stat-group.module.css +33 -66
  309. package/src/dashboard/widget-grid.module.css +11 -38
  310. package/src/dashboard/widget-grid.tsx +7 -6
  311. package/src/dashboard/widget-header.module.css +145 -133
  312. package/src/dashboard/widget-header.module.css.d.ts +2 -0
  313. package/src/dashboard/widget-header.tsx +45 -11
  314. package/src/data/data-table-state.ts +218 -0
  315. package/src/data/data-table.module.css +94 -0
  316. package/src/data/data-table.module.css.d.ts +11 -0
  317. package/src/data/data-table.tsx +547 -299
  318. package/src/data/virtual-list.tsx +31 -0
  319. package/src/forms/form-layout.module.css +20 -5
  320. package/src/forms/form-layout.module.css.d.ts +1 -0
  321. package/src/forms/form-layout.tsx +12 -10
  322. package/src/guard/index.ts +188 -3
  323. package/src/guard/types.ts +3 -2
  324. package/src/index.ts +2 -0
  325. package/src/notifications/center.tsx +3 -0
  326. package/src/provider/index.tsx +60 -7
  327. package/src/shell/app-brand.module.css +2 -2
  328. package/src/shell/app-brand.tsx +1 -0
  329. package/src/shell/app-header.module.css +18 -8
  330. package/src/shell/app-main.module.css +54 -3
  331. package/src/shell/app-main.module.css.d.ts +1 -0
  332. package/src/shell/app-mobile-nav.module.css +16 -4
  333. package/src/shell/app-sidebar.module.css +4 -6
  334. package/src/shell/aside-docking.ts +54 -0
  335. package/src/shell/index.tsx +178 -129
  336. package/src/shell/mobile-nav-model.ts +1 -1
  337. package/src/shell/page-aside.tsx +70 -21
  338. package/src/shell/page-bar.module.css +10 -34
  339. package/src/shell/page-bar.tsx +10 -5
  340. package/src/shell/use-breakpoint.ts +9 -45
  341. package/src/shell/use-size-class.ts +31 -0
  342. package/src/state/persisted-or-local.ts +10 -4
  343. package/src/state/persisted.ts +76 -17
  344. package/src/styles.css +41 -0
  345. package/src/surfaces.ts +9 -1
  346. package/src/theme/controls.module.css +7 -0
  347. package/src/theme/ctl-theme.tsx +15 -3
  348. package/src/theme/floating.module.css +37 -0
  349. package/src/theme/floating.module.css.d.ts +3 -0
  350. package/src/theme/index.ts +46 -8
  351. package/src/theme/nav-link.module.css +7 -0
  352. package/src/theme-lab/derive-controls.tsx +2 -0
  353. package/src/tokens/index.ts +20 -30
  354. package/src/tokens/palette.ts +6 -28
  355. package/src/tokens/size-classes.ts +46 -0
  356. package/dist/shell/isomorphic-layout-effect.d.ts.map +0 -1
  357. /package/dist/{shell → common}/isomorphic-layout-effect.js +0 -0
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Two small pieces shared by every touch-pinned surface in the chart layer — extracted so
3
+ * `useChartCursor`/`useDiscreteCursor`/`ChartLegend`'s disclosure cannot drift apart on the same
4
+ * contract (round 2's carried-forward gap: each had its own hand-rolled copy).
5
+ */
6
+ import { useCallback, useEffect, useMemo, useRef } from 'react'
7
+
8
+ /**
9
+ * Outside-tap / Escape / scroll (optionally resize) dismissal for a pinned/open floating element.
10
+ * Capture-phase `pointerdown` so a descendant that stops propagation on its own handler can't leave
11
+ * the dismissal stuck.
12
+ */
13
+ export function useDismissOnOutside({
14
+ enabled,
15
+ isInside,
16
+ onDismiss,
17
+ dismissOnResize = false,
18
+ scrollExemptInside = false,
19
+ }: {
20
+ enabled: boolean
21
+ /** A tap (or, with `scrollExemptInside`, a scroll) landing where this returns true doesn't
22
+ * dismiss. Omit to treat every tap as outside. */
23
+ isInside?: (target: Node) => boolean
24
+ onDismiss: () => void
25
+ /** Also dismiss on a window `resize` — `ChartLegend`'s disclosure needs this, the touch-pinned
26
+ * cursors don't (their host doesn't reflow independently of the chart it's pinned to). */
27
+ dismissOnResize?: boolean
28
+ /** Exempts a scroll whose target is `isInside` — `ChartLegend`'s own scrollable panel needs this
29
+ * (`overflowY: auto`), the cursor hooks don't (a chart has no internal scroller to protect). */
30
+ scrollExemptInside?: boolean
31
+ }): void {
32
+ useEffect(() => {
33
+ if (!enabled) return
34
+ const onPointerDown = (event: PointerEvent) => {
35
+ if (event.target instanceof Node && isInside?.(event.target) === true) return
36
+ onDismiss()
37
+ }
38
+ const onKeyDown = (event: KeyboardEvent) => {
39
+ if (event.key === 'Escape') onDismiss()
40
+ }
41
+ const onScroll = (event: Event) => {
42
+ if (scrollExemptInside && event.target instanceof Node && isInside?.(event.target) === true) {
43
+ return
44
+ }
45
+ onDismiss()
46
+ }
47
+ document.addEventListener('pointerdown', onPointerDown, true)
48
+ document.addEventListener('keydown', onKeyDown)
49
+ document.addEventListener('scroll', onScroll, true)
50
+ if (dismissOnResize) window.addEventListener('resize', onDismiss)
51
+ return () => {
52
+ document.removeEventListener('pointerdown', onPointerDown, true)
53
+ document.removeEventListener('keydown', onKeyDown)
54
+ document.removeEventListener('scroll', onScroll, true)
55
+ if (dismissOnResize) window.removeEventListener('resize', onDismiss)
56
+ }
57
+ }, [enabled, isInside, onDismiss, dismissOnResize, scrollExemptInside])
58
+ }
59
+
60
+ export type TouchPin = {
61
+ /** `onPointerDown` for a coarse press: snapshots whatever is currently committed (via
62
+ * `getCommitted`) so a later `cancel` can restore exactly that, then tracks `pointerId`. */
63
+ begin: (pointerId: number) => void
64
+ /** `onPointerUp` for the tracked pointer: the press is now committed — nothing left to revert. */
65
+ commit: (pointerId: number) => boolean
66
+ /** `onPointerCancel`/`onPointerLeave` for the tracked pointer: the browser took the gesture (a
67
+ * scroll) before the press committed — restores the pre-press value via `setCommitted`, or
68
+ * leaves it clear when nothing was committed before. Returns whether this pointer was tracked. */
69
+ cancel: (pointerId: number) => boolean
70
+ /** Hard reset — no restore, just drop any in-flight press bookkeeping (an explicit `clear()`, e.g.
71
+ * Escape or an outside tap, is about to overwrite the value directly anyway). */
72
+ reset: () => void
73
+ }
74
+
75
+ /**
76
+ * Provisional-press state machine shared by `useChartCursor`/`useDiscreteCursor`: a coarse press
77
+ * shows a value immediately (PROVISIONAL, not yet committed) — `commit` on `pointerup` leaves it in
78
+ * place, `cancel` on `pointercancel`/`pointerleave` (a scroll winning the gesture) restores whatever
79
+ * was committed before this press started, rather than dropping it. This is what keeps a previously
80
+ * COMMITTED pin alive through a second tap that gets cancelled instead of completed.
81
+ */
82
+ export function useTouchPin<V>({
83
+ getCommitted,
84
+ setCommitted,
85
+ }: {
86
+ /** Reads the value currently committed — snapshotted on `begin`, restored on `cancel`. */
87
+ getCommitted: () => V
88
+ setCommitted: (value: V) => void
89
+ }): TouchPin {
90
+ const pressRef = useRef<number | null>(null)
91
+ const backupRef = useRef<V | undefined>(undefined)
92
+ // Whether `backupRef` holds a real snapshot — `backupRef.current !== undefined` used to stand in
93
+ // for this, which silently dropped a genuinely committed `undefined` (a caller's `V` can legally
94
+ // include it): `cancel` would restore nothing at all instead of restoring "nothing was pinned".
95
+ const hasBackupRef = useRef(false)
96
+ const getCommittedRef = useRef(getCommitted)
97
+ getCommittedRef.current = getCommitted
98
+ const setCommittedRef = useRef(setCommitted)
99
+ setCommittedRef.current = setCommitted
100
+
101
+ const begin = useCallback((pointerId: number) => {
102
+ backupRef.current = getCommittedRef.current()
103
+ hasBackupRef.current = true
104
+ pressRef.current = pointerId
105
+ }, [])
106
+
107
+ const commit = useCallback((pointerId: number): boolean => {
108
+ if (pressRef.current !== pointerId) return false
109
+ pressRef.current = null
110
+ backupRef.current = undefined
111
+ hasBackupRef.current = false
112
+ return true
113
+ }, [])
114
+
115
+ const cancel = useCallback((pointerId: number): boolean => {
116
+ if (pressRef.current !== pointerId) return false
117
+ pressRef.current = null
118
+ const backup = backupRef.current
119
+ const hasBackup = hasBackupRef.current
120
+ backupRef.current = undefined
121
+ hasBackupRef.current = false
122
+ if (hasBackup) setCommittedRef.current(backup as V)
123
+ return true
124
+ }, [])
125
+
126
+ const reset = useCallback(() => {
127
+ pressRef.current = null
128
+ backupRef.current = undefined
129
+ hasBackupRef.current = false
130
+ }, [])
131
+
132
+ // Stable identity: `begin`/`commit`/`cancel`/`reset` are themselves already stable (empty-dep
133
+ // `useCallback`s), but the returned OBJECT wrapping them was a fresh literal every render — a
134
+ // consumer's own effect depending on the whole `TouchPin` (or a `useDismissOnOutside` closing
135
+ // over one of these) would re-subscribe on every render for no reason. Memoizing on the four
136
+ // (stable) functions costs nothing and makes the wrapper's identity match theirs.
137
+ return useMemo(() => ({ begin, commit, cancel, reset }), [begin, commit, cancel, reset])
138
+ }
@@ -0,0 +1,335 @@
1
+ import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'
2
+ import type {
3
+ KeyboardEventHandler,
4
+ PointerEvent as ReactPointerEvent,
5
+ PointerEventHandler,
6
+ } from 'react'
7
+ import { useDismissOnOutside, useTouchPin } from '../cursor/touch-pin'
8
+
9
+ /** A discrete target plus the viewport-space point its tooltip anchors to. */
10
+ export type DiscreteCursorTip<T> = { target: T; anchor: { x: number; y: number } }
11
+
12
+ type ActiveState = { key: string; anchor: { x: number; y: number } }
13
+
14
+ export type UseDiscreteCursorResult<T> = {
15
+ /** The target whose tooltip should render, resolved from whichever mode last set it (hover, a
16
+ * touch pin, or keyboard focus). `null` when nothing is active. */
17
+ tip: DiscreteCursorTip<T> | null
18
+ /** True once a coarse-pointer tap has PINNED `tip` open (survives pointer lift). */
19
+ isPinned: boolean
20
+ /** Pointer handlers for one target's shape — spread onto its `<g>`/`<rect>`. Fine pointers
21
+ * (mouse/pen) hover; a coarse pointer (touch) pins on `pointerdown` instead. */
22
+ pointerProps: (t: T) => {
23
+ onPointerEnter: PointerEventHandler<Element>
24
+ onPointerMove: PointerEventHandler<Element>
25
+ onPointerDown: PointerEventHandler<Element>
26
+ onPointerUp: PointerEventHandler<Element>
27
+ onPointerLeave: PointerEventHandler<Element>
28
+ onPointerCancel: PointerEventHandler<Element>
29
+ }
30
+ /** DOM id for a target's `role="option"` element — also `aria-activedescendant`'s value while
31
+ * that target is active. Spread `id={optionId(t)}` on the same node that gets `pointerProps`. */
32
+ optionId: (t: T) => string
33
+ /** Dismisses a pin and clears keyboard focus. Already wired to Escape and an outside tap while
34
+ * pinned; call it directly for anything else that should close the tooltip (e.g. data changing
35
+ * out from under the focused key). */
36
+ clear: () => void
37
+ /** Spread on the ONE shared, focusable host (the chart's `<svg>`). */
38
+ hostProps: {
39
+ ref: (node: Element | null) => void
40
+ tabIndex: 0
41
+ role: 'listbox'
42
+ 'aria-label': string
43
+ 'aria-activedescendant': string | undefined
44
+ onKeyDown: KeyboardEventHandler<Element>
45
+ onBlur: () => void
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Keyboard + focus + tap parity for a DISCRETE set of targets (donut slices, heatmap cells) — the
51
+ * counterpart `HoverOverlay` gives a continuous cartesian domain (`docs/CHARTS-SPEC.md` §4).
52
+ *
53
+ * **Role choice**: `listbox`/`option` (WAI-ARIA APG "Listbox"), not `slider`. A cartesian domain is
54
+ * one continuous value; a donut slice or heatmap cell is a named choice among discrete siblings.
55
+ * `aria-activedescendant` announces the focused option's id while DOM focus stays on the one host.
56
+ *
57
+ * Three input modes converge on one `tip`: hover (fine pointer, ephemeral), a provisional tap that
58
+ * `pointerup` commits into a pin (a `pointercancel` first — a scroll — clears it), and keyboard
59
+ * stepping on the one focusable host (`columns` maps Up/Down to a full-row jump for a 2D grid;
60
+ * Left/Right always step by one). Keyboard focus anchors the tooltip to the host's center — an
61
+ * accepted simplification; the value still reaches a screen reader via the option's text.
62
+ */
63
+ export function useDiscreteCursor<T>({
64
+ targets,
65
+ getKey,
66
+ columns,
67
+ ariaLabel = 'Chart data — use arrow keys to navigate, Escape to dismiss',
68
+ }: {
69
+ targets: readonly T[]
70
+ getKey: (t: T) => string
71
+ /** Row width for a 2D grid — see the Up/Down mapping above. Omit for a 1D ring. */
72
+ columns?: number
73
+ /** Accessible name for the shared listbox host. */
74
+ ariaLabel?: string
75
+ }): UseDiscreteCursorResult<T> {
76
+ const [active, setActive] = useState<ActiveState | null>(null)
77
+ const [pinned, setPinned] = useState(false)
78
+ const hostElRef = useRef<Element | null>(null)
79
+ const idPrefix = useId()
80
+
81
+ // Mirrors `active`/`pinned` so the provisional-press machine below can read the CURRENT committed
82
+ // value synchronously from an event handler, without either living in a ref of its own.
83
+ const activeRef = useRef(active)
84
+ activeRef.current = active
85
+ const pinnedRef = useRef(pinned)
86
+ pinnedRef.current = pinned
87
+
88
+ // Read through refs so a keypress or an outside tap arriving between renders (the two
89
+ // `document`-level listeners below) always sees the LATEST list/accessor, not the ones closed
90
+ // over when the effect was installed — `targets` and `columns` can both change every render (a
91
+ // filtered dataset, a resize).
92
+ const targetsRef = useRef(targets)
93
+ targetsRef.current = targets
94
+ const getKeyRef = useRef(getKey)
95
+ getKeyRef.current = getKey
96
+ const columnsRef = useRef(columns)
97
+ columnsRef.current = columns
98
+
99
+ // `pointermove`/`pointerenter` flood far faster than one `setActive` is worth on a fast scrub
100
+ // across many targets — coalesced to one commit per animation frame, the same treatment
101
+ // `useChartCursor`'s own hover path gives its (costlier) nearest-point search. Cancelled on
102
+ // unmount and in `clear()` — which `onPointerLeave`/`onPointerCancel` below also route through —
103
+ // so a frame already in flight cannot resurrect a hover the clear just dismissed.
104
+ const moveFrameRef = useRef<number | null>(null)
105
+ const pendingActiveRef = useRef<ActiveState | null>(null)
106
+ useEffect(
107
+ () => () => {
108
+ if (moveFrameRef.current !== null) cancelAnimationFrame(moveFrameRef.current)
109
+ },
110
+ [],
111
+ )
112
+ const scheduleActive = useCallback((next: ActiveState) => {
113
+ pendingActiveRef.current = next
114
+ if (moveFrameRef.current !== null) return
115
+ moveFrameRef.current = requestAnimationFrame(() => {
116
+ moveFrameRef.current = null
117
+ setActive(pendingActiveRef.current)
118
+ })
119
+ }, [])
120
+
121
+ // Cancels a hover frame already in flight and clears what it was about to commit — called by
122
+ // every SYNCHRONOUS writer of `active` (a touch press, a keyboard step, `clear()` itself) before
123
+ // it writes. Without this, a `pointermove` scheduled a tick earlier (coalesced onto
124
+ // `moveFrameRef`/`pendingActiveRef`, per `scheduleActive` above) is still in flight when one of
125
+ // those writes lands, and the frame fires a moment later and overwrites it right back with the
126
+ // stale hover target — the keyboard/touch target "reverts" to whatever was last hovered.
127
+ const cancelPendingFrame = useCallback(() => {
128
+ if (moveFrameRef.current !== null) {
129
+ cancelAnimationFrame(moveFrameRef.current)
130
+ moveFrameRef.current = null
131
+ }
132
+ pendingActiveRef.current = null
133
+ }, [])
134
+
135
+ // The provisional-press commit/revert machine (`docs/CHARTS-SPEC.md` §4) — shared with
136
+ // `useChartCursor`. The committed value IS `active` whenever `pinned` is true — there is no
137
+ // separate ref to keep in sync, `pinnedRef`/`activeRef` above already mirror both.
138
+ const touchPin = useTouchPin<ActiveState | null>({
139
+ getCommitted: () => (pinnedRef.current ? activeRef.current : null),
140
+ setCommitted: (value) => {
141
+ setActive(value)
142
+ setPinned(value !== null)
143
+ },
144
+ })
145
+
146
+ const clear = useCallback(() => {
147
+ cancelPendingFrame()
148
+ touchPin.reset()
149
+ setActive(null)
150
+ setPinned(false)
151
+ }, [touchPin, cancelPendingFrame])
152
+
153
+ // Coarse-pointer pin dismissal that can't be expressed as a per-target handler: a tap OUTSIDE
154
+ // the host, or Escape, from anywhere on the page. A tap landing back INSIDE the host is handled
155
+ // by the per-target `onPointerDown` in `pointerProps` instead (it MOVES the pin), so this
156
+ // listener only ever fires for the "elsewhere" case — same shape as `ChartLegend`'s disclosure
157
+ // dismissal. Shared with `useChartCursor` (`touch-pin.ts`).
158
+ const isInsideHost = useCallback(
159
+ (target: Node): boolean => hostElRef.current?.contains(target) === true,
160
+ [],
161
+ )
162
+ useDismissOnOutside({ enabled: pinned, isInside: isInsideHost, onDismiss: clear })
163
+
164
+ // Index in `targetsRef`, NOT the key: Heatmap's `cellKey` joins cells with `\u0000`, which would
165
+ // leak a NUL into a DOM id, and an id has to be a valid DOM token. Resolved by KEY, not reference:
166
+ // Heatmap builds a fresh `{ row, col, value }` object per cell render, so a reference lookup always
167
+ // misses. A `Map` built once per `targets` change keeps `optionId` O(1) on the render loop the
168
+ // touch-scrub path re-runs every frame.
169
+ const indexByKey = useMemo(() => {
170
+ const map = new Map<string, number>()
171
+ targets.forEach((t, i) => map.set(getKeyRef.current(t), i))
172
+ return map
173
+ }, [targets])
174
+ const optionId = useCallback(
175
+ (t: T): string => `${idPrefix}-${indexByKey.get(getKeyRef.current(t)) ?? -1}`,
176
+ [idPrefix, indexByKey],
177
+ )
178
+
179
+ // O(1) via `indexByKey` rather than a fresh scan — the same Map `optionId` already builds.
180
+ const findTarget = (key: string): T | undefined => {
181
+ const index = indexByKey.get(key)
182
+ return index === undefined ? undefined : targetsRef.current[index]
183
+ }
184
+
185
+ const pointerProps = (t: T): ReturnType<UseDiscreteCursorResult<T>['pointerProps']> => {
186
+ const key = getKeyRef.current(t)
187
+ const showAt = (event: ReactPointerEvent<Element>): void => {
188
+ scheduleActive({ key, anchor: { x: event.clientX, y: event.clientY } })
189
+ }
190
+ // Shared by `onPointerLeave` and `onPointerCancel`: a still-UNCOMMITTED press ending here is the
191
+ // browser taking the gesture (a scroll) — `touchPin.cancel` restores whatever was COMMITTED
192
+ // before this press started (or leaves it clear when nothing was), regardless of `pinned`. The
193
+ // two used to diverge (`onPointerCancel` checked this, `onPointerLeave` didn't), which left a
194
+ // stale press and a stuck readout on the leave-before-cancel ordering a scroll off a small hit
195
+ // target commonly produces.
196
+ const cancelIfUncommitted = (event: ReactPointerEvent<Element>): boolean =>
197
+ touchPin.cancel(event.pointerId)
198
+ // `pointerType !== 'mouse'` throughout (never `=== 'touch'`), matching `useChartCursor` —
199
+ // pen counts as coarse too, so a stylus tap gets the same pin-until-dismiss contract a finger
200
+ // does, not the fine-pointer hover path.
201
+ return {
202
+ onPointerEnter: (event) => {
203
+ // Guarded on `pinned`, not just pointer type: while a touch tap has pinned a DIFFERENT
204
+ // target, a stray mouse hover elsewhere (a hybrid touchscreen + mouse device) must not
205
+ // silently steal `active` out from under the pin — it would leave that new hover's
206
+ // tooltip stuck on lift, since leave/cancel below still see `pinned` and no-op.
207
+ if (event.pointerType !== 'mouse' || pinned) return
208
+ showAt(event)
209
+ },
210
+ onPointerMove: (event) => {
211
+ if (event.pointerType !== 'mouse' || pinned) return
212
+ showAt(event)
213
+ },
214
+ // Only a coarse pointer presses — a fine pointer's `down` is a no-op, it already shows via
215
+ // enter/move above. The press is PROVISIONAL: it snapshots whatever is committed now (so a
216
+ // cancel can restore it) BEFORE showing the readout, which only `onPointerUp` commits. On
217
+ // touch the readout anchors to the HOST's top edge rather than the finger, which would cover
218
+ // it; a missing host falls back to the pointer.
219
+ onPointerDown: (event) => {
220
+ if (event.pointerType === 'mouse') return
221
+ touchPin.begin(event.pointerId)
222
+ // A hover frame scheduled just before this press (e.g. a hybrid touch+mouse device) must
223
+ // not be allowed to land after this — see `cancelPendingFrame`'s own doc.
224
+ cancelPendingFrame()
225
+ const rect = hostElRef.current?.getBoundingClientRect()
226
+ setActive({
227
+ key,
228
+ anchor:
229
+ rect === undefined
230
+ ? { x: event.clientX, y: event.clientY }
231
+ : { x: rect.left + rect.width / 2, y: rect.top },
232
+ })
233
+ },
234
+ // A completed tap commits the pin. Only the tracked pointer commits — and only a coarse one:
235
+ // some browsers assign a touch's `pointerId` the same value (1) mouse pointers use, so a
236
+ // `pointerType` check keeps an unrelated mouse `pointerup` reusing that id from committing an
237
+ // abandoned touch press.
238
+ onPointerUp: (event) => {
239
+ if (event.pointerType === 'mouse') return
240
+ if (!touchPin.commit(event.pointerId)) return
241
+ setPinned(true)
242
+ },
243
+ // A still-uncommitted press ending here is the browser taking the gesture (a scroll); clear it
244
+ // regardless of `pinned`. Otherwise keep the fine-pointer/pinned guard. Routes through the
245
+ // same `clear()` the outside-tap/Escape dismissal uses (mirrors `useChartCursor`'s own
246
+ // `onPointerLeave`) rather than a bare `setActive(null)` — a `pointermove` scheduled just
247
+ // before this event fires is still in flight (coalesced onto `moveFrameRef`/
248
+ // `pendingActiveRef`), and only `clear()` cancels it. A bare `setActive(null)` left that frame
249
+ // free to land a tick later and resurrect the tooltip this leave/cancel was meant to dismiss.
250
+ onPointerLeave: (event) => {
251
+ if (cancelIfUncommitted(event)) return
252
+ if (event.pointerType !== 'mouse' || pinned) return
253
+ clear()
254
+ },
255
+ onPointerCancel: (event) => {
256
+ if (cancelIfUncommitted(event)) return
257
+ if (event.pointerType !== 'mouse' || pinned) return
258
+ clear()
259
+ },
260
+ }
261
+ }
262
+
263
+ const onKeyDown: KeyboardEventHandler<Element> = (event) => {
264
+ const list = targetsRef.current
265
+ if (list.length === 0) return
266
+ if (event.key === 'Escape') {
267
+ clear()
268
+ return
269
+ }
270
+ const cols = columnsRef.current
271
+ let delta = 0
272
+ switch (event.key) {
273
+ case 'ArrowRight':
274
+ delta = 1
275
+ break
276
+ case 'ArrowLeft':
277
+ delta = -1
278
+ break
279
+ case 'ArrowDown':
280
+ delta = cols ?? 1
281
+ break
282
+ case 'ArrowUp':
283
+ delta = -(cols ?? 1)
284
+ break
285
+ default:
286
+ return
287
+ }
288
+ event.preventDefault()
289
+
290
+ const currentIndex =
291
+ active === null ? -1 : list.findIndex((t) => getKeyRef.current(t) === active.key)
292
+ // Mirrors `useChartCursor`'s onKeyDown: no current focus starts one step before/after the
293
+ // domain edge, so the first arrow press lands on the first or last target.
294
+ const base = currentIndex === -1 ? (delta > 0 ? -1 : list.length) : currentIndex
295
+ const nextIndex = Math.min(Math.max(base + delta, 0), list.length - 1)
296
+ const next = list[nextIndex] as T
297
+
298
+ const rect = hostElRef.current?.getBoundingClientRect()
299
+ const anchor =
300
+ rect === undefined
301
+ ? { x: 0, y: 0 }
302
+ : { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 }
303
+ // A hover frame scheduled just before this keypress must not be allowed to land after this —
304
+ // see `cancelPendingFrame`'s own doc.
305
+ cancelPendingFrame()
306
+ setActive({ key: getKeyRef.current(next), anchor })
307
+ }
308
+
309
+ const tip: DiscreteCursorTip<T> | null = (() => {
310
+ if (active === null) return null
311
+ const target = findTarget(active.key)
312
+ return target === undefined ? null : { target, anchor: active.anchor }
313
+ })()
314
+
315
+ return {
316
+ tip,
317
+ isPinned: pinned,
318
+ pointerProps,
319
+ optionId,
320
+ clear,
321
+ hostProps: {
322
+ ref: (node) => {
323
+ hostElRef.current = node
324
+ },
325
+ tabIndex: 0,
326
+ role: 'listbox',
327
+ 'aria-label': ariaLabel,
328
+ 'aria-activedescendant': tip === null ? undefined : optionId(tip.target),
329
+ onKeyDown,
330
+ onBlur: () => {
331
+ if (!pinned) clear()
332
+ },
333
+ },
334
+ }
335
+ }
@@ -0,0 +1,26 @@
1
+ import { useLayoutEffect, useRef } from 'react'
2
+
3
+ /**
4
+ * A derived value that feeds back into its own next computation — hysteresis, "was X last frame" —
5
+ * without the anti-pattern of mutating a ref during render (`ChartCard`'s short-card flag, P0-1;
6
+ * `Donut`'s side/stacked layout, P2-10, both hit the SAME bug independently: a render a concurrent
7
+ * commit discards must not still poison the next real one's input).
8
+ *
9
+ * `compute` is called with the PREVIOUS committed value (never mutated here) and returns this
10
+ * render's value; the ref only updates in a `useLayoutEffect`, after the value has actually
11
+ * committed. Internal only — not on any public barrel.
12
+ *
13
+ * @example
14
+ * const short = useHysteresis(
15
+ * (wasShort) => measuredHeight > 0 && measuredHeight < (wasShort ? EXIT : ENTER),
16
+ * false,
17
+ * )
18
+ */
19
+ export function useHysteresis<T>(compute: (previous: T) => T, initial: T): T {
20
+ const ref = useRef(initial)
21
+ const value = compute(ref.current)
22
+ useLayoutEffect(() => {
23
+ ref.current = value
24
+ }, [value])
25
+ return value
26
+ }