velvetkit 0.2.2 → 0.4.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 (458) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -0
  3. data/README.md +30 -8
  4. data/lib/generators/velvetkit/add/add_generator.rb +77 -0
  5. data/lib/generators/velvetkit/helpers.rb +29 -2
  6. data/lib/generators/velvetkit/icons/icons_generator.rb +71 -0
  7. data/lib/generators/velvetkit/install/install_generator.rb +9 -12
  8. data/lib/velvetkit/icons.rb +148 -0
  9. data/lib/velvetkit/registry.rb +5 -2
  10. data/lib/velvetkit/version.rb +4 -1
  11. data/lib/velvetkit.rb +8 -0
  12. data/registry/app/assets/tailwind/velvetkit.css +90 -155
  13. data/registry/app/components/ui/alert.rb +1 -1
  14. data/registry/app/components/ui/chart.html.erb +47 -0
  15. data/registry/app/components/ui/chart.rb +177 -0
  16. data/registry/app/components/ui/command/group.rb +27 -0
  17. data/registry/app/components/ui/command/item.rb +64 -0
  18. data/registry/app/components/ui/command.html.erb +41 -0
  19. data/registry/app/components/ui/command.rb +122 -0
  20. data/registry/app/components/ui/confirm_dialog.html.erb +13 -0
  21. data/registry/app/components/ui/confirm_dialog.rb +55 -0
  22. data/registry/app/components/ui/date_picker.html.erb +58 -64
  23. data/registry/app/components/ui/date_picker.rb +199 -75
  24. data/registry/app/components/ui/empty_state.html.erb +20 -0
  25. data/registry/app/components/ui/empty_state.rb +46 -0
  26. data/registry/app/components/ui/file_field.html.erb +14 -4
  27. data/registry/app/components/ui/file_field.rb +179 -28
  28. data/registry/app/components/ui/gallery/image.rb +20 -0
  29. data/registry/app/components/ui/gallery.html.erb +52 -0
  30. data/registry/app/components/ui/gallery.rb +164 -0
  31. data/registry/app/components/ui/icon.html.erb +1 -1
  32. data/registry/app/components/ui/icon.rb +80 -8
  33. data/registry/app/components/ui/kbd.html.erb +27 -0
  34. data/registry/app/components/ui/kbd.rb +134 -0
  35. data/registry/app/components/ui/number_input.html.erb +11 -0
  36. data/registry/app/components/ui/number_input.rb +103 -0
  37. data/registry/app/components/ui/password_input.html.erb +1 -1
  38. data/registry/app/components/ui/progress/segment.rb +25 -0
  39. data/registry/app/components/ui/progress.html.erb +25 -0
  40. data/registry/app/components/ui/progress.rb +182 -0
  41. data/registry/app/components/ui/select.html.erb +24 -5
  42. data/registry/app/components/ui/select.rb +44 -5
  43. data/registry/app/components/ui/stepper/step.html.erb +27 -0
  44. data/registry/app/components/ui/stepper/step.rb +213 -0
  45. data/registry/app/components/ui/stepper.html.erb +19 -0
  46. data/registry/app/components/ui/stepper.rb +145 -0
  47. data/registry/app/components/ui/theme_toggle.rb +2 -4
  48. data/registry/app/components/ui/timeline/item.html.erb +21 -0
  49. data/registry/app/components/ui/timeline/item.rb +145 -0
  50. data/registry/app/components/ui/timeline.html.erb +1 -0
  51. data/registry/app/components/ui/timeline.rb +42 -0
  52. data/registry/app/components/ui/toast.rb +6 -6
  53. data/registry/app/components/ui/toggle_group/item.html.erb +1 -0
  54. data/registry/app/components/ui/toggle_group/item.rb +85 -0
  55. data/registry/app/components/ui/toggle_group.html.erb +1 -0
  56. data/registry/app/components/ui/toggle_group.rb +79 -0
  57. data/registry/app/javascript/chart_js.ts +37 -0
  58. data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
  59. data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
  60. data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
  61. data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
  62. data/registry/app/javascript/controllers/ui/confirm_dialog_controller.ts +110 -0
  63. data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
  64. data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
  65. data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
  66. data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
  67. data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
  68. data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
  69. data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
  70. data/registry/app/javascript/controllers/ui/stepper_controller.ts +187 -0
  71. data/registry/app/javascript/vanilla_calendar.ts +15 -0
  72. data/registry/components.yml +28 -5
  73. data/registry/config/locales/velvetkit.en.yml +84 -0
  74. data/registry/docs/agents.md +7 -4
  75. data/registry/icons/lucide/LICENSE +39 -0
  76. data/registry/icons/lucide/align-center.svg +17 -0
  77. data/registry/icons/lucide/align-left.svg +17 -0
  78. data/registry/icons/lucide/align-right.svg +17 -0
  79. data/registry/icons/lucide/arrow-left.svg +16 -0
  80. data/registry/icons/lucide/arrow-right.svg +16 -0
  81. data/registry/icons/lucide/badge-check.svg +16 -0
  82. data/registry/icons/lucide/bar-chart-3.svg +18 -0
  83. data/registry/icons/lucide/bell.svg +16 -0
  84. data/registry/icons/lucide/bold.svg +15 -0
  85. data/registry/icons/lucide/book-open.svg +16 -0
  86. data/registry/icons/lucide/calendar.svg +18 -0
  87. data/registry/icons/lucide/chart-column.svg +18 -0
  88. data/registry/icons/lucide/check.svg +15 -0
  89. data/registry/icons/lucide/chevron-down.svg +15 -0
  90. data/registry/icons/lucide/chevron-left.svg +15 -0
  91. data/registry/icons/lucide/chevron-right.svg +15 -0
  92. data/registry/icons/lucide/chevrons-up-down.svg +16 -0
  93. data/registry/icons/lucide/circle-alert.svg +17 -0
  94. data/registry/icons/lucide/circle-check.svg +16 -0
  95. data/registry/icons/lucide/cloud.svg +15 -0
  96. data/registry/icons/lucide/credit-card.svg +16 -0
  97. data/registry/icons/lucide/download.svg +17 -0
  98. data/registry/icons/lucide/ellipsis-vertical.svg +17 -0
  99. data/registry/icons/lucide/ellipsis.svg +17 -0
  100. data/registry/icons/lucide/eye-off.svg +18 -0
  101. data/registry/icons/lucide/eye.svg +16 -0
  102. data/registry/icons/lucide/file-plus.svg +18 -0
  103. data/registry/icons/lucide/file-text.svg +19 -0
  104. data/registry/icons/lucide/file.svg +16 -0
  105. data/registry/icons/lucide/folder-kanban.svg +18 -0
  106. data/registry/icons/lucide/folder.svg +15 -0
  107. data/registry/icons/lucide/git-branch.svg +18 -0
  108. data/registry/icons/lucide/globe.svg +17 -0
  109. data/registry/icons/lucide/graduation-cap.svg +17 -0
  110. data/registry/icons/lucide/grip-vertical.svg +20 -0
  111. data/registry/icons/lucide/home.svg +16 -0
  112. data/registry/icons/lucide/inbox.svg +16 -0
  113. data/registry/icons/lucide/info.svg +17 -0
  114. data/registry/icons/lucide/italic.svg +17 -0
  115. data/registry/icons/lucide/keyboard.svg +23 -0
  116. data/registry/icons/lucide/layout-dashboard.svg +18 -0
  117. data/registry/icons/lucide/layout-grid.svg +18 -0
  118. data/registry/icons/lucide/life-buoy.svg +20 -0
  119. data/registry/icons/lucide/list.svg +20 -0
  120. data/registry/icons/lucide/loader.svg +22 -0
  121. data/registry/icons/lucide/lock.svg +16 -0
  122. data/registry/icons/lucide/log-out.svg +17 -0
  123. data/registry/icons/lucide/mail.svg +16 -0
  124. data/registry/icons/lucide/menu.svg +17 -0
  125. data/registry/icons/lucide/message-circle.svg +15 -0
  126. data/registry/icons/lucide/minus.svg +15 -0
  127. data/registry/icons/lucide/monitor.svg +17 -0
  128. data/registry/icons/lucide/moon.svg +15 -0
  129. data/registry/icons/lucide/newspaper.svg +18 -0
  130. data/registry/icons/lucide/package.svg +18 -0
  131. data/registry/icons/lucide/panel-left.svg +16 -0
  132. data/registry/icons/lucide/pause.svg +16 -0
  133. data/registry/icons/lucide/percent.svg +17 -0
  134. data/registry/icons/lucide/plus.svg +16 -0
  135. data/registry/icons/lucide/printer.svg +17 -0
  136. data/registry/icons/lucide/qr-code.svg +26 -0
  137. data/registry/icons/lucide/receipt.svg +17 -0
  138. data/registry/icons/lucide/rocket.svg +18 -0
  139. data/registry/icons/lucide/rotate-cw.svg +16 -0
  140. data/registry/icons/lucide/search.svg +16 -0
  141. data/registry/icons/lucide/settings.svg +16 -0
  142. data/registry/icons/lucide/shield-check.svg +16 -0
  143. data/registry/icons/lucide/shield.svg +15 -0
  144. data/registry/icons/lucide/store.svg +17 -0
  145. data/registry/icons/lucide/sun-moon.svg +19 -0
  146. data/registry/icons/lucide/sun.svg +23 -0
  147. data/registry/icons/lucide/trash-2.svg +19 -0
  148. data/registry/icons/lucide/truck.svg +19 -0
  149. data/registry/icons/lucide/underline.svg +16 -0
  150. data/registry/icons/lucide/upload.svg +17 -0
  151. data/registry/icons/lucide/user-plus.svg +18 -0
  152. data/registry/icons/lucide/user.svg +16 -0
  153. data/registry/icons/lucide/users.svg +18 -0
  154. data/registry/icons/lucide/workflow.svg +17 -0
  155. data/registry/icons/lucide/x.svg +16 -0
  156. data/registry/icons/lucide/zoom-in.svg +18 -0
  157. data/registry/icons/lucide/zoom-out.svg +17 -0
  158. data/registry/icons/phosphor/LICENSE +21 -0
  159. data/registry/icons/phosphor/arrow-clockwise.svg +2 -0
  160. data/registry/icons/phosphor/arrow-left.svg +2 -0
  161. data/registry/icons/phosphor/arrow-right.svg +2 -0
  162. data/registry/icons/phosphor/bell.svg +2 -0
  163. data/registry/icons/phosphor/book-open.svg +2 -0
  164. data/registry/icons/phosphor/calendar-blank.svg +2 -0
  165. data/registry/icons/phosphor/caret-down.svg +2 -0
  166. data/registry/icons/phosphor/caret-left.svg +2 -0
  167. data/registry/icons/phosphor/caret-right.svg +2 -0
  168. data/registry/icons/phosphor/caret-up-down.svg +2 -0
  169. data/registry/icons/phosphor/chart-bar.svg +2 -0
  170. data/registry/icons/phosphor/chat-circle.svg +2 -0
  171. data/registry/icons/phosphor/check-circle.svg +2 -0
  172. data/registry/icons/phosphor/check.svg +2 -0
  173. data/registry/icons/phosphor/circle-half.svg +2 -0
  174. data/registry/icons/phosphor/cloud.svg +2 -0
  175. data/registry/icons/phosphor/credit-card.svg +2 -0
  176. data/registry/icons/phosphor/desktop.svg +2 -0
  177. data/registry/icons/phosphor/dots-six-vertical.svg +2 -0
  178. data/registry/icons/phosphor/dots-three-vertical.svg +2 -0
  179. data/registry/icons/phosphor/dots-three.svg +2 -0
  180. data/registry/icons/phosphor/download-simple.svg +2 -0
  181. data/registry/icons/phosphor/envelope.svg +2 -0
  182. data/registry/icons/phosphor/eye-slash.svg +2 -0
  183. data/registry/icons/phosphor/eye.svg +2 -0
  184. data/registry/icons/phosphor/file-plus.svg +2 -0
  185. data/registry/icons/phosphor/file-text.svg +2 -0
  186. data/registry/icons/phosphor/file.svg +2 -0
  187. data/registry/icons/phosphor/flow-arrow.svg +2 -0
  188. data/registry/icons/phosphor/folder.svg +2 -0
  189. data/registry/icons/phosphor/gear.svg +2 -0
  190. data/registry/icons/phosphor/git-branch.svg +2 -0
  191. data/registry/icons/phosphor/globe.svg +2 -0
  192. data/registry/icons/phosphor/graduation-cap.svg +2 -0
  193. data/registry/icons/phosphor/grid-four.svg +2 -0
  194. data/registry/icons/phosphor/house.svg +2 -0
  195. data/registry/icons/phosphor/info.svg +2 -0
  196. data/registry/icons/phosphor/kanban.svg +2 -0
  197. data/registry/icons/phosphor/keyboard.svg +2 -0
  198. data/registry/icons/phosphor/lifebuoy.svg +2 -0
  199. data/registry/icons/phosphor/list.svg +2 -0
  200. data/registry/icons/phosphor/lock.svg +2 -0
  201. data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
  202. data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
  203. data/registry/icons/phosphor/magnifying-glass.svg +2 -0
  204. data/registry/icons/phosphor/minus.svg +2 -0
  205. data/registry/icons/phosphor/moon.svg +2 -0
  206. data/registry/icons/phosphor/newspaper.svg +2 -0
  207. data/registry/icons/phosphor/package.svg +2 -0
  208. data/registry/icons/phosphor/pause.svg +2 -0
  209. data/registry/icons/phosphor/percent.svg +2 -0
  210. data/registry/icons/phosphor/plus.svg +2 -0
  211. data/registry/icons/phosphor/printer.svg +2 -0
  212. data/registry/icons/phosphor/qr-code.svg +2 -0
  213. data/registry/icons/phosphor/receipt.svg +2 -0
  214. data/registry/icons/phosphor/rocket.svg +2 -0
  215. data/registry/icons/phosphor/seal-check.svg +2 -0
  216. data/registry/icons/phosphor/shield-check.svg +2 -0
  217. data/registry/icons/phosphor/shield.svg +2 -0
  218. data/registry/icons/phosphor/sidebar.svg +2 -0
  219. data/registry/icons/phosphor/sign-out.svg +2 -0
  220. data/registry/icons/phosphor/spinner.svg +2 -0
  221. data/registry/icons/phosphor/squares-four.svg +2 -0
  222. data/registry/icons/phosphor/storefront.svg +2 -0
  223. data/registry/icons/phosphor/sun.svg +2 -0
  224. data/registry/icons/phosphor/text-align-center.svg +2 -0
  225. data/registry/icons/phosphor/text-align-left.svg +2 -0
  226. data/registry/icons/phosphor/text-align-right.svg +2 -0
  227. data/registry/icons/phosphor/text-b.svg +2 -0
  228. data/registry/icons/phosphor/text-italic.svg +2 -0
  229. data/registry/icons/phosphor/text-underline.svg +2 -0
  230. data/registry/icons/phosphor/trash.svg +2 -0
  231. data/registry/icons/phosphor/tray.svg +2 -0
  232. data/registry/icons/phosphor/truck.svg +2 -0
  233. data/registry/icons/phosphor/upload-simple.svg +2 -0
  234. data/registry/icons/phosphor/user-plus.svg +2 -0
  235. data/registry/icons/phosphor/user.svg +2 -0
  236. data/registry/icons/phosphor/users.svg +2 -0
  237. data/registry/icons/phosphor/warning-circle.svg +2 -0
  238. data/registry/icons/phosphor/x.svg +2 -0
  239. data/registry/icons/tabler/LICENSE +21 -0
  240. data/registry/icons/tabler/alert-circle.svg +18 -0
  241. data/registry/icons/tabler/align-center.svg +18 -0
  242. data/registry/icons/tabler/align-left.svg +18 -0
  243. data/registry/icons/tabler/align-right.svg +18 -0
  244. data/registry/icons/tabler/arrow-left.svg +18 -0
  245. data/registry/icons/tabler/arrow-right.svg +18 -0
  246. data/registry/icons/tabler/bell.svg +17 -0
  247. data/registry/icons/tabler/bold.svg +17 -0
  248. data/registry/icons/tabler/book.svg +20 -0
  249. data/registry/icons/tabler/building-store.svg +20 -0
  250. data/registry/icons/tabler/calendar.svg +21 -0
  251. data/registry/icons/tabler/chart-bar.svg +19 -0
  252. data/registry/icons/tabler/check.svg +16 -0
  253. data/registry/icons/tabler/chevron-down.svg +16 -0
  254. data/registry/icons/tabler/chevron-left.svg +16 -0
  255. data/registry/icons/tabler/chevron-right.svg +16 -0
  256. data/registry/icons/tabler/circle-check.svg +17 -0
  257. data/registry/icons/tabler/cloud.svg +16 -0
  258. data/registry/icons/tabler/credit-card.svg +19 -0
  259. data/registry/icons/tabler/device-desktop.svg +19 -0
  260. data/registry/icons/tabler/dots-vertical.svg +18 -0
  261. data/registry/icons/tabler/dots.svg +18 -0
  262. data/registry/icons/tabler/download.svg +18 -0
  263. data/registry/icons/tabler/eye-off.svg +18 -0
  264. data/registry/icons/tabler/eye.svg +17 -0
  265. data/registry/icons/tabler/file-plus.svg +19 -0
  266. data/registry/icons/tabler/file-text.svg +20 -0
  267. data/registry/icons/tabler/file.svg +17 -0
  268. data/registry/icons/tabler/folder.svg +16 -0
  269. data/registry/icons/tabler/git-branch.svg +21 -0
  270. data/registry/icons/tabler/grip-vertical.svg +21 -0
  271. data/registry/icons/tabler/home.svg +18 -0
  272. data/registry/icons/tabler/inbox.svg +17 -0
  273. data/registry/icons/tabler/info-circle.svg +18 -0
  274. data/registry/icons/tabler/italic.svg +18 -0
  275. data/registry/icons/tabler/keyboard.svg +23 -0
  276. data/registry/icons/tabler/layout-dashboard.svg +19 -0
  277. data/registry/icons/tabler/layout-grid.svg +19 -0
  278. data/registry/icons/tabler/layout-kanban.svg +19 -0
  279. data/registry/icons/tabler/layout-sidebar.svg +17 -0
  280. data/registry/icons/tabler/lifebuoy.svg +21 -0
  281. data/registry/icons/tabler/list.svg +21 -0
  282. data/registry/icons/tabler/loader.svg +23 -0
  283. data/registry/icons/tabler/lock.svg +18 -0
  284. data/registry/icons/tabler/logout.svg +18 -0
  285. data/registry/icons/tabler/mail.svg +17 -0
  286. data/registry/icons/tabler/menu-2.svg +18 -0
  287. data/registry/icons/tabler/message-circle.svg +16 -0
  288. data/registry/icons/tabler/minus.svg +16 -0
  289. data/registry/icons/tabler/moon.svg +16 -0
  290. data/registry/icons/tabler/news.svg +19 -0
  291. data/registry/icons/tabler/package.svg +20 -0
  292. data/registry/icons/tabler/percentage.svg +18 -0
  293. data/registry/icons/tabler/player-pause.svg +17 -0
  294. data/registry/icons/tabler/plus.svg +17 -0
  295. data/registry/icons/tabler/printer.svg +18 -0
  296. data/registry/icons/tabler/qrcode.svg +27 -0
  297. data/registry/icons/tabler/receipt.svg +16 -0
  298. data/registry/icons/tabler/rocket.svg +18 -0
  299. data/registry/icons/tabler/rosette-discount-check.svg +17 -0
  300. data/registry/icons/tabler/rotate-clockwise.svg +16 -0
  301. data/registry/icons/tabler/school.svg +17 -0
  302. data/registry/icons/tabler/search.svg +17 -0
  303. data/registry/icons/tabler/selector.svg +17 -0
  304. data/registry/icons/tabler/settings.svg +17 -0
  305. data/registry/icons/tabler/shield-check.svg +17 -0
  306. data/registry/icons/tabler/shield.svg +16 -0
  307. data/registry/icons/tabler/sitemap.svg +20 -0
  308. data/registry/icons/tabler/sun-moon.svg +21 -0
  309. data/registry/icons/tabler/sun.svg +17 -0
  310. data/registry/icons/tabler/trash.svg +20 -0
  311. data/registry/icons/tabler/truck.svg +18 -0
  312. data/registry/icons/tabler/underline.svg +17 -0
  313. data/registry/icons/tabler/upload.svg +18 -0
  314. data/registry/icons/tabler/user-plus.svg +19 -0
  315. data/registry/icons/tabler/user.svg +17 -0
  316. data/registry/icons/tabler/users.svg +19 -0
  317. data/registry/icons/tabler/world.svg +20 -0
  318. data/registry/icons/tabler/x.svg +17 -0
  319. data/registry/icons/tabler/zoom-in.svg +19 -0
  320. data/registry/icons/tabler/zoom-out.svg +18 -0
  321. data/registry/icons.yml +90 -0
  322. data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
  323. data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
  324. data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
  325. data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
  326. data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
  327. data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
  328. data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
  329. data/registry/test/components/previews/ui/chart_preview.rb +46 -0
  330. data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
  331. data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
  332. data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
  333. data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
  334. data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
  335. data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
  336. data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
  337. data/registry/test/components/previews/ui/command_preview.rb +25 -0
  338. data/registry/test/components/previews/ui/confirm_dialog_preview/destructive.html.erb +7 -0
  339. data/registry/test/components/previews/ui/confirm_dialog_preview/in_form.html.erb +6 -0
  340. data/registry/test/components/previews/ui/confirm_dialog_preview/on_link.html.erb +5 -0
  341. data/registry/test/components/previews/ui/confirm_dialog_preview/plain.html.erb +3 -0
  342. data/registry/test/components/previews/ui/confirm_dialog_preview/with_description.html.erb +4 -0
  343. data/registry/test/components/previews/ui/confirm_dialog_preview.rb +20 -0
  344. data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
  345. data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
  346. data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
  347. data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
  348. data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
  349. data/registry/test/components/previews/ui/empty_state_preview/in_card.html.erb +7 -0
  350. data/registry/test/components/previews/ui/empty_state_preview/no_results.html.erb +3 -0
  351. data/registry/test/components/previews/ui/empty_state_preview/outline.html.erb +3 -0
  352. data/registry/test/components/previews/ui/empty_state_preview/with_actions.html.erb +4 -0
  353. data/registry/test/components/previews/ui/empty_state_preview/with_media.html.erb +8 -0
  354. data/registry/test/components/previews/ui/empty_state_preview.rb +26 -0
  355. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
  356. data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
  357. data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
  358. data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
  359. data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
  360. data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
  361. data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
  362. data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
  363. data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
  364. data/registry/test/components/previews/ui/icon_preview/common.html.erb +19 -3
  365. data/registry/test/components/previews/ui/icon_preview/labelled.html.erb +4 -0
  366. data/registry/test/components/previews/ui/icon_preview/missing.html.erb +3 -0
  367. data/registry/test/components/previews/ui/icon_preview.rb +18 -3
  368. data/registry/test/components/previews/ui/input_group_preview/playground.html.erb +2 -2
  369. data/registry/test/components/previews/ui/input_group_preview.rb +3 -3
  370. data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
  371. data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
  372. data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
  373. data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
  374. data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
  375. data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
  376. data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
  377. data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
  378. data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
  379. data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
  380. data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
  381. data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
  382. data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
  383. data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
  384. data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
  385. data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
  386. data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
  387. data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
  388. data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
  389. data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
  390. data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
  391. data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
  392. data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
  393. data/registry/test/components/previews/ui/progress_preview.rb +46 -0
  394. data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
  395. data/registry/test/components/previews/ui/select_preview.rb +5 -0
  396. data/registry/test/components/previews/ui/stepper_preview/approvals.html.erb +9 -0
  397. data/registry/test/components/previews/ui/stepper_preview/checklist.html.erb +12 -0
  398. data/registry/test/components/previews/ui/stepper_preview/dot_wizard.html.erb +41 -0
  399. data/registry/test/components/previews/ui/stepper_preview/failed_deploy.html.erb +13 -0
  400. data/registry/test/components/previews/ui/stepper_preview/inline.html.erb +31 -0
  401. data/registry/test/components/previews/ui/stepper_preview/onboarding.html.erb +12 -0
  402. data/registry/test/components/previews/ui/stepper_preview/order_tracking.html.erb +10 -0
  403. data/registry/test/components/previews/ui/stepper_preview/price_calculator.html.erb +14 -0
  404. data/registry/test/components/previews/ui/stepper_preview/server_driven.html.erb +20 -0
  405. data/registry/test/components/previews/ui/stepper_preview/sidebar.html.erb +43 -0
  406. data/registry/test/components/previews/ui/stepper_preview/wizard.html.erb +55 -0
  407. data/registry/test/components/previews/ui/stepper_preview.rb +57 -0
  408. data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
  409. data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
  410. data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
  411. data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
  412. data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
  413. data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
  414. data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
  415. data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
  416. data/registry/test/components/previews/ui/toggle_group_preview/icon_only.html.erb +5 -0
  417. data/registry/test/components/previews/ui/toggle_group_preview/in_form.html.erb +11 -0
  418. data/registry/test/components/previews/ui/toggle_group_preview/multiple.html.erb +5 -0
  419. data/registry/test/components/previews/ui/toggle_group_preview/outline.html.erb +5 -0
  420. data/registry/test/components/previews/ui/toggle_group_preview/sizes.html.erb +9 -0
  421. data/registry/test/components/previews/ui/toggle_group_preview/with_icons.html.erb +5 -0
  422. data/registry/test/components/previews/ui/toggle_group_preview.rb +36 -0
  423. data/registry/test/components/ui/accordion/trigger_test.rb +1 -1
  424. data/registry/test/components/ui/alert_test.rb +10 -10
  425. data/registry/test/components/ui/badge_test.rb +3 -3
  426. data/registry/test/components/ui/banner_test.rb +3 -3
  427. data/registry/test/components/ui/breadcrumb/ellipsis_test.rb +2 -2
  428. data/registry/test/components/ui/button_test.rb +17 -14
  429. data/registry/test/components/ui/button_to_test.rb +2 -2
  430. data/registry/test/components/ui/chart_test.rb +187 -0
  431. data/registry/test/components/ui/checkbox_test.rb +2 -2
  432. data/registry/test/components/ui/command/group_test.rb +19 -0
  433. data/registry/test/components/ui/command/item_test.rb +47 -0
  434. data/registry/test/components/ui/command_test.rb +110 -0
  435. data/registry/test/components/ui/confirm_dialog_test.rb +68 -0
  436. data/registry/test/components/ui/date_picker_test.rb +129 -111
  437. data/registry/test/components/ui/empty_state_test.rb +87 -0
  438. data/registry/test/components/ui/file_field_test.rb +105 -9
  439. data/registry/test/components/ui/gallery_test.rb +192 -0
  440. data/registry/test/components/ui/icon_test.rb +60 -20
  441. data/registry/test/components/ui/input_group_test.rb +1 -1
  442. data/registry/test/components/ui/kbd_test.rb +150 -0
  443. data/registry/test/components/ui/link_test.rb +3 -3
  444. data/registry/test/components/ui/navigation_menu_test.rb +2 -2
  445. data/registry/test/components/ui/number_input_test.rb +155 -0
  446. data/registry/test/components/ui/progress_test.rb +188 -0
  447. data/registry/test/components/ui/select_test.rb +62 -0
  448. data/registry/test/components/ui/session_tabs_test.rb +3 -3
  449. data/registry/test/components/ui/spinner_test.rb +9 -9
  450. data/registry/test/components/ui/stepper_test.rb +239 -0
  451. data/registry/test/components/ui/tabs/trigger_test.rb +2 -2
  452. data/registry/test/components/ui/theme_toggle_test.rb +6 -8
  453. data/registry/test/components/ui/timeline_test.rb +175 -0
  454. data/registry/test/components/ui/toast_test.rb +11 -10
  455. data/registry/test/components/ui/toggle_group_test.rb +162 -0
  456. metadata +399 -3
  457. data/registry/app/javascript/cally.ts +0 -8
  458. data/registry/app/javascript/velvetkit_icons.ts +0 -78
