-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCommandPalette.tsx
More file actions
353 lines (298 loc) · 9.21 KB
/
Copy pathCommandPalette.tsx
File metadata and controls
353 lines (298 loc) · 9.21 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import {
Component,
createEffect,
createSignal,
createUniqueId,
JSX,
onMount,
onCleanup,
Show,
} from 'solid-js';
import { Transition } from 'solid-transition-group';
import { tinykeys } from 'tinykeys';
import { useStore } from './StoreContext';
import { CommandPalettePortal } from './CommandPalettePortal';
import { KbdShortcut } from './KbdShortcut/KbdShortcut';
import { ScrollAssist } from './ScrollAssist/ScrollAssist';
import { PanelResult } from './Panel/Result/Result';
import { PanelFooter } from './Panel/Footer/Footer';
import { createSearchResultList } from './createActionList';
import { runAction } from './actionUtils/actionUtils';
import { ActionId, WrappedAction } from './types';
import utilStyles from './utils.module.css';
import styles from './CommandPalette.module.css';
type InputEventHandler = JSX.EventHandlerUnion<HTMLInputElement, InputEvent>;
type ActiveItemId = null | ActionId;
type UserInteraction =
| 'idle'
| 'search'
| 'navigate-kbd'
| 'navigate-mouse'
| 'navigate-scroll-assist';
export interface CommandPaletteProps {
searchPlaceholder?: string;
}
const CommandPaletteInternal: Component<CommandPaletteProps> = (p) => {
const [state, storeMethods] = useStore();
const { closePalette, setSearchText, revertParentAction } = storeMethods;
const resultsList = createSearchResultList();
const [activeItemId, setActiveItemId] = createSignal<ActiveItemId>(null);
const [userInteraction, setUserInteraction] = createSignal<UserInteraction>('idle');
const searchLabelId = createUniqueId();
const searchInputId = createUniqueId();
const resultListId = createUniqueId();
let wrapperElem: undefined | HTMLDivElement;
let searchInputElem: undefined | HTMLInputElement;
let closeBtnElem: undefined | HTMLButtonElement;
let lastFocusedElem: null | HTMLElement;
function triggerRun(action: WrappedAction) {
runAction(action, state.actionsContext, storeMethods);
}
function activatePrevItem() {
const actionsList = resultsList();
const actionsCount = actionsList.length;
const activeActionId = activeItemId();
const currentActionIndex = actionsList.findIndex((action) => action.id === activeActionId);
if (currentActionIndex < 0) {
return;
}
const prevActionIndex = (actionsCount + currentActionIndex - 1) % actionsCount;
const prevActionId = actionsList[prevActionIndex].id;
setActiveItemId(prevActionId);
}
function activateNextItem() {
const actionsList = resultsList();
const actionsCount = actionsList.length;
const activeActionId = activeItemId();
const currentActionIndex = actionsList.findIndex((action) => action.id === activeActionId);
if (currentActionIndex < 0) {
return;
}
const nextActionIndex = (currentActionIndex + 1) % actionsCount;
const nextActionId = actionsList[nextActionIndex].id;
setActiveItemId(nextActionId);
}
function handleWrapperClick() {
closePalette();
}
function handlePanelClick(event: MouseEvent) {
event.stopPropagation();
}
const handleSearchInput: InputEventHandler = (event) => {
const newValue = event.currentTarget.value;
setUserInteraction('search');
setSearchText(newValue);
};
function handleActionItemSelect(action: WrappedAction) {
triggerRun(action);
}
function handleActionItemHover(action: WrappedAction) {
setUserInteraction('navigate-mouse');
setActiveItemId(action.id);
}
function handleKbdEnter(event: KeyboardEvent) {
const targetElem = event.target as HTMLElement;
if (closeBtnElem && closeBtnElem.contains(targetElem)) {
return;
}
event.preventDefault();
const activeActionId = activeItemId();
if (!activeActionId) {
return null;
}
const action = state.actions[activeActionId];
triggerRun(action);
}
function handleKbdPrev(event: KeyboardEvent) {
event.preventDefault();
setUserInteraction('navigate-kbd');
activatePrevItem();
}
function handleKbdNext(event: KeyboardEvent) {
event.preventDefault();
setUserInteraction('navigate-kbd');
activateNextItem();
}
function handleKbdFirst(event: KeyboardEvent) {
event.preventDefault();
const actionsList = resultsList();
const firstAction = actionsList[0];
if (firstAction) {
setUserInteraction('navigate-kbd');
setActiveItemId(firstAction.id);
}
}
function handleKbdLast(event: KeyboardEvent) {
event.preventDefault();
const actionsList = resultsList();
const lastAction = actionsList.at(-1);
if (lastAction) {
setUserInteraction('navigate-kbd');
setActiveItemId(lastAction.id);
}
}
function handleKbdDelete() {
const isSearchEmpty = state.searchText.length <= 0;
if (isSearchEmpty) {
revertParentAction();
}
}
function handleScrollAssistPrev() {
setUserInteraction('navigate-scroll-assist');
activatePrevItem();
}
function handleScrollAssistNext() {
setUserInteraction('navigate-scroll-assist');
activateNextItem();
}
function handleScrollAssistStop() {
setUserInteraction('idle');
}
function getScrollAssistStatus() {
if (userInteraction() === 'navigate-mouse') {
return 'available';
}
if (userInteraction() === 'navigate-scroll-assist') {
return 'running';
}
return 'stopped';
}
onMount(() => {
lastFocusedElem = document.activeElement as HTMLElement;
if (searchInputElem) {
searchInputElem.select();
}
if (wrapperElem) {
tinykeys(wrapperElem, {
Escape: (event) => {
event.preventDefault();
closePalette();
},
Enter: handleKbdEnter,
ArrowUp: handleKbdPrev,
ArrowDown: handleKbdNext,
PageUp: handleKbdFirst,
PageDown: handleKbdLast,
Backspace: handleKbdDelete,
});
}
});
onCleanup(() => {
if (lastFocusedElem) {
lastFocusedElem.focus();
}
lastFocusedElem = null;
});
createEffect(() => {
const actionsList = resultsList();
const firstResultId = actionsList[0]?.id;
if (firstResultId) {
setActiveItemId(firstResultId);
} else {
setActiveItemId(null);
}
});
return (
<div
class={styles.wrapper}
ref={wrapperElem}
onClick={handleWrapperClick}
>
<div class={styles.palette}>
<ScrollAssist
direction="up"
status={getScrollAssistStatus()}
onNavigate={handleScrollAssistPrev}
onStop={handleScrollAssistStop}
/>
<ScrollAssist
direction="down"
status={getScrollAssistStatus()}
onNavigate={handleScrollAssistNext}
onStop={handleScrollAssistStop}
/>
<div
role="combobox"
aria-expanded={true}
aria-haspopup="listbox"
aria-controls={resultListId}
aria-activedescendant={`scp-result-item-${activeItemId()}`}
aria-labelledby={searchLabelId}
class={styles.panel}
onClick={handlePanelClick}
>
<form
role="search"
class={styles.searchForm}
noValidate
onSubmit={(event) => {
event.preventDefault();
}}
>
<label
for={searchInputId}
id={searchLabelId}
class={utilStyles.visuallyHidden}
>
Search for an action and then select one of the option.
</label>
<input
type="search"
id={searchInputId}
class={styles.searchInput}
aria-autocomplete="list"
autocomplete="off"
autocapitalize="off"
spellcheck={false}
placeholder={p.searchPlaceholder || 'Type a command or search...'}
data-cp-kbd-shortcuts="disabled"
ref={searchInputElem}
value={state.searchText}
onInput={handleSearchInput}
/>
<button
type="button"
class={styles.closeBtn}
ref={closeBtnElem}
onClick={() => {
closePalette();
}}
>
<span class={utilStyles.visuallyHidden}>Close the Command Palette</span>
<KbdShortcut
shortcut="Escape"
aria-hidden
/>
</button>
</form>
<PanelResult
activeItemId={activeItemId()}
resultsList={resultsList()}
resultListId={resultListId}
searchLabelId={searchLabelId}
onActionItemHover={handleActionItemHover}
onActionItemSelect={handleActionItemSelect}
/>
<PanelFooter />
</div>
</div>
</div>
);
};
export const CommandPalette: Component<CommandPaletteProps> = (p) => {
const [state] = useStore();
return (
<CommandPalettePortal>
<Transition
enterClass={styles.animEnter}
enterActiveClass={styles.animEnterActive}
exitClass={styles.animExit}
exitActiveClass={styles.animExitActive}
>
<Show when={state.visibility === 'opened'}>
<CommandPaletteInternal {...p} />
</Show>
</Transition>
</CommandPalettePortal>
);
};