velvetkit 0.3.0 → 0.4.1

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 (216) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +109 -0
  3. data/README.md +5 -1
  4. data/lib/generators/velvetkit/add/add_generator.rb +58 -0
  5. data/lib/velvetkit/version.rb +4 -1
  6. data/lib/velvetkit.rb +7 -0
  7. data/registry/app/assets/tailwind/velvetkit.css +90 -155
  8. data/registry/app/components/ui/chart.html.erb +47 -0
  9. data/registry/app/components/ui/chart.rb +177 -0
  10. data/registry/app/components/ui/command/group.rb +27 -0
  11. data/registry/app/components/ui/command/item.rb +64 -0
  12. data/registry/app/components/ui/command.html.erb +41 -0
  13. data/registry/app/components/ui/command.rb +122 -0
  14. data/registry/app/components/ui/date_picker.html.erb +58 -64
  15. data/registry/app/components/ui/date_picker.rb +199 -75
  16. data/registry/app/components/ui/file_field.html.erb +14 -4
  17. data/registry/app/components/ui/file_field.rb +179 -28
  18. data/registry/app/components/ui/gallery/image.rb +20 -0
  19. data/registry/app/components/ui/gallery.html.erb +52 -0
  20. data/registry/app/components/ui/gallery.rb +164 -0
  21. data/registry/app/components/ui/kbd.html.erb +27 -0
  22. data/registry/app/components/ui/kbd.rb +134 -0
  23. data/registry/app/components/ui/money_input.html.erb +8 -0
  24. data/registry/app/components/ui/money_input.rb +153 -0
  25. data/registry/app/components/ui/number_input.html.erb +11 -0
  26. data/registry/app/components/ui/number_input.rb +103 -0
  27. data/registry/app/components/ui/progress/segment.rb +25 -0
  28. data/registry/app/components/ui/progress.html.erb +25 -0
  29. data/registry/app/components/ui/progress.rb +182 -0
  30. data/registry/app/components/ui/scan_input.html.erb +6 -0
  31. data/registry/app/components/ui/scan_input.rb +94 -0
  32. data/registry/app/components/ui/select.html.erb +24 -5
  33. data/registry/app/components/ui/select.rb +44 -5
  34. data/registry/app/components/ui/slider.html.erb +23 -0
  35. data/registry/app/components/ui/slider.rb +254 -0
  36. data/registry/app/components/ui/timeline/item.html.erb +21 -0
  37. data/registry/app/components/ui/timeline/item.rb +145 -0
  38. data/registry/app/components/ui/timeline.html.erb +1 -0
  39. data/registry/app/components/ui/timeline.rb +42 -0
  40. data/registry/app/javascript/chart_js.ts +37 -0
  41. data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
  42. data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
  43. data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
  44. data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
  45. data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
  46. data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
  47. data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
  48. data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
  49. data/registry/app/javascript/controllers/ui/money_input_controller.ts +228 -0
  50. data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
  51. data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
  52. data/registry/app/javascript/controllers/ui/scan_input_controller.ts +143 -0
  53. data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
  54. data/registry/app/javascript/controllers/ui/slider_controller.ts +216 -0
  55. data/registry/app/javascript/vanilla_calendar.ts +15 -0
  56. data/registry/components.yml +25 -3
  57. data/registry/config/locales/velvetkit.en.yml +79 -0
  58. data/registry/icons/lucide/download.svg +17 -0
  59. data/registry/icons/lucide/file-plus.svg +18 -0
  60. data/registry/icons/lucide/file.svg +16 -0
  61. data/registry/icons/lucide/folder.svg +15 -0
  62. data/registry/icons/lucide/keyboard.svg +23 -0
  63. data/registry/icons/lucide/minus.svg +15 -0
  64. data/registry/icons/lucide/package.svg +18 -0
  65. data/registry/icons/lucide/pause.svg +16 -0
  66. data/registry/icons/lucide/percent.svg +17 -0
  67. data/registry/icons/lucide/printer.svg +17 -0
  68. data/registry/icons/lucide/receipt.svg +17 -0
  69. data/registry/icons/lucide/scan-barcode.svg +21 -0
  70. data/registry/icons/lucide/sun-moon.svg +19 -0
  71. data/registry/icons/lucide/truck.svg +19 -0
  72. data/registry/icons/lucide/user-plus.svg +18 -0
  73. data/registry/icons/lucide/volume-2.svg +17 -0
  74. data/registry/icons/lucide/volume-x.svg +17 -0
  75. data/registry/icons/lucide/zoom-in.svg +18 -0
  76. data/registry/icons/lucide/zoom-out.svg +17 -0
  77. data/registry/icons/phosphor/barcode.svg +2 -0
  78. data/registry/icons/phosphor/circle-half.svg +2 -0
  79. data/registry/icons/phosphor/download-simple.svg +2 -0
  80. data/registry/icons/phosphor/file-plus.svg +2 -0
  81. data/registry/icons/phosphor/file.svg +2 -0
  82. data/registry/icons/phosphor/folder.svg +2 -0
  83. data/registry/icons/phosphor/keyboard.svg +2 -0
  84. data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
  85. data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
  86. data/registry/icons/phosphor/minus.svg +2 -0
  87. data/registry/icons/phosphor/package.svg +2 -0
  88. data/registry/icons/phosphor/pause.svg +2 -0
  89. data/registry/icons/phosphor/percent.svg +2 -0
  90. data/registry/icons/phosphor/printer.svg +2 -0
  91. data/registry/icons/phosphor/receipt.svg +2 -0
  92. data/registry/icons/phosphor/speaker-high.svg +2 -0
  93. data/registry/icons/phosphor/speaker-x.svg +2 -0
  94. data/registry/icons/phosphor/truck.svg +2 -0
  95. data/registry/icons/phosphor/user-plus.svg +2 -0
  96. data/registry/icons/tabler/barcode.svg +23 -0
  97. data/registry/icons/tabler/download.svg +18 -0
  98. data/registry/icons/tabler/file-plus.svg +19 -0
  99. data/registry/icons/tabler/file.svg +17 -0
  100. data/registry/icons/tabler/folder.svg +16 -0
  101. data/registry/icons/tabler/keyboard.svg +23 -0
  102. data/registry/icons/tabler/minus.svg +16 -0
  103. data/registry/icons/tabler/package.svg +20 -0
  104. data/registry/icons/tabler/percentage.svg +18 -0
  105. data/registry/icons/tabler/player-pause.svg +17 -0
  106. data/registry/icons/tabler/printer.svg +18 -0
  107. data/registry/icons/tabler/receipt.svg +16 -0
  108. data/registry/icons/tabler/sun-moon.svg +21 -0
  109. data/registry/icons/tabler/truck.svg +18 -0
  110. data/registry/icons/tabler/user-plus.svg +19 -0
  111. data/registry/icons/tabler/volume-3.svg +17 -0
  112. data/registry/icons/tabler/volume.svg +18 -0
  113. data/registry/icons/tabler/zoom-in.svg +19 -0
  114. data/registry/icons/tabler/zoom-out.svg +18 -0
  115. data/registry/icons.yml +19 -0
  116. data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
  117. data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
  118. data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
  119. data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
  120. data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
  121. data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
  122. data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
  123. data/registry/test/components/previews/ui/chart_preview.rb +46 -0
  124. data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
  125. data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
  126. data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
  127. data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
  128. data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
  129. data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
  130. data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
  131. data/registry/test/components/previews/ui/command_preview.rb +25 -0
  132. data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
  133. data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
  134. data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
  135. data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
  136. data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
  137. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
  138. data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
  139. data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
  140. data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
  141. data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
  142. data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
  143. data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
  144. data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
  145. data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
  146. data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
  147. data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
  148. data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
  149. data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
  150. data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
  151. data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
  152. data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
  153. data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
  154. data/registry/test/components/previews/ui/money_input_preview/cash_tendered.html.erb +24 -0
  155. data/registry/test/components/previews/ui/money_input_preview/currencies.html.erb +7 -0
  156. data/registry/test/components/previews/ui/money_input_preview/invoice.html.erb +24 -0
  157. data/registry/test/components/previews/ui/money_input_preview/sizes_and_states.html.erb +9 -0
  158. data/registry/test/components/previews/ui/money_input_preview.rb +34 -0
  159. data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
  160. data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
  161. data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
  162. data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
  163. data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
  164. data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
  165. data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
  166. data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
  167. data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
  168. data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
  169. data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
  170. data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
  171. data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
  172. data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
  173. data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
  174. data/registry/test/components/previews/ui/progress_preview.rb +46 -0
  175. data/registry/test/components/previews/ui/scan_input_preview/_simulate.html.erb +20 -0
  176. data/registry/test/components/previews/ui/scan_input_preview/library_checkout.html.erb +30 -0
  177. data/registry/test/components/previews/ui/scan_input_preview/playground.html.erb +13 -0
  178. data/registry/test/components/previews/ui/scan_input_preview/stock_count.html.erb +24 -0
  179. data/registry/test/components/previews/ui/scan_input_preview/ticket_check_in.html.erb +34 -0
  180. data/registry/test/components/previews/ui/scan_input_preview/till.html.erb +9 -0
  181. data/registry/test/components/previews/ui/scan_input_preview.rb +33 -0
  182. data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
  183. data/registry/test/components/previews/ui/select_preview.rb +5 -0
  184. data/registry/test/components/previews/ui/slider_preview/named_steps.html.erb +7 -0
  185. data/registry/test/components/previews/ui/slider_preview/opening_hours.html.erb +5 -0
  186. data/registry/test/components/previews/ui/slider_preview/price_range.html.erb +6 -0
  187. data/registry/test/components/previews/ui/slider_preview/sizes_and_states.html.erb +9 -0
  188. data/registry/test/components/previews/ui/slider_preview/text_size.html.erb +16 -0
  189. data/registry/test/components/previews/ui/slider_preview/tip.html.erb +22 -0
  190. data/registry/test/components/previews/ui/slider_preview/volume.html.erb +5 -0
  191. data/registry/test/components/previews/ui/slider_preview.rb +45 -0
  192. data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
  193. data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
  194. data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
  195. data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
  196. data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
  197. data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
  198. data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
  199. data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
  200. data/registry/test/components/ui/chart_test.rb +187 -0
  201. data/registry/test/components/ui/command/group_test.rb +19 -0
  202. data/registry/test/components/ui/command/item_test.rb +47 -0
  203. data/registry/test/components/ui/command_test.rb +110 -0
  204. data/registry/test/components/ui/date_picker_test.rb +129 -111
  205. data/registry/test/components/ui/file_field_test.rb +105 -9
  206. data/registry/test/components/ui/gallery_test.rb +192 -0
  207. data/registry/test/components/ui/kbd_test.rb +150 -0
  208. data/registry/test/components/ui/money_input_test.rb +149 -0
  209. data/registry/test/components/ui/number_input_test.rb +155 -0
  210. data/registry/test/components/ui/progress_test.rb +188 -0
  211. data/registry/test/components/ui/scan_input_test.rb +110 -0
  212. data/registry/test/components/ui/select_test.rb +62 -0
  213. data/registry/test/components/ui/slider_test.rb +190 -0
  214. data/registry/test/components/ui/timeline_test.rb +175 -0
  215. metadata +188 -2
  216. data/registry/app/javascript/cally.ts +0 -8
