@eifi1/ui-kit 0.4.0 → 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 +91 -20
  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 +96 -20
  63. package/dist/components/dropdown.js +77 -33
  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 +38 -6
  75. package/dist/components/file-dropzone.js +34 -9
  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 +28 -2
  105. package/dist/components/number-input.js +12 -2
  106. package/dist/components/number-input.js.map +1 -1
  107. package/dist/components/numpad-sheet.d.ts +32 -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 +15 -4
  176. package/dist/components/user-avatar.js +5 -4
  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 +27 -5
  270. package/dist/theme/palette-presets.js +231 -33
  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 +67 -14
  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,630 @@
1
+ import { forwardRef, useCallback, useEffect, useId, useImperativeHandle, useLayoutEffect, useRef, useState } from "react";
2
+ import type { ComponentPropsWithoutRef, PointerEvent as ReactPointerEvent, ReactNode } from "react";
3
+ import { Eraser, Keyboard, PenLine, Undo2 } from "lucide-react";
4
+ import { cn } from "../lib/cn";
5
+ import { useAnnounce } from "../hooks/use-announce";
6
+ import { useKitLabels } from "../i18n/kit-labels";
7
+ import { Button, FIELD_INVALID, Input } from "./ui";
8
+
9
+ /**
10
+ * A drawn signature, captured on a canvas and handed back as a PNG data URL.
11
+ *
12
+ * Kastlan wrote the first one (`features/handover/components/signature-pad.tsx`) for
13
+ * the handover protocol, where tenant and inspector each sign on the same phone. This
14
+ * keeps its contract — draw, get a PNG — and fixes what a copy per app would keep
15
+ * getting wrong:
16
+ *
17
+ * 1. **Strokes are data, not pixels.** Each stroke is kept as a list of points, and
18
+ * the canvas is a VIEW of them. That is what lets a resize (a phone rotating
19
+ * mid-signature) keep the drawing instead of wiping it — setting `canvas.width`
20
+ * clears the bitmap, so a pixels-only pad loses everything — and what makes undo
21
+ * one `pop()`. The pixel buffer is sized to `devicePixelRatio`, so a stroke is
22
+ * sharp on a 3× screen rather than upscaled from a 1× bitmap.
23
+ * 2. **Smooth, and pressure-aware.** Segments are quadratic curves through the
24
+ * midpoints between samples, so a fast stroke is a curve, not a polyline; a pen
25
+ * that reports pressure varies the width, a mouse or finger (whose `pressure` is a
26
+ * constant 0.5, or junk) draws at the base width.
27
+ * 3. **Two inks.** On screen the ink is the computed `color` of the canvas, which is
28
+ * `--text-primary` — so it reads in the dark theme, where a hard-coded near-black
29
+ * stroke vanished into the surface. The exported PNG is re-rendered from the same
30
+ * strokes in `exportInk` (near-black by default) on a transparent background. A
31
+ * signature is a DOCUMENT artefact: it is printed on the protocol, shown on a white
32
+ * card, embedded in a PDF — none of which follow the signer's theme. Exporting
33
+ * the on-screen ink would have produced a pale-grey-on-transparent PNG for every
34
+ * dark-mode signer, invisible on white paper.
35
+ *
36
+ * **Accessibility.** A canvas has no content a screen reader or a keyboard can reach,
37
+ * and no amount of ARIA changes that — drawing is a pointer act. What the pad does:
38
+ * the canvas is `role="img"`, named by the visible label and described by the
39
+ * instructions and by its state ("Nothing drawn yet" / "Signature drawn"), so a
40
+ * screen-reader user knows what the box is and whether it holds anything; undo and
41
+ * clear are real buttons, and their effect is announced. What it cannot do is let
42
+ * someone sign without a pointer — hence `allowTypedName`, which offers a typed name
43
+ * instead. It is OPT-IN, because whether a typed name counts as a signature is a
44
+ * product and legal decision, not a UI one (Kastlan's handover accepts one; a flow
45
+ * that needs a drawn mark would not). Where a drawn mark is not strictly required,
46
+ * turn it on.
47
+ */
48
+
49
+ /* ── Labels ──────────────────────────────────────────────────────────────── */
50
+
51
+ /** Every string the pad renders or speaks. */
52
+ export interface SignaturePadLabels {
53
+ /** Visible label above the pad, and the canvas's accessible name. The `label`
54
+ * prop overrides it per instance. */
55
+ label: string;
56
+ /** Under the label, and part of the canvas's description. */
57
+ instructions: string;
58
+ /** Appended to the instructions when `allowTypedName` is on. */
59
+ typedFallbackHint: string;
60
+ /** The canvas's state for a screen reader. */
61
+ empty: string;
62
+ signed: string;
63
+ undo: string;
64
+ clear: string;
65
+ /** Only rendered when `onSave` is given. */
66
+ save: string;
67
+ /** The mode switch (`allowTypedName`). */
68
+ useTyped: string;
69
+ useDrawn: string;
70
+ /** Label of the typed-name field. */
71
+ typedName: string;
72
+ /** Live-region messages after the matching button. */
73
+ cleared: string;
74
+ undone: string;
75
+ }
76
+
77
+ export const DEFAULT_SIGNATURE_PAD_LABELS: SignaturePadLabels = {
78
+ label: "Signature",
79
+ instructions: "Sign in the box with a mouse, your finger or a pen.",
80
+ typedFallbackHint: "If you cannot draw, type your name instead.",
81
+ empty: "Nothing drawn yet",
82
+ signed: "Signature drawn",
83
+ undo: "Undo last stroke",
84
+ clear: "Clear",
85
+ save: "Save signature",
86
+ useTyped: "Type name instead",
87
+ useDrawn: "Draw instead",
88
+ typedName: "Full name",
89
+ cleared: "Signature cleared",
90
+ undone: "Last stroke removed",
91
+ };
92
+
93
+ /* ── Types ───────────────────────────────────────────────────────────────── */
94
+
95
+ /** How the value handed back was made. */
96
+ export interface SignatureDetail {
97
+ method: "drawn" | "typed";
98
+ /** The name as typed, when `method` is `"typed"` — the PNG shows it, but a host
99
+ * that stores a typed name as TEXT (Kastlan does) wants the string. */
100
+ typedName?: string;
101
+ }
102
+
103
+ /** The imperative handle: `ref.current.toDataURL()` at submit time, for a form that
104
+ * does not want to mirror the value into state on every stroke. */
105
+ export interface SignaturePadHandle {
106
+ /** Remove every stroke and the typed name. Fires `onChange(null)`. */
107
+ clear: () => void;
108
+ /** The PNG, rendered in `exportInk` — or `null` while nothing is signed. */
109
+ toDataURL: () => string | null;
110
+ isEmpty: () => boolean;
111
+ }
112
+
113
+ export interface SignaturePadProps extends Omit<ComponentPropsWithoutRef<"div">, "onChange"> {
114
+ /** Visible label; defaults to `labels.label`. */
115
+ label?: ReactNode;
116
+ /** Replaces `labels.instructions` for this instance (e.g. a legal note). */
117
+ description?: ReactNode;
118
+ /**
119
+ * After every finished stroke, undo, clear and typed-name edit: the PNG, or `null`
120
+ * once the pad is empty. Not called mid-stroke — encoding a PNG per pointer move
121
+ * would stall the drawing on a phone.
122
+ */
123
+ onChange?: (dataUrl: string | null, detail: SignatureDetail) => void;
124
+ /** Renders a Save button, enabled once something is signed. */
125
+ onSave?: (dataUrl: string, detail: SignatureDetail) => void;
126
+ disabled?: boolean;
127
+ /** Paints the pad as wrong and sets `aria-invalid`. See {@link Input}'s `invalid`. */
128
+ invalid?: boolean;
129
+ /** Message under the pad; implies `invalid` and is attached with
130
+ * `aria-describedby`, like the kit's other fields. */
131
+ error?: ReactNode;
132
+ /** Offer a typed name as an alternative to drawing. See the accessibility note. */
133
+ allowTypedName?: boolean;
134
+ /** Ink of the exported PNG. Default near-black, legible on white. */
135
+ exportInk?: string;
136
+ /** Fill behind the exported PNG. Default transparent. */
137
+ exportBackground?: string;
138
+ /** Base stroke width in CSS pixels. Default 2.5. */
139
+ lineWidth?: number;
140
+ /** Classes for the canvas — its height, chiefly (default `h-40`). */
141
+ canvasClassName?: string;
142
+ /** User-facing strings; see {@link SignaturePadLabels}. */
143
+ labels?: Partial<SignaturePadLabels>;
144
+ }
145
+
146
+ /* ── Drawing ─────────────────────────────────────────────────────────────── */
147
+
148
+ interface Point {
149
+ x: number;
150
+ y: number;
151
+ /** 0–1. A constant 0.5 for anything that is not a pen. */
152
+ p: number;
153
+ }
154
+
155
+ type Stroke = Point[];
156
+
157
+ const DEFAULT_EXPORT_INK = "#111111";
158
+
159
+ // A handwriting face where the platform has one, `cursive` everywhere else. Only
160
+ // ever used to render a typed name onto the canvas.
161
+ const SCRIPT_FONT = `"Segoe Script", "Bradley Hand", "Brush Script MT", cursive`;
162
+
163
+ /** Width multiplier from pressure: 0.5 (the no-pen constant) is exactly 1. */
164
+ const pressureWidth = (p: number) => 0.4 + 1.2 * p;
165
+
166
+ const mid = (a: Point, b: Point): Point => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, p: (a.p + b.p) / 2 });
167
+
168
+ /**
169
+ * Segment `i` (1 ≤ i < n) of a stroke: a quadratic from the midpoint before sample
170
+ * `i - 1` to the midpoint after it, with the sample as the control point. The live
171
+ * drawing and the full repaint both go through here, so a stroke looks the same
172
+ * before and after a resize.
173
+ */
174
+ function drawSegment(ctx: CanvasRenderingContext2D, pts: Stroke, i: number, scale: number, width: number) {
175
+ const a = i === 1 ? pts[0] : mid(pts[i - 2], pts[i - 1]);
176
+ const c = pts[i - 1];
177
+ const b = mid(pts[i - 1], pts[i]);
178
+ ctx.lineWidth = width * scale * pressureWidth((a.p + b.p) / 2);
179
+ ctx.beginPath();
180
+ ctx.moveTo(a.x * scale, a.y * scale);
181
+ ctx.quadraticCurveTo(c.x * scale, c.y * scale, b.x * scale, b.y * scale);
182
+ ctx.stroke();
183
+ }
184
+
185
+ /** The last half-segment, from the final midpoint to the final sample. Drawn once the
186
+ * stroke is finished — while drawing, the pen is still there and it would be redrawn
187
+ * on every move. A single tap is a dot. */
188
+ function drawTail(ctx: CanvasRenderingContext2D, pts: Stroke, scale: number, width: number) {
189
+ const last = pts[pts.length - 1];
190
+ if (pts.length === 1) {
191
+ ctx.beginPath();
192
+ ctx.arc(last.x * scale, last.y * scale, (width * scale * pressureWidth(last.p)) / 2, 0, Math.PI * 2);
193
+ ctx.fill();
194
+ return;
195
+ }
196
+ const from = pts.length === 2 ? pts[0] : mid(pts[pts.length - 2], last);
197
+ ctx.lineWidth = width * scale * pressureWidth(last.p);
198
+ ctx.beginPath();
199
+ ctx.moveTo(from.x * scale, from.y * scale);
200
+ ctx.lineTo(last.x * scale, last.y * scale);
201
+ ctx.stroke();
202
+ }
203
+
204
+ function prepare(ctx: CanvasRenderingContext2D, ink: string, dpr: number) {
205
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
206
+ ctx.lineCap = "round";
207
+ ctx.lineJoin = "round";
208
+ ctx.strokeStyle = ink;
209
+ ctx.fillStyle = ink;
210
+ }
211
+
212
+ /** A typed name, as large as fits, on the pad's baseline. */
213
+ function drawName(ctx: CanvasRenderingContext2D, name: string, w: number, h: number) {
214
+ let size = Math.min(48, h * 0.45);
215
+ ctx.font = `italic ${size}px ${SCRIPT_FONT}`;
216
+ const room = w * 0.85;
217
+ const measured = ctx.measureText(name).width;
218
+ if (measured > room) {
219
+ size = Math.max(12, (size * room) / measured);
220
+ ctx.font = `italic ${size}px ${SCRIPT_FONT}`;
221
+ }
222
+ ctx.textAlign = "center";
223
+ ctx.textBaseline = "alphabetic";
224
+ ctx.fillText(name, w / 2, h * 0.68);
225
+ }
226
+
227
+ /* ── The component ───────────────────────────────────────────────────────── */
228
+
229
+ export const SignaturePad = forwardRef<SignaturePadHandle, SignaturePadProps>(function SignaturePad(
230
+ {
231
+ label,
232
+ description,
233
+ onChange,
234
+ onSave,
235
+ disabled,
236
+ invalid,
237
+ error,
238
+ allowTypedName,
239
+ exportInk = DEFAULT_EXPORT_INK,
240
+ exportBackground,
241
+ lineWidth = 2.5,
242
+ canvasClassName,
243
+ labels: labelsProp,
244
+ className,
245
+ ...rest
246
+ },
247
+ ref,
248
+ ) {
249
+ // Prop > <UiKitProvider> > English, like every labelled component in the kit.
250
+ const labels = useKitLabels("signaturePad", DEFAULT_SIGNATURE_PAD_LABELS, labelsProp);
251
+ const { announce, regionProps } = useAnnounce();
252
+ const labelId = useId();
253
+ const descId = useId();
254
+ const statusId = useId();
255
+ const errorId = useId();
256
+
257
+ const canvasRef = useRef<HTMLCanvasElement>(null);
258
+ /**
259
+ * What is on the pad lives in a ref, not in state: a pointer move must not
260
+ * re-render, and `onChange` has to see the pad AFTER a change, from inside the
261
+ * handler that made it — before React has rendered anything. The state below
262
+ * mirrors the parts the markup needs (empty or not, which mode, the field's text).
263
+ */
264
+ const scene = useRef<{ strokes: Stroke[]; mode: "drawn" | "typed"; name: string }>({
265
+ strokes: [],
266
+ mode: "drawn",
267
+ name: "",
268
+ });
269
+ const live = useRef<{ id: number; stroke: Stroke } | null>(null);
270
+ /**
271
+ * The canvas width, in CSS px, the stored points are relative to. Fixed by the
272
+ * first stroke; a later resize scales the drawing UNIFORMLY by `width / refWidth`,
273
+ * so a rotated phone shows the same signature larger or smaller, never stretched.
274
+ */
275
+ const refWidth = useRef(0);
276
+ const [strokeCount, setStrokeCount] = useState(0);
277
+ const [mode, setMode] = useState<"drawn" | "typed">("drawn");
278
+ const [typedName, setTypedName] = useState("");
279
+
280
+ const typed = mode === "typed";
281
+ const isEmpty = typed ? typedName.trim() === "" : strokeCount === 0;
282
+
283
+ // Everything the paint and export paths read that can change between renders, so
284
+ // the observers below subscribe once instead of on every keystroke.
285
+ const opts = useRef({ lineWidth, exportInk, exportBackground });
286
+ useEffect(() => {
287
+ opts.current = { lineWidth, exportInk, exportBackground };
288
+ }, [lineWidth, exportInk, exportBackground]);
289
+
290
+ const sceneEmpty = () => {
291
+ const s = scene.current;
292
+ return s.mode === "typed" ? s.name.trim() === "" : s.strokes.length === 0;
293
+ };
294
+
295
+ /** The whole scene, painted into `ctx` at `w`×`h` CSS px. */
296
+ const paintInto = useCallback((ctx: CanvasRenderingContext2D, ink: string, w: number, h: number, dpr: number) => {
297
+ const { lineWidth: width } = opts.current;
298
+ const s = scene.current;
299
+ prepare(ctx, ink, dpr);
300
+ if (s.mode === "typed") {
301
+ if (s.name.trim()) drawName(ctx, s.name.trim(), w, h);
302
+ return;
303
+ }
304
+ const scale = refWidth.current > 0 ? w / refWidth.current : 1;
305
+ for (const stroke of s.strokes) {
306
+ for (let i = 1; i < stroke.length; i += 1) drawSegment(ctx, stroke, i, scale, width);
307
+ drawTail(ctx, stroke, scale, width);
308
+ }
309
+ const pending = live.current?.stroke;
310
+ if (pending) for (let i = 1; i < pending.length; i += 1) drawSegment(ctx, pending, i, scale, width);
311
+ }, []);
312
+
313
+ /** The ink on screen: the canvas's own computed `color`, i.e. `--text-primary`. */
314
+ const screenInk = (canvas: HTMLCanvasElement) =>
315
+ // Read as `color` rather than as the custom property: `--text-primary` is whatever
316
+ // the palette wrote (a `color-mix()` in some presets), which a 2D context may not
317
+ // parse; `color` has already been resolved by the browser.
318
+ getComputedStyle(canvas).color || opts.current.exportInk;
319
+
320
+ /** Repaint the visible canvas from the scene, in the theme's ink. */
321
+ const repaint = useCallback(() => {
322
+ const canvas = canvasRef.current;
323
+ const ctx = canvas?.getContext("2d");
324
+ if (!canvas || !ctx) return;
325
+ const dpr = window.devicePixelRatio || 1;
326
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
327
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
328
+ paintInto(ctx, screenInk(canvas), canvas.width / dpr, canvas.height / dpr, dpr);
329
+ }, [paintInto]);
330
+
331
+ /** Size the bitmap to the element × devicePixelRatio, then repaint. */
332
+ const resize = useCallback(() => {
333
+ const canvas = canvasRef.current;
334
+ if (!canvas) return;
335
+ const rect = canvas.getBoundingClientRect();
336
+ const dpr = window.devicePixelRatio || 1;
337
+ const w = Math.round(rect.width * dpr);
338
+ const h = Math.round(rect.height * dpr);
339
+ // Assigning `width` clears the bitmap even when the value is unchanged, so only
340
+ // when it moved — and the repaint puts the strokes back either way.
341
+ if (canvas.width !== w) canvas.width = w;
342
+ if (canvas.height !== h) canvas.height = h;
343
+ repaint();
344
+ }, [repaint]);
345
+
346
+ useLayoutEffect(() => {
347
+ resize();
348
+ }, [resize]);
349
+
350
+ // A changed base width redraws what is already there.
351
+ useEffect(() => {
352
+ repaint();
353
+ }, [lineWidth, repaint]);
354
+
355
+ useEffect(() => {
356
+ const canvas = canvasRef.current;
357
+ if (!canvas) return;
358
+ const cleanups: Array<() => void> = [];
359
+ if (typeof ResizeObserver !== "undefined") {
360
+ const ro = new ResizeObserver(() => resize());
361
+ ro.observe(canvas);
362
+ cleanups.push(() => ro.disconnect());
363
+ }
364
+ // The ink follows the theme, and the theme is a class (`.dark`) or inline
365
+ // variables (the palette store) on <html> — neither of which tells a canvas.
366
+ if (typeof MutationObserver !== "undefined") {
367
+ const mo = new MutationObserver(() => repaint());
368
+ mo.observe(document.documentElement, { attributes: true, attributeFilter: ["class", "style", "data-theme"] });
369
+ cleanups.push(() => mo.disconnect());
370
+ }
371
+ const mq = typeof window.matchMedia === "function" ? window.matchMedia("(prefers-color-scheme: dark)") : null;
372
+ if (mq?.addEventListener) {
373
+ mq.addEventListener("change", repaint);
374
+ cleanups.push(() => mq.removeEventListener("change", repaint));
375
+ }
376
+ return () => cleanups.forEach((fn) => fn());
377
+ }, [resize, repaint]);
378
+
379
+ /** The PNG, re-rendered off-screen in the export ink — `null` while empty. */
380
+ const exportPng = useCallback((): string | null => {
381
+ const canvas = canvasRef.current;
382
+ if (!canvas || sceneEmpty()) return null;
383
+ const { exportInk: ink, exportBackground: bg } = opts.current;
384
+ const dpr = window.devicePixelRatio || 1;
385
+ const out = document.createElement("canvas");
386
+ out.width = canvas.width;
387
+ out.height = canvas.height;
388
+ const ctx = out.getContext("2d");
389
+ if (!ctx) return null;
390
+ if (bg) {
391
+ ctx.fillStyle = bg;
392
+ ctx.fillRect(0, 0, out.width, out.height);
393
+ }
394
+ paintInto(ctx, ink, out.width / dpr, out.height / dpr, dpr);
395
+ return out.toDataURL("image/png");
396
+ }, [paintInto]);
397
+
398
+ const detail = (): SignatureDetail => {
399
+ const s = scene.current;
400
+ return s.mode === "typed" ? { method: "typed", typedName: s.name.trim() } : { method: "drawn" };
401
+ };
402
+
403
+ /** After every change to the scene: sync the markup's mirror, repaint, tell the host. */
404
+ const onChangeRef = useRef(onChange);
405
+ useEffect(() => {
406
+ onChangeRef.current = onChange;
407
+ }, [onChange]);
408
+ const commit = () => {
409
+ const s = scene.current;
410
+ setStrokeCount(s.strokes.length);
411
+ setMode(s.mode);
412
+ setTypedName(s.name);
413
+ repaint();
414
+ onChangeRef.current?.(exportPng(), detail());
415
+ };
416
+
417
+ const clearScene = () => {
418
+ scene.current = { ...scene.current, strokes: [], name: "" };
419
+ live.current = null;
420
+ refWidth.current = 0;
421
+ commit();
422
+ };
423
+
424
+ useImperativeHandle(ref, () => ({ clear: clearScene, toDataURL: exportPng, isEmpty: sceneEmpty }));
425
+
426
+ /* ── Pointer ── */
427
+
428
+ const toPoint = (e: { clientX: number; clientY: number; pressure: number; pointerType: string }, rect: DOMRect): Point => {
429
+ const scale = refWidth.current > 0 && rect.width > 0 ? rect.width / refWidth.current : 1;
430
+ return {
431
+ x: (e.clientX - rect.left) / scale,
432
+ y: (e.clientY - rect.top) / scale,
433
+ // Only a pen's pressure means anything: a mouse reports 0.5 while pressed and
434
+ // touch hardware reports 0, 1 or noise.
435
+ p: e.pointerType === "pen" && e.pressure > 0 ? e.pressure : 0.5,
436
+ };
437
+ };
438
+
439
+ const onPointerDown = (e: ReactPointerEvent<HTMLCanvasElement>) => {
440
+ // One stroke at a time: a palm resting beside the pen is a second pointer.
441
+ if (disabled || typed || live.current) return;
442
+ if (e.pointerType === "mouse" && e.button !== 0) return;
443
+ e.preventDefault();
444
+ const canvas = e.currentTarget;
445
+ canvas.setPointerCapture?.(e.pointerId);
446
+ const rect = canvas.getBoundingClientRect();
447
+ if (refWidth.current === 0) refWidth.current = rect.width || 1;
448
+ live.current = { id: e.pointerId, stroke: [toPoint(e, rect)] };
449
+ };
450
+
451
+ const onPointerMove = (e: ReactPointerEvent<HTMLCanvasElement>) => {
452
+ const pending = live.current;
453
+ if (!pending || pending.id !== e.pointerId) return;
454
+ e.preventDefault();
455
+ const canvas = e.currentTarget;
456
+ const rect = canvas.getBoundingClientRect();
457
+ // Coalesced events: the samples the browser merged into this one move. A fast
458
+ // flick on a 120 Hz pen is otherwise a handful of long straight chords.
459
+ const samples = e.nativeEvent.getCoalescedEvents?.() ?? [];
460
+ const events = samples.length > 0 ? samples : [e];
461
+ const ctx = canvas.getContext("2d");
462
+ if (ctx) prepare(ctx, screenInk(canvas), window.devicePixelRatio || 1);
463
+ const scale = refWidth.current > 0 && rect.width > 0 ? rect.width / refWidth.current : 1;
464
+ for (const ev of events) {
465
+ pending.stroke.push(toPoint(ev, rect));
466
+ // Only the newest segment — a full repaint per move is O(ink) on every event.
467
+ if (ctx) drawSegment(ctx, pending.stroke, pending.stroke.length - 1, scale, opts.current.lineWidth);
468
+ }
469
+ };
470
+
471
+ const endStroke = (e: ReactPointerEvent<HTMLCanvasElement>) => {
472
+ const pending = live.current;
473
+ if (!pending || pending.id !== e.pointerId) return;
474
+ e.currentTarget.releasePointerCapture?.(e.pointerId);
475
+ live.current = null;
476
+ scene.current = { ...scene.current, strokes: [...scene.current.strokes, pending.stroke] };
477
+ commit();
478
+ };
479
+
480
+ /* ── Buttons ── */
481
+
482
+ const undo = () => {
483
+ if (disabled || scene.current.strokes.length === 0) return;
484
+ const strokes = scene.current.strokes.slice(0, -1);
485
+ if (strokes.length === 0) refWidth.current = 0;
486
+ scene.current = { ...scene.current, strokes };
487
+ commit();
488
+ announce(labels.undone);
489
+ };
490
+
491
+ const clear = () => {
492
+ if (disabled || sceneEmpty()) return;
493
+ clearScene();
494
+ announce(labels.cleared);
495
+ };
496
+
497
+ const switchMode = () => {
498
+ if (disabled) return;
499
+ scene.current = { ...scene.current, mode: scene.current.mode === "drawn" ? "typed" : "drawn" };
500
+ commit();
501
+ };
502
+
503
+ const onTypedChange = (name: string) => {
504
+ scene.current = { ...scene.current, name };
505
+ commit();
506
+ };
507
+
508
+ const save = () => {
509
+ if (disabled) return;
510
+ const png = exportPng();
511
+ if (png) onSave?.(png, detail());
512
+ };
513
+
514
+ const hasError = error !== undefined && error !== null && error !== false && error !== "";
515
+ const isInvalid = Boolean(invalid) || hasError;
516
+ const describedBy = [descId, statusId, hasError ? errorId : null].filter(Boolean).join(" ");
517
+
518
+ // `aria-disabled` rather than `disabled` on undo/clear: they disable THEMSELVES
519
+ // (clear empties the pad, the last undo does too), and a natively disabled button
520
+ // drops keyboard focus to <body> the moment it is activated.
521
+ const softDisabled = "aria-disabled:cursor-not-allowed aria-disabled:opacity-50";
522
+
523
+ return (
524
+ <div {...rest} className={cn("relative space-y-1.5", className)} data-empty={isEmpty || undefined}>
525
+ <div>
526
+ <span id={labelId} className="block text-sm font-medium text-[var(--text-primary)]">
527
+ {label ?? labels.label}
528
+ </span>
529
+ <p id={descId} className="text-xs text-[var(--text-muted)]">
530
+ {description ?? labels.instructions}
531
+ {allowTypedName && !typed ? ` ${labels.typedFallbackHint}` : null}
532
+ </p>
533
+ </div>
534
+
535
+ <div className="relative">
536
+ <canvas
537
+ ref={canvasRef}
538
+ role="img"
539
+ aria-labelledby={labelId}
540
+ aria-describedby={describedBy}
541
+ // No `aria-invalid` / `aria-disabled`: `img` supports neither. The error
542
+ // reaches a screen reader through `aria-describedby`; the attributes below
543
+ // are for styling and tests.
544
+ data-invalid={isInvalid || undefined}
545
+ data-disabled={disabled || undefined}
546
+ onPointerDown={onPointerDown}
547
+ onPointerMove={onPointerMove}
548
+ onPointerUp={endStroke}
549
+ onPointerCancel={endStroke}
550
+ className={cn(
551
+ // `touch-none` stops the page scrolling under a finger that is signing.
552
+ "block h-40 w-full touch-none select-none rounded-md border border-[var(--border)] bg-[var(--bg-surface)] text-[var(--text-primary)]",
553
+ typed || disabled ? "cursor-default" : "cursor-crosshair",
554
+ disabled && "opacity-60",
555
+ isInvalid && FIELD_INVALID,
556
+ canvasClassName,
557
+ )}
558
+ />
559
+ {/* The signing line — where people expect to sign. Decoration only. */}
560
+ <div
561
+ aria-hidden="true"
562
+ className="pointer-events-none absolute inset-x-6 bottom-8 border-b border-dashed border-[var(--border-strong)]"
563
+ />
564
+ <span id={statusId} className="sr-only">
565
+ {isEmpty ? labels.empty : labels.signed}
566
+ </span>
567
+ </div>
568
+
569
+ {typed && (
570
+ <Input
571
+ label={labels.typedName}
572
+ value={typedName}
573
+ autoComplete="name"
574
+ disabled={disabled}
575
+ onChange={(e) => onTypedChange(e.target.value)}
576
+ />
577
+ )}
578
+
579
+ <div className="flex flex-wrap items-center gap-2">
580
+ {!typed && (
581
+ <Button
582
+ type="button"
583
+ variant="ghost"
584
+ onClick={undo}
585
+ aria-disabled={disabled || strokeCount === 0 || undefined}
586
+ className={softDisabled}
587
+ >
588
+ <Undo2 aria-hidden="true" className="size-4" />
589
+ {labels.undo}
590
+ </Button>
591
+ )}
592
+ <Button
593
+ type="button"
594
+ variant="ghost"
595
+ onClick={clear}
596
+ aria-disabled={disabled || isEmpty || undefined}
597
+ className={softDisabled}
598
+ >
599
+ <Eraser aria-hidden="true" className="size-4" />
600
+ {labels.clear}
601
+ </Button>
602
+ {allowTypedName && (
603
+ <Button
604
+ type="button"
605
+ variant="secondary"
606
+ onClick={switchMode}
607
+ disabled={disabled}
608
+ >
609
+ {typed ? <PenLine aria-hidden="true" className="size-4" /> : <Keyboard aria-hidden="true" className="size-4" />}
610
+ {typed ? labels.useDrawn : labels.useTyped}
611
+ </Button>
612
+ )}
613
+ {onSave && (
614
+ <Button type="button" variant="brand" onClick={save} disabled={disabled || isEmpty} className="ms-auto">
615
+ {labels.save}
616
+ </Button>
617
+ )}
618
+ </div>
619
+
620
+ {hasError && (
621
+ <p id={errorId} className="mt-1 text-[11px] leading-tight text-[var(--danger)]">
622
+ {error}
623
+ </p>
624
+ )}
625
+ {/* Rendered from the start and never unmounted — see useAnnounce. */}
626
+ <div {...regionProps} />
627
+ </div>
628
+ );
629
+ });
630
+ SignaturePad.displayName = "SignaturePad";