-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathcommand_controller.js
More file actions
160 lines (131 loc) · 4 KB
/
command_controller.js
File metadata and controls
160 lines (131 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Controller } from "@hotwired/stimulus";
import Fuse from "fuse.js";
const OPEN_DIALOG_SELECTOR = "[data-ruby-ui--command-dialog]";
// Connects to data-controller="ruby-ui--command"
export default class extends Controller {
static targets = ["input", "group", "item", "empty", "content"];
static values = {
open: {
type: Boolean,
default: false,
},
};
connect() {
this.selectedIndex = -1;
if (!this.hasInputTarget) {
return;
}
this.inputTarget.focus();
this.searchIndex = this.buildSearchIndex();
this.toggleVisibility(this.emptyTargets, false);
if (this.openValue && this.hasContentTarget) {
this.open();
}
}
open(e) {
if (e) {
e.preventDefault();
}
if (!this.hasContentTarget) {
return;
}
const openDialog = document.querySelector(OPEN_DIALOG_SELECTOR);
if (openDialog) {
this.focusDialogInput(openDialog);
return;
}
document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
// prevent scroll on body
document.body.classList.add("overflow-hidden");
}
dismiss() {
// allow scroll on body
document.body.classList.remove("overflow-hidden");
// remove the element
this.element.remove();
}
filter(e) {
// Deselect any previously selected item
this.deselectAll();
const query = e.target.value.toLowerCase();
if (query.length === 0) {
this.resetVisibility();
return;
}
this.toggleVisibility(this.itemTargets, false);
const results = this.searchIndex.search(query);
results.forEach((result) =>
this.toggleVisibility([result.item.element], true),
);
this.toggleVisibility(this.emptyTargets, results.length === 0);
this.updateGroupVisibility();
}
toggleVisibility(elements, isVisible) {
elements.forEach((el) => el.classList.toggle("hidden", !isVisible));
}
updateGroupVisibility() {
this.groupTargets.forEach((group) => {
const hasVisibleItems =
group.querySelectorAll(
"[data-ruby-ui--command-target='item']:not(.hidden)",
).length > 0;
this.toggleVisibility([group], hasVisibleItems);
});
}
resetVisibility() {
this.toggleVisibility(this.itemTargets, true);
this.toggleVisibility(this.groupTargets, true);
this.toggleVisibility(this.emptyTargets, false);
}
buildSearchIndex() {
const options = {
keys: ["value"],
threshold: 0.2,
includeMatches: true,
};
const items = this.itemTargets.map((el) => ({
value: el.dataset.value,
element: el,
}));
return new Fuse(items, options);
}
handleKeydown(e) {
const visibleItems = this.itemTargets.filter(
(item) => !item.classList.contains("hidden"),
);
if (e.key === "ArrowDown") {
e.preventDefault();
this.updateSelectedItem(visibleItems, 1);
} else if (e.key === "ArrowUp") {
e.preventDefault();
this.updateSelectedItem(visibleItems, -1);
} else if (e.key === "Enter" && this.selectedIndex !== -1) {
e.preventDefault();
visibleItems[this.selectedIndex].click();
}
}
updateSelectedItem(visibleItems, direction) {
if (this.selectedIndex >= 0) {
this.toggleAriaSelected(visibleItems[this.selectedIndex], false);
}
this.selectedIndex += direction;
// Ensure the selected index is within the bounds of the visible items
if (this.selectedIndex < 0) {
this.selectedIndex = visibleItems.length - 1;
} else if (this.selectedIndex >= visibleItems.length) {
this.selectedIndex = 0;
}
this.toggleAriaSelected(visibleItems[this.selectedIndex], true);
}
toggleAriaSelected(element, isSelected) {
element.setAttribute("aria-selected", isSelected.toString());
}
deselectAll() {
this.itemTargets.forEach((item) => this.toggleAriaSelected(item, false));
this.selectedIndex = -1;
}
focusDialogInput(dialog) {
const input = dialog.querySelector("[data-ruby-ui--command-target='input']");
input?.focus();
}
}