velvetkit 0.3.0 → 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 (175) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -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/number_input.html.erb +11 -0
  24. data/registry/app/components/ui/number_input.rb +103 -0
  25. data/registry/app/components/ui/progress/segment.rb +25 -0
  26. data/registry/app/components/ui/progress.html.erb +25 -0
  27. data/registry/app/components/ui/progress.rb +182 -0
  28. data/registry/app/components/ui/select.html.erb +24 -5
  29. data/registry/app/components/ui/select.rb +44 -5
  30. data/registry/app/components/ui/timeline/item.html.erb +21 -0
  31. data/registry/app/components/ui/timeline/item.rb +145 -0
  32. data/registry/app/components/ui/timeline.html.erb +1 -0
  33. data/registry/app/components/ui/timeline.rb +42 -0
  34. data/registry/app/javascript/chart_js.ts +37 -0
  35. data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
  36. data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
  37. data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
  38. data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
  39. data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
  40. data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
  41. data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
  42. data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
  43. data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
  44. data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
  45. data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
  46. data/registry/app/javascript/vanilla_calendar.ts +15 -0
  47. data/registry/components.yml +19 -3
  48. data/registry/config/locales/velvetkit.en.yml +73 -0
  49. data/registry/icons/lucide/download.svg +17 -0
  50. data/registry/icons/lucide/file-plus.svg +18 -0
  51. data/registry/icons/lucide/file.svg +16 -0
  52. data/registry/icons/lucide/folder.svg +15 -0
  53. data/registry/icons/lucide/keyboard.svg +23 -0
  54. data/registry/icons/lucide/minus.svg +15 -0
  55. data/registry/icons/lucide/package.svg +18 -0
  56. data/registry/icons/lucide/pause.svg +16 -0
  57. data/registry/icons/lucide/percent.svg +17 -0
  58. data/registry/icons/lucide/printer.svg +17 -0
  59. data/registry/icons/lucide/receipt.svg +17 -0
  60. data/registry/icons/lucide/sun-moon.svg +19 -0
  61. data/registry/icons/lucide/truck.svg +19 -0
  62. data/registry/icons/lucide/user-plus.svg +18 -0
  63. data/registry/icons/lucide/zoom-in.svg +18 -0
  64. data/registry/icons/lucide/zoom-out.svg +17 -0
  65. data/registry/icons/phosphor/circle-half.svg +2 -0
  66. data/registry/icons/phosphor/download-simple.svg +2 -0
  67. data/registry/icons/phosphor/file-plus.svg +2 -0
  68. data/registry/icons/phosphor/file.svg +2 -0
  69. data/registry/icons/phosphor/folder.svg +2 -0
  70. data/registry/icons/phosphor/keyboard.svg +2 -0
  71. data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
  72. data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
  73. data/registry/icons/phosphor/minus.svg +2 -0
  74. data/registry/icons/phosphor/package.svg +2 -0
  75. data/registry/icons/phosphor/pause.svg +2 -0
  76. data/registry/icons/phosphor/percent.svg +2 -0
  77. data/registry/icons/phosphor/printer.svg +2 -0
  78. data/registry/icons/phosphor/receipt.svg +2 -0
  79. data/registry/icons/phosphor/truck.svg +2 -0
  80. data/registry/icons/phosphor/user-plus.svg +2 -0
  81. data/registry/icons/tabler/download.svg +18 -0
  82. data/registry/icons/tabler/file-plus.svg +19 -0
  83. data/registry/icons/tabler/file.svg +17 -0
  84. data/registry/icons/tabler/folder.svg +16 -0
  85. data/registry/icons/tabler/keyboard.svg +23 -0
  86. data/registry/icons/tabler/minus.svg +16 -0
  87. data/registry/icons/tabler/package.svg +20 -0
  88. data/registry/icons/tabler/percentage.svg +18 -0
  89. data/registry/icons/tabler/player-pause.svg +17 -0
  90. data/registry/icons/tabler/printer.svg +18 -0
  91. data/registry/icons/tabler/receipt.svg +16 -0
  92. data/registry/icons/tabler/sun-moon.svg +21 -0
  93. data/registry/icons/tabler/truck.svg +18 -0
  94. data/registry/icons/tabler/user-plus.svg +19 -0
  95. data/registry/icons/tabler/zoom-in.svg +19 -0
  96. data/registry/icons/tabler/zoom-out.svg +18 -0
  97. data/registry/icons.yml +16 -0
  98. data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
  99. data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
  100. data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
  101. data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
  102. data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
  103. data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
  104. data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
  105. data/registry/test/components/previews/ui/chart_preview.rb +46 -0
  106. data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
  107. data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
  108. data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
  109. data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
  110. data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
  111. data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
  112. data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
  113. data/registry/test/components/previews/ui/command_preview.rb +25 -0
  114. data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
  115. data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
  116. data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
  117. data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
  118. data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
  119. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
  120. data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
  121. data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
  122. data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
  123. data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
  124. data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
  125. data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
  126. data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
  127. data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
  128. data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
  129. data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
  130. data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
  131. data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
  132. data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
  133. data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
  134. data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
  135. data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
  136. data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
  137. data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
  138. data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
  139. data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
  140. data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
  141. data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
  142. data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
  143. data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
  144. data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
  145. data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
  146. data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
  147. data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
  148. data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
  149. data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
  150. data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
  151. data/registry/test/components/previews/ui/progress_preview.rb +46 -0
  152. data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
  153. data/registry/test/components/previews/ui/select_preview.rb +5 -0
  154. data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
  155. data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
  156. data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
  157. data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
  158. data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
  159. data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
  160. data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
  161. data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
  162. data/registry/test/components/ui/chart_test.rb +187 -0
  163. data/registry/test/components/ui/command/group_test.rb +19 -0
  164. data/registry/test/components/ui/command/item_test.rb +47 -0
  165. data/registry/test/components/ui/command_test.rb +110 -0
  166. data/registry/test/components/ui/date_picker_test.rb +129 -111
  167. data/registry/test/components/ui/file_field_test.rb +105 -9
  168. data/registry/test/components/ui/gallery_test.rb +192 -0
  169. data/registry/test/components/ui/kbd_test.rb +150 -0
  170. data/registry/test/components/ui/number_input_test.rb +155 -0
  171. data/registry/test/components/ui/progress_test.rb +188 -0
  172. data/registry/test/components/ui/select_test.rb +62 -0
  173. data/registry/test/components/ui/timeline_test.rb +175 -0
  174. metadata +147 -2
  175. data/registry/app/javascript/cally.ts +0 -8
