@eifi1/ui-kit 0.4.1 → 0.5.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 (410) hide show
  1. package/LICENSE +32 -0
  2. package/README.md +498 -57
  3. package/dist/chart.d.ts +10 -0
  4. package/dist/chart.js +10 -0
  5. package/dist/chart.js.map +1 -0
  6. package/dist/components/account-settings.d.ts +11 -0
  7. package/dist/components/account-settings.js +19 -12
  8. package/dist/components/account-settings.js.map +1 -1
  9. package/dist/components/alert-banner.d.ts +10 -6
  10. package/dist/components/alert-banner.js +20 -16
  11. package/dist/components/alert-banner.js.map +1 -1
  12. package/dist/components/amount-input.d.ts +30 -4
  13. package/dist/components/amount-input.js +36 -14
  14. package/dist/components/amount-input.js.map +1 -1
  15. package/dist/components/calculator.d.ts +24 -7
  16. package/dist/components/calculator.js +37 -20
  17. package/dist/components/calculator.js.map +1 -1
  18. package/dist/components/chart-zoom.d.ts +156 -0
  19. package/dist/components/chart-zoom.js +262 -0
  20. package/dist/components/chart-zoom.js.map +1 -0
  21. package/dist/components/chart.d.ts +28 -3
  22. package/dist/components/chart.js +71 -32
  23. package/dist/components/chart.js.map +1 -1
  24. package/dist/components/checkbox.d.ts +51 -0
  25. package/dist/components/checkbox.js +123 -0
  26. package/dist/components/checkbox.js.map +1 -0
  27. package/dist/components/chip.d.ts +128 -0
  28. package/dist/components/chip.js +357 -0
  29. package/dist/components/chip.js.map +1 -0
  30. package/dist/components/choice-parts.d.ts +24 -0
  31. package/dist/components/choice-parts.js +18 -0
  32. package/dist/components/choice-parts.js.map +1 -0
  33. package/dist/components/combobox-core.d.ts +52 -9
  34. package/dist/components/combobox-core.js +89 -19
  35. package/dist/components/combobox-core.js.map +1 -1
  36. package/dist/components/combobox.d.ts +43 -33
  37. package/dist/components/combobox.js +409 -340
  38. package/dist/components/combobox.js.map +1 -1
  39. package/dist/components/currency-select.d.ts +48 -8
  40. package/dist/components/currency-select.js +107 -65
  41. package/dist/components/currency-select.js.map +1 -1
  42. package/dist/components/data-table-filter-popover.d.ts +15 -3
  43. package/dist/components/data-table-filter-popover.js +23 -17
  44. package/dist/components/data-table-filter-popover.js.map +1 -1
  45. package/dist/components/data-table-filters.d.ts +1 -1
  46. package/dist/components/data-table-filters.js +3 -2
  47. package/dist/components/data-table-filters.js.map +1 -1
  48. package/dist/components/data-table-labels.d.ts +53 -2
  49. package/dist/components/data-table-labels.js +31 -2
  50. package/dist/components/data-table-labels.js.map +1 -1
  51. package/dist/components/data-table-pagination.d.ts +3 -1
  52. package/dist/components/data-table-pagination.js +16 -10
  53. package/dist/components/data-table-pagination.js.map +1 -1
  54. package/dist/components/data-table-sort.js +1 -0
  55. package/dist/components/data-table-sort.js.map +1 -1
  56. package/dist/components/data-table.d.ts +1 -1
  57. package/dist/components/data-table.js +201 -85
  58. package/dist/components/data-table.js.map +1 -1
  59. package/dist/components/date-picker.d.ts +25 -9
  60. package/dist/components/date-picker.js +232 -94
  61. package/dist/components/date-picker.js.map +1 -1
  62. package/dist/components/dropdown.d.ts +74 -19
  63. package/dist/components/dropdown.js +72 -22
  64. package/dist/components/dropdown.js.map +1 -1
  65. package/dist/components/entity-combobox.d.ts +5 -4
  66. package/dist/components/entity-combobox.js +107 -76
  67. package/dist/components/entity-combobox.js.map +1 -1
  68. package/dist/components/facing-pair.d.ts +36 -0
  69. package/dist/components/facing-pair.js +40 -0
  70. package/dist/components/facing-pair.js.map +1 -0
  71. package/dist/components/field-sync.d.ts +164 -0
  72. package/dist/components/field-sync.js +264 -0
  73. package/dist/components/field-sync.js.map +1 -0
  74. package/dist/components/file-dropzone.d.ts +21 -7
  75. package/dist/components/file-dropzone.js +28 -7
  76. package/dist/components/file-dropzone.js.map +1 -1
  77. package/dist/components/full-bleed-dialog.d.ts +37 -26
  78. package/dist/components/full-bleed-dialog.js +20 -5
  79. package/dist/components/full-bleed-dialog.js.map +1 -1
  80. package/dist/components/grouped-picker.d.ts +21 -10
  81. package/dist/components/grouped-picker.js +66 -52
  82. package/dist/components/grouped-picker.js.map +1 -1
  83. package/dist/components/hover-menu.d.ts +20 -4
  84. package/dist/components/hover-menu.js +15 -4
  85. package/dist/components/hover-menu.js.map +1 -1
  86. package/dist/components/mini-calendar.d.ts +102 -14
  87. package/dist/components/mini-calendar.js +256 -70
  88. package/dist/components/mini-calendar.js.map +1 -1
  89. package/dist/components/modal.d.ts +9 -3
  90. package/dist/components/modal.js +34 -45
  91. package/dist/components/modal.js.map +1 -1
  92. package/dist/components/month-picker.d.ts +79 -0
  93. package/dist/components/month-picker.js +342 -0
  94. package/dist/components/month-picker.js.map +1 -0
  95. package/dist/components/multi-entity-combobox.d.ts +7 -6
  96. package/dist/components/multi-entity-combobox.js +112 -72
  97. package/dist/components/multi-entity-combobox.js.map +1 -1
  98. package/dist/components/multi-select.d.ts +13 -11
  99. package/dist/components/multi-select.js +161 -84
  100. package/dist/components/multi-select.js.map +1 -1
  101. package/dist/components/number-field.d.ts +92 -0
  102. package/dist/components/number-field.js +111 -0
  103. package/dist/components/number-field.js.map +1 -0
  104. package/dist/components/number-input.d.ts +21 -2
  105. package/dist/components/number-input.js +2 -1
  106. package/dist/components/number-input.js.map +1 -1
  107. package/dist/components/numpad-sheet.d.ts +25 -4
  108. package/dist/components/numpad-sheet.js +30 -13
  109. package/dist/components/numpad-sheet.js.map +1 -1
  110. package/dist/components/page-contents.d.ts +90 -0
  111. package/dist/components/page-contents.js +170 -0
  112. package/dist/components/page-contents.js.map +1 -0
  113. package/dist/components/password-strength.d.ts +106 -0
  114. package/dist/components/password-strength.js +132 -0
  115. package/dist/components/password-strength.js.map +1 -0
  116. package/dist/components/picker-sheet.d.ts +23 -15
  117. package/dist/components/picker-sheet.js +34 -14
  118. package/dist/components/picker-sheet.js.map +1 -1
  119. package/dist/components/popover.d.ts +62 -4
  120. package/dist/components/popover.js +52 -2
  121. package/dist/components/popover.js.map +1 -1
  122. package/dist/components/search-field.d.ts +25 -7
  123. package/dist/components/search-field.js +7 -5
  124. package/dist/components/search-field.js.map +1 -1
  125. package/dist/components/series-chart-labels.d.ts +14 -0
  126. package/dist/components/series-chart-labels.js +11 -0
  127. package/dist/components/series-chart-labels.js.map +1 -0
  128. package/dist/components/series-chart.d.ts +207 -0
  129. package/dist/components/series-chart.js +223 -0
  130. package/dist/components/series-chart.js.map +1 -0
  131. package/dist/components/settings-fields.d.ts +14 -4
  132. package/dist/components/settings-fields.js +19 -8
  133. package/dist/components/settings-fields.js.map +1 -1
  134. package/dist/components/signature-pad.d.ts +119 -0
  135. package/dist/components/signature-pad.js +405 -0
  136. package/dist/components/signature-pad.js.map +1 -0
  137. package/dist/components/slider.d.ts +80 -0
  138. package/dist/components/slider.js +155 -0
  139. package/dist/components/slider.js.map +1 -0
  140. package/dist/components/sparkline.d.ts +93 -0
  141. package/dist/components/sparkline.js +205 -0
  142. package/dist/components/sparkline.js.map +1 -0
  143. package/dist/components/stat-tile.d.ts +151 -0
  144. package/dist/components/stat-tile.js +236 -0
  145. package/dist/components/stat-tile.js.map +1 -0
  146. package/dist/components/swipeable-row.d.ts +25 -2
  147. package/dist/components/swipeable-row.js +31 -0
  148. package/dist/components/swipeable-row.js.map +1 -1
  149. package/dist/components/switch.d.ts +60 -0
  150. package/dist/components/switch.js +81 -0
  151. package/dist/components/switch.js.map +1 -0
  152. package/dist/components/time-input.d.ts +58 -0
  153. package/dist/components/time-input.js +50 -0
  154. package/dist/components/time-input.js.map +1 -0
  155. package/dist/components/toggle-group.d.ts +18 -3
  156. package/dist/components/toggle-group.js +10 -6
  157. package/dist/components/toggle-group.js.map +1 -1
  158. package/dist/components/toggle-legend.d.ts +115 -0
  159. package/dist/components/toggle-legend.js +114 -0
  160. package/dist/components/toggle-legend.js.map +1 -0
  161. package/dist/components/tooltip.d.ts +40 -11
  162. package/dist/components/tooltip.js +77 -20
  163. package/dist/components/tooltip.js.map +1 -1
  164. package/dist/components/treemap.d.ts +132 -0
  165. package/dist/components/treemap.js +179 -0
  166. package/dist/components/treemap.js.map +1 -0
  167. package/dist/components/ui.d.ts +159 -46
  168. package/dist/components/ui.js +220 -115
  169. package/dist/components/ui.js.map +1 -1
  170. package/dist/components/use-mobile-reveal.js +1 -0
  171. package/dist/components/use-mobile-reveal.js.map +1 -1
  172. package/dist/components/use-table-state.d.ts +1 -1
  173. package/dist/components/use-table-state.js +38 -17
  174. package/dist/components/use-table-state.js.map +1 -1
  175. package/dist/components/user-avatar.d.ts +5 -3
  176. package/dist/components/user-avatar.js +4 -2
  177. package/dist/components/user-avatar.js.map +1 -1
  178. package/dist/components/wizard-stepper.js +3 -2
  179. package/dist/components/wizard-stepper.js.map +1 -1
  180. package/dist/{data-table-filters-aXyf0Xub.d.ts → data-table-filters-DM1auUfH.d.ts} +27 -5
  181. package/dist/data-table.d.ts +7 -0
  182. package/dist/data-table.js +12 -0
  183. package/dist/data-table.js.map +1 -0
  184. package/dist/feedback/feedback-attachment.js +12 -11
  185. package/dist/feedback/feedback-attachment.js.map +1 -1
  186. package/dist/feedback/feedback-dialog.js +2 -1
  187. package/dist/feedback/feedback-dialog.js.map +1 -1
  188. package/dist/feedback/feedback-inbox.d.ts +33 -7
  189. package/dist/feedback/feedback-inbox.js +49 -37
  190. package/dist/feedback/feedback-inbox.js.map +1 -1
  191. package/dist/feedback.d.ts +5 -0
  192. package/dist/feedback.js +5 -0
  193. package/dist/feedback.js.map +1 -0
  194. package/dist/hooks/use-anchored-panel.js +1 -0
  195. package/dist/hooks/use-anchored-panel.js.map +1 -1
  196. package/dist/hooks/use-anchored-rect.js +1 -0
  197. package/dist/hooks/use-anchored-rect.js.map +1 -1
  198. package/dist/hooks/use-announce.d.ts +46 -0
  199. package/dist/hooks/use-announce.js +37 -0
  200. package/dist/hooks/use-announce.js.map +1 -0
  201. package/dist/hooks/use-body-scroll-lock.js +1 -0
  202. package/dist/hooks/use-body-scroll-lock.js.map +1 -1
  203. package/dist/hooks/use-close-transition.js +1 -0
  204. package/dist/hooks/use-close-transition.js.map +1 -1
  205. package/dist/hooks/use-dismiss.js +1 -0
  206. package/dist/hooks/use-dismiss.js.map +1 -1
  207. package/dist/hooks/use-focus-trap.d.ts +16 -0
  208. package/dist/hooks/use-focus-trap.js +67 -0
  209. package/dist/hooks/use-focus-trap.js.map +1 -0
  210. package/dist/hooks/use-media-query.js +1 -0
  211. package/dist/hooks/use-media-query.js.map +1 -1
  212. package/dist/hooks/use-overlay-history.js +27 -1
  213. package/dist/hooks/use-overlay-history.js.map +1 -1
  214. package/dist/hooks/use-row-swipe.js +1 -0
  215. package/dist/hooks/use-row-swipe.js.map +1 -1
  216. package/dist/i18n/defaults.d.ts +31 -0
  217. package/dist/i18n/defaults.js +63 -0
  218. package/dist/i18n/defaults.js.map +1 -0
  219. package/dist/i18n/kit-labels.d.ts +250 -0
  220. package/dist/i18n/kit-labels.js +184 -0
  221. package/dist/i18n/kit-labels.js.map +1 -0
  222. package/dist/index.d.ts +47 -22
  223. package/dist/index.js +28 -0
  224. package/dist/index.js.map +1 -1
  225. package/dist/lib/calc.js +1 -0
  226. package/dist/lib/calc.js.map +1 -1
  227. package/dist/lib/cn.js +1 -0
  228. package/dist/lib/cn.js.map +1 -1
  229. package/dist/lib/dates.js +1 -0
  230. package/dist/lib/dates.js.map +1 -1
  231. package/dist/lib/logger.js +1 -0
  232. package/dist/lib/logger.js.map +1 -1
  233. package/dist/lib/safe-storage.js +1 -0
  234. package/dist/lib/safe-storage.js.map +1 -1
  235. package/dist/search/command-palette.d.ts +11 -1
  236. package/dist/search/command-palette.js +21 -15
  237. package/dist/search/command-palette.js.map +1 -1
  238. package/dist/search.d.ts +2 -0
  239. package/dist/search.js +3 -0
  240. package/dist/search.js.map +1 -0
  241. package/dist/shell/app-shell.d.ts +33 -4
  242. package/dist/shell/app-shell.js +275 -78
  243. package/dist/shell/app-shell.js.map +1 -1
  244. package/dist/shell/option-switcher-menu.js +3 -2
  245. package/dist/shell/option-switcher-menu.js.map +1 -1
  246. package/dist/shell/role-switcher.d.ts +6 -0
  247. package/dist/shell/role-switcher.js +7 -4
  248. package/dist/shell/role-switcher.js.map +1 -1
  249. package/dist/shell/top-bar.d.ts +15 -7
  250. package/dist/shell/top-bar.js +3 -5
  251. package/dist/shell/top-bar.js.map +1 -1
  252. package/dist/shell/topbar-action-menu.js +3 -2
  253. package/dist/shell/topbar-action-menu.js.map +1 -1
  254. package/dist/shell/topbar-controls.d.ts +5 -2
  255. package/dist/shell/topbar-controls.js +20 -15
  256. package/dist/shell/topbar-controls.js.map +1 -1
  257. package/dist/shell.d.ts +13 -0
  258. package/dist/shell.js +8 -0
  259. package/dist/shell.js.map +1 -0
  260. package/dist/theme/chart-palette.d.ts +19 -12
  261. package/dist/theme/chart-palette.js +24 -30
  262. package/dist/theme/chart-palette.js.map +1 -1
  263. package/dist/theme/color.d.ts +78 -0
  264. package/dist/theme/color.js +148 -0
  265. package/dist/theme/color.js.map +1 -0
  266. package/dist/theme/palette-derive.d.ts +140 -0
  267. package/dist/theme/palette-derive.js +376 -0
  268. package/dist/theme/palette-derive.js.map +1 -0
  269. package/dist/theme/palette-presets.d.ts +14 -1
  270. package/dist/theme/palette-presets.js +231 -3
  271. package/dist/theme/palette-presets.js.map +1 -1
  272. package/dist/theme/palette-store.js +1 -0
  273. package/dist/theme/palette-store.js.map +1 -1
  274. package/dist/theme/theme-store.js +1 -0
  275. package/dist/theme/theme-store.js.map +1 -1
  276. package/dist/tour/tour.d.ts +2 -1
  277. package/dist/tour/tour.js +15 -9
  278. package/dist/tour/tour.js.map +1 -1
  279. package/dist/tour.d.ts +2 -0
  280. package/dist/tour.js +3 -0
  281. package/dist/tour.js.map +1 -0
  282. package/dist/wizard/stepper-nav.js +10 -8
  283. package/dist/wizard/stepper-nav.js.map +1 -1
  284. package/dist/wizard/types.d.ts +18 -2
  285. package/dist/wizard/types.js +4 -1
  286. package/dist/wizard/types.js.map +1 -1
  287. package/dist/wizard/use-wizard.js +12 -4
  288. package/dist/wizard/use-wizard.js.map +1 -1
  289. package/dist/wizard/validation.js +1 -0
  290. package/dist/wizard/validation.js.map +1 -1
  291. package/dist/wizard/wizard-context.js +1 -0
  292. package/dist/wizard/wizard-context.js.map +1 -1
  293. package/dist/wizard/wizard-step.js +1 -0
  294. package/dist/wizard/wizard-step.js.map +1 -1
  295. package/dist/wizard/wizard-summary.js +5 -3
  296. package/dist/wizard/wizard-summary.js.map +1 -1
  297. package/dist/wizard.d.ts +8 -0
  298. package/dist/wizard.js +9 -0
  299. package/dist/wizard.js.map +1 -0
  300. package/package.json +66 -13
  301. package/src/chart.ts +22 -0
  302. package/src/components/account-settings.tsx +275 -0
  303. package/src/components/alert-banner.tsx +75 -0
  304. package/src/components/amount-input.tsx +471 -0
  305. package/src/components/calculator.tsx +252 -0
  306. package/src/components/chart-zoom.tsx +499 -0
  307. package/src/components/chart.tsx +363 -0
  308. package/src/components/checkbox.tsx +222 -0
  309. package/src/components/chip.tsx +570 -0
  310. package/src/components/choice-parts.ts +33 -0
  311. package/src/components/combobox-core.tsx +540 -0
  312. package/src/components/combobox.tsx +1026 -0
  313. package/src/components/currency-select.tsx +269 -0
  314. package/src/components/data-table-filter-popover.tsx +252 -0
  315. package/src/components/data-table-filters.ts +176 -0
  316. package/src/components/data-table-labels.ts +190 -0
  317. package/src/components/data-table-pagination.tsx +126 -0
  318. package/src/components/data-table-sort.ts +70 -0
  319. package/src/components/data-table.tsx +1740 -0
  320. package/src/components/date-picker.tsx +631 -0
  321. package/src/components/dropdown.tsx +341 -0
  322. package/src/components/entity-combobox.tsx +215 -0
  323. package/src/components/facing-pair.ts +86 -0
  324. package/src/components/field-sync.tsx +472 -0
  325. package/src/components/file-dropzone.tsx +181 -0
  326. package/src/components/full-bleed-dialog.tsx +179 -0
  327. package/src/components/grouped-picker.tsx +157 -0
  328. package/src/components/hover-menu.tsx +183 -0
  329. package/src/components/mini-calendar.tsx +554 -0
  330. package/src/components/modal.tsx +287 -0
  331. package/src/components/month-picker.tsx +549 -0
  332. package/src/components/multi-entity-combobox.tsx +226 -0
  333. package/src/components/multi-select.tsx +303 -0
  334. package/src/components/number-field.tsx +225 -0
  335. package/src/components/number-input.tsx +211 -0
  336. package/src/components/numpad-sheet.tsx +264 -0
  337. package/src/components/page-contents.tsx +289 -0
  338. package/src/components/password-strength.tsx +258 -0
  339. package/src/components/picker-sheet.tsx +251 -0
  340. package/src/components/popover.tsx +195 -0
  341. package/src/components/search-field.tsx +146 -0
  342. package/src/components/series-chart-labels.ts +26 -0
  343. package/src/components/series-chart.tsx +481 -0
  344. package/src/components/settings-fields.tsx +68 -0
  345. package/src/components/signature-pad.tsx +630 -0
  346. package/src/components/slider.tsx +313 -0
  347. package/src/components/sparkline.tsx +329 -0
  348. package/src/components/stat-tile.tsx +448 -0
  349. package/src/components/swipeable-row.tsx +271 -0
  350. package/src/components/switch.tsx +170 -0
  351. package/src/components/time-input.tsx +107 -0
  352. package/src/components/toggle-group.tsx +153 -0
  353. package/src/components/toggle-legend.tsx +229 -0
  354. package/src/components/tooltip.tsx +477 -0
  355. package/src/components/treemap.tsx +390 -0
  356. package/src/components/ui.tsx +1284 -0
  357. package/src/components/use-mobile-reveal.ts +54 -0
  358. package/src/components/use-table-state.ts +295 -0
  359. package/src/components/user-avatar.tsx +61 -0
  360. package/src/components/wizard-stepper.tsx +37 -0
  361. package/src/data-table.ts +19 -0
  362. package/src/feedback/feedback-attachment.tsx +246 -0
  363. package/src/feedback/feedback-dialog.tsx +209 -0
  364. package/src/feedback/feedback-inbox.tsx +615 -0
  365. package/src/feedback.ts +8 -0
  366. package/src/hooks/use-anchored-panel.ts +169 -0
  367. package/src/hooks/use-anchored-rect.ts +78 -0
  368. package/src/hooks/use-announce.ts +85 -0
  369. package/src/hooks/use-body-scroll-lock.ts +94 -0
  370. package/src/hooks/use-close-transition.ts +99 -0
  371. package/src/hooks/use-dismiss.ts +77 -0
  372. package/src/hooks/use-focus-trap.ts +160 -0
  373. package/src/hooks/use-media-query.ts +23 -0
  374. package/src/hooks/use-overlay-history.ts +345 -0
  375. package/src/hooks/use-row-swipe.ts +212 -0
  376. package/src/i18n/defaults.ts +68 -0
  377. package/src/i18n/kit-labels.tsx +447 -0
  378. package/src/index.ts +169 -0
  379. package/src/lib/calc.ts +309 -0
  380. package/src/lib/cn.ts +5 -0
  381. package/src/lib/dates.ts +187 -0
  382. package/src/lib/logger.ts +73 -0
  383. package/src/lib/safe-storage.ts +37 -0
  384. package/src/search/command-palette.tsx +342 -0
  385. package/src/search.ts +3 -0
  386. package/src/shell/app-shell.tsx +772 -0
  387. package/src/shell/option-switcher-menu.tsx +72 -0
  388. package/src/shell/role-switcher.tsx +46 -0
  389. package/src/shell/top-bar.tsx +39 -0
  390. package/src/shell/topbar-action-menu.tsx +91 -0
  391. package/src/shell/topbar-controls.tsx +229 -0
  392. package/src/shell.ts +10 -0
  393. package/src/theme/chart-palette.ts +145 -0
  394. package/src/theme/color.ts +266 -0
  395. package/src/theme/palette-derive.ts +681 -0
  396. package/src/theme/palette-presets.ts +760 -0
  397. package/src/theme/palette-store.ts +94 -0
  398. package/src/theme/theme-store.ts +145 -0
  399. package/src/tour/tour.tsx +586 -0
  400. package/src/tour.ts +3 -0
  401. package/src/vite-env.d.ts +26 -0
  402. package/src/wizard/stepper-nav.tsx +221 -0
  403. package/src/wizard/types.ts +178 -0
  404. package/src/wizard/use-wizard.ts +345 -0
  405. package/src/wizard/validation.ts +37 -0
  406. package/src/wizard/wizard-context.tsx +29 -0
  407. package/src/wizard/wizard-step.tsx +14 -0
  408. package/src/wizard/wizard-summary.tsx +69 -0
  409. package/src/wizard.ts +14 -0
  410. package/tokens.css +222 -20
