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,130 +1,362 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
- import Combobox from "@github/combobox-nav";
3
2
  import {
4
- computePosition,
5
- offset,
6
- flip,
7
- shift,
8
- autoUpdate,
9
- } from "@floating-ui/dom";
3
+ observeOverlayPosition,
4
+ positionOverlay,
5
+ } from "controllers/nk/overlay_position";
6
+
7
+ // The Combobox component translates these and passes them in as Stimulus
8
+ // values. They remain here as the shipped English fallbacks.
9
+ const INVALID_SELECTION_MESSAGE = "Choose an option.";
10
+ const NO_RESULTS_MESSAGE = "No options found.";
11
+ const RESULTS_ONE_MESSAGE = "%{count} option available.";
12
+ const RESULTS_OTHER_MESSAGE = "%{count} options available.";
13
+
14
+ const OPTION_LABEL_SELECTOR = '[data-slot="combobox-option-label"]';
10
15
 
11
16
  export default class extends Controller {
12
- static targets = ["input", "list", "hiddenField", "clearButton"];
17
+ static targets = [
18
+ "control",
19
+ "input",
20
+ "native",
21
+ "value",
22
+ "listbox",
23
+ "option",
24
+ "status",
25
+ ];
13
26
  static values = {
14
- open: { type: Boolean, default: false },
15
- // Options for floating-ui
16
- placement: { type: String },
17
- // Options for combobox-nav
18
- tabInsertsSuggestions: { type: Boolean },
19
- firstOptionSelectionMode: { type: String },
20
- scrollIntoViewOptions: { type: Object },
27
+ open: Boolean,
28
+ required: Boolean,
29
+ invalidSelection: String,
30
+ noResults: String,
31
+ resultsOne: String,
32
+ resultsOther: String,
21
33
  };
22
34
 
23
- connect() {
24
- this.combobox = new Combobox(this.inputTarget, this.listTarget, {
25
- tabInsertsSuggestions: this.tabInsertsSuggestionsValue,
26
- firstOptionSelectionMode: this.firstOptionSelectionModeValue,
27
- scrollIntoViewOptions: this.scrollIntoViewOptionsValue,
28
- });
35
+ get invalidSelectionMessage() {
36
+ return this.invalidSelectionValue || INVALID_SELECTION_MESSAGE;
37
+ }
29
38
 
30
- this.updatePosition();
39
+ get noResultsMessage() {
40
+ return this.noResultsValue || NO_RESULTS_MESSAGE;
41
+ }
31
42
 
32
- this.listTarget.addEventListener("combobox-commit", (event) => {
33
- this.select(event);
34
- this.close();
35
- });
43
+ resultsMessage(count) {
44
+ const template =
45
+ count === 1
46
+ ? this.resultsOneValue || RESULTS_ONE_MESSAGE
47
+ : this.resultsOtherValue || RESULTS_OTHER_MESSAGE;
48
+
49
+ return template.replace("%{count}", String(count));
50
+ }
51
+
52
+ connect() {
53
+ this.activeOption = null;
54
+ this.enhanced = true;
55
+ this.nativeRequired = this.requiredValue;
56
+ this.element.dataset.enhanced = "true";
57
+ this.controlTarget.hidden = false;
58
+ this.nativeTarget.hidden = true;
59
+ this.valueTarget.required = false;
60
+ this.syncSelection();
61
+ this.syncValidity();
62
+ this.reflectOpenState();
36
63
  }
37
64
 
38
65
  disconnect() {
39
- this.combobox.destroy();
66
+ this.stopPositioning?.();
67
+ this.enhanced = false;
68
+ const control = this.element.querySelector(
69
+ ':scope > [data-slot="combobox-control"]',
70
+ );
71
+ const input = control?.querySelector('[data-nk--combobox-target~="input"]');
72
+ const native = this.element.querySelector(
73
+ ':scope > [data-slot="combobox-native"]',
74
+ );
75
+ const value = native?.querySelector('[data-nk--combobox-target~="value"]');
76
+ const listbox = this.element.querySelector(
77
+ ':scope > [data-slot="combobox-listbox"]',
78
+ );
79
+ const status = this.element.querySelector(
80
+ ':scope > [data-slot="combobox-status"]',
81
+ );
82
+
83
+ this.element.setAttribute("data-nk--combobox-open-value", "false");
84
+ this.element.dataset.state = "closed";
85
+ if (listbox) {
86
+ listbox.dataset.state = "closed";
87
+ listbox.hidden = true;
88
+ }
89
+ if (input) {
90
+ input.setAttribute("aria-expanded", "false");
91
+ input.setCustomValidity("");
92
+ input.removeAttribute("aria-activedescendant");
93
+ }
94
+ if (control) control.hidden = true;
95
+ if (native) native.hidden = false;
96
+ if (value) value.required = this.nativeRequired;
97
+ if (status) status.textContent = "";
98
+ delete this.element.dataset.enhanced;
40
99
  }
41
100
 
42
- select(event) {
43
- this.inputTarget.value = event.target.textContent;
44
- this.hiddenFieldTarget.value =
45
- event.target.dataset.value || event.target.textContent;
101
+ controlTargetConnected(control) {
102
+ if (this.enhanced) control.hidden = false;
103
+ }
104
+
105
+ nativeTargetConnected(native) {
106
+ if (this.enhanced) native.hidden = true;
107
+ }
108
+
109
+ valueTargetConnected(value) {
110
+ if (!this.enhanced) return;
111
+
112
+ this.nativeRequired = this.requiredValue;
113
+ value.required = false;
114
+ if (this.hasInputTarget) {
115
+ this.syncSelection();
116
+ this.syncValidity();
117
+ }
118
+ }
119
+
120
+ inputTargetConnected() {
121
+ if (!this.enhanced) return;
122
+
123
+ if (this.hasValueTarget) this.syncValidity();
124
+ this.reflectOpenState();
125
+ }
126
+
127
+ inputTargetDisconnected() {
128
+ this.activeOption = null;
129
+ }
130
+
131
+ listboxTargetConnected() {
132
+ if (this.enhanced) this.reflectOpenState();
133
+ }
134
+
135
+ optionTargetConnected() {
136
+ if (this.enhanced && this.hasValueTarget) this.syncSelection();
137
+ }
138
+
139
+ optionTargetDisconnected(option) {
140
+ if (this.activeOption === option) this.setActive(null);
46
141
  }
47
142
 
48
143
  open() {
144
+ if (this.inputTarget.disabled) return;
145
+
49
146
  this.openValue = true;
50
147
  }
51
148
 
52
149
  close() {
53
150
  this.openValue = false;
151
+ this.setActive(null);
54
152
  }
55
153
 
56
- focusShift({ target }) {
57
- if (!this.openValue) return;
58
- if (this.element.contains(target)) return;
59
-
60
- this.close();
154
+ closeFromOutside(event) {
155
+ if (!this.element.contains(event.target)) this.close();
61
156
  }
62
157
 
63
- windowClick({ target }) {
64
- if (!this.openValue) return;
65
- if (this.element.contains(target)) return;
158
+ filter() {
159
+ const query = this.inputTarget.value.trim().toLocaleLowerCase();
160
+ let exactMatch = null;
66
161
 
67
- this.close();
162
+ this.optionTargets.forEach((option) => {
163
+ const label = this.optionLabel(option);
164
+ option.hidden = !label.toLocaleLowerCase().includes(query);
165
+
166
+ if (
167
+ label.toLocaleLowerCase() === query &&
168
+ option.getAttribute("aria-disabled") !== "true"
169
+ ) {
170
+ exactMatch = option;
171
+ }
172
+ });
173
+
174
+ this.setSubmittedValue(exactMatch?.dataset.value ?? "");
175
+ this.syncSelection();
176
+ this.setActive(null);
177
+ this.syncValidity();
178
+ this.announceResults();
179
+ this.open();
68
180
  }
69
181
 
70
- clear() {
71
- this.combobox.resetSelection();
72
- this.inputTarget.value = "";
73
- this.input();
74
- this.hiddenFieldTarget.value = "";
182
+ select(event) {
183
+ this.commit(event.currentTarget);
75
184
  }
76
185
 
77
- input(_event) {
78
- if (!this.isOpen) this.open();
186
+ activate(event) {
187
+ this.setActive(event.currentTarget);
188
+ }
79
189
 
80
- const filter = this.inputTarget.value.toLowerCase();
190
+ navigate(event) {
191
+ const options = this.visibleOptions;
192
+ const currentIndex = options.indexOf(this.activeOption);
193
+ let nextIndex;
81
194
 
82
- Array.from(this.listTarget.children).forEach((item) => {
83
- const value = item.dataset.value?.toLowerCase();
84
- const text = item.textContent.toLowerCase();
195
+ switch (event.key) {
196
+ case "ArrowDown":
197
+ event.preventDefault();
198
+ this.open();
199
+ nextIndex = (currentIndex + 1) % options.length;
200
+ break;
201
+ case "ArrowUp":
202
+ event.preventDefault();
203
+ this.open();
204
+ nextIndex = (currentIndex - 1 + options.length) % options.length;
205
+ break;
206
+ case "Home":
207
+ if (!this.openValue) return;
208
+ event.preventDefault();
209
+ nextIndex = 0;
210
+ break;
211
+ case "End":
212
+ if (!this.openValue) return;
213
+ event.preventDefault();
214
+ nextIndex = options.length - 1;
215
+ break;
216
+ case "Enter":
217
+ if (!this.openValue || !this.activeOption) return;
218
+ event.preventDefault();
219
+ this.commit(this.activeOption);
220
+ return;
221
+ case "Escape":
222
+ if (!this.openValue) return;
223
+ event.preventDefault();
224
+ this.close();
225
+ return;
226
+ case "Tab":
227
+ this.close();
228
+ this.syncValidity();
229
+ return;
230
+ default:
231
+ return;
232
+ }
85
233
 
86
- if (value?.includes(filter) || text.includes(filter)) {
87
- item.setAttribute("role", "option");
88
- } else {
89
- item.removeAttribute("role");
90
- }
91
- });
234
+ if (options.length > 0) this.setActive(options[nextIndex]);
235
+ }
92
236
 
93
- this.hiddenFieldTarget.value = this.inputTarget.value;
237
+ validate() {
238
+ this.syncValidity();
94
239
  }
95
240
 
96
- openValueChanged(state, _previous) {
97
- if (!this.combobox) return;
241
+ openValueChanged(open) {
242
+ if (!this.enhanced) return;
98
243
 
99
- if (state) {
100
- this.combobox.start();
244
+ this.reflectOpenState(open);
245
+ }
101
246
 
102
- this.listTarget.dataset.state = "open";
247
+ reflectOpenState(open = this.openValue) {
248
+ if (!this.hasListboxTarget || !this.hasInputTarget) return;
103
249
 
104
- this.clearAutoUpdate = autoUpdate(
105
- this.inputTarget,
106
- this.listTarget,
107
- this.updatePosition,
250
+ const state = open ? "open" : "closed";
251
+ this.element.dataset.state = state;
252
+ this.listboxTarget.dataset.state = state;
253
+ this.listboxTarget.hidden = !open;
254
+ this.inputTarget.setAttribute("aria-expanded", String(open));
255
+
256
+ if (open && (!this.activeOption || !this.activeOption.isConnected)) {
257
+ const selected = this.optionTargets.find(
258
+ (option) =>
259
+ option.getAttribute("aria-selected") === "true" && !option.hidden,
260
+ );
261
+ this.setActive(selected ?? this.visibleOptions[0] ?? null);
262
+ } else if (open && this.activeOption) {
263
+ this.inputTarget.setAttribute(
264
+ "aria-activedescendant",
265
+ this.activeOption.id,
108
266
  );
267
+ }
268
+
269
+ if (open) {
270
+ this.startPositioning();
109
271
  } else {
110
- this.combobox.stop();
272
+ this.stopPositioning?.();
273
+ this.stopPositioning = null;
274
+ }
275
+ }
111
276
 
112
- this.listTarget.dataset.state = "closed";
277
+ startPositioning() {
278
+ this.stopPositioning?.();
279
+ const update = () =>
280
+ positionOverlay(
281
+ this.inputTarget,
282
+ this.listboxTarget,
283
+ this.element.dataset.placement,
284
+ );
113
285
 
114
- if (this.clearAutoUpdate) {
115
- this.clearAutoUpdate();
116
- }
117
- }
286
+ update();
287
+ this.stopPositioning = observeOverlayPosition(update);
118
288
  }
119
289
 
120
- updatePosition = () => {
121
- computePosition(this.inputTarget, this.listTarget, {
122
- placement: this.placementValue,
123
- middleware: [offset(5), flip(), shift({ padding: 5 })],
124
- }).then(({ x, y }) => {
125
- this.listTarget.style.left = `${x}px`;
126
- this.listTarget.style.top = `${y}px`;
127
- this.listTarget.style.width = `${this.inputTarget.clientWidth}px`;
290
+ commit(option) {
291
+ if (!option || option.getAttribute("aria-disabled") === "true") return;
292
+
293
+ this.inputTarget.value = this.optionLabel(option);
294
+ this.setSubmittedValue(option.dataset.value);
295
+ this.syncSelection();
296
+ this.syncValidity();
297
+ this.close();
298
+ this.inputTarget.focus();
299
+ }
300
+
301
+ setSubmittedValue(value) {
302
+ if (this.valueTarget.value === value) return;
303
+
304
+ this.valueTarget.value = value;
305
+ this.valueTarget.dispatchEvent(new Event("change", { bubbles: true }));
306
+ }
307
+
308
+ syncSelection() {
309
+ this.optionTargets.forEach((option) => {
310
+ const selected = option.dataset.value === this.valueTarget.value;
311
+ option.setAttribute("aria-selected", String(selected));
312
+ option.dataset.state = selected ? "selected" : "unselected";
128
313
  });
129
- };
314
+ }
315
+
316
+ syncValidity() {
317
+ const hasVisibleValue = this.inputTarget.value.trim() !== "";
318
+ const hasSubmittedValue = this.valueTarget.value !== "";
319
+ const invalid =
320
+ (this.requiredValue && !hasSubmittedValue) ||
321
+ (hasVisibleValue && !hasSubmittedValue);
322
+ this.inputTarget.setCustomValidity(
323
+ invalid ? this.invalidSelectionMessage : "",
324
+ );
325
+ this.inputTarget.setAttribute("aria-invalid", String(invalid));
326
+ }
327
+
328
+ setActive(option) {
329
+ this.optionTargets.forEach((candidate) => {
330
+ candidate.dataset.active = String(candidate === option);
331
+ });
332
+
333
+ this.activeOption = option;
334
+
335
+ if (option && this.hasInputTarget) {
336
+ this.inputTarget.setAttribute("aria-activedescendant", option.id);
337
+ option.scrollIntoView({ block: "nearest" });
338
+ } else if (this.hasInputTarget) {
339
+ this.inputTarget.removeAttribute("aria-activedescendant");
340
+ }
341
+ }
342
+
343
+ optionLabel(option) {
344
+ const label = option.querySelector(OPTION_LABEL_SELECTOR) ?? option;
345
+
346
+ return label.textContent.trim();
347
+ }
348
+
349
+ announceResults() {
350
+ const count = this.optionTargets.filter((option) => !option.hidden).length;
351
+
352
+ this.statusTarget.textContent =
353
+ count === 0 ? this.noResultsMessage : this.resultsMessage(count);
354
+ }
355
+
356
+ get visibleOptions() {
357
+ return this.optionTargets.filter(
358
+ (option) =>
359
+ !option.hidden && option.getAttribute("aria-disabled") !== "true",
360
+ );
361
+ }
130
362
  }
@@ -0,0 +1,249 @@
1
+ import { Controller } from "@hotwired/stimulus";
2
+
3
+ export default class extends Controller {
4
+ static targets = [
5
+ "trigger",
6
+ "panel",
7
+ "search",
8
+ "form",
9
+ "frame",
10
+ "input",
11
+ "destination",
12
+ "empty",
13
+ "status",
14
+ ];
15
+ static values = {
16
+ empty: String,
17
+ resultsOne: String,
18
+ resultsOther: String,
19
+ };
20
+
21
+ connect() {
22
+ this.connected = true;
23
+ this.restoreFocus = true;
24
+ this.loadedQuery = this.inputTarget.value;
25
+ this.triggerTarget.removeAttribute("command");
26
+ this.searchTarget.hidden = false;
27
+ this.element.dataset.enhanced = "true";
28
+ }
29
+
30
+ disconnect() {
31
+ this.connected = false;
32
+ clearTimeout(this.searchTimer);
33
+ const trigger = this.element.querySelector(
34
+ ':scope > [data-slot="command-palette-trigger"]',
35
+ );
36
+ const panel = this.element.querySelector(
37
+ ':scope > [data-slot="command-palette-panel"]',
38
+ );
39
+ const search = panel?.querySelector('[data-slot="command-palette-search"]');
40
+ const input = panel?.querySelector('[data-slot="command-palette-input"]');
41
+ const empty = panel?.querySelector('[data-slot="command-palette-empty"]');
42
+ const status = panel?.querySelector('[data-slot="command-palette-status"]');
43
+
44
+ this.restoreFocus = false;
45
+ if (panel?.open) panel.close();
46
+ if (input) input.value = "";
47
+ panel
48
+ ?.querySelectorAll('[data-slot="command-palette-destination"]')
49
+ .forEach((destination) => {
50
+ destination.hidden = false;
51
+ });
52
+ if (empty) empty.hidden = true;
53
+ if (status) status.textContent = "";
54
+ trigger?.setAttribute("command", "show-modal");
55
+ if (search) search.hidden = true;
56
+ delete this.element.dataset.enhanced;
57
+ }
58
+
59
+ open(event) {
60
+ event?.preventDefault();
61
+ if (!this.canOpen) return;
62
+
63
+ this.returnFocus = document.activeElement;
64
+ this.restoreFocus = true;
65
+ this.reset();
66
+ this.panelTarget.showModal();
67
+ this.inputTarget.focus();
68
+ if (this.hasFormTarget) this.submitSearch();
69
+ }
70
+
71
+ shortcut(event) {
72
+ if (
73
+ event.defaultPrevented ||
74
+ event.repeat ||
75
+ event.isComposing ||
76
+ event.altKey ||
77
+ event.shiftKey ||
78
+ event.key.toLocaleLowerCase() !== "k" ||
79
+ (!event.metaKey && !event.ctrlKey)
80
+ )
81
+ return;
82
+
83
+ if (this.panelTarget.open) {
84
+ event.preventDefault();
85
+ this.panelTarget.close();
86
+ } else {
87
+ if (!this.canOpen) return;
88
+
89
+ event.preventDefault();
90
+ this.open();
91
+ }
92
+ }
93
+
94
+ filter() {
95
+ const query = this.inputTarget.value.trim().toLocaleLowerCase();
96
+
97
+ this.destinationTargets.forEach((destination) => {
98
+ destination.hidden = !destination.textContent
99
+ .toLocaleLowerCase()
100
+ .includes(query);
101
+ });
102
+
103
+ this.updateResultState();
104
+ }
105
+
106
+ search() {
107
+ clearTimeout(this.searchTimer);
108
+ this.searchTimer = setTimeout(() => this.submitSearch(), 150);
109
+ }
110
+
111
+ navigateInput(event) {
112
+ const destinations = this.visibleDestinations;
113
+
114
+ if (
115
+ event.key === "Enter" &&
116
+ this.hasFormTarget &&
117
+ this.inputTarget.value !== this.loadedQuery
118
+ ) {
119
+ event.preventDefault();
120
+ this.submitSearch();
121
+ return;
122
+ }
123
+
124
+ if (destinations.length === 0) return;
125
+
126
+ if (event.key === "Enter") {
127
+ event.preventDefault();
128
+ destinations[0].click();
129
+ } else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
130
+ event.preventDefault();
131
+ (event.key === "ArrowDown"
132
+ ? destinations[0]
133
+ : destinations.at(-1)
134
+ ).focus();
135
+ }
136
+ }
137
+
138
+ navigateDestination(event) {
139
+ const destinations = this.visibleDestinations;
140
+ const currentIndex = destinations.indexOf(event.currentTarget);
141
+ const nextIndex = {
142
+ ArrowDown: (currentIndex + 1) % destinations.length,
143
+ ArrowUp: (currentIndex - 1 + destinations.length) % destinations.length,
144
+ Home: 0,
145
+ End: destinations.length - 1,
146
+ }[event.key];
147
+
148
+ if (nextIndex === undefined) return;
149
+
150
+ event.preventDefault();
151
+ destinations[nextIndex].focus();
152
+ }
153
+
154
+ select() {
155
+ this.restoreFocus = false;
156
+ this.panelTarget.close();
157
+ }
158
+
159
+ closeForVisit() {
160
+ if (!this.panelTarget.open) return;
161
+
162
+ this.restoreFocus = false;
163
+ this.panelTarget.close();
164
+ }
165
+
166
+ loading() {
167
+ this.frameTarget.setAttribute("aria-busy", "true");
168
+ }
169
+
170
+ loaded() {
171
+ this.frameTarget.removeAttribute("aria-busy");
172
+ }
173
+
174
+ destinationTargetConnected() {
175
+ this.scheduleResultUpdate();
176
+ }
177
+
178
+ destinationTargetDisconnected() {
179
+ this.scheduleResultUpdate();
180
+ }
181
+
182
+ scheduleResultUpdate() {
183
+ if (this.resultUpdateScheduled) return;
184
+
185
+ this.resultUpdateScheduled = true;
186
+ queueMicrotask(() => {
187
+ this.resultUpdateScheduled = false;
188
+ if (!this.connected || !this.panelTarget.open || !this.hasFormTarget)
189
+ return;
190
+
191
+ this.loadedQuery = this.inputTarget.value;
192
+ this.updateResultState();
193
+ });
194
+ }
195
+
196
+ closed() {
197
+ const returnFocus = this.returnFocus;
198
+
199
+ this.reset();
200
+ this.returnFocus = null;
201
+ if (this.restoreFocus && returnFocus?.isConnected) returnFocus.focus();
202
+ this.restoreFocus = true;
203
+ }
204
+
205
+ reset() {
206
+ clearTimeout(this.searchTimer);
207
+ if (this.hasInputTarget) this.inputTarget.value = "";
208
+ this.destinationTargets.forEach((destination) => {
209
+ destination.hidden = false;
210
+ });
211
+ if (this.hasEmptyTarget) this.emptyTarget.hidden = true;
212
+ if (this.hasStatusTarget) this.statusTarget.textContent = "";
213
+ }
214
+
215
+ resultsMessage(count) {
216
+ const template =
217
+ count === 1 ? this.resultsOneValue : this.resultsOtherValue;
218
+
219
+ return template.replace("%{count}", String(count));
220
+ }
221
+
222
+ submitSearch() {
223
+ clearTimeout(this.searchTimer);
224
+ this.formTarget.requestSubmit();
225
+ }
226
+
227
+ updateResultState() {
228
+ const count = this.visibleDestinations.length;
229
+ this.emptyTarget.hidden = count !== 0;
230
+ this.statusTarget.textContent =
231
+ count === 0 ? this.emptyValue : this.resultsMessage(count);
232
+ }
233
+
234
+ get visibleDestinations() {
235
+ return this.destinationTargets.filter((destination) => !destination.hidden);
236
+ }
237
+
238
+ get canOpen() {
239
+ if (
240
+ this.panelTarget.open ||
241
+ this.triggerTarget.getClientRects().length === 0
242
+ )
243
+ return false;
244
+
245
+ const activeModal = document.querySelector("dialog:modal");
246
+
247
+ return !activeModal || activeModal.contains(this.element);
248
+ }
249
+ }