nitro_kit 0.9.0 → 2.0.0.alpha.2

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 (219) hide show
  1. checksums.yaml +4 -4
  2. data/.agents/plugins/marketplace.json +20 -0
  3. data/CHANGELOG.md +172 -0
  4. data/LICENSE +28 -0
  5. data/README.md +22 -15
  6. data/STYLE_GUIDE.md +425 -0
  7. data/app/assets/stylesheets/nitro_kit-tailwind-v4.css +38 -0
  8. data/app/assets/stylesheets/nitro_kit.css +7239 -0
  9. data/app/components/nitro_kit/accordion.rb +125 -65
  10. data/app/components/nitro_kit/alert.rb +80 -43
  11. data/app/components/nitro_kit/app_navigation.rb +307 -0
  12. data/app/components/nitro_kit/app_shell.rb +227 -0
  13. data/app/components/nitro_kit/appearance_bootstrap.rb +146 -0
  14. data/app/components/nitro_kit/appearance_picker.rb +156 -0
  15. data/app/components/nitro_kit/auth_shell.rb +34 -0
  16. data/app/components/nitro_kit/avatar.rb +85 -29
  17. data/app/components/nitro_kit/avatar_stack.rb +155 -9
  18. data/app/components/nitro_kit/badge.rb +43 -81
  19. data/app/components/nitro_kit/button.rb +180 -103
  20. data/app/components/nitro_kit/button_group.rb +63 -11
  21. data/app/components/nitro_kit/button_to.rb +69 -0
  22. data/app/components/nitro_kit/card.rb +42 -45
  23. data/app/components/nitro_kit/checkbox.rb +115 -62
  24. data/app/components/nitro_kit/checkbox_group.rb +131 -22
  25. data/app/components/nitro_kit/choice.rb +55 -0
  26. data/app/components/nitro_kit/combobox.rb +292 -105
  27. data/app/components/nitro_kit/command_palette.rb +412 -0
  28. data/app/components/nitro_kit/component.rb +322 -46
  29. data/app/components/nitro_kit/container.rb +32 -0
  30. data/app/components/nitro_kit/control_group.rb +45 -0
  31. data/app/components/nitro_kit/danger_zone.rb +78 -0
  32. data/app/components/nitro_kit/data_section.rb +106 -0
  33. data/app/components/nitro_kit/details_table.rb +229 -0
  34. data/app/components/nitro_kit/dialog.rb +257 -83
  35. data/app/components/nitro_kit/dropdown.rb +335 -122
  36. data/app/components/nitro_kit/dropzone.rb +326 -0
  37. data/app/components/nitro_kit/empty_state.rb +99 -0
  38. data/app/components/nitro_kit/field.rb +461 -236
  39. data/app/components/nitro_kit/field_group.rb +15 -8
  40. data/app/components/nitro_kit/fieldset.rb +37 -39
  41. data/app/components/nitro_kit/flex.rb +49 -0
  42. data/app/components/nitro_kit/form_builder.rb +313 -72
  43. data/app/components/nitro_kit/form_section.rb +79 -0
  44. data/app/components/nitro_kit/grid.rb +34 -0
  45. data/app/components/nitro_kit/icon.rb +54 -25
  46. data/app/components/nitro_kit/input.rb +97 -10
  47. data/app/components/nitro_kit/label.rb +22 -6
  48. data/app/components/nitro_kit/layout_options.rb +7 -0
  49. data/app/components/nitro_kit/page_header.rb +76 -0
  50. data/app/components/nitro_kit/pagination.rb +355 -69
  51. data/app/components/nitro_kit/pagination_bar.rb +90 -0
  52. data/app/components/nitro_kit/progressive_image.rb +181 -0
  53. data/app/components/nitro_kit/radio_button.rb +77 -55
  54. data/app/components/nitro_kit/radio_button_group.rb +107 -36
  55. data/app/components/nitro_kit/responsive_value.rb +94 -0
  56. data/app/components/nitro_kit/rich_text_area.rb +31 -0
  57. data/app/components/nitro_kit/select.rb +129 -64
  58. data/app/components/nitro_kit/settings_layout.rb +152 -0
  59. data/app/components/nitro_kit/sheet.rb +202 -0
  60. data/app/components/nitro_kit/stat_grid.rb +75 -0
  61. data/app/components/nitro_kit/switch.rb +108 -51
  62. data/app/components/nitro_kit/table.rb +237 -45
  63. data/app/components/nitro_kit/tabs.rb +168 -63
  64. data/app/components/nitro_kit/textarea.rb +81 -10
  65. data/app/components/nitro_kit/toast.rb +211 -80
  66. data/app/components/nitro_kit/toolbar.rb +75 -0
  67. data/app/components/nitro_kit/tooltip.rb +235 -34
  68. data/app/components/nitro_kit/typeset.rb +28 -0
  69. data/app/javascript/controllers/nk/app_shell_controller.js +224 -0
  70. data/app/javascript/controllers/nk/appearance_controller.js +73 -0
  71. data/app/javascript/controllers/nk/avatar_controller.js +15 -0
  72. data/app/javascript/controllers/nk/button_controller.js +56 -0
  73. data/app/javascript/controllers/nk/checkable_controller.js +41 -0
  74. data/app/javascript/controllers/nk/combobox_controller.js +314 -82
  75. data/app/javascript/controllers/nk/command_palette_controller.js +249 -0
  76. data/app/javascript/controllers/nk/dialog_controller.js +14 -10
  77. data/app/javascript/controllers/nk/dropdown_controller.js +106 -58
  78. data/app/javascript/controllers/nk/dropzone/direct_upload.js +61 -0
  79. data/app/javascript/controllers/nk/dropzone/file_rules.js +77 -0
  80. data/app/javascript/controllers/nk/dropzone/form_submit_lock.js +30 -0
  81. data/app/javascript/controllers/nk/dropzone_controller.js +482 -0
  82. data/app/javascript/controllers/nk/overlay_position.js +37 -0
  83. data/app/javascript/controllers/nk/progressive_image_controller.js +116 -0
  84. data/app/javascript/controllers/nk/tabs_controller.js +135 -23
  85. data/app/javascript/controllers/nk/toast_controller.js +112 -45
  86. data/app/javascript/controllers/nk/tooltip_controller.js +12 -47
  87. data/config/importmap.rb +2 -0
  88. data/config/locales/en.yml +98 -0
  89. data/docs/agent_guide.md +139 -0
  90. data/docs/agent_native_spec.md +422 -0
  91. data/docs/component_contracts.md +263 -0
  92. data/docs/customization.md +595 -0
  93. data/docs/hotwire.md +94 -0
  94. data/docs/initialization_prompt.md +36 -0
  95. data/docs/migration_1_to_2.md +318 -0
  96. data/docs/new_app_strategy.md +22 -0
  97. data/docs/patterns/application_foundation.md +136 -0
  98. data/docs/patterns/crud_resource.md +144 -0
  99. data/docs/patterns/destructive_action.md +105 -0
  100. data/docs/patterns/flash_and_toast.md +57 -0
  101. data/docs/patterns/inline_edit.md +59 -0
  102. data/docs/patterns/queryable_collection.md +187 -0
  103. data/docs/patterns/resource_form.md +126 -0
  104. data/docs/rails_conventions.md +95 -0
  105. data/docs/rails_integration.md +514 -0
  106. data/lib/generators/nitro_kit/install_generator.rb +48 -0
  107. data/lib/generators/nitro_kit/upgrade_smoke_tests_generator.rb +67 -0
  108. data/lib/nitro_kit/engine.rb +20 -0
  109. data/lib/nitro_kit/installation.rb +671 -0
  110. data/lib/nitro_kit/migration_inventory.rb +345 -0
  111. data/lib/nitro_kit/upgrade_smoke_test.rb +403 -0
  112. data/lib/nitro_kit/version.rb +1 -1
  113. data/lib/nitro_kit.rb +5 -42
  114. data/lib/rails/commands/nitro_kit/nitro_kit_command.rb +42 -0
  115. data/lib/tasks/nitro_kit_tasks.rake +79 -4
  116. data/plugins/nitro-kit/.codex-plugin/plugin.json +28 -0
  117. data/plugins/nitro-kit/skills/nitro-kit-hotwire/SKILL.md +67 -0
  118. data/plugins/nitro-kit/skills/nitro-kit-hotwire/agents/openai.yaml +4 -0
  119. data/plugins/nitro-kit/skills/nitro-kit-rails/SKILL.md +60 -0
  120. data/plugins/nitro-kit/skills/nitro-kit-rails/agents/openai.yaml +4 -0
  121. data/plugins/nitro-kit/skills/nitro-kit-ui/SKILL.md +62 -0
  122. data/plugins/nitro-kit/skills/nitro-kit-ui/agents/openai.yaml +4 -0
  123. data/src/stylesheets/nitro_kit/components/accordion.css +119 -0
  124. data/src/stylesheets/nitro_kit/components/alert.css +82 -0
  125. data/src/stylesheets/nitro_kit/components/app_navigation.css +156 -0
  126. data/src/stylesheets/nitro_kit/components/app_shell.css +605 -0
  127. data/src/stylesheets/nitro_kit/components/appearance_picker.css +117 -0
  128. data/src/stylesheets/nitro_kit/components/auth_shell.css +8 -0
  129. data/src/stylesheets/nitro_kit/components/avatar.css +81 -0
  130. data/src/stylesheets/nitro_kit/components/avatar_stack.css +80 -0
  131. data/src/stylesheets/nitro_kit/components/badge.css +76 -0
  132. data/src/stylesheets/nitro_kit/components/button.css +283 -0
  133. data/src/stylesheets/nitro_kit/components/button_group.css +53 -0
  134. data/src/stylesheets/nitro_kit/components/button_to.css +6 -0
  135. data/src/stylesheets/nitro_kit/components/card.css +70 -0
  136. data/src/stylesheets/nitro_kit/components/checkbox.css +160 -0
  137. data/src/stylesheets/nitro_kit/components/checkbox_group.css +61 -0
  138. data/src/stylesheets/nitro_kit/components/combobox.css +148 -0
  139. data/src/stylesheets/nitro_kit/components/command_palette.css +255 -0
  140. data/src/stylesheets/nitro_kit/components/container.css +25 -0
  141. data/src/stylesheets/nitro_kit/components/control_group.css +149 -0
  142. data/src/stylesheets/nitro_kit/components/danger_zone.css +54 -0
  143. data/src/stylesheets/nitro_kit/components/data_section.css +40 -0
  144. data/src/stylesheets/nitro_kit/components/details_table.css +53 -0
  145. data/src/stylesheets/nitro_kit/components/dialog.css +80 -0
  146. data/src/stylesheets/nitro_kit/components/dropdown.css +152 -0
  147. data/src/stylesheets/nitro_kit/components/dropzone.css +198 -0
  148. data/src/stylesheets/nitro_kit/components/empty_state.css +53 -0
  149. data/src/stylesheets/nitro_kit/components/field.css +79 -0
  150. data/src/stylesheets/nitro_kit/components/field_group.css +6 -0
  151. data/src/stylesheets/nitro_kit/components/fieldset.css +28 -0
  152. data/src/stylesheets/nitro_kit/components/flex.css +455 -0
  153. data/src/stylesheets/nitro_kit/components/form_section.css +71 -0
  154. data/src/stylesheets/nitro_kit/components/grid.css +308 -0
  155. data/src/stylesheets/nitro_kit/components/icon.css +26 -0
  156. data/src/stylesheets/nitro_kit/components/input.css +80 -0
  157. data/src/stylesheets/nitro_kit/components/label.css +9 -0
  158. data/src/stylesheets/nitro_kit/components/layout.css +440 -0
  159. data/src/stylesheets/nitro_kit/components/page_header.css +61 -0
  160. data/src/stylesheets/nitro_kit/components/pagination.css +119 -0
  161. data/src/stylesheets/nitro_kit/components/pagination_bar.css +41 -0
  162. data/src/stylesheets/nitro_kit/components/palette.css +113 -0
  163. data/src/stylesheets/nitro_kit/components/progressive_image.css +139 -0
  164. data/src/stylesheets/nitro_kit/components/radio_button.css +127 -0
  165. data/src/stylesheets/nitro_kit/components/radio_button_group.css +143 -0
  166. data/src/stylesheets/nitro_kit/components/rich_text_area.css +21 -0
  167. data/src/stylesheets/nitro_kit/components/select.css +70 -0
  168. data/src/stylesheets/nitro_kit/components/settings_layout.css +100 -0
  169. data/src/stylesheets/nitro_kit/components/sheet.css +100 -0
  170. data/src/stylesheets/nitro_kit/components/stat_grid.css +51 -0
  171. data/src/stylesheets/nitro_kit/components/switch.css +152 -0
  172. data/src/stylesheets/nitro_kit/components/table.css +116 -0
  173. data/src/stylesheets/nitro_kit/components/tabs.css +138 -0
  174. data/src/stylesheets/nitro_kit/components/textarea.css +39 -0
  175. data/src/stylesheets/nitro_kit/components/toast.css +131 -0
  176. data/src/stylesheets/nitro_kit/components/toolbar.css +78 -0
  177. data/src/stylesheets/nitro_kit/components/tooltip.css +124 -0
  178. data/src/stylesheets/nitro_kit/components/typeset.css +191 -0
  179. data/src/stylesheets/nitro_kit/layers.css +7 -0
  180. data/src/stylesheets/nitro_kit/reset.css +192 -0
  181. data/src/stylesheets/nitro_kit/tokens.css +198 -0
  182. metadata +182 -51
  183. data/Rakefile +0 -8
  184. data/app/assets/tailwind/application.css +0 -49
  185. data/app/components/nitro_kit/datepicker.rb +0 -9
  186. data/app/helpers/nitro_kit/accordion_helper.rb +0 -9
  187. data/app/helpers/nitro_kit/alert_helper.rb +0 -11
  188. data/app/helpers/nitro_kit/avatar_helper.rb +0 -13
  189. data/app/helpers/nitro_kit/badge_helper.rb +0 -13
  190. data/app/helpers/nitro_kit/button_group_helper.rb +0 -9
  191. data/app/helpers/nitro_kit/button_helper.rb +0 -49
  192. data/app/helpers/nitro_kit/card_helper.rb +0 -9
  193. data/app/helpers/nitro_kit/checkbox_helper.rb +0 -50
  194. data/app/helpers/nitro_kit/combobox_helper.rb +0 -9
  195. data/app/helpers/nitro_kit/datepicker_helper.rb +0 -9
  196. data/app/helpers/nitro_kit/dialog_helper.rb +0 -9
  197. data/app/helpers/nitro_kit/dropdown_helper.rb +0 -9
  198. data/app/helpers/nitro_kit/field_group_helper.rb +0 -9
  199. data/app/helpers/nitro_kit/field_helper.rb +0 -9
  200. data/app/helpers/nitro_kit/fieldset_helper.rb +0 -9
  201. data/app/helpers/nitro_kit/form_helper.rb +0 -13
  202. data/app/helpers/nitro_kit/icon_helper.rb +0 -9
  203. data/app/helpers/nitro_kit/input_helper.rb +0 -37
  204. data/app/helpers/nitro_kit/label_helper.rb +0 -19
  205. data/app/helpers/nitro_kit/pagination_helper.rb +0 -46
  206. data/app/helpers/nitro_kit/radio_button_helper.rb +0 -23
  207. data/app/helpers/nitro_kit/select_helper.rb +0 -24
  208. data/app/helpers/nitro_kit/switch_helper.rb +0 -9
  209. data/app/helpers/nitro_kit/table_helper.rb +0 -9
  210. data/app/helpers/nitro_kit/tabs_helper.rb +0 -9
  211. data/app/helpers/nitro_kit/textarea_helper.rb +0 -9
  212. data/app/helpers/nitro_kit/toast_helper.rb +0 -25
  213. data/app/helpers/nitro_kit/tooltip_helper.rb +0 -9
  214. data/app/javascript/controllers/nk/accordion_controller.js +0 -18
  215. data/app/javascript/controllers/nk/datepicker_controller.js +0 -5
  216. data/app/javascript/controllers/nk/switch_controller.js +0 -32
  217. data/lib/generators/nitro_kit/component_generator.rb +0 -84
  218. data/lib/nitro_kit/schema_builder.rb +0 -129
  219. data/lib/nitro_kit/variants.rb +0 -21