@@ -0,0 +1,313 @@
1
+ import { forwardRef, useId } from "react";
2
+ import type { CSSProperties, ChangeEvent, InputHTMLAttributes, ReactNode } from "react";
3
+ import { cn } from "../lib/cn";
4
+
5
+ /**
6
+ * A range control on a native `<input type="range">`.
7
+ *
8
+ * Lenkbank wrote the first one (`shared/ui/slider-field.tsx`) and then the case for
9
+ * moving it here (`docs/ui-kit-slider-proposal.md`). This is that proposal's "stage 1"
10
+ * primitive — the track, the thumb, the scale and the accessibility — plus the label
11
+ * line and tick marks its SpeedSlider needed. The four decisions it argues should be
12
+ * made once are made here, and each carries its note below:
13
+ *
14
+ * 1. a logarithmic scale whose far-left stop means exactly zero ({@link SliderProps.zeroStop});
15
+ * 2. `aria-valuetext`, so a log track does not announce its 0–1 position as the value;
16
+ * 3. a 24px pointer target, which needs the track and thumb drawn separately;
17
+ * 4. the number is the truth and the track a view of it: a value outside `min`–`max`
18
+ * is kept and the thumb pins at the end — nothing here clamps what it was given.
19
+ *
20
+ * Controlled only, like `ToggleGroup` and `NumberInput`: there is no uncontrolled mode
21
+ * to get wrong, and neither app wants one. The package does no number formatting and
22
+ * holds no locale — `formatValue` is how the caller's `Intl` result gets in.
23
+ *
24
+ * Not here, deliberately (the proposal's list): two thumbs, vertical orientation, and
25
+ * a number box. A `NumberInput` goes in the `readout` slot when a typed value is
26
+ * wanted beside the track.
27
+ */
28
+
29
+ /** On a log scale with {@link SliderProps.zeroStop}, the share of the track that is
30
+ * the "off" stop. The first position to its right is `min`. */
31
+ const ZERO_STOP = 0.02;
32
+
33
+ /** Position resolution on a log track — 1000 stops, fine enough that a drag reads as
34
+ * continuous. PageUp/PageDown still cross a tenth of the track at a time. */
35
+ const LOG_STEP = 0.001;
36
+
37
+ /**
38
+ * Where a value sits on a logarithmic track, 0–1.
39
+ *
40
+ * With `zeroStop`, zero and below is 0 and the decades between `min` and `max` are
41
+ * mapped onto the rest; without it, `min` is the far left. A value past either end
42
+ * pins there — that is the DISPLAY clamping, never the value's.
43
+ */
44
+ export function toLogPosition(value: number, min: number, max: number, zeroStop = true): number {
45
+ if (value <= 0) return 0;
46
+ const share = Math.log(Math.max(value, min) / min) / Math.log(max / min);
47
+ const clamped = Math.min(1, Math.max(0, share));
48
+ return zeroStop ? ZERO_STOP + (1 - ZERO_STOP) * clamped : clamped;
49
+ }
50
+
51
+ /** The inverse of {@link toLogPosition}: the value at a track position. */
52
+ export function fromLogPosition(position: number, min: number, max: number, zeroStop = true): number {
53
+ if (zeroStop) {
54
+ if (position < ZERO_STOP) return 0;
55
+ return min * Math.pow(max / min, (position - ZERO_STOP) / (1 - ZERO_STOP));
56
+ }
57
+ return min * Math.pow(max / min, position);
58
+ }
59
+
60
+ // The input is the pointer target, so its box is `h-6` (24px, WCAG 2.5.8) and the
61
+ // track that LOOKS like a track is a 6px strip drawn inside it. Once the box is taller
62
+ // than the track its background cannot be the track, and with `appearance-none`
63
+ // nothing else draws one — measured in Chromium by Lenkbank, that is a bare thumb
64
+ // floating on nothing, in the browser's blue. Hence the vendor pseudo-elements: the
65
+ // two engines share no selector for either half, so each half is spelt twice.
66
+ //
67
+ // The fill up to the thumb: WebKit has no progress pseudo-element, so its track is a
68
+ // two-stop gradient that breaks at `--slider-fill` (set inline from the position), and
69
+ // the gradient runs the other way in a right-to-left document, where the native range
70
+ // runs the other way too. Firefox draws the fill itself with `::-moz-range-progress`,
71
+ // which follows the direction on its own.
72
+ //
73
+ // The thumb's `-mt-[5px]` centres a 16px thumb on a 6px track: WebKit lays the thumb
74
+ // out from the track's top edge, Firefox centres it, so only one carries the offset.
75
+ //
76
+ // The focus ring goes on the THUMB, not the box: the box is the full width of the
77
+ // row, and a ring round all of it says "this row is focused" rather than "the handle
78
+ // you are about to move is". Offset in the surface colour, because the thumb is
79
+ // --brand and a --brand ring touching it would be invisible.
80
+ const TRACK = [
81
+ "block h-6 w-full cursor-pointer appearance-none bg-transparent focus:outline-none disabled:cursor-not-allowed",
82
+ // WebKit / Blink
83
+ "[&::-webkit-slider-runnable-track]:h-1.5 [&::-webkit-slider-runnable-track]:rounded-full",
84
+ "[&::-webkit-slider-runnable-track]:bg-[linear-gradient(to_right,var(--brand)_var(--slider-fill),var(--border-strong)_var(--slider-fill))]",
85
+ "rtl:[&::-webkit-slider-runnable-track]:bg-[linear-gradient(to_left,var(--brand)_var(--slider-fill),var(--border-strong)_var(--slider-fill))]",
86
+ "[&::-webkit-slider-thumb]:-mt-[5px] [&::-webkit-slider-thumb]:size-4 [&::-webkit-slider-thumb]:appearance-none",
87
+ "[&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-[var(--brand)] [&::-webkit-slider-thumb]:shadow",
88
+ "focus-visible:[&::-webkit-slider-thumb]:ring-2 focus-visible:[&::-webkit-slider-thumb]:ring-[var(--brand)]",
89
+ "focus-visible:[&::-webkit-slider-thumb]:ring-offset-2 focus-visible:[&::-webkit-slider-thumb]:ring-offset-[var(--bg-surface)]",
90
+ // Firefox
91
+ "[&::-moz-range-track]:h-1.5 [&::-moz-range-track]:rounded-full [&::-moz-range-track]:bg-[var(--border-strong)]",
92
+ "[&::-moz-range-progress]:h-1.5 [&::-moz-range-progress]:rounded-full [&::-moz-range-progress]:bg-[var(--brand)]",
93
+ "[&::-moz-range-thumb]:size-4 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0",
94
+ "[&::-moz-range-thumb]:bg-[var(--brand)] [&::-moz-range-thumb]:shadow",
95
+ "focus-visible:[&::-moz-range-thumb]:ring-2 focus-visible:[&::-moz-range-thumb]:ring-[var(--brand)]",
96
+ "focus-visible:[&::-moz-range-thumb]:ring-offset-2 focus-visible:[&::-moz-range-thumb]:ring-offset-[var(--bg-surface)]",
97
+ ].join(" ");
98
+
99
+ /** A tick under the track. A bare number is a tick with no words. */
100
+ export interface SliderMark {
101
+ value: number;
102
+ /** Shown under the tick — "30 km/h", "measured". Visual only: the value itself is
103
+ * announced through `aria-valuetext`, so the marks row is hidden from assistive
104
+ * technology rather than read out as a list of numbers after the slider. */
105
+ label?: ReactNode;
106
+ }
107
+
108
+ export interface SliderProps
109
+ extends Omit<
110
+ InputHTMLAttributes<HTMLInputElement>,
111
+ "type" | "value" | "defaultValue" | "onChange" | "min" | "max" | "step" | "children"
112
+ > {
113
+ /** The value. On a log scale this is the real value, not the track position. */
114
+ value: number;
115
+ /** The new value — a number, not the DOM event, like `ToggleGroup`. Fired on every
116
+ * movement; debounce at the call site if each one costs a request. */
117
+ onChange: (value: number) => void;
118
+ /** The track's ends. Not a validity range: a `value` outside it is kept and the
119
+ * thumb pins at the end (see the module note). On a log scale `min` must be
120
+ * above zero — zero is what {@link zeroStop} is for. */
121
+ min: number;
122
+ max: number;
123
+ /** Step on a linear scale (the native default is 1). Ignored on a log scale, which
124
+ * steps by track position — a fixed step is wrong at one end of every decade. */
125
+ step?: number;
126
+ /**
127
+ * How the track maps to the value. `"log"` for quantities that run over decades — a
128
+ * proportional gain of 0.5 and one of 200 are both ordinary, and on a linear track
129
+ * everything under a tenth of the range is the first pixel. Falls back to linear if
130
+ * `min` is not above zero or `max` not above `min`, where a logarithm has no answer.
131
+ */
132
+ scale?: "linear" | "log";
133
+ /**
134
+ * Log scale only: the far-left stop means exactly 0 ("off"), and the first step to
135
+ * its right is `min`. A logarithm cannot hold zero, and zero is often the question —
136
+ * should there be an integral term at all? Default `true`; turn it off for a scale
137
+ * where zero is meaningless (a zoom factor, a playback rate).
138
+ */
139
+ zeroStop?: boolean;
140
+ /**
141
+ * What a screen reader says for the value — "12.5 mm", "Off". Sets
142
+ * `aria-valuetext`. Without it a log track would announce its 0–1 POSITION ("0.63"
143
+ * for a gain of 12.5), so on a log scale the raw value is announced instead; pass
144
+ * this for anything with a unit or a locale.
145
+ */
146
+ formatValue?: (value: number) => string;
147
+ /** The words above the track, as a `<label for>`. Without it, pass `aria-label`. */
148
+ label?: ReactNode;
149
+ /** Something that belongs to the label rather than the value — in practice a
150
+ * `FieldHint` "?". Rendered beside the label, outside the `<label>` element, so it
151
+ * never becomes part of the slider's accessible name. */
152
+ hint?: ReactNode;
153
+ /** The value as the reader sees it, at the end of the label line — a formatted
154
+ * number, or a `NumberInput` for typing one. Rendered as given. */
155
+ readout?: ReactNode;
156
+ /** Ticks under the track, at these values. Marks outside `min`–`max` are dropped. */
157
+ marks?: ReadonlyArray<number | SliderMark>;
158
+ /** Classes for the `<input>`. `className` styles the outer wrapper. */
159
+ inputClassName?: string;
160
+ }
161
+
162
+ /**
163
+ * A native range input with the kit's track and thumb, an optional label line
164
+ * (`label`, `hint`, `readout`), tick `marks`, and a logarithmic `scale`.
165
+ *
166
+ * Every other prop reaches the `<input>` — `name`, `id`, `aria-*`, `data-*`, `onBlur`,
167
+ * `onPointerUp` for commit-on-release — and `disabled` also fades the label line.
168
+ */
169
+ export const Slider = forwardRef<HTMLInputElement, SliderProps>(function Slider(
170
+ {
171
+ value,
172
+ onChange,
173
+ min,
174
+ max,
175
+ step,
176
+ scale = "linear",
177
+ zeroStop = true,
178
+ formatValue,
179
+ label,
180
+ hint,
181
+ readout,
182
+ marks,
183
+ className,
184
+ inputClassName,
185
+ id,
186
+ disabled,
187
+ style,
188
+ ...rest
189
+ },
190
+ ref,
191
+ ) {
192
+ const generated = useId();
193
+ const inputId = id ?? generated;
194
+ const log = scale === "log" && min > 0 && max > min;
195
+
196
+ // Everything the native element sees is a POSITION; everything the caller sees is a
197
+ // value. On a linear track the two are the same number.
198
+ const trackMin = log ? 0 : min;
199
+ const trackMax = log ? 1 : max;
200
+ const position = (v: number) => (log ? toLogPosition(v, min, max, zeroStop) : Math.min(max, Math.max(min, v)));
201
+ const current = position(value);
202
+ // 0–1 share of the track, for the fill and the marks. A zero-width range (min ===
203
+ // max) has no share to speak of; call it empty rather than divide by zero.
204
+ const share = (p: number) => (trackMax > trackMin ? (p - trackMin) / (trackMax - trackMin) : 0);
205
+ const outOfRange = value < min || value > max;
206
+
207
+ // `aria-valuetext` replaces the announced NUMBER and leaves the announced name
208
+ // alone. The caller's words first (`formatValue`, then a hand-written
209
+ // `aria-valuetext`); failing those, the real value wherever the
210
+ // native one would lie — every log position, and a linear value the thumb is only
211
+ // pinned at. `toPrecision(4)` keeps a log value from being read out to sixteen
212
+ // digits; it is not formatting, and `formatValue` is how to get a locale.
213
+ const valueText = formatValue
214
+ ? formatValue(value)
215
+ : (rest["aria-valuetext"] ??
216
+ (log || outOfRange ? String(Number(value.toPrecision(4))) : undefined));
217
+
218
+ const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
219
+ const raw = Number(e.target.value);
220
+ if (!log) return onChange(raw);
221
+ // The gap between the zero stop and `min` has to be crossable by keyboard. An
222
+ // arrow key moves one LOG_STEP, which from zero lands inside the gap — and a gap
223
+ // that rounded to the nearer end would send it straight back to zero, trapping
224
+ // a keyboard user at "off". So inside the gap, moving up from zero means `min`
225
+ // and anything else means zero.
226
+ if (zeroStop && raw > 0 && raw < ZERO_STOP) return onChange(value <= 0 ? min : 0);
227
+ onChange(fromLogPosition(raw, min, max, zeroStop));
228
+ };
229
+
230
+ const normalizedMarks = (marks ?? [])
231
+ .map((m) => (typeof m === "number" ? { value: m } : m))
232
+ .filter((m) => m.value >= min && m.value <= max);
233
+ const hasMarkLabels = normalizedMarks.some((m) => m.label !== undefined);
234
+
235
+ const hasHeader = label !== undefined || hint !== undefined || readout !== undefined;
236
+ const plainReadout = typeof readout === "string" || typeof readout === "number";
237
+
238
+ return (
239
+ <div className={cn("w-full", disabled && "opacity-60", className)}>
240
+ {hasHeader && (
241
+ // `select-none` on the words is not a nicety (Lenkbank feedback #93): a
242
+ // mousedown on text beside a range input starts Chrome's native text drag, the
243
+ // cursor turns to no-drop and the thumb stops following the pointer for the
244
+ // rest of the gesture. The readout is only made unselectable when it is plain
245
+ // text — a NumberInput in that slot has to stay selectable to be editable.
246
+ <div className="flex items-center gap-2">
247
+ {label !== undefined && (
248
+ <label
249
+ htmlFor={inputId}
250
+ className="min-w-0 select-none text-xs font-medium text-[var(--text-secondary)]"
251
+ >
252
+ {label}
253
+ </label>
254
+ )}
255
+ {hint !== undefined && <span className="flex shrink-0 items-center">{hint}</span>}
256
+ {readout !== undefined && (
257
+ <span
258
+ className={cn(
259
+ "ms-auto shrink-0 text-xs tabular-nums text-[var(--text-primary)]",
260
+ plainReadout && "select-none",
261
+ )}
262
+ >
263
+ {readout}
264
+ </span>
265
+ )}
266
+ </div>
267
+ )}
268
+ <input
269
+ ref={ref}
270
+ id={label !== undefined ? inputId : id}
271
+ disabled={disabled}
272
+ {...rest}
273
+ type="range"
274
+ min={trackMin}
275
+ max={trackMax}
276
+ step={log ? LOG_STEP : step}
277
+ value={current}
278
+ aria-valuetext={valueText}
279
+ onChange={handleChange}
280
+ // The fill's break point, merged under the caller's own style.
281
+ style={{ ...style, "--slider-fill": `${share(current) * 100}%` } as CSSProperties}
282
+ className={cn(TRACK, inputClassName)}
283
+ />
284
+ {normalizedMarks.length > 0 && (
285
+ // `mx-2` insets the row by half a thumb on each side, which is exactly the
286
+ // range the thumb's CENTRE travels — so a tick at 0% sits under the thumb at
287
+ // `min`, not under the track's rounded end. Each tick is a zero-width column
288
+ // centred on its point, which centres its label on it in either direction
289
+ // without a `translate` that would need flipping for right-to-left.
290
+ <div
291
+ aria-hidden
292
+ className={cn("relative mx-2 -mt-1.5", hasMarkLabels ? "h-5" : "h-1.5")}
293
+ >
294
+ {normalizedMarks.map((m) => (
295
+ <span
296
+ key={m.value}
297
+ className="absolute top-0 flex w-0 flex-col items-center"
298
+ style={{ insetInlineStart: `${share(position(m.value)) * 100}%` }}
299
+ >
300
+ <span className="h-1.5 w-px shrink-0 bg-[var(--border-strong)]" />
301
+ {m.label !== undefined && (
302
+ <span className="mt-0.5 select-none whitespace-nowrap text-[11px] leading-tight text-[var(--text-muted)]">
303
+ {m.label}
304
+ </span>
305
+ )}
306
+ </span>
307
+ ))}
308
+ </div>
309
+ )}
310
+ </div>
311
+ );
312
+ });
313
+ Slider.displayName = "Slider";
@@ -0,0 +1,329 @@
1
+ import type { ComponentPropsWithoutRef } from "react";
2
+ import { cn } from "../lib/cn";
3
+ import { useKitLabels, useKitLocale } from "../i18n/kit-labels";
4
+
5
+ /**
6
+ * The words a sparkline speaks. It has no axes, no tooltip and no legend, so its
7
+ * accessible name is the ONLY way a screen reader learns what the shape says —
8
+ * "Sparkline, image" is not a summary. Each message takes the values already
9
+ * formatted, so the digits follow the locale and the sentence stays translatable.
10
+ */
11
+ export interface SparklineLabels {
12
+ /** Last value above the first: "Rising from 12 to 40". */
13
+ rising: (first: string, last: string) => string;
14
+ /** Last value below the first. */
15
+ falling: (first: string, last: string) => string;
16
+ /** First and last are equal — the shape may still have moved in between. */
17
+ flat: (value: string) => string;
18
+ /** One value is a dot, not a trend. */
19
+ single: (value: string) => string;
20
+ /** No value at all (empty, or every point a gap). */
21
+ noData: string;
22
+ /** How the series' own name is joined to the summary when `label` is given. */
23
+ named: (name: string, summary: string) => string;
24
+ }
25
+
26
+ export const DEFAULT_SPARKLINE_LABELS: SparklineLabels = {
27
+ rising: (first, last) => `Rising from ${first} to ${last}`,
28
+ falling: (first, last) => `Falling from ${first} to ${last}`,
29
+ flat: (value) => `Unchanged at ${value}`,
30
+ single: (value) => `One value: ${value}`,
31
+ noData: "No data",
32
+ named: (name, summary) => `${name}: ${summary}`,
33
+ };
34
+
35
+ /**
36
+ * What colours the marks. Token names, not colours: every one resolves to a variable
37
+ * in tokens.css, so the sparkline re-skins with the palette like everything else.
38
+ *
39
+ * - `chart` — the first series colour (`--chart-1`), the default.
40
+ * - `income` / `expense` / `net` — the money trio.
41
+ * - `signed` — a money series read by sign: bars each take income or expense by their
42
+ * own value, a line or area takes the colour of its LAST value (where it ended up).
43
+ */
44
+ export type SparklineTone =
45
+ | "chart"
46
+ | "brand"
47
+ | "muted"
48
+ | "income"
49
+ | "expense"
50
+ | "net"
51
+ | "signed"
52
+ | "success"
53
+ | "warning"
54
+ | "danger"
55
+ | "info";
56
+
57
+ // `color`, not `stroke`/`fill`: every mark paints with `currentColor`, so one class
58
+ // per element is enough and a signed bar chart needs no second lookup table.
59
+ const TONE_CLASS: Record<Exclude<SparklineTone, "signed">, string> = {
60
+ chart: "text-[var(--chart-1)]",
61
+ brand: "text-[var(--brand)]",
62
+ muted: "text-[var(--text-muted)]",
63
+ income: "text-[var(--money-income)]",
64
+ expense: "text-[var(--money-expense)]",
65
+ net: "text-[var(--money-net)]",
66
+ success: "text-[var(--success)]",
67
+ warning: "text-[var(--warning)]",
68
+ danger: "text-[var(--danger)]",
69
+ info: "text-[var(--info)]",
70
+ };
71
+
72
+ const signedClass = (v: number) => (v < 0 ? TONE_CLASS.expense : TONE_CLASS.income);
73
+
74
+ /** `children` is the marks and `width`/`height` are numbers here (or `fluid`), so
75
+ * those three are the sparkline's own; everything else lands on the `<svg>`. */
76
+ export interface SparklineProps
77
+ extends Omit<ComponentPropsWithoutRef<"svg">, "children" | "width" | "height"> {
78
+ /** The series, oldest first. `null`/`undefined`/`NaN` is a GAP — the line breaks
79
+ * there rather than drawing a zero or bridging across the missing point. */
80
+ data: ReadonlyArray<number | null | undefined>;
81
+ /** Width in px. Ignored under `fluid`. */
82
+ width?: number;
83
+ height?: number;
84
+ /** Fill the container's width instead of a fixed one (the height stays fixed). */
85
+ fluid?: boolean;
86
+ variant?: "line" | "area" | "bar";
87
+ tone?: SparklineTone;
88
+ /** Lower / upper end of the scale. Unset, the scale is the data's own tight range
89
+ * (bars always include zero, since a bar's length IS its value). */
90
+ min?: number;
91
+ max?: number;
92
+ /** A dashed hairline at this value, e.g. `0` for a series that crosses zero, or a
93
+ * target. It is pulled into the scale unless `min`/`max` exclude it. */
94
+ referenceValue?: number;
95
+ /** Dot on the last point (line/area) or dim every bar but the last. Default true —
96
+ * the last value is the one the number beside a sparkline almost always is. */
97
+ highlightLast?: boolean;
98
+ strokeWidth?: number;
99
+ /** What the series is ("Price"), prefixed to the spoken summary. */
100
+ label?: string;
101
+ /** How a value is spoken in the summary. Defaults to `Intl.NumberFormat` in the
102
+ * kit's locale, at most two decimals. */
103
+ formatValue?: (value: number) => string;
104
+ locale?: string;
105
+ labels?: Partial<SparklineLabels>;
106
+ }
107
+
108
+ const DEFAULT_WIDTH = 72;
109
+ const DEFAULT_HEIGHT = 24;
110
+
111
+ // One formatter per locale, not per render: a sparkline is a table-cell component and
112
+ // a hundred rows would otherwise build a hundred identical `Intl.NumberFormat`s.
113
+ const formatters = new Map<string, Intl.NumberFormat>();
114
+ function defaultFormat(locale: string | undefined): (v: number) => string {
115
+ const key = locale ?? "";
116
+ let f = formatters.get(key);
117
+ if (!f) {
118
+ f = new Intl.NumberFormat(locale, { maximumFractionDigits: 2 });
119
+ formatters.set(key, f);
120
+ }
121
+ return (v) => f.format(v);
122
+ }
123
+
124
+ const isValue = (v: number | null | undefined): v is number => typeof v === "number" && Number.isFinite(v);
125
+
126
+ /** The spoken summary, without the series name. Exported for callers that want the
127
+ * same sentence elsewhere (a table's row description, a tooltip). */
128
+ export function sparklineSummary(
129
+ data: ReadonlyArray<number | null | undefined>,
130
+ labels: SparklineLabels,
131
+ format: (value: number) => string,
132
+ ): string {
133
+ const values = data.filter(isValue);
134
+ if (values.length === 0) return labels.noData;
135
+ const first = values[0];
136
+ const last = values[values.length - 1];
137
+ if (values.length === 1) return labels.single(format(last));
138
+ if (last > first) return labels.rising(format(first), format(last));
139
+ if (last < first) return labels.falling(format(first), format(last));
140
+ return labels.flat(format(last));
141
+ }
142
+
143
+ /**
144
+ * A tiny trend line: shape only — no axes, no tooltip, no legend, no chart library.
145
+ *
146
+ * It is plain SVG on purpose. The two copies it replaces were recharts `LineChart`s,
147
+ * which is a ResizeObserver, a React tree of a dozen components and a 433 KB import
148
+ * per table cell; a sparkline is one `<path>`, cheap enough for every row of a table
149
+ * and small enough to live on the main barrel rather than behind `/chart`.
150
+ *
151
+ * The scale is TIGHT by default (the data's own min to max). A price that moved from
152
+ * 9.75 to 12.40 drawn against an implicit zero baseline is a nearly flat line at the
153
+ * top of the box — the one thing a shape-only chart must not do (keksdose dev#449).
154
+ * Bars are the exception: a bar's length is its value, so their scale includes zero.
155
+ *
156
+ * `role="img"` with a generated summary ("Rising from 12 to 40") as its name — the
157
+ * numbers beside it are the values, the sparkline says which way they have moved.
158
+ *
159
+ * Under `fluid` the SVG is stretched non-uniformly to the container's width, so every
160
+ * stroke is `non-scaling-stroke` and the last-point dot is a zero-length ROUND-CAPPED
161
+ * stroke rather than a `<circle>`: a stroke keeps its width under that transform,
162
+ * where a circle would be squashed into an ellipse.
163
+ */
164
+ export function Sparkline({
165
+ data,
166
+ width = DEFAULT_WIDTH,
167
+ height = DEFAULT_HEIGHT,
168
+ fluid = false,
169
+ variant = "line",
170
+ tone = "chart",
171
+ min,
172
+ max,
173
+ referenceValue,
174
+ highlightLast = true,
175
+ strokeWidth = 1.5,
176
+ label,
177
+ formatValue,
178
+ locale,
179
+ labels,
180
+ className,
181
+ style,
182
+ ...rest
183
+ }: SparklineProps) {
184
+ const text = useKitLabels("sparkline", DEFAULT_SPARKLINE_LABELS, labels);
185
+ const kitLocale = useKitLocale(locale);
186
+ const format = formatValue ?? defaultFormat(kitLocale);
187
+
188
+ const summary = sparklineSummary(data, text, format);
189
+ const name = label ? text.named(label, summary) : summary;
190
+
191
+ const W = width;
192
+ const H = height;
193
+ const points = data.map((v) => (isValue(v) ? v : null));
194
+ const values = points.filter(isValue);
195
+ const n = points.length;
196
+
197
+ // ── Scale ──
198
+ const bounds = [...values];
199
+ if (referenceValue !== undefined && isValue(referenceValue)) bounds.push(referenceValue);
200
+ if (variant === "bar") bounds.push(0);
201
+ let lo = min ?? (bounds.length ? Math.min(...bounds) : 0);
202
+ let hi = max ?? (bounds.length ? Math.max(...bounds) : 1);
203
+ if (hi === lo) {
204
+ // A constant series: centre it rather than divide by zero.
205
+ lo -= 1;
206
+ hi += 1;
207
+ }
208
+ const dot = strokeWidth + 1.5;
209
+ const pad = highlightLast && variant !== "bar" ? dot : strokeWidth / 2;
210
+ const clamp = (v: number) => Math.min(hi, Math.max(lo, v));
211
+ const y = (v: number) => pad + ((hi - clamp(v)) / (hi - lo)) * (H - 2 * pad);
212
+ const x = (i: number) => (n <= 1 ? W / 2 : pad + (i * (W - 2 * pad)) / (n - 1));
213
+ const r = (v: number) => Math.round(v * 100) / 100;
214
+
215
+ // Last DEFINED point — the series may well end in a gap. (No `findLastIndex`: the
216
+ // package targets ES2022.)
217
+ let lastIndex = -1;
218
+ for (let i = n - 1; i >= 0 && lastIndex < 0; i -= 1) if (points[i] !== null) lastIndex = i;
219
+ const lastValue = lastIndex >= 0 ? (points[lastIndex] ?? 0) : 0;
220
+ const lineClass = tone === "signed" ? signedClass(lastValue) : TONE_CLASS[tone];
221
+
222
+ // Contiguous runs between gaps. A run of one point is drawn as a dot, so a value
223
+ // isolated between two gaps is still visible.
224
+ const runs: Array<Array<[number, number]>> = [];
225
+ let run: Array<[number, number]> = [];
226
+ points.forEach((v, i) => {
227
+ if (v === null) {
228
+ if (run.length) runs.push(run);
229
+ run = [];
230
+ } else {
231
+ run.push([i, v]);
232
+ }
233
+ });
234
+ if (run.length) runs.push(run);
235
+
236
+ const linePath = (seg: Array<[number, number]>) =>
237
+ seg.map(([i, v], k) => `${k === 0 ? "M" : "L"}${r(x(i))} ${r(y(v))}`).join(" ");
238
+ const dotPath = (i: number, v: number) => `M${r(x(i))} ${r(y(v))} h0`;
239
+ // The area closes to zero when zero is on the scale, else to the bottom edge.
240
+ const areaBase = y(lo <= 0 && hi >= 0 ? 0 : lo);
241
+
242
+ const STROKE = {
243
+ fill: "none",
244
+ stroke: "currentColor",
245
+ strokeLinecap: "round",
246
+ strokeLinejoin: "round",
247
+ vectorEffect: "non-scaling-stroke",
248
+ } as const;
249
+
250
+ return (
251
+ <svg
252
+ role="img"
253
+ aria-label={name}
254
+ {...rest}
255
+ width={fluid ? "100%" : W}
256
+ height={H}
257
+ viewBox={`0 0 ${W} ${H}`}
258
+ preserveAspectRatio="none"
259
+ className={cn(fluid ? "block w-full" : "inline-block shrink-0 align-middle", "overflow-visible", className)}
260
+ style={style}
261
+ >
262
+ {referenceValue !== undefined && isValue(referenceValue) && referenceValue >= lo && referenceValue <= hi && (
263
+ <path
264
+ d={`M0 ${r(y(referenceValue))} H${W}`}
265
+ {...STROKE}
266
+ strokeWidth={1}
267
+ strokeDasharray="2 2"
268
+ className="text-[var(--border-strong)]"
269
+ />
270
+ )}
271
+
272
+ {variant === "bar"
273
+ ? (() => {
274
+ const slot = W / Math.max(n, 1);
275
+ const bw = Math.max(1, slot * 0.7);
276
+ const base = y(0);
277
+ return points.map((v, i) => {
278
+ if (v === null) return null;
279
+ const top = Math.min(y(v), base);
280
+ return (
281
+ <rect
282
+ key={i}
283
+ x={r(i * slot + (slot - bw) / 2)}
284
+ y={r(top)}
285
+ width={r(bw)}
286
+ height={r(Math.abs(y(v) - base))}
287
+ fill="currentColor"
288
+ opacity={highlightLast && i !== lastIndex ? 0.55 : 1}
289
+ className={tone === "signed" ? signedClass(v) : TONE_CLASS[tone]}
290
+ />
291
+ );
292
+ });
293
+ })()
294
+ : runs.map((seg, k) =>
295
+ seg.length === 1 ? (
296
+ <path
297
+ key={k}
298
+ d={dotPath(seg[0][0], seg[0][1])}
299
+ {...STROKE}
300
+ strokeWidth={strokeWidth * 2}
301
+ className={lineClass}
302
+ />
303
+ ) : (
304
+ <g key={k} className={lineClass}>
305
+ {variant === "area" && (
306
+ <path
307
+ d={`${linePath(seg)} L${r(x(seg[seg.length - 1][0]))} ${r(areaBase)} L${r(x(seg[0][0]))} ${r(areaBase)} Z`}
308
+ fill="currentColor"
309
+ fillOpacity={0.15}
310
+ stroke="none"
311
+ />
312
+ )}
313
+ <path d={linePath(seg)} {...STROKE} strokeWidth={strokeWidth} />
314
+ </g>
315
+ ),
316
+ )}
317
+
318
+ {highlightLast && variant !== "bar" && lastIndex >= 0 && (
319
+ <path
320
+ d={dotPath(lastIndex, lastValue)}
321
+ {...STROKE}
322
+ strokeWidth={dot * 2 - 1}
323
+ className={lineClass}
324
+ data-sparkline-last=""
325
+ />
326
+ )}
327
+ </svg>
328
+ );
329
+ }