@@ -11,9 +11,28 @@ import { useDismiss } from './_internal/use_dismiss.js';
11
11
  // The hidden native <select> holds the value. Every pick is written to it and
12
12
  // announced with `input` and `change`, so forms and listeners see an ordinary
13
13
  // select. A multiple select stays open while you tick options.
14
+ //
15
+ // With a `url`, the search box asks the server instead of filtering what's on
16
+ // the page. A picked option is added to the native select if it isn't there
17
+ // yet, so it submits, and stays chosen when it drops out of later results.
18
+ const SEARCH_DELAY = 250;
19
+ const SLOW_SEARCH = 200;
20
+ // How long a search's results are reused instead of asked for again.
21
+ const REMEMBER_FOR = 60_000;
22
+
23
+ type Result = { value: string; label: string };
24
+
25
+ // Results by URL, shared by every select on the page that asks the same
26
+ // server the same thing, so reopening a list or retyping a search is instant.
27
+ const remembered = new Map<string, { at: number; results: Result[] }>();
28
+ // Searches on their way, by URL, so the same one is never asked for twice.
29
+ const pending = new Map<string, Promise<Result[]>>();
14
30
  export default class extends Controller<HTMLElement> {
15
- static targets = ['trigger', 'value', 'panel', 'listbox', 'option', 'native', 'search', 'empty'];
16
- static values = { multiple: Boolean, summary: String, placeholder: String };
31
+ static targets = ['trigger', 'value', 'panel', 'listbox', 'option', 'native', 'search', 'empty', 'status', 'template', 'searchIcon', 'spinner'];
32
+ static values = {
33
+ multiple: Boolean, summary: String, placeholder: String,
34
+ url: String, param: { type: String, default: 'q' }, searching: String, failed: String,
35
+ };
17
36
 
18
37
  declare readonly triggerTarget: HTMLButtonElement;
19
38
  declare readonly valueTarget: HTMLElement;
@@ -27,6 +46,18 @@ export default class extends Controller<HTMLElement> {
27
46
  declare readonly multipleValue: boolean;
28
47
  declare readonly summaryValue: string;
29
48
  declare readonly placeholderValue: string;
49
+ declare readonly listboxTarget: HTMLElement;
50
+ declare readonly hasStatusTarget: boolean;
51
+ declare readonly statusTarget: HTMLElement;
52
+ declare readonly templateTarget: HTMLTemplateElement;
53
+ declare readonly hasSpinnerTarget: boolean;
54
+ declare readonly spinnerTarget: HTMLElement;
55
+ declare readonly searchIconTarget: HTMLElement;
56
+ declare readonly hasUrlValue: boolean;
57
+ declare readonly urlValue: string;
58
+ declare readonly paramValue: string;
59
+ declare readonly searchingValue: string;
60
+ declare readonly failedValue: string;
30
61
 
31
62
  private isOpen = false;
32
63
  private active: HTMLElement | null = null;
@@ -34,6 +65,12 @@ export default class extends Controller<HTMLElement> {
34
65
  private typedAt = 0;
35
66
  private floatingCleanup: (() => void) | null = null;
36
67
  private dismissCleanup: (() => void) | null = null;
68
+ // The search whose results the list is waiting for. An older one still on
69
+ // its way finishes and is remembered, but doesn't fill the list.
70
+ private latestKey = '';
71
+ private searchTimer?: number;
72
+ private slowTimer?: number;
73
+ private resultCount = 0;
37
74
  private onReset = () => requestAnimationFrame(() => this.sync());
38
75
  private onInvalid = () => this.triggerTarget.setAttribute('aria-invalid', 'true');
39
76
 
@@ -46,6 +83,7 @@ export default class extends Controller<HTMLElement> {
46
83
  disconnect() {
47
84
  this.nativeTarget.form?.removeEventListener('reset', this.onReset);
48
85
  this.nativeTarget.removeEventListener('invalid', this.onInvalid);
86
+ this.stopSearch();
49
87
  this.teardown();
50
88
  }
51
89
 
@@ -63,6 +101,12 @@ export default class extends Controller<HTMLElement> {
63
101
  if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(event.key)) {
64
102
  event.preventDefault();
65
103
  this.open(event.key === 'ArrowUp' ? 'last' : 'first');
104
+ } else if (this.isPrintable(event) && this.remote) {
105
+ // Typing on a closed server-searched select starts a search with it.
106
+ event.preventDefault();
107
+ this.open();
108
+ this.searchTarget.value = event.key;
109
+ this.filter();
66
110
  } else if (this.isPrintable(event) && !this.multipleValue) {
67
111
  this.typeahead(event.key, true);
68
112
  }
@@ -108,6 +152,11 @@ export default class extends Controller<HTMLElement> {
108
152
 
109
153
  filter() {
110
154
  if (!this.hasSearchTarget) return;
155
+ if (this.remote) {
156
+ window.clearTimeout(this.searchTimer);
157
+ this.searchTimer = window.setTimeout(() => this.fetchResults(this.searchTarget.value.trim()), SEARCH_DELAY);
158
+ return;
159
+ }
111
160
 
112
161
  const query = this.searchTarget.value.trim().toLowerCase();
113
162
  this.optionTargets.forEach((option) => {
@@ -154,7 +203,9 @@ export default class extends Controller<HTMLElement> {
154
203
 
155
204
  if (this.hasSearchTarget) {
156
205
  this.searchTarget.value = '';
157
- this.filter();
206
+ // A server-searched list starts with what the server offers for nothing typed.
207
+ if (this.remote) this.fetchResults('');
208
+ else this.filter();
158
209
  this.searchTarget.focus();
159
210
  }
160
211
 
@@ -167,6 +218,7 @@ export default class extends Controller<HTMLElement> {
167
218
  if (!this.isOpen) return;
168
219
 
169
220
  this.teardown();
221
+ this.stopSearch();
170
222
  this.panelTarget.hidden = true;
171
223
  this.panelTarget.dataset.state = 'closed';
172
224
  this.triggerTarget.setAttribute('aria-expanded', 'false');
@@ -181,9 +233,15 @@ export default class extends Controller<HTMLElement> {
181
233
  const native = this.nativeTarget;
182
234
  const before = Array.from(native.selectedOptions).map((o) => o.value).join('\n');
183
235
 
236
+ // A fetched option the native select hasn't seen yet joins it, so it submits.
237
+ let item = Array.from(native.options).find((o) => o.value === value);
238
+ if (!item) {
239
+ item = new Option(this.label(option), value);
240
+ native.add(item);
241
+ }
242
+
184
243
  if (this.multipleValue) {
185
- const item = Array.from(native.options).find((o) => o.value === value);
186
- if (item) item.selected = !item.selected;
244
+ item.selected = !item.selected;
187
245
  } else {
188
246
  native.value = value;
189
247
  }
@@ -207,9 +265,10 @@ export default class extends Controller<HTMLElement> {
207
265
  if (check) check.hidden = !on;
208
266
  });
209
267
 
210
- const labels = this.optionTargets
211
- .filter((o) => o.dataset.value !== '' && chosen.has(o.dataset.value ?? ''))
212
- .map((o) => this.label(o));
268
+ // Named from the native select, which also holds choices no longer in the list.
269
+ const labels = Array.from(this.nativeTarget.selectedOptions)
270
+ .filter((o) => o.value !== '')
271
+ .map((o) => o.text.trim());
213
272
  const text = labels.length > 2 ? this.summaryValue.replace('%{count}', String(labels.length)) : labels.join(', ');
214
273
  this.valueTarget.textContent = text || this.placeholderValue;
215
274
  this.valueTarget.classList.toggle('text-muted-foreground', !text);
@@ -248,6 +307,137 @@ export default class extends Controller<HTMLElement> {
248
307
  else this.highlight(match);
249
308
  }
250
309
 
310
+ // ── Searching the server ─────────────────────────────────────────────────
311
+
312
+ private get remote(): boolean {
313
+ return this.hasUrlValue && this.urlValue !== '' && this.hasSearchTarget;
314
+ }
315
+
316
+ // Asks for the options matching `query`: from the last minute's results if
317
+ // it can, joining the same search if it's already on its way, otherwise
318
+ // from the server. Nothing is cancelled, since the server has the request
319
+ // either way; an outdated answer is kept for later but only the newest
320
+ // search fills the list.
321
+ private async fetchResults(query: string) {
322
+ const url = new URL(this.urlValue, window.location.href);
323
+ url.searchParams.set(this.paramValue, query);
324
+ const key = url.toString();
325
+ this.latestKey = key;
326
+ window.clearTimeout(this.slowTimer);
327
+
328
+ const known = remembered.get(key);
329
+ if (known && Date.now() - known.at < REMEMBER_FOR) {
330
+ this.showResults(known.results);
331
+ this.showStatus(null);
332
+ return;
333
+ }
334
+
335
+ this.listboxTarget.setAttribute('aria-busy', 'true');
336
+ this.slowTimer = window.setTimeout(() => this.showStatus(this.searchingValue, true), SLOW_SEARCH);
337
+ try {
338
+ const results = await this.ask(url, key);
339
+ if (this.latestKey !== key || !this.isOpen) return;
340
+
341
+ this.showResults(results);
342
+ this.showStatus(null);
343
+ } catch {
344
+ if (this.latestKey !== key || !this.isOpen) return;
345
+
346
+ this.showResults([]);
347
+ if (this.hasEmptyTarget) this.emptyTarget.hidden = true;
348
+ this.showStatus(this.failedValue);
349
+ } finally {
350
+ if (this.latestKey === key) this.listboxTarget.removeAttribute('aria-busy');
351
+ }
352
+ }
353
+
354
+ private ask(url: URL, key: string): Promise<Result[]> {
355
+ let request = pending.get(key);
356
+ if (!request) {
357
+ request = fetch(url, { headers: { Accept: 'application/json' } })
358
+ .then((response) => {
359
+ if (!response.ok) throw new Error(`${response.status}`);
360
+ return response.json();
361
+ })
362
+ .then((data) => {
363
+ const results = this.normalise(data);
364
+ remembered.set(key, { at: Date.now(), results });
365
+ return results;
366
+ })
367
+ .finally(() => pending.delete(key));
368
+ pending.set(key, request);
369
+ }
370
+ return request;
371
+ }
372
+
373
+ // Accepts [{ value, label }] or [[label, value]], as the server prefers.
374
+ private normalise(data: unknown): Result[] {
375
+ if (!Array.isArray(data)) return [];
376
+
377
+ return data.flatMap((item) => {
378
+ if (Array.isArray(item)) return [{ label: String(item[0] ?? ''), value: String(item[1] ?? '') }];
379
+ if (item && typeof item === 'object') {
380
+ const { value, label } = item as { value?: unknown; label?: unknown };
381
+ return [{ value: String(value ?? ''), label: String(label ?? value ?? '') }];
382
+ }
383
+ return [];
384
+ });
385
+ }
386
+
387
+ // Swaps the listed options for the new results. The empty "prompt" option,
388
+ // if there is one, stays first.
389
+ private showResults(results: Result[]) {
390
+ this.optionTargets.filter((o) => o.dataset.value !== '').forEach((o) => o.remove());
391
+ const base = this.listboxTarget.id;
392
+ results.forEach((result) => {
393
+ const option = this.templateTarget.content.firstElementChild!.cloneNode(true) as HTMLElement;
394
+ option.id = `${base}-result-${this.resultCount++}`;
395
+ option.dataset.value = result.value;
396
+ option.querySelector('.truncate')!.textContent = result.label;
397
+ this.listboxTarget.append(option);
398
+ });
399
+
400
+ this.sync();
401
+ if (this.hasEmptyTarget) this.emptyTarget.hidden = results.length > 0;
402
+ const visible = this.visible();
403
+ const chosen = visible.find((o) => o.getAttribute('aria-selected') === 'true' && o.dataset.value !== '');
404
+ this.highlight(chosen ?? visible.find((o) => o.dataset.value !== '') ?? visible[0] ?? null);
405
+ }
406
+
407
+ // A slow search shows as a spinner in the search box, where people are
408
+ // looking, and is said to screen readers; a failure is written out for all.
409
+ private showStatus(text: string | null, busy = false) {
410
+ if (!this.hasStatusTarget) return;
411
+
412
+ window.clearTimeout(this.slowTimer);
413
+ this.statusTarget.textContent = text ?? '';
414
+ this.statusTarget.hidden = !text;
415
+ this.statusTarget.classList.toggle('sr-only', busy);
416
+ this.setSpinning(busy);
417
+ if (busy && this.hasEmptyTarget) this.emptyTarget.hidden = true;
418
+ }
419
+
420
+ private setSpinning(on: boolean) {
421
+ if (!this.hasSpinnerTarget) return;
422
+
423
+ this.spinnerTarget.classList.toggle('hidden', !on);
424
+ this.spinnerTarget.classList.toggle('flex', on);
425
+ this.searchIconTarget.classList.toggle('hidden', on);
426
+ this.searchIconTarget.classList.toggle('flex', !on);
427
+ }
428
+
429
+ // Closing, or leaving the page, stops the search waiting to be asked and
430
+ // the spinner. One already on its way still finishes and is remembered, so
431
+ // its results are there on reopening; it just doesn't fill a closed list.
432
+ private stopSearch() {
433
+ window.clearTimeout(this.searchTimer);
434
+ window.clearTimeout(this.slowTimer);
435
+ this.latestKey = '';
436
+ this.setSpinning(false);
437
+ this.listboxTarget.removeAttribute('aria-busy');
438
+ if (this.hasStatusTarget) this.statusTarget.hidden = true;
439
+ }
440
+
251
441
  private visible() {
252
442
  return this.optionTargets.filter((o) => !o.hidden);
253
443
  }
@@ -0,0 +1,216 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ // Connects to data-controller="ui--slider"
4
+ //
5
+ // The handles are native range inputs, which do the keyboard, the form and
6
+ // the screen reader, and can't be seen. This places the knobs and the filled
7
+ // part of the track, keeps the shown value and aria-valuetext current, and
8
+ // takes the pointer: a press anywhere on the track grabs the nearest handle,
9
+ // so a range's two handles can be dragged even when they sit on top of each
10
+ // other, and they never pass. While dragged, a knob follows the pointer
11
+ // freely and the value keeps to the step; let go, it glides onto the value.
12
+ type PickEvent = MouseEvent & { params: { value: number } };
13
+
14
+ export default class extends Controller<HTMLElement> {
15
+ static targets = ['input', 'track', 'output'];
16
+ static values = { format: String, rangeFormat: String, labels: Object };
17
+
18
+ declare readonly inputTargets: HTMLInputElement[];
19
+ declare readonly trackTarget: HTMLElement;
20
+ declare readonly outputTarget: HTMLOutputElement;
21
+ declare readonly hasOutputTarget: boolean;
22
+ declare readonly formatValue: string;
23
+ declare readonly rangeFormatValue: string;
24
+ declare readonly labelsValue: Record<string, string>;
25
+
26
+ private dragging?: HTMLInputElement;
27
+ private pointerId?: number;
28
+ private startX = 0;
29
+ private before = '';
30
+ // Where the pointer is along the track, 0 to 1, while it holds a handle:
31
+ // the knob follows this rather than the value, which keeps to the step.
32
+ private held?: number;
33
+
34
+ connect() {
35
+ this.element.closest('form')?.addEventListener('reset', this.afterReset);
36
+ this.sync();
37
+ }
38
+
39
+ disconnect() {
40
+ this.element.closest('form')?.removeEventListener('reset', this.afterReset);
41
+ this.release();
42
+ }
43
+
44
+ // On every input: a range's handles can meet but not cross.
45
+ sync(event?: Event) {
46
+ const [low, high] = this.inputTargets;
47
+ if (high) {
48
+ if (event?.target === low && low.valueAsNumber > high.valueAsNumber) low.value = high.value;
49
+ if (event?.target === high && high.valueAsNumber < low.valueAsNumber) high.value = low.value;
50
+ }
51
+
52
+ let from = high ? this.fraction(low) : 0;
53
+ let to = this.fraction(high ?? low);
54
+ if (this.dragging && this.held !== undefined) {
55
+ if (high && this.dragging === low) from = Math.min(this.held, to);
56
+ else to = Math.max(this.held, from);
57
+ }
58
+ this.element.style.setProperty('--from', String(from));
59
+ this.element.style.setProperty('--to', String(to));
60
+ for (const input of this.inputTargets) input.setAttribute('aria-valuetext', this.text(input.value));
61
+ if (this.hasOutputTarget) {
62
+ this.outputTarget.textContent = high
63
+ ? this.rangeFormatValue.replace('%{from}', this.text(low.value)).replace('%{to}', this.text(high.value))
64
+ : this.text(low.value);
65
+ }
66
+ }
67
+
68
+ press(event: PointerEvent) {
69
+ if (event.button !== 0 || this.inputTargets[0].disabled) return;
70
+
71
+ event.preventDefault();
72
+ const at = this.fractionAt(event.clientX);
73
+ const value = this.valueOf(at);
74
+ this.dragging = this.nearest(value);
75
+ this.startX = event.clientX;
76
+ this.pointerId = event.pointerId;
77
+ this.before = this.inputTargets.map((input) => input.value).join();
78
+ this.trackTarget.setPointerCapture(event.pointerId);
79
+ this.trackTarget.addEventListener('pointermove', this.move);
80
+ this.trackTarget.addEventListener('pointerup', this.release);
81
+ this.trackTarget.addEventListener('pointercancel', this.release);
82
+ // Two handles on one spot: which one is decided by the way the pointer moves.
83
+ if (this.dragging) this.moveTo(this.dragging, value, at);
84
+ }
85
+
86
+ // A mark: the nearest handle goes there, as if the track were pressed.
87
+ pick(event: PickEvent) {
88
+ const value = event.params.value;
89
+ const input = this.nearest(value) ?? this.inputTargets[0];
90
+ const before = input.value;
91
+ this.moveTo(input, value);
92
+ input.focus({ focusVisible: false } as FocusOptions);
93
+ if (input.value !== before) input.dispatchEvent(new Event('change', { bubbles: true }));
94
+ }
95
+
96
+ // Once it moves, the knob keeps up with the pointer: no gliding.
97
+ private move = (event: PointerEvent) => {
98
+ const at = this.fractionAt(event.clientX);
99
+ if (!this.dragging) {
100
+ if (Math.abs(event.clientX - this.startX) < 2) return;
101
+
102
+ const [low, high] = this.inputTargets;
103
+ this.dragging = (event.clientX < this.startX) !== this.rtl ? low : high;
104
+ }
105
+ this.element.dataset.dragging = '';
106
+ this.moveTo(this.dragging, this.valueOf(at), at);
107
+ };
108
+
109
+ private release = () => {
110
+ const input = this.dragging;
111
+ if (this.pointerId !== undefined && this.trackTarget.hasPointerCapture(this.pointerId)) {
112
+ this.trackTarget.releasePointerCapture(this.pointerId);
113
+ }
114
+ this.trackTarget.removeEventListener('pointermove', this.move);
115
+ this.trackTarget.removeEventListener('pointerup', this.release);
116
+ this.trackTarget.removeEventListener('pointercancel', this.release);
117
+ for (const each of this.inputTargets) delete each.dataset.active;
118
+ delete this.element.dataset.dragging;
119
+ this.dragging = undefined;
120
+ this.held = undefined;
121
+ this.pointerId = undefined;
122
+ // The knob glides from the pointer onto the value.
123
+ this.sync();
124
+ if (input && this.inputTargets.map((each) => each.value).join() !== this.before) {
125
+ input.dispatchEvent(new Event('change', { bubbles: true }));
126
+ }
127
+ };
128
+
129
+ private afterReset = () => requestAnimationFrame(() => this.sync());
130
+
131
+ // Sets the value the way the keyboard would, so step and the other handle
132
+ // are respected, and fires input as the browser does. `at` is the pointer's
133
+ // place, for the knob to follow.
134
+ private moveTo(input: HTMLInputElement, value: number, at?: number) {
135
+ const [low, high] = this.inputTargets;
136
+ let next = value;
137
+ if (high && input === low) next = Math.min(next, high.valueAsNumber);
138
+ if (high && input === high) next = Math.max(next, low.valueAsNumber);
139
+
140
+ input.dataset.active = '';
141
+ // Focused for the keyboard to carry on from, without the keyboard's ring.
142
+ input.focus({ preventScroll: true, focusVisible: false } as FocusOptions);
143
+ this.held = at;
144
+ if (String(next) === input.value) {
145
+ this.sync();
146
+ return;
147
+ }
148
+
149
+ input.value = String(next);
150
+ input.dispatchEvent(new Event('input', { bubbles: true }));
151
+ }
152
+
153
+ // The handle closest to a value; none when a range's two are equally close
154
+ // and on one spot, until the pointer moves.
155
+ private nearest(value: number): HTMLInputElement | undefined {
156
+ const [low, high] = this.inputTargets;
157
+ if (!high) return low;
158
+ if (low.value === high.value) {
159
+ if (value === low.valueAsNumber) return undefined;
160
+ return value < low.valueAsNumber ? low : high;
161
+ }
162
+
163
+ return Math.abs(value - low.valueAsNumber) <= Math.abs(value - high.valueAsNumber) ? low : high;
164
+ }
165
+
166
+ // How far along the track a point is, 0 to 1. The knob's centre can't
167
+ // reach the very ends, so neither does the scale.
168
+ private fractionAt(clientX: number): number {
169
+ const rect = this.trackTarget.getBoundingClientRect();
170
+ const thumb = this.thumbWidth;
171
+ const fraction = Math.min(1, Math.max(0, (clientX - rect.left - thumb / 2) / Math.max(1, rect.width - thumb)));
172
+ return this.rtl ? 1 - fraction : fraction;
173
+ }
174
+
175
+ // The value at a fraction of the track, snapped to step.
176
+ private valueOf(fraction: number): number {
177
+ const { min, max, step } = this.bounds(this.inputTargets[0]);
178
+ const steps = Math.round((fraction * (max - min)) / step);
179
+ return Number(Math.min(max, min + steps * step).toFixed(this.decimals(step)));
180
+ }
181
+
182
+ private fraction(input: HTMLInputElement): number {
183
+ const { min, max } = this.bounds(input);
184
+ return max === min ? 0 : (input.valueAsNumber - min) / (max - min);
185
+ }
186
+
187
+ private bounds(input: HTMLInputElement) {
188
+ const min = parseFloat(input.min) || 0;
189
+ const max = Number.isNaN(parseFloat(input.max)) ? 100 : parseFloat(input.max);
190
+ const step = parseFloat(input.step) || 1;
191
+ return { min, max, step };
192
+ }
193
+
194
+ // Matches the server's: the value as the step writes it, "3" not "3.0".
195
+ private text(value: string): string {
196
+ const number = String(Number(Number(value).toFixed(this.decimals(this.bounds(this.inputTargets[0]).step))));
197
+ return this.labelsValue[number] ?? this.formatValue.replace('%{value}', number);
198
+ }
199
+
200
+ private decimals(step: number): number {
201
+ return (String(step).split('.')[1] ?? '').length;
202
+ }
203
+
204
+ // --thumb, in pixels: a custom property comes back as written, "0.5rem".
205
+ private get thumbWidth(): number {
206
+ const written = getComputedStyle(this.element).getPropertyValue('--thumb').trim();
207
+ const size = parseFloat(written) || 0;
208
+ if (!written.endsWith('rem')) return size;
209
+
210
+ return size * parseFloat(getComputedStyle(document.documentElement).fontSize);
211
+ }
212
+
213
+ private get rtl(): boolean {
214
+ return getComputedStyle(this.element).direction === 'rtl';
215
+ }
216
+ }
@@ -0,0 +1,15 @@
1
+ // Vanilla Calendar Pro, built as its own entry point rather than bundled into
2
+ // application.js, and loaded the first time a date picker opens. Its layout
3
+ // stylesheet and the light theme come out of the same build as
4
+ // vanilla_calendar.css; the kit points the theme's colours at its own tokens
5
+ // (velvetkit.css), so it follows dark mode.
6
+ //
7
+ // Same shape as ag_grid.ts and chart_js.ts: a bun code-split chunk can't be
8
+ // used because Propshaft serves digested filenames, so Ruby hands the
9
+ // controller both URLs.
10
+ import 'vanilla-calendar-pro/styles/layout.css';
11
+ import 'vanilla-calendar-pro/styles/layout/months.css';
12
+ import 'vanilla-calendar-pro/styles/themes/light.css';
13
+
14
+ // `months` is the extension that shows more than one month at once, for a range.
15
+ export { Calendar, months } from 'vanilla-calendar-pro';
@@ -9,9 +9,11 @@
9
9
  packages:
10
10
  "@floating-ui/dom": "^1.7.4"
11
11
  "@hotwired/stimulus": "^3.2.2"
12
+ "@rails/activestorage": "^8.1.4"
12
13
  ag-grid-community: "^36.1.0"
13
- cally: "^0.9.2"
14
+ chart.js: "^4.6.1"
14
15
  tw-animate-css: "^1.4.0"
16
+ vanilla-calendar-pro: "^3.4.0"
15
17
 
16
18
  # Versions of the gems a component needs beyond view_component and tailwind_merge.
17
19
  gems:
@@ -40,16 +42,20 @@ components:
40
42
  description: A button that submits a one-off form, like Rails' button_to.
41
43
  card:
42
44
  description: A bordered container with header, content and footer.
45
+ chart:
46
+ description: Line and bar charts, with a legend, tooltips and the numbers as a table, on Chart.js.
43
47
  checkbox:
44
48
  description: A checkbox with label, standalone or bound to a form builder.
45
49
  collapsible:
46
50
  description: A section that opens and closes under its trigger, like a branch of a nav tree.
51
+ command:
52
+ description: A search box over pages, actions and records, inline or opened with ⌘K.
47
53
  confirm_dialog:
48
54
  description: A styled dialog that answers data-turbo-confirm, in place of the browser's confirm box.
49
55
  data_grid:
50
56
  description: A sortable, filterable, server-paged table on AG Grid.
51
57
  date_picker:
52
- description: A typed date field with a calendar, on Cally.
58
+ description: A typed date, or a range of two with presets, with a calendar under it, on Vanilla Calendar Pro.
53
59
  dialog:
54
60
  description: A modal dialog on the native <dialog> element.
55
61
  divider:
@@ -57,9 +63,11 @@ components:
57
63
  empty_state:
58
64
  description: What a page shows when it has nothing to list yet, with a way to add the first thing.
59
65
  file_field:
60
- description: A drag-and-drop file picker with a label, hint and error message, standalone or with a form builder.
66
+ description: A drag-and-drop file picker with previews, and direct upload to storage, standalone or with a form builder.
61
67
  form_field:
62
68
  description: A label, hint and error message wired to any form control.
69
+ gallery:
70
+ description: A product's photos, with thumbnails, swiping, and a full-screen view to zoom in.
63
71
  icon:
64
72
  description: An SVG icon from your chosen icon library, drawn inline on the server.
65
73
  image:
@@ -68,14 +76,20 @@ components:
68
76
  description: A text field with label, hint and error message, standalone or with a form builder.
69
77
  input_group:
70
78
  description: A text field with addons before or after it.
79
+ kbd:
80
+ description: A keyboard key or shortcut, shown as each reader's computer labels it.
71
81
  label:
72
82
  description: A form label with a required marker.
73
83
  link:
74
84
  description: A link styled as text or as a button.
75
85
  menu:
76
86
  description: A dropdown menu with keyboard navigation.
87
+ money_input:
88
+ description: An amount of money, grouped as it's typed with its currency sign, posting the plain number.
77
89
  navigation_menu:
78
90
  description: Site navigation with links and hover or click panels of rich links.
91
+ number_input:
92
+ description: A number field with - and + buttons that keep counting while held.
79
93
  pagination:
80
94
  description: Page links for a long list, from a Pagy object.
81
95
  gems: [pagy]
@@ -83,10 +97,14 @@ components:
83
97
  description: A password field with a show/hide toggle.
84
98
  popover:
85
99
  description: Floating content anchored to a trigger.
100
+ progress:
101
+ description: A bar showing how far along a task is, or that it's still going.
86
102
  radio_group:
87
103
  description: A set of radio buttons.
88
104
  native_select:
89
105
  description: The browser's own select, styled closed; best for phones' system pickers.
106
+ scan_input:
107
+ description: A field for a barcode or QR scanner, submitting each code as it's scanned and keeping focus.
90
108
  select:
91
109
  description: A styled dropdown with keyboard navigation and type-ahead, posting like a native select.
92
110
  session_tabs:
@@ -97,6 +115,8 @@ components:
97
115
  description: A collapsible application sidebar with groups and menus.
98
116
  skeleton:
99
117
  description: A pulsing placeholder shown in a piece of content's shape while it loads.
118
+ slider:
119
+ description: A handle to drag along a track for a value, or two for a range, on the browser's own range input.
100
120
  spinner:
101
121
  description: A loading indicator.
102
122
  stepper:
@@ -117,6 +137,8 @@ components:
117
137
  description: A light/dark/system theme switcher.
118
138
  files:
119
139
  - app/views/layouts/_theme_script.html.erb
140
+ timeline:
141
+ description: What happened and what's still to come, as a list of dated entries.
120
142
  toast:
121
143
  description: A stacked, auto-dismissing message, pushed from the client or a Turbo Stream.
122
144
  toggle_group: