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.
- checksums.yaml +4 -4
- data/.agents/plugins/marketplace.json +20 -0
- data/CHANGELOG.md +172 -0
- data/LICENSE +28 -0
- data/README.md +22 -15
- data/STYLE_GUIDE.md +425 -0
- data/app/assets/stylesheets/nitro_kit-tailwind-v4.css +38 -0
- data/app/assets/stylesheets/nitro_kit.css +7239 -0
- data/app/components/nitro_kit/accordion.rb +125 -65
- data/app/components/nitro_kit/alert.rb +80 -43
- data/app/components/nitro_kit/app_navigation.rb +307 -0
- data/app/components/nitro_kit/app_shell.rb +227 -0
- data/app/components/nitro_kit/appearance_bootstrap.rb +146 -0
- data/app/components/nitro_kit/appearance_picker.rb +156 -0
- data/app/components/nitro_kit/auth_shell.rb +34 -0
- data/app/components/nitro_kit/avatar.rb +85 -29
- data/app/components/nitro_kit/avatar_stack.rb +155 -9
- data/app/components/nitro_kit/badge.rb +43 -81
- data/app/components/nitro_kit/button.rb +180 -103
- data/app/components/nitro_kit/button_group.rb +63 -11
- data/app/components/nitro_kit/button_to.rb +69 -0
- data/app/components/nitro_kit/card.rb +42 -45
- data/app/components/nitro_kit/checkbox.rb +115 -62
- data/app/components/nitro_kit/checkbox_group.rb +131 -22
- data/app/components/nitro_kit/choice.rb +55 -0
- data/app/components/nitro_kit/combobox.rb +292 -105
- data/app/components/nitro_kit/command_palette.rb +412 -0
- data/app/components/nitro_kit/component.rb +322 -46
- data/app/components/nitro_kit/container.rb +32 -0
- data/app/components/nitro_kit/control_group.rb +45 -0
- data/app/components/nitro_kit/danger_zone.rb +78 -0
- data/app/components/nitro_kit/data_section.rb +106 -0
- data/app/components/nitro_kit/details_table.rb +229 -0
- data/app/components/nitro_kit/dialog.rb +257 -83
- data/app/components/nitro_kit/dropdown.rb +335 -122
- data/app/components/nitro_kit/dropzone.rb +326 -0
- data/app/components/nitro_kit/empty_state.rb +99 -0
- data/app/components/nitro_kit/field.rb +461 -236
- data/app/components/nitro_kit/field_group.rb +15 -8
- data/app/components/nitro_kit/fieldset.rb +37 -39
- data/app/components/nitro_kit/flex.rb +49 -0
- data/app/components/nitro_kit/form_builder.rb +313 -72
- data/app/components/nitro_kit/form_section.rb +79 -0
- data/app/components/nitro_kit/grid.rb +34 -0
- data/app/components/nitro_kit/icon.rb +54 -25
- data/app/components/nitro_kit/input.rb +97 -10
- data/app/components/nitro_kit/label.rb +22 -6
- data/app/components/nitro_kit/layout_options.rb +7 -0
- data/app/components/nitro_kit/page_header.rb +76 -0
- data/app/components/nitro_kit/pagination.rb +355 -69
- data/app/components/nitro_kit/pagination_bar.rb +90 -0
- data/app/components/nitro_kit/progressive_image.rb +181 -0
- data/app/components/nitro_kit/radio_button.rb +77 -55
- data/app/components/nitro_kit/radio_button_group.rb +107 -36
- data/app/components/nitro_kit/responsive_value.rb +94 -0
- data/app/components/nitro_kit/rich_text_area.rb +31 -0
- data/app/components/nitro_kit/select.rb +129 -64
- data/app/components/nitro_kit/settings_layout.rb +152 -0
- data/app/components/nitro_kit/sheet.rb +202 -0
- data/app/components/nitro_kit/stat_grid.rb +75 -0
- data/app/components/nitro_kit/switch.rb +108 -51
- data/app/components/nitro_kit/table.rb +237 -45
- data/app/components/nitro_kit/tabs.rb +168 -63
- data/app/components/nitro_kit/textarea.rb +81 -10
- data/app/components/nitro_kit/toast.rb +211 -80
- data/app/components/nitro_kit/toolbar.rb +75 -0
- data/app/components/nitro_kit/tooltip.rb +235 -34
- data/app/components/nitro_kit/typeset.rb +28 -0
- data/app/javascript/controllers/nk/app_shell_controller.js +224 -0
- data/app/javascript/controllers/nk/appearance_controller.js +73 -0
- data/app/javascript/controllers/nk/avatar_controller.js +15 -0
- data/app/javascript/controllers/nk/button_controller.js +56 -0
- data/app/javascript/controllers/nk/checkable_controller.js +41 -0
- data/app/javascript/controllers/nk/combobox_controller.js +314 -82
- data/app/javascript/controllers/nk/command_palette_controller.js +249 -0
- data/app/javascript/controllers/nk/dialog_controller.js +14 -10
- data/app/javascript/controllers/nk/dropdown_controller.js +106 -58
- data/app/javascript/controllers/nk/dropzone/direct_upload.js +61 -0
- data/app/javascript/controllers/nk/dropzone/file_rules.js +77 -0
- data/app/javascript/controllers/nk/dropzone/form_submit_lock.js +30 -0
- data/app/javascript/controllers/nk/dropzone_controller.js +482 -0
- data/app/javascript/controllers/nk/overlay_position.js +37 -0
- data/app/javascript/controllers/nk/progressive_image_controller.js +116 -0
- data/app/javascript/controllers/nk/tabs_controller.js +135 -23
- data/app/javascript/controllers/nk/toast_controller.js +112 -45
- data/app/javascript/controllers/nk/tooltip_controller.js +12 -47
- data/config/importmap.rb +2 -0
- data/config/locales/en.yml +98 -0
- data/docs/agent_guide.md +139 -0
- data/docs/agent_native_spec.md +422 -0
- data/docs/component_contracts.md +263 -0
- data/docs/customization.md +595 -0
- data/docs/hotwire.md +94 -0
- data/docs/initialization_prompt.md +36 -0
- data/docs/migration_1_to_2.md +318 -0
- data/docs/new_app_strategy.md +22 -0
- data/docs/patterns/application_foundation.md +136 -0
- data/docs/patterns/crud_resource.md +144 -0
- data/docs/patterns/destructive_action.md +105 -0
- data/docs/patterns/flash_and_toast.md +57 -0
- data/docs/patterns/inline_edit.md +59 -0
- data/docs/patterns/queryable_collection.md +187 -0
- data/docs/patterns/resource_form.md +126 -0
- data/docs/rails_conventions.md +95 -0
- data/docs/rails_integration.md +514 -0
- data/lib/generators/nitro_kit/install_generator.rb +48 -0
- data/lib/generators/nitro_kit/upgrade_smoke_tests_generator.rb +67 -0
- data/lib/nitro_kit/engine.rb +20 -0
- data/lib/nitro_kit/installation.rb +671 -0
- data/lib/nitro_kit/migration_inventory.rb +345 -0
- data/lib/nitro_kit/upgrade_smoke_test.rb +403 -0
- data/lib/nitro_kit/version.rb +1 -1
- data/lib/nitro_kit.rb +5 -42
- data/lib/rails/commands/nitro_kit/nitro_kit_command.rb +42 -0
- data/lib/tasks/nitro_kit_tasks.rake +79 -4
- data/plugins/nitro-kit/.codex-plugin/plugin.json +28 -0
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/SKILL.md +67 -0
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/agents/openai.yaml +4 -0
- data/plugins/nitro-kit/skills/nitro-kit-rails/SKILL.md +60 -0
- data/plugins/nitro-kit/skills/nitro-kit-rails/agents/openai.yaml +4 -0
- data/plugins/nitro-kit/skills/nitro-kit-ui/SKILL.md +62 -0
- data/plugins/nitro-kit/skills/nitro-kit-ui/agents/openai.yaml +4 -0
- data/src/stylesheets/nitro_kit/components/accordion.css +119 -0
- data/src/stylesheets/nitro_kit/components/alert.css +82 -0
- data/src/stylesheets/nitro_kit/components/app_navigation.css +156 -0
- data/src/stylesheets/nitro_kit/components/app_shell.css +605 -0
- data/src/stylesheets/nitro_kit/components/appearance_picker.css +117 -0
- data/src/stylesheets/nitro_kit/components/auth_shell.css +8 -0
- data/src/stylesheets/nitro_kit/components/avatar.css +81 -0
- data/src/stylesheets/nitro_kit/components/avatar_stack.css +80 -0
- data/src/stylesheets/nitro_kit/components/badge.css +76 -0
- data/src/stylesheets/nitro_kit/components/button.css +283 -0
- data/src/stylesheets/nitro_kit/components/button_group.css +53 -0
- data/src/stylesheets/nitro_kit/components/button_to.css +6 -0
- data/src/stylesheets/nitro_kit/components/card.css +70 -0
- data/src/stylesheets/nitro_kit/components/checkbox.css +160 -0
- data/src/stylesheets/nitro_kit/components/checkbox_group.css +61 -0
- data/src/stylesheets/nitro_kit/components/combobox.css +148 -0
- data/src/stylesheets/nitro_kit/components/command_palette.css +255 -0
- data/src/stylesheets/nitro_kit/components/container.css +25 -0
- data/src/stylesheets/nitro_kit/components/control_group.css +149 -0
- data/src/stylesheets/nitro_kit/components/danger_zone.css +54 -0
- data/src/stylesheets/nitro_kit/components/data_section.css +40 -0
- data/src/stylesheets/nitro_kit/components/details_table.css +53 -0
- data/src/stylesheets/nitro_kit/components/dialog.css +80 -0
- data/src/stylesheets/nitro_kit/components/dropdown.css +152 -0
- data/src/stylesheets/nitro_kit/components/dropzone.css +198 -0
- data/src/stylesheets/nitro_kit/components/empty_state.css +53 -0
- data/src/stylesheets/nitro_kit/components/field.css +79 -0
- data/src/stylesheets/nitro_kit/components/field_group.css +6 -0
- data/src/stylesheets/nitro_kit/components/fieldset.css +28 -0
- data/src/stylesheets/nitro_kit/components/flex.css +455 -0
- data/src/stylesheets/nitro_kit/components/form_section.css +71 -0
- data/src/stylesheets/nitro_kit/components/grid.css +308 -0
- data/src/stylesheets/nitro_kit/components/icon.css +26 -0
- data/src/stylesheets/nitro_kit/components/input.css +80 -0
- data/src/stylesheets/nitro_kit/components/label.css +9 -0
- data/src/stylesheets/nitro_kit/components/layout.css +440 -0
- data/src/stylesheets/nitro_kit/components/page_header.css +61 -0
- data/src/stylesheets/nitro_kit/components/pagination.css +119 -0
- data/src/stylesheets/nitro_kit/components/pagination_bar.css +41 -0
- data/src/stylesheets/nitro_kit/components/palette.css +113 -0
- data/src/stylesheets/nitro_kit/components/progressive_image.css +139 -0
- data/src/stylesheets/nitro_kit/components/radio_button.css +127 -0
- data/src/stylesheets/nitro_kit/components/radio_button_group.css +143 -0
- data/src/stylesheets/nitro_kit/components/rich_text_area.css +21 -0
- data/src/stylesheets/nitro_kit/components/select.css +70 -0
- data/src/stylesheets/nitro_kit/components/settings_layout.css +100 -0
- data/src/stylesheets/nitro_kit/components/sheet.css +100 -0
- data/src/stylesheets/nitro_kit/components/stat_grid.css +51 -0
- data/src/stylesheets/nitro_kit/components/switch.css +152 -0
- data/src/stylesheets/nitro_kit/components/table.css +116 -0
- data/src/stylesheets/nitro_kit/components/tabs.css +138 -0
- data/src/stylesheets/nitro_kit/components/textarea.css +39 -0
- data/src/stylesheets/nitro_kit/components/toast.css +131 -0
- data/src/stylesheets/nitro_kit/components/toolbar.css +78 -0
- data/src/stylesheets/nitro_kit/components/tooltip.css +124 -0
- data/src/stylesheets/nitro_kit/components/typeset.css +191 -0
- data/src/stylesheets/nitro_kit/layers.css +7 -0
- data/src/stylesheets/nitro_kit/reset.css +192 -0
- data/src/stylesheets/nitro_kit/tokens.css +198 -0
- metadata +182 -51
- data/Rakefile +0 -8
- data/app/assets/tailwind/application.css +0 -49
- data/app/components/nitro_kit/datepicker.rb +0 -9
- data/app/helpers/nitro_kit/accordion_helper.rb +0 -9
- data/app/helpers/nitro_kit/alert_helper.rb +0 -11
- data/app/helpers/nitro_kit/avatar_helper.rb +0 -13
- data/app/helpers/nitro_kit/badge_helper.rb +0 -13
- data/app/helpers/nitro_kit/button_group_helper.rb +0 -9
- data/app/helpers/nitro_kit/button_helper.rb +0 -49
- data/app/helpers/nitro_kit/card_helper.rb +0 -9
- data/app/helpers/nitro_kit/checkbox_helper.rb +0 -50
- data/app/helpers/nitro_kit/combobox_helper.rb +0 -9
- data/app/helpers/nitro_kit/datepicker_helper.rb +0 -9
- data/app/helpers/nitro_kit/dialog_helper.rb +0 -9
- data/app/helpers/nitro_kit/dropdown_helper.rb +0 -9
- data/app/helpers/nitro_kit/field_group_helper.rb +0 -9
- data/app/helpers/nitro_kit/field_helper.rb +0 -9
- data/app/helpers/nitro_kit/fieldset_helper.rb +0 -9
- data/app/helpers/nitro_kit/form_helper.rb +0 -13
- data/app/helpers/nitro_kit/icon_helper.rb +0 -9
- data/app/helpers/nitro_kit/input_helper.rb +0 -37
- data/app/helpers/nitro_kit/label_helper.rb +0 -19
- data/app/helpers/nitro_kit/pagination_helper.rb +0 -46
- data/app/helpers/nitro_kit/radio_button_helper.rb +0 -23
- data/app/helpers/nitro_kit/select_helper.rb +0 -24
- data/app/helpers/nitro_kit/switch_helper.rb +0 -9
- data/app/helpers/nitro_kit/table_helper.rb +0 -9
- data/app/helpers/nitro_kit/tabs_helper.rb +0 -9
- data/app/helpers/nitro_kit/textarea_helper.rb +0 -9
- data/app/helpers/nitro_kit/toast_helper.rb +0 -25
- data/app/helpers/nitro_kit/tooltip_helper.rb +0 -9
- data/app/javascript/controllers/nk/accordion_controller.js +0 -18
- data/app/javascript/controllers/nk/datepicker_controller.js +0 -5
- data/app/javascript/controllers/nk/switch_controller.js +0 -32
- data/lib/generators/nitro_kit/component_generator.rb +0 -84
- data/lib/nitro_kit/schema_builder.rb +0 -129
- 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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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 = [
|
|
17
|
+
static targets = [
|
|
18
|
+
"control",
|
|
19
|
+
"input",
|
|
20
|
+
"native",
|
|
21
|
+
"value",
|
|
22
|
+
"listbox",
|
|
23
|
+
"option",
|
|
24
|
+
"status",
|
|
25
|
+
];
|
|
13
26
|
static values = {
|
|
14
|
-
open:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
firstOptionSelectionMode: this.firstOptionSelectionModeValue,
|
|
27
|
-
scrollIntoViewOptions: this.scrollIntoViewOptionsValue,
|
|
28
|
-
});
|
|
35
|
+
get invalidSelectionMessage() {
|
|
36
|
+
return this.invalidSelectionValue || INVALID_SELECTION_MESSAGE;
|
|
37
|
+
}
|
|
29
38
|
|
|
30
|
-
|
|
39
|
+
get noResultsMessage() {
|
|
40
|
+
return this.noResultsValue || NO_RESULTS_MESSAGE;
|
|
41
|
+
}
|
|
31
42
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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.
|
|
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
|
-
|
|
43
|
-
this.
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
57
|
-
if (!this.
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
158
|
+
filter() {
|
|
159
|
+
const query = this.inputTarget.value.trim().toLocaleLowerCase();
|
|
160
|
+
let exactMatch = null;
|
|
66
161
|
|
|
67
|
-
this.
|
|
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
|
-
|
|
71
|
-
this.
|
|
72
|
-
this.inputTarget.value = "";
|
|
73
|
-
this.input();
|
|
74
|
-
this.hiddenFieldTarget.value = "";
|
|
182
|
+
select(event) {
|
|
183
|
+
this.commit(event.currentTarget);
|
|
75
184
|
}
|
|
76
185
|
|
|
77
|
-
|
|
78
|
-
|
|
186
|
+
activate(event) {
|
|
187
|
+
this.setActive(event.currentTarget);
|
|
188
|
+
}
|
|
79
189
|
|
|
80
|
-
|
|
190
|
+
navigate(event) {
|
|
191
|
+
const options = this.visibleOptions;
|
|
192
|
+
const currentIndex = options.indexOf(this.activeOption);
|
|
193
|
+
let nextIndex;
|
|
81
194
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
} else {
|
|
89
|
-
item.removeAttribute("role");
|
|
90
|
-
}
|
|
91
|
-
});
|
|
234
|
+
if (options.length > 0) this.setActive(options[nextIndex]);
|
|
235
|
+
}
|
|
92
236
|
|
|
93
|
-
|
|
237
|
+
validate() {
|
|
238
|
+
this.syncValidity();
|
|
94
239
|
}
|
|
95
240
|
|
|
96
|
-
openValueChanged(
|
|
97
|
-
if (!this.
|
|
241
|
+
openValueChanged(open) {
|
|
242
|
+
if (!this.enhanced) return;
|
|
98
243
|
|
|
99
|
-
|
|
100
|
-
|
|
244
|
+
this.reflectOpenState(open);
|
|
245
|
+
}
|
|
101
246
|
|
|
102
|
-
|
|
247
|
+
reflectOpenState(open = this.openValue) {
|
|
248
|
+
if (!this.hasListboxTarget || !this.hasInputTarget) return;
|
|
103
249
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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.
|
|
272
|
+
this.stopPositioning?.();
|
|
273
|
+
this.stopPositioning = null;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
111
276
|
|
|
112
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
}
|
|
286
|
+
update();
|
|
287
|
+
this.stopPositioning = observeOverlayPosition(update);
|
|
118
288
|
}
|
|
119
289
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
+
}
|