@@ -0,0 +1,454 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+ import { isApple, isTyping, matchesShortcut, parseShortcut, usableWhileTyping, type Shortcut } from './_internal/shortcut.js';
3
+
4
+ // Connects to data-controller="ui--command"
5
+ //
6
+ // A combobox over a listbox, as WAI-ARIA describes it: focus stays in the
7
+ // search box and the highlighted item is announced with aria-activedescendant.
8
+ //
9
+ // Typing hides the items that don't match and puts the best first, within
10
+ // their group. With a url, it also asks the server, the way UI::Select does:
11
+ // spaced out, a spinner when slow, nothing cancelled, answers remembered.
12
+ //
13
+ // With dialog, the panel lives in a native <dialog>: the shortcut toggles it
14
+ // from anywhere, and the platform gives the focus trap, Escape and the inert
15
+ // page behind it.
16
+ const SEARCH_DELAY = 200;
17
+ const SLOW_SEARCH = 200;
18
+ const REMEMBER_FOR = 60_000;
19
+
20
+ type Result = { label: string; value: string; href?: string; description?: string; group?: string };
21
+
22
+ const remembered = new Map<string, { at: number; results: Result[] }>();
23
+ const pending = new Map<string, Promise<Result[]>>();
24
+
25
+ type Turbo = { visit: (url: string) => void };
26
+
27
+ export default class extends Controller<HTMLElement> {
28
+ static targets = ['dialog', 'panel', 'input', 'list', 'group', 'item', 'results', 'empty', 'status', 'template', 'searchIcon', 'spinner'];
29
+ static values = {
30
+ dialog: Boolean, shortcut: String, sortGroups: Boolean, url: String, param: { type: String, default: 'q' },
31
+ resultsHeading: String, empty: String, searching: String, failed: String,
32
+ };
33
+
34
+ declare readonly hasDialogTarget: boolean;
35
+ declare readonly dialogTarget: HTMLDialogElement;
36
+ declare readonly inputTarget: HTMLInputElement;
37
+ declare readonly listTarget: HTMLElement;
38
+ declare readonly groupTargets: HTMLElement[];
39
+ declare readonly itemTargets: HTMLElement[];
40
+ declare readonly resultsTarget: HTMLElement;
41
+ declare readonly emptyTarget: HTMLElement;
42
+ declare readonly statusTarget: HTMLElement;
43
+ declare readonly templateTarget: HTMLTemplateElement;
44
+ declare readonly searchIconTarget: HTMLElement;
45
+ declare readonly spinnerTarget: HTMLElement;
46
+ declare readonly dialogValue: boolean;
47
+ declare readonly shortcutValue: string;
48
+ declare readonly sortGroupsValue: boolean;
49
+ declare readonly urlValue: string;
50
+ declare readonly paramValue: string;
51
+ declare readonly resultsHeadingValue: string;
52
+ declare readonly emptyValue: string;
53
+ declare readonly searchingValue: string;
54
+ declare readonly failedValue: string;
55
+
56
+ private shortcuts: Shortcut[] = [];
57
+ private apple = false;
58
+ private highlighted: HTMLElement | null = null;
59
+ private searchTimer?: number;
60
+ private slowTimer?: number;
61
+ private latestKey = '';
62
+ private resultCount = 0;
63
+
64
+ connect() {
65
+ this.apple = isApple();
66
+ this.itemTargets.forEach((item, index) => { item.dataset.order = String(index); });
67
+ this.groupTargets.forEach((group, index) => { group.dataset.order = String(index); });
68
+ if (this.dialogValue && this.hasDialogTarget) {
69
+ this.shortcuts = this.shortcutValue.split(/\s+/).filter(Boolean).map(parseShortcut);
70
+ if (this.shortcuts.length) document.addEventListener('keydown', this.onShortcut);
71
+ this.dialogTarget.addEventListener('click', this.onBackdrop);
72
+ this.dialogTarget.addEventListener('close', this.onClose);
73
+ }
74
+ this.filter('');
75
+ }
76
+
77
+ disconnect() {
78
+ document.removeEventListener('keydown', this.onShortcut);
79
+ if (this.hasDialogTarget) {
80
+ this.dialogTarget.removeEventListener('click', this.onBackdrop);
81
+ this.dialogTarget.removeEventListener('close', this.onClose);
82
+ if (this.dialogTarget.open) this.dialogTarget.close();
83
+ }
84
+ this.stopSearch();
85
+ }
86
+
87
+ // ── Opening and closing (dialog) ─────────────────────────────────────────
88
+
89
+ open(event?: Event) {
90
+ event?.preventDefault();
91
+ if (!this.hasDialogTarget || this.dialogTarget.open) return;
92
+
93
+ this.inputTarget.value = '';
94
+ this.filter('');
95
+ this.dialogTarget.showModal();
96
+ this.inputTarget.focus();
97
+ }
98
+
99
+ close() {
100
+ if (this.hasDialogTarget && this.dialogTarget.open) this.dialogTarget.close();
101
+ }
102
+
103
+ private onShortcut = (event: KeyboardEvent) => {
104
+ if (event.defaultPrevented || event.repeat || event.isComposing) return;
105
+ const shortcut = this.shortcuts.find((s) => matchesShortcut(event, s, this.apple));
106
+ if (!shortcut) return;
107
+ // A bare-key shortcut, like "/", is typing when in a field, this search
108
+ // box included: there it's a "/" in the query, not a way to close.
109
+ if (isTyping(event.target) && !usableWhileTyping(shortcut)) return;
110
+
111
+ event.preventDefault();
112
+ if (this.dialogTarget.open) this.close();
113
+ else this.open();
114
+ };
115
+
116
+ private onBackdrop = (event: MouseEvent) => {
117
+ if (event.target === this.dialogTarget) this.close();
118
+ };
119
+
120
+ private onClose = () => {
121
+ this.stopSearch();
122
+ };
123
+
124
+ // ── Typing ───────────────────────────────────────────────────────────────
125
+
126
+ search() {
127
+ const query = this.inputTarget.value.trim();
128
+ this.filter(query);
129
+ this.announce();
130
+ if (!this.remote) return;
131
+
132
+ window.clearTimeout(this.searchTimer);
133
+ if (!query) {
134
+ this.stopSearch();
135
+ this.clearResults();
136
+ this.updateEmpty();
137
+ return;
138
+ }
139
+ this.searchTimer = window.setTimeout(() => this.fetchResults(query), SEARCH_DELAY);
140
+ }
141
+
142
+ // Hides what doesn't match, best matches first in each group; an empty
143
+ // query puts everything back as written. With sortGroups, the groups follow
144
+ // their best match too.
145
+ private filter(query: string) {
146
+ const words = query.toLowerCase().split(/\s+/).filter(Boolean);
147
+ const best = new Map<HTMLElement, number>();
148
+
149
+ this.groupTargets.forEach((group) => {
150
+ const items = this.itemTargets.filter((item) => item.closest('[data-ui--command-target="group"]') === group);
151
+ const scored = items.map((item) => ({ item, score: words.length ? this.score(item, words) : 1 }));
152
+ scored.forEach(({ item, score }) => { item.hidden = score <= 0; });
153
+ scored
154
+ .sort((a, b) => (words.length ? b.score - a.score : 0) || Number(a.item.dataset.order) - Number(b.item.dataset.order))
155
+ .forEach(({ item }) => group.append(item));
156
+ group.hidden = scored.every(({ score }) => score <= 0);
157
+ best.set(group, Math.max(0, ...scored.map(({ score }) => score)));
158
+ });
159
+
160
+ if (this.sortGroupsValue) {
161
+ const results = this.resultsTarget;
162
+ [...this.groupTargets]
163
+ .sort((a, b) => (words.length ? best.get(b)! - best.get(a)! : 0) || Number(a.dataset.order) - Number(b.dataset.order))
164
+ .forEach((group) => results.before(group));
165
+ }
166
+
167
+ this.updateEmpty();
168
+ this.highlight(this.visible()[0] ?? null);
169
+ }
170
+
171
+ // Every word has to be found. A label starting with it beats a word in the
172
+ // label starting with it, which beats it anywhere, which beats keywords,
173
+ // which beats an abbreviation ("nwinv" for New invoice).
174
+ private score(item: HTMLElement, words: string[]) {
175
+ const label = (item.dataset.label ?? item.textContent ?? '').toLowerCase();
176
+ const keywords = (item.dataset.keywords ?? '').toLowerCase();
177
+ let total = 0;
178
+ for (const word of words) {
179
+ let best = 0;
180
+ if (label.startsWith(word)) best = 1000 - label.length;
181
+ else if (new RegExp(`(^|[\\s\\-_/.])${word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`).test(label)) best = 800 - label.length;
182
+ else if (label.includes(word)) best = 600 - label.indexOf(word);
183
+ else if (keywords.includes(word)) best = 400;
184
+ else if (this.inOrder(label, word)) best = 100;
185
+ if (best <= 0) return 0;
186
+ total += best;
187
+ }
188
+ return total;
189
+ }
190
+
191
+ // An abbreviation: the letters in order, starting a word, each next one
192
+ // later in the same word or starting another. "nwproj" is New project;
193
+ // "dal" isn't hold this sale, since its d is in the middle of a word.
194
+ private inOrder(text: string, word: string, from = 0, after = -1): boolean {
195
+ if (word === '') return true;
196
+ for (let at = text.indexOf(word[0], from); at !== -1; at = text.indexOf(word[0], at + 1)) {
197
+ const startsWord = at === 0 || /[\s\-_/.,]/.test(text[at - 1]);
198
+ const sameWord = after >= 0 && !/[\s\-_/.,]/.test(text.slice(after + 1, at));
199
+ if ((startsWord || sameWord) && this.inOrder(text, word.slice(1), at + 1, at)) return true;
200
+ }
201
+ return false;
202
+ }
203
+
204
+ // ── Moving and picking ───────────────────────────────────────────────────
205
+
206
+ navigate(event: KeyboardEvent) {
207
+ const items = this.visible();
208
+ const at = this.highlighted ? items.indexOf(this.highlighted) : -1;
209
+
210
+ switch (event.key) {
211
+ case 'ArrowDown':
212
+ event.preventDefault();
213
+ this.highlight(items[(at + 1) % items.length] ?? null, true);
214
+ break;
215
+ case 'ArrowUp':
216
+ event.preventDefault();
217
+ this.highlight(items[(at - 1 + items.length) % items.length] ?? null, true);
218
+ break;
219
+ case 'PageDown':
220
+ event.preventDefault();
221
+ this.highlight(items[Math.min(at + 8, items.length - 1)] ?? null, true);
222
+ break;
223
+ case 'PageUp':
224
+ event.preventDefault();
225
+ this.highlight(items[Math.max(at - 8, 0)] ?? null, true);
226
+ break;
227
+ case 'Enter':
228
+ if (event.isComposing) return;
229
+ event.preventDefault();
230
+ if (this.highlighted) this.choose(this.highlighted, event.metaKey || event.ctrlKey);
231
+ break;
232
+ case 'Escape':
233
+ // In a dialog, Escape closes it (the platform does that). On a page,
234
+ // it clears the search first.
235
+ if (!this.dialogValue && this.inputTarget.value) {
236
+ event.preventDefault();
237
+ this.inputTarget.value = '';
238
+ this.search();
239
+ }
240
+ break;
241
+ }
242
+ }
243
+
244
+ hover(event: PointerEvent) {
245
+ const item = (event.target as HTMLElement).closest<HTMLElement>('[role="option"]');
246
+ if (item && item !== this.highlighted && !item.hidden && item.getAttribute('aria-disabled') !== 'true') this.highlight(item);
247
+ }
248
+
249
+ pick(event: MouseEvent) {
250
+ const item = (event.target as HTMLElement).closest<HTMLElement>('[role="option"]');
251
+ if (!item) return;
252
+ event.preventDefault();
253
+ this.choose(item, event.metaKey || event.ctrlKey);
254
+ }
255
+
256
+ private choose(item: HTMLElement, newTab = false) {
257
+ if (item.getAttribute('aria-disabled') === 'true') return;
258
+
259
+ const detail = { value: item.dataset.value ?? '', label: item.dataset.label ?? '', href: item.dataset.href ?? null };
260
+ const picked = this.dispatch('select', { detail, cancelable: true, target: item });
261
+ if (picked.defaultPrevented) return;
262
+
263
+ this.close();
264
+ if (!detail.href) return;
265
+ if (newTab) {
266
+ window.open(detail.href, '_blank', 'noopener');
267
+ return;
268
+ }
269
+ const turbo = (window as unknown as { Turbo?: Turbo }).Turbo;
270
+ if (turbo) turbo.visit(detail.href);
271
+ else window.location.assign(detail.href);
272
+ }
273
+
274
+ private highlight(item: HTMLElement | null, scroll = false) {
275
+ this.highlighted?.removeAttribute('data-highlighted');
276
+ this.highlighted?.setAttribute('aria-selected', 'false');
277
+ this.highlighted = item;
278
+ if (!item) {
279
+ this.inputTarget.removeAttribute('aria-activedescendant');
280
+ return;
281
+ }
282
+ item.setAttribute('data-highlighted', '');
283
+ item.setAttribute('aria-selected', 'true');
284
+ this.inputTarget.setAttribute('aria-activedescendant', item.id);
285
+ if (scroll) item.scrollIntoView({ block: 'nearest' });
286
+ }
287
+
288
+ private visible() {
289
+ return [...this.listTarget.querySelectorAll<HTMLElement>('[role="option"]')]
290
+ .filter((item) => !item.hidden && !item.closest('[hidden]') && item.getAttribute('aria-disabled') !== 'true');
291
+ }
292
+
293
+ // For analytics and the like: what was searched and how much it found,
294
+ // server results included once they're in.
295
+ private announce() {
296
+ const query = this.inputTarget.value.trim();
297
+ const matches = [...this.listTarget.querySelectorAll<HTMLElement>('[role="option"]')]
298
+ .filter((item) => !item.hidden && !item.closest('[hidden]')).length;
299
+ this.dispatch('search', { detail: { query, matches } });
300
+ }
301
+
302
+ private updateEmpty() {
303
+ const query = this.inputTarget.value.trim();
304
+ const searching = this.listTarget.getAttribute('aria-busy') === 'true';
305
+ // Disabled items count as found: they're shown, just not pickable.
306
+ const shown = [...this.listTarget.querySelectorAll<HTMLElement>('[role="option"]')].filter((item) => !item.hidden && !item.closest('[hidden]'));
307
+ const none = shown.length === 0 && !this.statusTarget.textContent;
308
+ this.emptyTarget.textContent = this.emptyValue;
309
+ this.emptyTarget.hidden = !(query && none && !searching);
310
+ }
311
+
312
+ // ── Searching the server ─────────────────────────────────────────────────
313
+
314
+ private get remote() {
315
+ return this.urlValue !== '';
316
+ }
317
+
318
+ private async fetchResults(query: string) {
319
+ const url = new URL(this.urlValue, window.location.href);
320
+ url.searchParams.set(this.paramValue, query);
321
+ const key = url.toString();
322
+ this.latestKey = key;
323
+ window.clearTimeout(this.slowTimer);
324
+
325
+ const known = remembered.get(key);
326
+ if (known && Date.now() - known.at < REMEMBER_FOR) {
327
+ this.showResults(known.results);
328
+ return;
329
+ }
330
+
331
+ this.listTarget.setAttribute('aria-busy', 'true');
332
+ this.slowTimer = window.setTimeout(() => this.showStatus(this.searchingValue, true), SLOW_SEARCH);
333
+ try {
334
+ const results = await this.ask(url, key);
335
+ if (this.latestKey !== key) return;
336
+ this.listTarget.removeAttribute('aria-busy');
337
+ this.showResults(results);
338
+ this.announce();
339
+ } catch {
340
+ if (this.latestKey !== key) return;
341
+ this.listTarget.removeAttribute('aria-busy');
342
+ this.clearResults();
343
+ this.showStatus(this.failedValue);
344
+ this.highlight(this.visible()[0] ?? null);
345
+ }
346
+ }
347
+
348
+ private ask(url: URL, key: string): Promise<Result[]> {
349
+ let request = pending.get(key);
350
+ if (!request) {
351
+ request = fetch(url, { headers: { Accept: 'application/json' } })
352
+ .then((response) => {
353
+ if (!response.ok) throw new Error(`${response.status}`);
354
+ return response.json();
355
+ })
356
+ .then((data) => {
357
+ const results = this.normalise(data);
358
+ remembered.set(key, { at: Date.now(), results });
359
+ return results;
360
+ })
361
+ .finally(() => pending.delete(key));
362
+ pending.set(key, request);
363
+ }
364
+ return request;
365
+ }
366
+
367
+ private normalise(data: unknown): Result[] {
368
+ if (!Array.isArray(data)) return [];
369
+ return data.flatMap((item) => {
370
+ if (!item || typeof item !== 'object') return [];
371
+ const { label, value, href, url, description, group } = item as Record<string, unknown>;
372
+ const link = href ?? url;
373
+ const text = String(label ?? value ?? '');
374
+ if (!text) return [];
375
+ return [{
376
+ label: text, value: String(value ?? link ?? text), href: link ? String(link) : undefined,
377
+ description: description ? String(description) : undefined, group: group ? String(group) : undefined,
378
+ }];
379
+ });
380
+ }
381
+
382
+ // Server results go after what's on the page, grouped as the server says.
383
+ private showResults(results: Result[]) {
384
+ this.clearResults();
385
+ this.showStatus(null);
386
+ const groups = new Map<string, Result[]>();
387
+ results.forEach((result) => {
388
+ const heading = result.group || this.resultsHeadingValue;
389
+ groups.set(heading, [...(groups.get(heading) ?? []), result]);
390
+ });
391
+
392
+ groups.forEach((list, heading) => {
393
+ const group = document.createElement('div');
394
+ group.setAttribute('role', 'group');
395
+ group.className = 'py-1 [&:not(:first-child)]:border-t';
396
+ const title = document.createElement('div');
397
+ title.id = `${this.listTarget.id}-results-${this.resultCount++}`;
398
+ title.className = 'px-2 pt-1.5 pb-1 text-xs font-medium text-muted-foreground';
399
+ title.textContent = heading;
400
+ group.setAttribute('aria-labelledby', title.id);
401
+ group.append(title);
402
+ list.forEach((result) => group.append(this.buildItem(result)));
403
+ this.resultsTarget.append(group);
404
+ });
405
+
406
+ this.updateEmpty();
407
+ // Keep the highlight on what's on the page if something there matched.
408
+ if (!this.highlighted || this.highlighted.hidden) this.highlight(this.visible()[0] ?? null);
409
+ }
410
+
411
+ private buildItem(result: Result) {
412
+ const item = this.templateTarget.content.firstElementChild!.cloneNode(true) as HTMLElement;
413
+ item.id = `${this.listTarget.id}-result-${this.resultCount++}`;
414
+ item.removeAttribute('data-ui--command-target');
415
+ item.dataset.value = result.value;
416
+ item.dataset.label = result.label;
417
+ if (result.href) item.dataset.href = result.href;
418
+ const body = item.querySelector<HTMLElement>('span.flex-col')!;
419
+ body.firstElementChild!.textContent = result.label;
420
+ if (result.description) {
421
+ const description = document.createElement('span');
422
+ description.className = 'truncate text-xs text-muted-foreground';
423
+ description.textContent = result.description;
424
+ body.append(description);
425
+ }
426
+ return item;
427
+ }
428
+
429
+ private clearResults() {
430
+ if (this.highlighted && this.resultsTarget.contains(this.highlighted)) this.highlight(null);
431
+ this.resultsTarget.replaceChildren();
432
+ }
433
+
434
+ private showStatus(text: string | null, busy = false) {
435
+ window.clearTimeout(this.slowTimer);
436
+ this.statusTarget.textContent = text ?? '';
437
+ this.statusTarget.hidden = !text;
438
+ // While searching, the spinner says it; the words are for screen readers.
439
+ this.statusTarget.classList.toggle('sr-only', busy);
440
+ this.spinnerTarget.classList.toggle('hidden', !busy);
441
+ this.spinnerTarget.classList.toggle('flex', busy);
442
+ this.searchIconTarget.classList.toggle('hidden', busy);
443
+ this.searchIconTarget.classList.toggle('flex', !busy);
444
+ this.updateEmpty();
445
+ }
446
+
447
+ private stopSearch() {
448
+ window.clearTimeout(this.searchTimer);
449
+ window.clearTimeout(this.slowTimer);
450
+ this.latestKey = '';
451
+ this.listTarget.removeAttribute('aria-busy');
452
+ this.showStatus(null);
453
+ }
454
+ }
@@ -0,0 +1,110 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ type ConfirmMethod = (message: string, element: HTMLFormElement, submitter?: HTMLElement | null) => Promise<boolean>;
4
+
5
+ interface TurboGlobal {
6
+ config?: { forms?: { confirm?: ConfirmMethod } };
7
+ setConfirmMethod?: (method: ConfirmMethod) => void;
8
+ }
9
+
10
+ /**
11
+ * UI::ConfirmDialog controller — answers Turbo's data-turbo-confirm with a
12
+ * native <dialog> instead of window.confirm().
13
+ *
14
+ * The confirm and cancel buttons submit a method="dialog" form, so the
15
+ * browser closes the dialog and sets its returnValue; Escape closes it with
16
+ * an empty one. Either way the `close` event resolves Turbo's promise.
17
+ *
18
+ * Turbo copies only data-turbo-confirm when it turns a data-turbo-method link
19
+ * into a form, so the other data-confirm-* options are also looked up on the
20
+ * element that was last clicked.
21
+ */
22
+ export default class extends Controller<HTMLDialogElement> {
23
+ static targets = ['panel', 'title', 'description', 'cancel', 'confirm', 'destructive'];
24
+ static values = { confirmLabel: String, cancelLabel: String };
25
+
26
+ declare readonly panelTarget: HTMLElement;
27
+ declare readonly titleTarget: HTMLElement;
28
+ declare readonly descriptionTarget: HTMLElement;
29
+ declare readonly cancelTarget: HTMLButtonElement;
30
+ declare readonly confirmTarget: HTMLButtonElement;
31
+ declare readonly destructiveTarget: HTMLButtonElement;
32
+ declare confirmLabelValue: string;
33
+ declare cancelLabelValue: string;
34
+
35
+ private previous?: ConfirmMethod;
36
+ private clicked: Element | null = null;
37
+ private resolve?: (answer: boolean) => void;
38
+
39
+ connect() {
40
+ document.addEventListener('click', this.remember, true);
41
+ this.element.addEventListener('close', this.closed);
42
+ this.element.addEventListener('click', this.backdropClick);
43
+
44
+ const turbo = (window as unknown as { Turbo?: TurboGlobal }).Turbo;
45
+ if (turbo?.config?.forms) {
46
+ this.previous = turbo.config.forms.confirm;
47
+ turbo.config.forms.confirm = this.confirm;
48
+ } else {
49
+ turbo?.setConfirmMethod?.(this.confirm);
50
+ }
51
+ }
52
+
53
+ disconnect() {
54
+ document.removeEventListener('click', this.remember, true);
55
+ this.element.removeEventListener('close', this.closed);
56
+ this.element.removeEventListener('click', this.backdropClick);
57
+
58
+ const turbo = (window as unknown as { Turbo?: TurboGlobal }).Turbo;
59
+ if (turbo?.config?.forms && turbo.config.forms.confirm === this.confirm) {
60
+ turbo.config.forms.confirm = this.previous;
61
+ }
62
+ this.answer(false);
63
+ if (this.element.open) this.element.close();
64
+ }
65
+
66
+ confirm = (message: string, form: HTMLFormElement, submitter?: HTMLElement | null): Promise<boolean> => {
67
+ this.answer(false);
68
+ const sources = [submitter, form, this.clicked?.getAttribute('data-turbo-confirm') === message ? this.clicked : null];
69
+ const option = (name: string) =>
70
+ sources.find((el) => el?.hasAttribute(`data-confirm-${name}`))?.getAttribute(`data-confirm-${name}`) ?? null;
71
+
72
+ this.titleTarget.textContent = message;
73
+ const description = option('description');
74
+ this.descriptionTarget.textContent = description ?? '';
75
+ this.descriptionTarget.hidden = !description;
76
+
77
+ const destructive = option('variant') === 'destructive';
78
+ const label = option('button') ?? this.confirmLabelValue;
79
+ this.confirmTarget.hidden = destructive;
80
+ this.destructiveTarget.hidden = !destructive;
81
+ this.confirmTarget.textContent = label;
82
+ this.destructiveTarget.textContent = label;
83
+ this.cancelTarget.textContent = option('cancel') ?? this.cancelLabelValue;
84
+
85
+ this.element.returnValue = '';
86
+ this.element.showModal();
87
+ this.panelTarget.dataset.state = 'open';
88
+ this.cancelTarget.focus();
89
+
90
+ return new Promise((resolve) => (this.resolve = resolve));
91
+ };
92
+
93
+ private remember = (event: Event) => {
94
+ this.clicked = (event.target as Element | null)?.closest?.('[data-turbo-confirm]') ?? null;
95
+ };
96
+
97
+ private backdropClick = (event: MouseEvent) => {
98
+ if (event.target === this.element) this.element.close('cancel');
99
+ };
100
+
101
+ private closed = () => {
102
+ this.panelTarget.dataset.state = 'closed';
103
+ this.answer(this.element.returnValue === 'confirm');
104
+ };
105
+
106
+ private answer(value: boolean) {
107
+ this.resolve?.(value);
108
+ this.resolve = undefined;
109
+ }
110
+ }