base-cmdk 0.1.0 → 0.1.1
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.
- package/README.md +19 -5
- package/dist/index.d.ts +284 -61
- package/dist/index.js +914 -15
- package/package.json +11 -5
- package/dist/collection.d.ts +0 -11
- package/dist/collection.js +0 -100
- package/dist/command-score.d.ts +0 -1
- package/dist/command-score.js +0 -123
- package/dist/context.d.ts +0 -19
- package/dist/context.js +0 -48
- package/dist/order.d.ts +0 -6
- package/dist/order.js +0 -66
- package/dist/parts.d.ts +0 -86
- package/dist/parts.js +0 -257
- package/dist/root.d.ts +0 -17
- package/dist/root.js +0 -354
- package/dist/types.d.ts +0 -160
- package/dist/types.js +0 -1
package/dist/parts.js
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import * as React from "react";
|
|
4
|
-
import { Dialog as BaseDialog } from "@base-ui/react/dialog";
|
|
5
|
-
import { useMergedRefs } from "@base-ui/utils/useMergedRefs";
|
|
6
|
-
import { useValueAsRef } from "@base-ui/utils/useValueAsRef";
|
|
7
|
-
import { useIsoLayoutEffect as useLayoutEffect } from "@base-ui/utils/useIsoLayoutEffect";
|
|
8
|
-
import { CommandRoot } from "./root.js";
|
|
9
|
-
import { GroupContext, SELECT_EVENT, useCommand, useCommandState, useStore, wrapChildren, } from "./context.js";
|
|
10
|
-
function declaredText(children) {
|
|
11
|
-
return React.Children.toArray(children)
|
|
12
|
-
.map((child) => {
|
|
13
|
-
if (typeof child === "string" || typeof child === "number")
|
|
14
|
-
return String(child);
|
|
15
|
-
if (React.isValidElement(child) &&
|
|
16
|
-
(typeof child.type === "string" || child.type === React.Fragment))
|
|
17
|
-
return declaredText(child.props.children);
|
|
18
|
-
return "";
|
|
19
|
-
})
|
|
20
|
-
.join("");
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Command menu item. Becomes active on pointer enter or through keyboard navigation.
|
|
24
|
-
* Preferably pass a `value`, otherwise the value will be inferred from `children` or
|
|
25
|
-
* the rendered item's `textContent`.
|
|
26
|
-
*/
|
|
27
|
-
export const CommandItem = React.forwardRef((props, forwardedRef) => {
|
|
28
|
-
const generatedId = React.useId();
|
|
29
|
-
const id = props.id ?? generatedId;
|
|
30
|
-
const ref = React.useRef(null);
|
|
31
|
-
const mergedRef = useMergedRefs(ref, forwardedRef);
|
|
32
|
-
const groupContext = React.useContext(GroupContext);
|
|
33
|
-
const parent = React.useRef(null);
|
|
34
|
-
const context = useCommand();
|
|
35
|
-
const propsRef = useValueAsRef(props);
|
|
36
|
-
const forceMount = props.forceMount ?? groupContext?.forceMount;
|
|
37
|
-
const value = React.useRef("");
|
|
38
|
-
const initialValue = React.useMemo(() => props.value ?? declaredText(props.children), [props.value, props.children]);
|
|
39
|
-
useLayoutEffect(() => {
|
|
40
|
-
const text = props.value ??
|
|
41
|
-
(typeof props.children === "string" ? props.children : ref.current?.textContent);
|
|
42
|
-
value.current = (text ?? (value.current || initialValue)).trim();
|
|
43
|
-
if (ref.current)
|
|
44
|
-
parent.current = ref.current.parentElement;
|
|
45
|
-
ref.current?.setAttribute("data-value", value.current);
|
|
46
|
-
context.updateItem(id, {
|
|
47
|
-
value: value.current,
|
|
48
|
-
keywords: props.keywords?.map((word) => word.trim()) ?? [],
|
|
49
|
-
groupId: groupContext?.id,
|
|
50
|
-
parent: parent.current,
|
|
51
|
-
forced: Boolean(forceMount),
|
|
52
|
-
disabled: Boolean(props.disabled),
|
|
53
|
-
hidden: Boolean(props.hidden || props.inert),
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
const { removeItem } = context;
|
|
57
|
-
useLayoutEffect(() => () => removeItem(id), [id, removeItem]);
|
|
58
|
-
const store = useStore();
|
|
59
|
-
const selected = useCommandState((state) => state.selectedItemId === id);
|
|
60
|
-
const render = useCommandState((state) => !state.filtered.ranked ||
|
|
61
|
-
forceMount ||
|
|
62
|
-
(state.filtered.items.has(id)
|
|
63
|
-
? (state.filtered.items.get(id) ?? 0) > 0
|
|
64
|
-
: state.ready ||
|
|
65
|
-
!initialValue ||
|
|
66
|
-
context.initialScore(initialValue.trim(), props.keywords) > 0));
|
|
67
|
-
useLayoutEffect(() => {
|
|
68
|
-
const element = ref.current;
|
|
69
|
-
if (!element || props.disabled)
|
|
70
|
-
return;
|
|
71
|
-
element.addEventListener(SELECT_EVENT, onSelect);
|
|
72
|
-
return () => element.removeEventListener(SELECT_EVENT, onSelect);
|
|
73
|
-
}, [render, props.onSelect, props.disabled]);
|
|
74
|
-
function onSelect() {
|
|
75
|
-
if (!ref.current || ref.current.closest("[hidden], [inert]") || propsRef.current.disabled)
|
|
76
|
-
return;
|
|
77
|
-
select();
|
|
78
|
-
propsRef.current.onSelect?.(value.current);
|
|
79
|
-
}
|
|
80
|
-
function select() {
|
|
81
|
-
store.setState("value", value.current, true);
|
|
82
|
-
}
|
|
83
|
-
const { disabled, value: _, onSelect: __, forceMount: ___, keywords: ____, ...etc } = props;
|
|
84
|
-
if (!render)
|
|
85
|
-
return null;
|
|
86
|
-
return (_jsx("div", { ref: mergedRef, ...etc, id: id, "cmdk-item": "", role: "option", "aria-disabled": Boolean(disabled), "aria-selected": Boolean(selected), "data-disabled": Boolean(disabled), "data-selected": Boolean(selected), onPointerMove: (event) => {
|
|
87
|
-
etc.onPointerMove?.(event);
|
|
88
|
-
if (!disabled && !context.disablePointerSelection && !event.defaultPrevented)
|
|
89
|
-
select();
|
|
90
|
-
}, onMouseDown: (event) => {
|
|
91
|
-
etc.onMouseDown?.(event);
|
|
92
|
-
const item = event.currentTarget;
|
|
93
|
-
const active = item.getRootNode().activeElement;
|
|
94
|
-
const control = event.target.closest("a[href], button, input, select, textarea, [contenteditable]:not([contenteditable=false]), [tabindex]");
|
|
95
|
-
if (!event.defaultPrevented &&
|
|
96
|
-
event.button === 0 &&
|
|
97
|
-
!event.metaKey &&
|
|
98
|
-
!event.ctrlKey &&
|
|
99
|
-
!event.altKey &&
|
|
100
|
-
!event.shiftKey &&
|
|
101
|
-
active?.matches("[cmdk-input]") &&
|
|
102
|
-
active.closest("[cmdk-root]") === item.closest("[cmdk-root]") &&
|
|
103
|
-
!(control && item.contains(control)))
|
|
104
|
-
event.preventDefault();
|
|
105
|
-
}, onClick: (event) => {
|
|
106
|
-
etc.onClick?.(event);
|
|
107
|
-
if (!disabled && !event.defaultPrevented)
|
|
108
|
-
onSelect();
|
|
109
|
-
}, children: props.children }));
|
|
110
|
-
});
|
|
111
|
-
/**
|
|
112
|
-
* Group command menu items together with a heading.
|
|
113
|
-
* Grouped items are always shown together.
|
|
114
|
-
*/
|
|
115
|
-
export const CommandGroup = React.forwardRef((props, forwardedRef) => {
|
|
116
|
-
const { heading, children: _children, forceMount, value, hidden, ...etc } = props;
|
|
117
|
-
const id = React.useId();
|
|
118
|
-
const ref = React.useRef(null);
|
|
119
|
-
const mergedRef = useMergedRefs(ref, forwardedRef);
|
|
120
|
-
const headingRef = React.useRef(null);
|
|
121
|
-
const headingId = React.useId();
|
|
122
|
-
const parentId = React.useContext(GroupContext)?.id;
|
|
123
|
-
const context = useCommand();
|
|
124
|
-
const render = useCommandState((state) => !state.ready || !state.filtered.ranked || state.filtered.groups.has(id));
|
|
125
|
-
const { updateGroup, removeGroup } = context;
|
|
126
|
-
useLayoutEffect(() => updateGroup(id, Boolean(forceMount), Boolean(hidden), parentId), [id, forceMount, hidden, parentId, updateGroup]);
|
|
127
|
-
useLayoutEffect(() => () => removeGroup(id), [id, removeGroup]);
|
|
128
|
-
useLayoutEffect(() => {
|
|
129
|
-
const name = value ?? headingRef.current?.textContent;
|
|
130
|
-
if (name != null)
|
|
131
|
-
ref.current?.setAttribute("data-value", name.trim());
|
|
132
|
-
});
|
|
133
|
-
const contextValue = React.useMemo(() => ({ id, forceMount }), [id, forceMount]);
|
|
134
|
-
return (_jsxs("div", { ref: mergedRef, ...etc, "cmdk-group": "", "data-cmdk-group-id": id, role: "presentation", hidden: hidden || !render, children: [heading && (_jsx("div", { ref: headingRef, "cmdk-group-heading": "", "aria-hidden": true, id: headingId, children: heading })), wrapChildren(props, (child) => (_jsx("div", { "cmdk-group-items": "", role: "group", "aria-labelledby": heading ? headingId : undefined, children: _jsx(GroupContext.Provider, { value: contextValue, children: child }) })))] }));
|
|
135
|
-
});
|
|
136
|
-
/**
|
|
137
|
-
* A visual and semantic separator between items or groups.
|
|
138
|
-
* Visible when the search query is empty or `alwaysRender` is true, hidden otherwise.
|
|
139
|
-
*/
|
|
140
|
-
export const CommandSeparator = React.forwardRef((props, forwardedRef) => {
|
|
141
|
-
const { alwaysRender, ...etc } = props;
|
|
142
|
-
const ref = React.useRef(null);
|
|
143
|
-
const mergedRef = useMergedRefs(ref, forwardedRef);
|
|
144
|
-
const render = useCommandState((state) => !state.search);
|
|
145
|
-
if (!alwaysRender && !render)
|
|
146
|
-
return null;
|
|
147
|
-
return _jsx("div", { ref: mergedRef, ...etc, "cmdk-separator": "", role: "separator" });
|
|
148
|
-
});
|
|
149
|
-
/**
|
|
150
|
-
* Command menu input.
|
|
151
|
-
* All props are forwarded to the underyling `input` element.
|
|
152
|
-
*/
|
|
153
|
-
export const CommandInput = React.forwardRef((props, forwardedRef) => {
|
|
154
|
-
const { onValueChange, defaultValue, ...etc } = props;
|
|
155
|
-
const isControlled = props.value != null;
|
|
156
|
-
const store = useStore();
|
|
157
|
-
const search = useCommandState((state) => state.search);
|
|
158
|
-
const selectedItemId = useCommandState((state) => state.selectedItemId);
|
|
159
|
-
const context = useCommand();
|
|
160
|
-
const initialDefault = React.useRef(defaultValue);
|
|
161
|
-
const { setInputId } = context;
|
|
162
|
-
useLayoutEffect(() => {
|
|
163
|
-
setInputId(props.id);
|
|
164
|
-
return () => setInputId(undefined);
|
|
165
|
-
}, [props.id, setInputId]);
|
|
166
|
-
useLayoutEffect(() => {
|
|
167
|
-
if (initialDefault.current != null)
|
|
168
|
-
store.setState("search", String(initialDefault.current));
|
|
169
|
-
}, [store]);
|
|
170
|
-
useLayoutEffect(() => {
|
|
171
|
-
if (props.value != null) {
|
|
172
|
-
store.setState("search", props.value);
|
|
173
|
-
}
|
|
174
|
-
}, [props.value]);
|
|
175
|
-
return (_jsx("input", { ref: forwardedRef, ...etc, "cmdk-input": "", autoComplete: "off", autoCorrect: "off", spellCheck: false, "aria-autocomplete": "list", role: "combobox", "aria-expanded": true, "aria-controls": context.listId, "aria-labelledby": etc["aria-labelledby"] ?? (etc["aria-label"] ? undefined : context.labelledBy), "aria-activedescendant": selectedItemId, id: props.id ?? context.inputId, type: "text", value: isControlled ? props.value : search, onChange: (e) => {
|
|
176
|
-
if (!isControlled) {
|
|
177
|
-
store.setState("search", e.target.value);
|
|
178
|
-
}
|
|
179
|
-
onValueChange?.(e.target.value);
|
|
180
|
-
} }));
|
|
181
|
-
});
|
|
182
|
-
/**
|
|
183
|
-
* Contains `Item`, `Group`, and `Separator`.
|
|
184
|
-
* Use the `--cmdk-list-height` CSS variable to animate height based on the number of results.
|
|
185
|
-
*/
|
|
186
|
-
export const CommandList = React.forwardRef((props, forwardedRef) => {
|
|
187
|
-
const { children: _children, label = "Suggestions", ...etc } = props;
|
|
188
|
-
const ref = React.useRef(null);
|
|
189
|
-
const mergedRef = useMergedRefs(ref, forwardedRef);
|
|
190
|
-
const height = React.useRef(null);
|
|
191
|
-
const selectedItemId = useCommandState((state) => state.selectedItemId);
|
|
192
|
-
const context = useCommand();
|
|
193
|
-
const innerRef = useMergedRefs(height, context.listInnerRef);
|
|
194
|
-
const { setListId, markDirty } = context;
|
|
195
|
-
useLayoutEffect(() => {
|
|
196
|
-
const list = height.current;
|
|
197
|
-
if (!list)
|
|
198
|
-
return;
|
|
199
|
-
const observer = new MutationObserver((changes) => {
|
|
200
|
-
if (changes.some(({ target, attributeName }) => attributeName === "inert" || !target.matches("[cmdk-group]")))
|
|
201
|
-
markDirty();
|
|
202
|
-
});
|
|
203
|
-
const options = { attributes: true, attributeFilter: ["hidden", "inert"] };
|
|
204
|
-
observer.observe(list, { ...options, subtree: true });
|
|
205
|
-
for (let parent = list.parentElement; parent; parent = parent.parentElement)
|
|
206
|
-
observer.observe(parent, options);
|
|
207
|
-
return () => observer.disconnect();
|
|
208
|
-
}, [markDirty]);
|
|
209
|
-
useLayoutEffect(() => {
|
|
210
|
-
setListId(props.id);
|
|
211
|
-
return () => setListId(undefined);
|
|
212
|
-
}, [props.id, setListId]);
|
|
213
|
-
React.useEffect(() => {
|
|
214
|
-
if (height.current && ref.current) {
|
|
215
|
-
const el = height.current;
|
|
216
|
-
const wrapper = ref.current;
|
|
217
|
-
let animationFrame;
|
|
218
|
-
const observer = new ResizeObserver(() => {
|
|
219
|
-
animationFrame = requestAnimationFrame(() => {
|
|
220
|
-
const height = el.offsetHeight;
|
|
221
|
-
wrapper.style.setProperty(`--cmdk-list-height`, height.toFixed(1) + "px");
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
observer.observe(el);
|
|
225
|
-
return () => {
|
|
226
|
-
if (animationFrame !== undefined) {
|
|
227
|
-
cancelAnimationFrame(animationFrame);
|
|
228
|
-
}
|
|
229
|
-
observer.unobserve(el);
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
}, []);
|
|
233
|
-
return (_jsx("div", { ref: mergedRef, ...etc, "cmdk-list": "", role: "listbox", tabIndex: -1, "aria-activedescendant": selectedItemId, "aria-label": etc["aria-label"] ?? label, id: props.id ?? context.listId, children: wrapChildren(props, (child) => (_jsx("div", { ref: innerRef, "cmdk-list-sizer": "", children: child }))) }));
|
|
234
|
-
});
|
|
235
|
-
/**
|
|
236
|
-
* Renders the command menu in a Base UI Dialog.
|
|
237
|
-
*/
|
|
238
|
-
export const CommandDialog = React.forwardRef((props, forwardedRef) => {
|
|
239
|
-
const { open, defaultOpen, modal, onOpenChange, onOpenChangeComplete, disablePointerDismissal, actionsRef, handle, triggerId, defaultTriggerId, overlayClassName, contentClassName, container, ...etc } = props;
|
|
240
|
-
return (_jsx(BaseDialog.Root, { open: open, defaultOpen: defaultOpen, modal: modal, onOpenChange: onOpenChange, onOpenChangeComplete: onOpenChangeComplete, disablePointerDismissal: disablePointerDismissal, actionsRef: actionsRef, handle: handle, triggerId: triggerId, defaultTriggerId: defaultTriggerId, children: _jsxs(BaseDialog.Portal, { container: container, children: [_jsx(BaseDialog.Backdrop, { "cmdk-overlay": "", className: overlayClassName }), _jsx(BaseDialog.Popup, { "aria-labelledby": props["aria-labelledby"], "aria-label": props.label ?? props["aria-label"], "cmdk-dialog": "", className: contentClassName, children: _jsx(CommandRoot, { ref: forwardedRef, ...etc }) })] }) }));
|
|
241
|
-
});
|
|
242
|
-
/**
|
|
243
|
-
* Automatically renders when there are no results for the search query.
|
|
244
|
-
*/
|
|
245
|
-
export const CommandEmpty = React.forwardRef((props, forwardedRef) => {
|
|
246
|
-
const render = useCommandState((state) => state.ready && state.filtered.count === 0);
|
|
247
|
-
if (!render)
|
|
248
|
-
return null;
|
|
249
|
-
return _jsx("div", { ref: forwardedRef, ...props, "cmdk-empty": "", role: "presentation" });
|
|
250
|
-
});
|
|
251
|
-
/**
|
|
252
|
-
* You should conditionally render this with `progress` while loading asynchronous items.
|
|
253
|
-
*/
|
|
254
|
-
export const CommandLoading = React.forwardRef((props, forwardedRef) => {
|
|
255
|
-
const { progress, children: _children, label = "Loading...", ...etc } = props;
|
|
256
|
-
return (_jsx("div", { ref: forwardedRef, ...etc, "cmdk-loading": "", role: "progressbar", "aria-valuenow": progress, "aria-valuemin": 0, "aria-valuemax": 100, "aria-label": label, children: wrapChildren(props, (child) => (_jsx("div", { "aria-hidden": true, children: child }))) }));
|
|
257
|
-
});
|
package/dist/root.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
export { defaultFilter } from "./collection.js";
|
|
3
|
-
export declare const CommandRoot: React.ForwardRefExoticComponent<{
|
|
4
|
-
children?: React.ReactNode;
|
|
5
|
-
} & Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & {
|
|
6
|
-
label?: string;
|
|
7
|
-
shouldFilter?: boolean;
|
|
8
|
-
defaultSearch?: string;
|
|
9
|
-
filter?: import("./types.js").CommandFilter;
|
|
10
|
-
defaultValue?: string;
|
|
11
|
-
value?: string;
|
|
12
|
-
onValueChange?: (value: string) => void;
|
|
13
|
-
loop?: boolean;
|
|
14
|
-
tabToGroup?: boolean;
|
|
15
|
-
disablePointerSelection?: boolean;
|
|
16
|
-
vimBindings?: boolean;
|
|
17
|
-
} & React.RefAttributes<HTMLDivElement>>;
|
package/dist/root.js
DELETED
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import * as React from "react";
|
|
4
|
-
import { useRefWithInit } from "@base-ui/utils/useRefWithInit";
|
|
5
|
-
import { useValueAsRef } from "@base-ui/utils/useValueAsRef";
|
|
6
|
-
import { useIsoLayoutEffect as useLayoutEffect } from "@base-ui/utils/useIsoLayoutEffect";
|
|
7
|
-
import { CommandContext, GroupContext, StoreContext, GROUP_SELECTOR, GROUP_HEADING_SELECTOR, VALID_ITEM_SELECTOR, SELECT_EVENT, VALUE_ATTR, wrapChildren, } from "./context.js";
|
|
8
|
-
import { createCollection, defaultFilter } from "./collection.js";
|
|
9
|
-
import { trackOrder } from "./order.js";
|
|
10
|
-
import { CommandInput, CommandList } from "./parts.js";
|
|
11
|
-
export { defaultFilter } from "./collection.js";
|
|
12
|
-
// Resolve declarative input/list props before SSR renders either sibling.
|
|
13
|
-
function readParts(children) {
|
|
14
|
-
let input;
|
|
15
|
-
let list;
|
|
16
|
-
function visit(nodes) {
|
|
17
|
-
React.Children.forEach(nodes, (node) => {
|
|
18
|
-
if ((input && list) || !React.isValidElement(node))
|
|
19
|
-
return;
|
|
20
|
-
if (node.type === CommandInput)
|
|
21
|
-
input ??= node.props;
|
|
22
|
-
else if (node.type === CommandList)
|
|
23
|
-
list ??= node.props;
|
|
24
|
-
else if (typeof node.type === "string" ||
|
|
25
|
-
node.type === React.Fragment ||
|
|
26
|
-
node.type === React.Suspense)
|
|
27
|
-
visit(node.props.children);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
visit(children);
|
|
31
|
-
return { input, list };
|
|
32
|
-
}
|
|
33
|
-
export const CommandRoot = React.forwardRef((props, forwardedRef) => {
|
|
34
|
-
const { label, children: _children, value, defaultValue, defaultSearch, onValueChange: _onValueChange, filter, shouldFilter, loop: _loop, tabToGroup = false, disablePointerSelection, vimBindings = true, ...etc } = props;
|
|
35
|
-
const propsRef = useValueAsRef(props);
|
|
36
|
-
const declared = React.useMemo(() => readParts(props.children), [props.children]);
|
|
37
|
-
const generatedListId = React.useId();
|
|
38
|
-
const [mountedListId, setListId] = React.useState();
|
|
39
|
-
const listId = declared.list?.id ?? mountedListId ?? generatedListId;
|
|
40
|
-
const labelId = React.useId();
|
|
41
|
-
const generatedInputId = React.useId();
|
|
42
|
-
const [mountedInputId, setInputId] = React.useState();
|
|
43
|
-
const inputId = declared.input?.id ?? mountedInputId ?? generatedInputId;
|
|
44
|
-
const listInnerRef = React.useRef(null);
|
|
45
|
-
const [, invalidate] = React.useReducer((n) => n + 1, 0);
|
|
46
|
-
const collection = useRefWithInit(() => createCollection(invalidate)).current;
|
|
47
|
-
const listeners = useRefWithInit(() => new Set()).current;
|
|
48
|
-
const state = useRefWithInit(() => {
|
|
49
|
-
const search = String(declared.input?.value ?? declared.input?.defaultValue ?? defaultSearch ?? "");
|
|
50
|
-
return {
|
|
51
|
-
search,
|
|
52
|
-
value: (value ?? defaultValue ?? "").trim(),
|
|
53
|
-
ready: false,
|
|
54
|
-
filtered: {
|
|
55
|
-
...collection.filterItems(search),
|
|
56
|
-
ranked: shouldFilter !== false && Boolean(search),
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
});
|
|
60
|
-
const ordering = React.useRef(null);
|
|
61
|
-
const resetSelection = React.useRef(false);
|
|
62
|
-
const accessibleName = label ?? props["aria-label"];
|
|
63
|
-
const labelledBy = props["aria-labelledby"] ?? (accessibleName ? labelId : undefined);
|
|
64
|
-
function publish(change) {
|
|
65
|
-
state.current = { ...state.current, ...change };
|
|
66
|
-
listeners.forEach((listener) => listener());
|
|
67
|
-
}
|
|
68
|
-
function restoreOrder() {
|
|
69
|
-
ordering.current?.arrange();
|
|
70
|
-
ordering.current?.disconnect();
|
|
71
|
-
ordering.current = null;
|
|
72
|
-
}
|
|
73
|
-
const store = useRefWithInit(() => ({
|
|
74
|
-
initialSnapshot: state.current,
|
|
75
|
-
snapshot: () => state.current,
|
|
76
|
-
subscribe(cb) {
|
|
77
|
-
listeners.add(cb);
|
|
78
|
-
return () => listeners.delete(cb);
|
|
79
|
-
},
|
|
80
|
-
setState(key, next, skipScroll) {
|
|
81
|
-
if (state.current[key] === next)
|
|
82
|
-
return;
|
|
83
|
-
if (key === "search") {
|
|
84
|
-
if (!next)
|
|
85
|
-
restoreOrder();
|
|
86
|
-
resetSelection.current = true;
|
|
87
|
-
publish({ search: next, filtered: collection.filterItems(next) });
|
|
88
|
-
invalidate();
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
if (propsRef.current.value === undefined) {
|
|
92
|
-
publish({ value: next, selectedItemId: getItemByValue(next)?.id });
|
|
93
|
-
const focused = focusedMenuElement();
|
|
94
|
-
if (focused?.matches("[cmdk-root]"))
|
|
95
|
-
focused.querySelector("[cmdk-input]")?.focus();
|
|
96
|
-
}
|
|
97
|
-
propsRef.current.onValueChange?.(next);
|
|
98
|
-
if (!skipScroll)
|
|
99
|
-
scrollSelectedIntoView();
|
|
100
|
-
},
|
|
101
|
-
})).current;
|
|
102
|
-
const context = React.useMemo(() => ({
|
|
103
|
-
...collection,
|
|
104
|
-
disablePointerSelection,
|
|
105
|
-
initialScore: (item, keywords) => (filter ?? defaultFilter)(item, store.initialSnapshot.search, keywords?.map((word) => word.trim())),
|
|
106
|
-
setInputId,
|
|
107
|
-
setListId,
|
|
108
|
-
labelledBy,
|
|
109
|
-
listId,
|
|
110
|
-
labelId,
|
|
111
|
-
inputId,
|
|
112
|
-
listInnerRef,
|
|
113
|
-
}), [collection, disablePointerSelection, labelledBy, listId, labelId, inputId, filter, store]);
|
|
114
|
-
useLayoutEffect(() => {
|
|
115
|
-
if (collection.refresh(filter, shouldFilter)) {
|
|
116
|
-
if (shouldFilter === false)
|
|
117
|
-
restoreOrder();
|
|
118
|
-
publish({ filtered: collection.filterItems(state.current.search), ready: true });
|
|
119
|
-
// The rows must commit their visibility before resolving the active option.
|
|
120
|
-
invalidate();
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
const list = listInnerRef.current;
|
|
124
|
-
if (list && state.current.filtered.ranked) {
|
|
125
|
-
if (ordering.current?.list !== list) {
|
|
126
|
-
ordering.current?.disconnect();
|
|
127
|
-
ordering.current = trackOrder(list, () => ordering.current?.arrange(state.current.filtered));
|
|
128
|
-
}
|
|
129
|
-
ordering.current.arrange(state.current.filtered);
|
|
130
|
-
}
|
|
131
|
-
else if (ordering.current)
|
|
132
|
-
restoreOrder();
|
|
133
|
-
const selectedValue = value?.trim() ?? state.current.value;
|
|
134
|
-
let selected = getItemByValue(selectedValue);
|
|
135
|
-
if (value !== undefined && state.current.value !== selectedValue) {
|
|
136
|
-
publish({ value: selectedValue, selectedItemId: selected?.id });
|
|
137
|
-
}
|
|
138
|
-
const selectFirst = value === undefined && (resetSelection.current || !selected);
|
|
139
|
-
resetSelection.current = false;
|
|
140
|
-
if (selectFirst) {
|
|
141
|
-
const first = getValidItems()[0];
|
|
142
|
-
store.setState("value", first?.getAttribute(VALUE_ATTR) ?? "");
|
|
143
|
-
selected = first;
|
|
144
|
-
}
|
|
145
|
-
if (state.current.selectedItemId !== selected?.id)
|
|
146
|
-
publish({ selectedItemId: selected?.id });
|
|
147
|
-
});
|
|
148
|
-
useLayoutEffect(() => () => {
|
|
149
|
-
ordering.current?.disconnect();
|
|
150
|
-
ordering.current = null;
|
|
151
|
-
}, []);
|
|
152
|
-
function focusedMenuElement() {
|
|
153
|
-
const list = listInnerRef.current;
|
|
154
|
-
const tree = list?.getRootNode();
|
|
155
|
-
const focused = tree?.activeElement;
|
|
156
|
-
return focused && focused.closest("[cmdk-root]") === list?.closest("[cmdk-root]")
|
|
157
|
-
? focused
|
|
158
|
-
: null;
|
|
159
|
-
}
|
|
160
|
-
function isAvailable(item) {
|
|
161
|
-
return (!item.closest("[hidden], [inert]") &&
|
|
162
|
-
item.closest("[cmdk-list-sizer]") === listInnerRef.current);
|
|
163
|
-
}
|
|
164
|
-
function getValidItems() {
|
|
165
|
-
return Array.from(listInnerRef.current?.querySelectorAll(VALID_ITEM_SELECTOR) ?? []).filter(isAvailable);
|
|
166
|
-
}
|
|
167
|
-
function getItemByValue(value) {
|
|
168
|
-
if (!value)
|
|
169
|
-
return;
|
|
170
|
-
const query = VALID_ITEM_SELECTOR + `[${VALUE_ATTR}="${CSS.escape(value)}"]`;
|
|
171
|
-
return Array.from(listInnerRef.current?.querySelectorAll(query) ?? []).find(isAvailable);
|
|
172
|
-
}
|
|
173
|
-
function getSelectedItem() {
|
|
174
|
-
return getItemByValue(state.current.value);
|
|
175
|
-
}
|
|
176
|
-
function scrollSelectedIntoView() {
|
|
177
|
-
// Wait for popover positioning to complete before scrolling
|
|
178
|
-
requestAnimationFrame(() => {
|
|
179
|
-
if (!focusedMenuElement()?.matches("[cmdk-input], [cmdk-root], [cmdk-list]"))
|
|
180
|
-
return;
|
|
181
|
-
const item = getSelectedItem();
|
|
182
|
-
if (item) {
|
|
183
|
-
if (item.parentElement?.firstElementChild === item) {
|
|
184
|
-
// First item in Group, ensure heading is in view
|
|
185
|
-
item
|
|
186
|
-
.closest(GROUP_SELECTOR)
|
|
187
|
-
?.querySelector(GROUP_HEADING_SELECTOR)
|
|
188
|
-
?.scrollIntoView({ block: "nearest" });
|
|
189
|
-
}
|
|
190
|
-
// Ensure the item is always in view
|
|
191
|
-
item.scrollIntoView({ block: "nearest" });
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
/** Setters */
|
|
196
|
-
function updateSelectedToIndex(index) {
|
|
197
|
-
const items = getValidItems();
|
|
198
|
-
const item = items[index];
|
|
199
|
-
if (item)
|
|
200
|
-
store.setState("value", item.getAttribute(VALUE_ATTR) ?? "");
|
|
201
|
-
}
|
|
202
|
-
function updateSelectedByItem(change) {
|
|
203
|
-
const selected = getSelectedItem();
|
|
204
|
-
const items = getValidItems();
|
|
205
|
-
const index = items.findIndex((item) => item === selected);
|
|
206
|
-
// Get item at this index
|
|
207
|
-
let newSelected = items[index + change];
|
|
208
|
-
if (propsRef.current?.loop) {
|
|
209
|
-
newSelected =
|
|
210
|
-
index + change < 0
|
|
211
|
-
? items[items.length - 1]
|
|
212
|
-
: index + change === items.length
|
|
213
|
-
? items[0]
|
|
214
|
-
: items[index + change];
|
|
215
|
-
}
|
|
216
|
-
if (newSelected)
|
|
217
|
-
store.setState("value", newSelected.getAttribute(VALUE_ATTR) ?? "");
|
|
218
|
-
}
|
|
219
|
-
function updateSelectedByGroup(change, cycle = false) {
|
|
220
|
-
const currentGroup = getSelectedItem()?.closest(GROUP_SELECTOR);
|
|
221
|
-
const groups = new Map();
|
|
222
|
-
for (const option of getValidItems()) {
|
|
223
|
-
const group = option.closest(GROUP_SELECTOR);
|
|
224
|
-
if (group && !groups.has(group))
|
|
225
|
-
groups.set(group, option);
|
|
226
|
-
}
|
|
227
|
-
const index = currentGroup ? [...groups.keys()].indexOf(currentGroup) : -1;
|
|
228
|
-
const items = [...groups.values()];
|
|
229
|
-
if (cycle && items.length < 2)
|
|
230
|
-
return;
|
|
231
|
-
const nextIndex = cycle ? (index + change + items.length) % items.length : index + change;
|
|
232
|
-
const item = index < 0 ? undefined : items[nextIndex];
|
|
233
|
-
if (item) {
|
|
234
|
-
store.setState("value", item.getAttribute(VALUE_ATTR) ?? "");
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
updateSelectedByItem(change);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
const last = () => updateSelectedToIndex(getValidItems().length - 1);
|
|
241
|
-
const next = (e) => {
|
|
242
|
-
e.preventDefault();
|
|
243
|
-
if (e.metaKey) {
|
|
244
|
-
// Last item
|
|
245
|
-
last();
|
|
246
|
-
}
|
|
247
|
-
else if (e.altKey) {
|
|
248
|
-
// Next group
|
|
249
|
-
updateSelectedByGroup(1);
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
// Next item
|
|
253
|
-
updateSelectedByItem(1);
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
const prev = (e) => {
|
|
257
|
-
e.preventDefault();
|
|
258
|
-
if (e.metaKey) {
|
|
259
|
-
// First item
|
|
260
|
-
updateSelectedToIndex(0);
|
|
261
|
-
}
|
|
262
|
-
else if (e.altKey) {
|
|
263
|
-
// Previous group
|
|
264
|
-
updateSelectedByGroup(-1);
|
|
265
|
-
}
|
|
266
|
-
else {
|
|
267
|
-
// Previous item
|
|
268
|
-
updateSelectedByItem(-1);
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
return (_jsxs("div", { ref: forwardedRef, tabIndex: -1, ...etc, "cmdk-root": "", onKeyDown: (e) => {
|
|
272
|
-
etc.onKeyDown?.(e);
|
|
273
|
-
// Check if IME composition is finished before triggering key binds
|
|
274
|
-
// This prevents unwanted triggering while user is still inputting text with IME
|
|
275
|
-
// e.keyCode === 229 is for the CJK IME with Legacy Browser [https://w3c.github.io/uievents/#determine-keydown-keyup-keyCode]
|
|
276
|
-
// isComposing is for the CJK IME with Modern Browser [https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent/isComposing]
|
|
277
|
-
const isComposing = e.nativeEvent.isComposing || e.keyCode === 229;
|
|
278
|
-
if (e.defaultPrevented ||
|
|
279
|
-
isComposing ||
|
|
280
|
-
(e.target !== e.currentTarget &&
|
|
281
|
-
(!(e.target instanceof Element) ||
|
|
282
|
-
!e.target.matches("[cmdk-input], [cmdk-list]") ||
|
|
283
|
-
e.target.closest("[cmdk-root]") !== e.currentTarget))) {
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
switch (e.key) {
|
|
287
|
-
case "Tab": {
|
|
288
|
-
if (tabToGroup && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
289
|
-
e.preventDefault();
|
|
290
|
-
updateSelectedByGroup(e.shiftKey ? -1 : 1, true);
|
|
291
|
-
}
|
|
292
|
-
break;
|
|
293
|
-
}
|
|
294
|
-
case "n":
|
|
295
|
-
case "j": {
|
|
296
|
-
// vim keybind down
|
|
297
|
-
if (vimBindings && e.ctrlKey) {
|
|
298
|
-
next(e);
|
|
299
|
-
}
|
|
300
|
-
break;
|
|
301
|
-
}
|
|
302
|
-
case "ArrowDown": {
|
|
303
|
-
next(e);
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
case "p":
|
|
307
|
-
case "k": {
|
|
308
|
-
// vim keybind up
|
|
309
|
-
if (vimBindings && e.ctrlKey) {
|
|
310
|
-
prev(e);
|
|
311
|
-
}
|
|
312
|
-
break;
|
|
313
|
-
}
|
|
314
|
-
case "ArrowUp": {
|
|
315
|
-
prev(e);
|
|
316
|
-
break;
|
|
317
|
-
}
|
|
318
|
-
case "Home": {
|
|
319
|
-
// First item
|
|
320
|
-
e.preventDefault();
|
|
321
|
-
updateSelectedToIndex(0);
|
|
322
|
-
break;
|
|
323
|
-
}
|
|
324
|
-
case "End": {
|
|
325
|
-
// Last item
|
|
326
|
-
e.preventDefault();
|
|
327
|
-
last();
|
|
328
|
-
break;
|
|
329
|
-
}
|
|
330
|
-
case "Enter": {
|
|
331
|
-
// Trigger item onSelect
|
|
332
|
-
e.preventDefault();
|
|
333
|
-
const item = getSelectedItem();
|
|
334
|
-
if (item) {
|
|
335
|
-
const event = new Event(SELECT_EVENT);
|
|
336
|
-
item.dispatchEvent(event);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}, children: [accessibleName && (_jsx("label", { "cmdk-label": "", htmlFor: context.inputId, id: context.labelId,
|
|
341
|
-
// Screen reader only
|
|
342
|
-
style: srOnlyStyles, children: accessibleName })), wrapChildren(props, (child) => (_jsx(StoreContext.Provider, { value: store, children: _jsx(CommandContext.Provider, { value: context, children: _jsx(GroupContext.Provider, { value: null, children: child }) }) })))] }));
|
|
343
|
-
});
|
|
344
|
-
const srOnlyStyles = {
|
|
345
|
-
position: "absolute",
|
|
346
|
-
width: "1px",
|
|
347
|
-
height: "1px",
|
|
348
|
-
padding: "0",
|
|
349
|
-
margin: "-1px",
|
|
350
|
-
overflow: "hidden",
|
|
351
|
-
clip: "rect(0, 0, 0, 0)",
|
|
352
|
-
whiteSpace: "nowrap",
|
|
353
|
-
borderWidth: "0",
|
|
354
|
-
};
|