@@ -1,19 +1,23 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  export default class extends Controller {
4
- static targets = ["trigger", "dialog"];
4
+ static targets = ["panel"];
5
+ static values = { dismissible: Boolean };
5
6
 
6
- open() {
7
- this.dialogTarget.showModal();
8
- }
7
+ dismiss(event) {
8
+ if (!this.dismissibleValue || event.target !== this.panelTarget) return;
9
+
10
+ const rect = this.panelTarget.getBoundingClientRect();
11
+ const inside =
12
+ event.clientX >= rect.left &&
13
+ event.clientX <= rect.right &&
14
+ event.clientY >= rect.top &&
15
+ event.clientY <= rect.bottom;
9
16
 
10
- close() {
11
- this.dialogTarget.close();
17
+ if (!inside) this.panelTarget.close();
12
18
  }
13
19
 
14
- clickOutside(event) {
15
- if (event.target === this.dialogTarget) {
16
- this.close();
17
- }
20
+ cancel(event) {
21
+ if (!this.dismissibleValue) event.preventDefault();
18
22
  }
19
23
  }
@@ -1,78 +1,126 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
2
  import {
3
- computePosition,
4
- offset,
5
- flip,
6
- shift,
7
- autoUpdate,
8
- } from "@floating-ui/dom";
3
+ observeOverlayPosition,
4
+ positionOverlay,
5
+ } from "controllers/nk/overlay_position";
9
6
 
10
7
  export default class extends Controller {
11
- static targets = ["trigger", "content"];
12
- static values = {
13
- placement: { type: String, default: "bottom" },
14
- };
8
+ static targets = ["trigger", "content", "item"];
15
9
 
16
- connect() {
17
- this.updatePosition();
10
+ disconnect() {
11
+ this.stopPositioning?.();
18
12
  }
19
13
 
20
- disconnect() {
21
- this.close();
14
+ openFromKeyboard(event) {
15
+ if (!["ArrowDown", "ArrowUp"].includes(event.key)) return;
16
+ if (this.triggerTarget.disabled) return;
17
+
18
+ event.preventDefault();
19
+ this.focusLast = event.key === "ArrowUp";
20
+
21
+ if (this.contentTarget.matches(":popover-open")) {
22
+ this.focusInitialItem();
23
+ } else {
24
+ this.contentTarget.showPopover();
25
+ }
22
26
  }
23
27
 
24
- get isExpanded() {
25
- return this.triggerTarget.getAttribute("aria-expanded") === "true";
28
+ rememberFocus(event) {
29
+ if (event.newState === "open") return;
30
+
31
+ this.hadFocus =
32
+ this.restoreOnClose !== false &&
33
+ this.contentTarget.contains(document.activeElement);
26
34
  }
27
35
 
28
- updatePosition = () => {
29
- computePosition(this.triggerTarget, this.contentTarget, {
30
- placement: this.placementValue,
31
- middleware: [offset(5), flip(), shift({ padding: 5 })],
32
- }).then(({ x, y }) => {
33
- this.contentTarget.style.left = `${x}px`;
34
- this.contentTarget.style.top = `${y}px`;
35
- });
36
- };
37
-
38
- open = () => {
39
- this.triggerTarget.setAttribute("aria-expanded", "true");
40
- this.contentTarget.setAttribute("aria-hidden", "false");
41
-
42
- document.addEventListener("click", this.clickOutside);
43
-
44
- this.clearAutoUpdate = autoUpdate(
45
- this.triggerTarget,
46
- this.contentTarget,
47
- this.updatePosition,
48
- );
49
- };
36
+ focusOpened(event) {
37
+ if (event.newState === "open") {
38
+ this.startPositioning();
39
+ this.focusInitialItem();
40
+ } else {
41
+ this.stopPositioning?.();
42
+ this.stopPositioning = null;
43
+ this.focusLast = false;
44
+ this.restoreFocus();
45
+ }
46
+ }
50
47
 
51
- close = () => {
52
- this.triggerTarget.setAttribute("aria-expanded", "false");
53
- this.contentTarget.setAttribute("aria-hidden", "true");
48
+ restoreFocus() {
49
+ if (!this.hadFocus) return;
54
50
 
55
- document.removeEventListener("click", this.clickOutside);
51
+ this.hadFocus = false;
52
+ this.triggerTarget.focus();
53
+ }
54
+
55
+ startPositioning() {
56
+ this.stopPositioning?.();
57
+ const update = () =>
58
+ positionOverlay(
59
+ this.triggerTarget,
60
+ this.contentTarget,
61
+ this.element.dataset.placement,
62
+ );
63
+
64
+ update();
65
+ this.stopPositioning = observeOverlayPosition(update);
66
+ }
56
67
 
57
- if (this.clearAutoUpdate) {
58
- this.clearAutoUpdate();
68
+ navigate(event) {
69
+ switch (event.key) {
70
+ case "Escape":
71
+ event.preventDefault();
72
+ this.hide();
73
+ return;
74
+ case "Tab":
75
+ this.hide({ restoreFocus: false });
76
+ return;
59
77
  }
60
- };
61
78
 
62
- toggle = () => {
63
- if (this.isExpanded) {
64
- this.close();
79
+ const items = this.enabledItems;
80
+ if (items.length === 0) return;
81
+
82
+ const currentIndex = items.indexOf(document.activeElement);
83
+ const nextIndex = {
84
+ ArrowDown: (currentIndex + 1) % items.length,
85
+ ArrowUp: (currentIndex - 1 + items.length) % items.length,
86
+ Home: 0,
87
+ End: items.length - 1,
88
+ }[event.key];
89
+
90
+ if (nextIndex === undefined) return;
91
+
92
+ event.preventDefault();
93
+ items[nextIndex].focus();
94
+ }
95
+
96
+ select() {
97
+ this.hide();
98
+ }
99
+
100
+ focusInitialItem() {
101
+ const item = this.focusLast
102
+ ? this.enabledItems.at(-1)
103
+ : this.enabledItems[0];
104
+
105
+ this.focusLast = false;
106
+ if (item) {
107
+ item.focus();
65
108
  } else {
66
- this.open();
109
+ this.contentTarget.focus();
67
110
  }
68
- };
69
-
70
- clickOutside = (event) => {
71
- if (
72
- !this.contentTarget.contains(event.target) &&
73
- !this.triggerTarget.contains(event.target)
74
- ) {
75
- this.close();
111
+ }
112
+
113
+ hide({ restoreFocus = true } = {}) {
114
+ this.restoreOnClose = restoreFocus;
115
+ if (this.contentTarget.matches(":popover-open")) {
116
+ this.contentTarget.hidePopover();
76
117
  }
77
- };
118
+ this.restoreOnClose = true;
119
+ }
120
+
121
+ get enabledItems() {
122
+ return this.itemTargets.filter(
123
+ (item) => !item.disabled && item.getAttribute("aria-disabled") !== "true",
124
+ );
125
+ }
78
126
  }
@@ -0,0 +1,61 @@
1
+ import { DirectUpload } from "@rails/activestorage";
2
+
3
+ export default class DirectUploadSession {
4
+ constructor(file, url, { onProgress, onComplete }) {
5
+ this.file = file;
6
+ this.url = url;
7
+ this.onProgress = onProgress;
8
+ this.onComplete = onComplete;
9
+ }
10
+
11
+ start() {
12
+ this.upload = new DirectUpload(this.file, this.url, {
13
+ directUploadWillCreateBlobWithXHR: (request) => {
14
+ this.bindRequest(request);
15
+ },
16
+ directUploadWillStoreFileWithXHR: (request) => {
17
+ this.bindRequest(request, { tracksProgress: true });
18
+ },
19
+ });
20
+
21
+ this.upload.create((error, attributes) => {
22
+ if (this.cancelled) return;
23
+
24
+ this.releaseRequest();
25
+ this.onComplete(error, attributes);
26
+ });
27
+ }
28
+
29
+ cancel() {
30
+ this.cancelled = true;
31
+ this.request?.abort();
32
+ this.releaseRequest();
33
+ }
34
+
35
+ bindRequest(request, { tracksProgress = false } = {}) {
36
+ if (this.cancelled) {
37
+ queueMicrotask(() => request.abort());
38
+ return;
39
+ }
40
+
41
+ this.releaseRequest();
42
+ this.request = request;
43
+
44
+ if (tracksProgress) {
45
+ this.progressListener = (event) => this.onProgress(event);
46
+ request.upload.addEventListener("progress", this.progressListener);
47
+ }
48
+ }
49
+
50
+ releaseRequest() {
51
+ if (this.request && this.progressListener) {
52
+ this.request.upload.removeEventListener(
53
+ "progress",
54
+ this.progressListener,
55
+ );
56
+ }
57
+
58
+ this.request = null;
59
+ this.progressListener = null;
60
+ }
61
+ }
@@ -0,0 +1,77 @@
1
+ // Every reason is a caller-supplied template so the locale layer, not this
2
+ // module, owns user-facing text.
3
+ export function interpolate(template, replacements = {}) {
4
+ return template.replace(/%\{(\w+)\}/g, (match, name) =>
5
+ Object.hasOwn(replacements, name) ? String(replacements[name]) : match,
6
+ );
7
+ }
8
+
9
+ export function validateFiles(files, { maxFiles, maxBytes, accept, messages }) {
10
+ let acceptedCount = 0;
11
+ const selections = files.map((file) => {
12
+ const rejection = fileRejection(file, { maxBytes, accept, messages });
13
+ if (rejection) return rejection;
14
+
15
+ if (acceptedCount >= maxFiles) {
16
+ return {
17
+ file,
18
+ reason: interpolate(
19
+ maxFiles === 1 ? messages.maxFilesOne : messages.maxFilesOther,
20
+ { count: maxFiles },
21
+ ),
22
+ };
23
+ }
24
+
25
+ acceptedCount += 1;
26
+ return { file, reason: null };
27
+ });
28
+
29
+ return {
30
+ accepted: selections
31
+ .filter(({ reason }) => !reason)
32
+ .map(({ file }) => file),
33
+ rejected: selections.filter(({ reason }) => reason),
34
+ };
35
+ }
36
+
37
+ export function rejectionMessage(rejected) {
38
+ return Array.from(new Set(rejected.map(({ reason }) => reason))).join(" ");
39
+ }
40
+
41
+ export function formatBytes(bytes) {
42
+ if (bytes < 1024) return `${bytes} B`;
43
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
44
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
45
+ }
46
+
47
+ function fileRejection(file, { maxBytes, accept, messages }) {
48
+ if (maxBytes && file.size > maxBytes) {
49
+ return {
50
+ file,
51
+ reason: interpolate(messages.tooLarge, {
52
+ name: file.name,
53
+ size: formatBytes(maxBytes),
54
+ }),
55
+ };
56
+ }
57
+ if (accept && !accepts(file, accept)) {
58
+ return {
59
+ file,
60
+ reason: interpolate(messages.notAccepted, { name: file.name }),
61
+ };
62
+ }
63
+
64
+ return null;
65
+ }
66
+
67
+ function accepts(file, accept) {
68
+ return accept.split(",").some((rawRule) => {
69
+ const rule = rawRule.trim().toLowerCase();
70
+ const type = file.type.toLowerCase();
71
+ const name = file.name.toLowerCase();
72
+
73
+ if (rule.startsWith(".")) return name.endsWith(rule);
74
+ if (rule.endsWith("/*")) return type.startsWith(rule.slice(0, -1));
75
+ return type === rule;
76
+ });
77
+ }
@@ -0,0 +1,30 @@
1
+ const submitControlSelector = "button[type='submit'], input[type='submit']";
2
+ const locks = new WeakMap();
3
+
4
+ export function acquireFormSubmitLock(form, owner) {
5
+ if (!form) return;
6
+
7
+ let lock = locks.get(form);
8
+ if (!lock) {
9
+ lock = { owners: new Set(), controls: new Map() };
10
+ locks.set(form, lock);
11
+ }
12
+
13
+ form.querySelectorAll(submitControlSelector).forEach((control) => {
14
+ if (!lock.controls.has(control)) {
15
+ lock.controls.set(control, control.disabled);
16
+ }
17
+ control.disabled = true;
18
+ });
19
+ lock.owners.add(owner);
20
+ }
21
+
22
+ export function releaseFormSubmitLock(form, owner) {
23
+ const lock = form && locks.get(form);
24
+ if (!lock || !lock.owners.delete(owner) || lock.owners.size > 0) return;
25
+
26
+ lock.controls.forEach((initiallyDisabled, control) => {
27
+ if (control.isConnected) control.disabled = initiallyDisabled;
28
+ });
29
+ locks.delete(form);
30
+ }