@@ -1,25 +1,82 @@
1
1
  import { Controller } from '@hotwired/stimulus';
2
+ import { DirectUpload } from '@rails/activestorage';
3
+
4
+ // Connects to data-controller="ui--file-field"
5
+ //
6
+ // The <label> around the input already opens the system picker, with no
7
+ // JavaScript. This adds what a label can't: dropping files on it, a tile for
8
+ // each chosen file with a thumbnail and a way to take it out again, checking
9
+ // type and size as files arrive, and, with direct upload, sending each file to
10
+ // storage straight away with a progress bar.
11
+ //
12
+ // Without direct upload the input itself carries the files, so it's rebuilt
13
+ // to match the tiles whenever one is added or taken out. With it, the input
14
+ // is emptied as each file starts uploading and a hidden field takes the
15
+ // file's signed id instead, so the form submits ids, not bytes.
16
+ type Labels = {
17
+ remove: string; uploading: string; uploaded: string; failed: string; retry: string;
18
+ too_big: string; max: string | null; wrong_type: string; waiting: string; added: string; removed: string;
19
+ units: { byte: string; kb: string; mb: string; gb: string };
20
+ };
21
+ type Upload = { xhr?: XMLHttpRequest; done: boolean };
2
22
 
3
- /**
4
- * UI::FileField controller — drag-and-drop over a native <input type="file">.
5
- *
6
- * The <label> wrapping the input already opens the native picker on click,
7
- * no JS needed for that. This only adds what a label can't: highlighting the
8
- * dropzone while dragging, assigning dropped files to the input, and
9
- * listing the chosen file names.
10
- */
11
23
  export default class extends Controller<HTMLElement> {
12
- static targets = ['dropzone', 'input', 'filenames'];
24
+ static targets = ['dropzone', 'input', 'filenames', 'list', 'template', 'item', 'notice', 'status'];
25
+ static values = {
26
+ preview: Boolean,
27
+ directUpload: Boolean,
28
+ url: String,
29
+ name: String,
30
+ multiple: Boolean,
31
+ accept: String,
32
+ maxSize: Number,
33
+ required: Boolean,
34
+ labels: Object,
35
+ };
13
36
 
14
37
  declare readonly dropzoneTarget: HTMLElement;
15
38
  declare readonly inputTarget: HTMLInputElement;
16
39
  declare readonly hasFilenamesTarget: boolean;
17
40
  declare readonly filenamesTarget: HTMLElement;
41
+ declare readonly listTarget: HTMLElement;
42
+ declare readonly templateTarget: HTMLTemplateElement;
43
+ declare readonly itemTargets: HTMLElement[];
44
+ declare readonly hasNoticeTarget: boolean;
45
+ declare readonly noticeTarget: HTMLElement;
46
+ declare readonly hasStatusTarget: boolean;
47
+ declare readonly statusTarget: HTMLElement;
48
+ declare readonly previewValue: boolean;
49
+ declare readonly directUploadValue: boolean;
50
+ declare readonly urlValue: string;
51
+ declare readonly nameValue: string;
52
+ declare readonly multipleValue: boolean;
53
+ declare readonly acceptValue: string;
54
+ declare readonly maxSizeValue: number;
55
+ declare readonly requiredValue: boolean;
56
+ declare readonly labelsValue: Labels;
57
+
58
+ // What each tile stands for, beyond its markup.
59
+ private files = new WeakMap<HTMLElement, File>();
60
+ private uploads = new WeakMap<HTMLElement, Upload>();
61
+ private objectUrls = new WeakMap<HTMLElement, string>();
62
+ private waitingToSubmit = false;
63
+ private onSubmit = (event: SubmitEvent) => this.holdForUploads(event);
18
64
 
19
65
  connect() {
20
- this.updateNames();
66
+ // With direct upload the files travel separately, so the input itself
67
+ // sends nothing, not even an empty file.
68
+ if (this.directUploadValue) this.inputTarget.removeAttribute('name');
69
+ this.inputTarget.form?.addEventListener('submit', this.onSubmit);
70
+ if (!this.previewValue) this.updateNames();
71
+ }
72
+
73
+ disconnect() {
74
+ this.inputTarget.form?.removeEventListener('submit', this.onSubmit);
75
+ this.itemTargets.forEach((item) => this.forget(item));
21
76
  }
22
77
 
78
+ // ── Arriving files ──────────────────────────────────────────────────────
79
+
23
80
  dragOver(event: DragEvent) {
24
81
  event.preventDefault();
25
82
  if (this.inputTarget.disabled) return;
@@ -34,16 +91,231 @@ export default class extends Controller<HTMLElement> {
34
91
  event.preventDefault();
35
92
  delete this.dropzoneTarget.dataset.drag;
36
93
 
37
- const files = event.dataTransfer?.files;
38
- if (!files || !files.length || this.inputTarget.disabled) return;
94
+ const files = Array.from(event.dataTransfer?.files ?? []);
95
+ if (!files.length || this.inputTarget.disabled) return;
96
+
97
+ if (this.previewValue) {
98
+ this.add(this.multipleValue ? files : files.slice(0, 1));
99
+ } else {
100
+ this.inputTarget.files = event.dataTransfer!.files;
101
+ this.inputTarget.dispatchEvent(new Event('change', { bubbles: true }));
102
+ }
103
+ }
104
+
105
+ // The picker's choice. Without previews, only the names are listed.
106
+ chosen() {
107
+ if (!this.previewValue) return this.updateNames();
39
108
 
40
- this.inputTarget.files = files;
41
- this.inputTarget.dispatchEvent(new Event('change', { bubbles: true }));
109
+ const files = Array.from(this.inputTarget.files ?? []);
110
+ // The input's own list is replaced as the tiles are made, so the files
111
+ // already chosen aren't counted twice.
112
+ const fresh = files.filter((file) => !this.itemTargets.some((item) => this.files.get(item) === file));
113
+ if (fresh.length) this.add(fresh);
42
114
  }
43
115
 
44
116
  updateNames() {
45
117
  if (!this.hasFilenamesTarget) return;
46
- const names = Array.from(this.inputTarget.files ?? []).map((file) => file.name);
47
- this.filenamesTarget.textContent = names.join(', ');
118
+ this.filenamesTarget.textContent = Array.from(this.inputTarget.files ?? []).map((file) => file.name).join(', ');
119
+ }
120
+
121
+ private add(files: File[]) {
122
+ // A single file replaces whatever was there, kept files included.
123
+ if (!this.multipleValue) this.itemTargets.forEach((item) => this.discard(item));
124
+
125
+ files.forEach((file) => {
126
+ const item = this.makeTile(file);
127
+ const problem = this.problemWith(file);
128
+ if (problem) {
129
+ this.setState(item, 'error', problem);
130
+ return;
131
+ }
132
+ this.files.set(item, file);
133
+ if (this.directUploadValue) this.upload(item, file);
134
+ });
135
+ this.syncInput();
136
+ this.say(this.labelsValue.added.replace('%{name}', files.map((f) => f.name).join(', ')));
137
+ }
138
+
139
+ private makeTile(file: File): HTMLElement {
140
+ const item = this.templateTarget.content.firstElementChild!.cloneNode(true) as HTMLElement;
141
+ this.part(item, 'name').textContent = file.name;
142
+ this.part(item, 'name').title = file.name;
143
+ this.part(item, 'meta').textContent = this.size(file.size);
144
+ this.part(item, 'remove').setAttribute('aria-label', this.labelsValue.remove.replace('%{name}', file.name));
145
+ this.part(item, 'progress').setAttribute('aria-label', file.name);
146
+
147
+ if (file.type.startsWith('image/')) {
148
+ const url = URL.createObjectURL(file);
149
+ this.objectUrls.set(item, url);
150
+ const thumb = this.part(item, 'thumb') as HTMLImageElement;
151
+ thumb.src = url;
152
+ thumb.hidden = false;
153
+ this.part(item, 'icon').hidden = true;
154
+ }
155
+ this.listTarget.append(item);
156
+ return item;
157
+ }
158
+
159
+ // The same rules as `accept` and `max_size`, for dropped files too, which
160
+ // the picker's filter never sees.
161
+ private problemWith(file: File): string | null {
162
+ if (this.acceptValue && !this.accepts(file)) return this.labelsValue.wrong_type;
163
+ if (this.maxSizeValue && file.size > this.maxSizeValue) {
164
+ return this.labelsValue.too_big.replace('%{max}', this.labelsValue.max ?? this.size(this.maxSizeValue));
165
+ }
166
+ return null;
167
+ }
168
+
169
+ private accepts(file: File) {
170
+ return this.acceptValue.split(',').map((rule) => rule.trim().toLowerCase()).filter(Boolean).some((rule) => {
171
+ if (rule.startsWith('.')) return file.name.toLowerCase().endsWith(rule);
172
+ if (rule.endsWith('/*')) return file.type.toLowerCase().startsWith(rule.slice(0, -1));
173
+ return file.type.toLowerCase() === rule;
174
+ });
175
+ }
176
+
177
+ // ── Taking files out ────────────────────────────────────────────────────
178
+
179
+ remove(event: Event) {
180
+ const item = (event.currentTarget as HTMLElement).closest<HTMLElement>('[data-ui--file-field-target="item"]');
181
+ if (!item) return;
182
+
183
+ const name = this.part(item, 'name').textContent ?? '';
184
+ this.discard(item);
185
+ this.syncInput();
186
+ this.say(this.labelsValue.removed.replace('%{name}', name));
187
+ this.inputTarget.focus();
188
+ }
189
+
190
+ private discard(item: HTMLElement) {
191
+ this.forget(item);
192
+ item.remove();
193
+ this.settle();
194
+ }
195
+
196
+ private forget(item: HTMLElement) {
197
+ this.uploads.get(item)?.xhr?.abort();
198
+ const url = this.objectUrls.get(item);
199
+ if (url) URL.revokeObjectURL(url);
200
+ }
201
+
202
+ // ── Uploading ───────────────────────────────────────────────────────────
203
+
204
+ retry(event: Event) {
205
+ const item = (event.currentTarget as HTMLElement).closest<HTMLElement>('[data-ui--file-field-target="item"]');
206
+ const file = item && this.files.get(item);
207
+ if (item && file) this.upload(item, file);
208
+ }
209
+
210
+ private upload(item: HTMLElement, file: File) {
211
+ const state: Upload = { done: false };
212
+ this.uploads.set(item, state);
213
+ this.setState(item, 'uploading', this.labelsValue.uploading);
214
+ this.setProgress(item, 0);
215
+
216
+ const watch = (xhr: XMLHttpRequest) => {
217
+ state.xhr = xhr;
218
+ xhr.upload.addEventListener('progress', (event) => {
219
+ if (event.lengthComputable) this.setProgress(item, event.loaded / event.total);
220
+ });
221
+ };
222
+ new DirectUpload(file, this.urlValue, {
223
+ directUploadWillCreateBlobWithXHR: (xhr: XMLHttpRequest) => { state.xhr = xhr; },
224
+ directUploadWillStoreFileWithXHR: watch,
225
+ }).create((error: Error | null, blob?: { signed_id: string }) => {
226
+ state.done = true;
227
+ if (!item.isConnected) return;
228
+ if (error || !blob) {
229
+ this.setState(item, 'error', this.labelsValue.failed);
230
+ this.part(item, 'retry').hidden = false;
231
+ } else {
232
+ this.keep(item, blob.signed_id);
233
+ this.setState(item, 'uploaded', `${this.size(file.size)} · ${this.labelsValue.uploaded}`);
234
+ }
235
+ this.settle();
236
+ });
237
+ }
238
+
239
+ // The uploaded file's signed id goes in the form in its place.
240
+ private keep(item: HTMLElement, signedId: string) {
241
+ const field = document.createElement('input');
242
+ field.type = 'hidden';
243
+ field.name = this.nameValue;
244
+ field.value = signedId;
245
+ field.dataset.role = 'signed';
246
+ item.append(field);
247
+ }
248
+
249
+ private setState(item: HTMLElement, state: string, meta: string) {
250
+ item.dataset.state = state;
251
+ this.part(item, 'meta').textContent = meta;
252
+ this.part(item, 'retry').hidden = true;
253
+ this.part(item, 'progress').hidden = state !== 'uploading';
254
+ }
255
+
256
+ private setProgress(item: HTMLElement, fraction: number) {
257
+ const percent = Math.round(fraction * 100);
258
+ this.part(item, 'bar').style.width = `${percent}%`;
259
+ this.part(item, 'progress').setAttribute('aria-valuenow', String(percent));
260
+ }
261
+
262
+ private get uploading() {
263
+ return this.itemTargets.some((item) => item.dataset.state === 'uploading');
264
+ }
265
+
266
+ // Sending the form mid-upload waits for the uploads, then sends it.
267
+ private holdForUploads(event: SubmitEvent) {
268
+ if (!this.uploading) return;
269
+
270
+ event.preventDefault();
271
+ event.stopImmediatePropagation();
272
+ this.waitingToSubmit = true;
273
+ if (this.hasNoticeTarget) this.noticeTarget.textContent = this.labelsValue.waiting;
274
+ }
275
+
276
+ private settle() {
277
+ if (this.uploading) return;
278
+
279
+ if (this.hasNoticeTarget) this.noticeTarget.textContent = '';
280
+ if (this.waitingToSubmit) {
281
+ this.waitingToSubmit = false;
282
+ this.inputTarget.form?.requestSubmit();
283
+ }
284
+ }
285
+
286
+ // ── The input itself ────────────────────────────────────────────────────
287
+
288
+ // Without direct upload the input carries the files the tiles show; with
289
+ // it, the input is emptied, since the files travel separately. Either way a
290
+ // required field counts a kept or uploaded file as filled.
291
+ private syncInput() {
292
+ const carried = this.directUploadValue ? [] : this.itemTargets.map((item) => this.files.get(item)).filter((f): f is File => !!f);
293
+ const list = new DataTransfer();
294
+ carried.forEach((file) => list.items.add(file));
295
+ this.inputTarget.files = list.files;
296
+
297
+ const filled = this.itemTargets.some((item) => item.dataset.state !== 'error');
298
+ if (this.requiredValue) this.inputTarget.required = !filled;
299
+ }
300
+
301
+ private say(text: string) {
302
+ if (this.hasStatusTarget) this.statusTarget.textContent = text;
303
+ }
304
+
305
+ private part(item: HTMLElement, role: string): HTMLElement {
306
+ return item.querySelector<HTMLElement>(`[data-role="${role}"]`)!;
307
+ }
308
+
309
+ // Written the way Rails writes it for the kept files beside it ("1.2 MB"),
310
+ // in the units the locale gives.
311
+ private size(bytes: number): string {
312
+ const { units } = this.labelsValue;
313
+ const steps: [number, string][] = [[1024 ** 3, units.gb], [1024 ** 2, units.mb], [1024, units.kb]];
314
+ const [divisor, unit] = steps.find(([limit]) => bytes >= limit) ?? [1, units.byte];
315
+ const value = bytes / divisor;
316
+ const number = new Intl.NumberFormat(document.documentElement.lang || undefined, {
317
+ maximumFractionDigits: value >= 100 || divisor === 1 ? 0 : value >= 10 ? 1 : 2,
318
+ }).format(value);
319
+ return `${number} ${unit}`;
48
320
  }
49
321
  }