@nucleux/command 0.2.0
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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/index.cjs +219 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +192 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nucleux
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @nucleux/command
|
|
2
|
+
|
|
3
|
+
Searchable command palette — Nucleux agentic UI for React.
|
|
4
|
+
|
|
5
|
+
Part of **[Nucleux](https://github.com/intelligaia-open-labs/nucleux)** — agentic UI components for React. Install a single component, or the full kit via [`@nucleux/react`](https://www.npmjs.com/package/@nucleux/react).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @nucleux/command @nucleux/tokens
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`react` / `react-dom` are peer dependencies.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { Command } from "@nucleux/command";
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires the theme once in your app: `import "@nucleux/tokens/styles.css";`
|
|
22
|
+
and the Tailwind preset from `@nucleux/tokens/preset`.
|
|
23
|
+
|
|
24
|
+
## License
|
|
25
|
+
|
|
26
|
+
MIT © Nucleux
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var lucideReact = require('lucide-react');
|
|
6
|
+
var utils = require('@nucleux/utils');
|
|
7
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
+
|
|
9
|
+
function _interopNamespace(e) {
|
|
10
|
+
if (e && e.__esModule) return e;
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
28
|
+
|
|
29
|
+
// src/index.tsx
|
|
30
|
+
var CommandContext = React__namespace.createContext(null);
|
|
31
|
+
var useCommand = (component) => {
|
|
32
|
+
const ctx = React__namespace.useContext(CommandContext);
|
|
33
|
+
if (!ctx) throw new Error(`${component} must be used within a Command`);
|
|
34
|
+
return ctx;
|
|
35
|
+
};
|
|
36
|
+
var matches = (text, search) => text.toLowerCase().includes(search.trim().toLowerCase());
|
|
37
|
+
var Command = React__namespace.forwardRef(
|
|
38
|
+
({ className, children, ...props }, ref) => {
|
|
39
|
+
const [search, setSearch] = React__namespace.useState("");
|
|
40
|
+
const [activeValue, setActiveValue] = React__namespace.useState(null);
|
|
41
|
+
const [, setEmpty] = React__namespace.useState(false);
|
|
42
|
+
const listRef = React__namespace.useRef(null);
|
|
43
|
+
const reportEmpty = React__namespace.useCallback((e) => setEmpty(e), []);
|
|
44
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
45
|
+
"div",
|
|
46
|
+
{
|
|
47
|
+
ref,
|
|
48
|
+
className: utils.cn(
|
|
49
|
+
"flex w-full flex-col overflow-hidden rounded-lg border border-border bg-background text-foreground shadow-md",
|
|
50
|
+
className
|
|
51
|
+
),
|
|
52
|
+
...props,
|
|
53
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
54
|
+
CommandContext.Provider,
|
|
55
|
+
{
|
|
56
|
+
value: { search, setSearch, activeValue, setActiveValue, listRef, reportEmpty },
|
|
57
|
+
children
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
Command.displayName = "Command";
|
|
65
|
+
var CommandInput = React__namespace.forwardRef(
|
|
66
|
+
({ className, placeholder = "Type a command or search\u2026", ...props }, ref) => {
|
|
67
|
+
const { search, setSearch, listRef, activeValue, setActiveValue } = useCommand("CommandInput");
|
|
68
|
+
const visibleItems = () => Array.from(listRef.current?.querySelectorAll("[data-command-item]") ?? []);
|
|
69
|
+
const handleKeyDown = (e) => {
|
|
70
|
+
const items = visibleItems();
|
|
71
|
+
if (items.length === 0) return;
|
|
72
|
+
const values = items.map((el) => el.dataset.value ?? "");
|
|
73
|
+
const idx = values.indexOf(activeValue ?? "");
|
|
74
|
+
if (e.key === "ArrowDown") {
|
|
75
|
+
e.preventDefault();
|
|
76
|
+
setActiveValue(values[(idx + 1) % values.length] ?? null);
|
|
77
|
+
} else if (e.key === "ArrowUp") {
|
|
78
|
+
e.preventDefault();
|
|
79
|
+
setActiveValue(values[(idx - 1 + values.length) % values.length] ?? null);
|
|
80
|
+
} else if (e.key === "Home") {
|
|
81
|
+
e.preventDefault();
|
|
82
|
+
setActiveValue(values[0] ?? null);
|
|
83
|
+
} else if (e.key === "End") {
|
|
84
|
+
e.preventDefault();
|
|
85
|
+
setActiveValue(values[values.length - 1] ?? null);
|
|
86
|
+
} else if (e.key === "Enter") {
|
|
87
|
+
e.preventDefault();
|
|
88
|
+
const el = items.find((i) => i.dataset.value === activeValue) ?? items[0];
|
|
89
|
+
el?.click();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2 border-b border-border px-3", children: [
|
|
93
|
+
/* @__PURE__ */ jsxRuntime.jsx(lucideReact.Search, { "aria-hidden": true, className: "h-4 w-4 shrink-0 text-muted-foreground" }),
|
|
94
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
95
|
+
"input",
|
|
96
|
+
{
|
|
97
|
+
ref,
|
|
98
|
+
type: "text",
|
|
99
|
+
role: "combobox",
|
|
100
|
+
"aria-expanded": "true",
|
|
101
|
+
"aria-controls": "command-list",
|
|
102
|
+
value: search,
|
|
103
|
+
placeholder,
|
|
104
|
+
onChange: (e) => {
|
|
105
|
+
setSearch(e.target.value);
|
|
106
|
+
setActiveValue(null);
|
|
107
|
+
},
|
|
108
|
+
onKeyDown: handleKeyDown,
|
|
109
|
+
className: utils.cn(
|
|
110
|
+
"flex h-11 w-full bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground",
|
|
111
|
+
className
|
|
112
|
+
),
|
|
113
|
+
...props
|
|
114
|
+
}
|
|
115
|
+
)
|
|
116
|
+
] });
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
CommandInput.displayName = "CommandInput";
|
|
120
|
+
var CommandList = React__namespace.forwardRef(
|
|
121
|
+
({ className, children, ...props }, ref) => {
|
|
122
|
+
const { listRef, search, reportEmpty, activeValue, setActiveValue } = useCommand("CommandList");
|
|
123
|
+
const setRefs = (node) => {
|
|
124
|
+
listRef.current = node;
|
|
125
|
+
if (typeof ref === "function") ref(node);
|
|
126
|
+
else if (ref) ref.current = node;
|
|
127
|
+
};
|
|
128
|
+
React__namespace.useEffect(() => {
|
|
129
|
+
const items = Array.from(
|
|
130
|
+
listRef.current?.querySelectorAll("[data-command-item]") ?? []
|
|
131
|
+
);
|
|
132
|
+
reportEmpty(items.length === 0);
|
|
133
|
+
const values = items.map((el) => el.dataset.value ?? "");
|
|
134
|
+
if (values.length && !values.includes(activeValue ?? "")) setActiveValue(values[0] ?? null);
|
|
135
|
+
}, [search, listRef, reportEmpty, activeValue, setActiveValue]);
|
|
136
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
137
|
+
"div",
|
|
138
|
+
{
|
|
139
|
+
ref: setRefs,
|
|
140
|
+
id: "command-list",
|
|
141
|
+
role: "listbox",
|
|
142
|
+
className: utils.cn("max-h-72 overflow-y-auto overflow-x-hidden p-1", className),
|
|
143
|
+
...props,
|
|
144
|
+
children
|
|
145
|
+
}
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
CommandList.displayName = "CommandList";
|
|
150
|
+
var CommandGroup = React__namespace.forwardRef(
|
|
151
|
+
({ className, heading, children, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsxs("div", { ref, role: "group", className: utils.cn("overflow-hidden py-1", className), ...props, children: [
|
|
152
|
+
heading && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-2 py-1.5 text-xs font-medium text-muted-foreground", children: heading }),
|
|
153
|
+
children
|
|
154
|
+
] })
|
|
155
|
+
);
|
|
156
|
+
CommandGroup.displayName = "CommandGroup";
|
|
157
|
+
var CommandItem = React__namespace.forwardRef(
|
|
158
|
+
({ className, value, disabled = false, onSelect, onClick, children, ...props }, ref) => {
|
|
159
|
+
const { search, activeValue, setActiveValue } = useCommand("CommandItem");
|
|
160
|
+
if (search && !matches(value, search)) return null;
|
|
161
|
+
const active = activeValue === value;
|
|
162
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
163
|
+
"div",
|
|
164
|
+
{
|
|
165
|
+
ref,
|
|
166
|
+
"data-command-item": "",
|
|
167
|
+
"data-value": value,
|
|
168
|
+
role: "option",
|
|
169
|
+
"aria-selected": active,
|
|
170
|
+
"aria-disabled": disabled || void 0,
|
|
171
|
+
onMouseMove: () => !disabled && setActiveValue(value),
|
|
172
|
+
onClick: (e) => {
|
|
173
|
+
onClick?.(e);
|
|
174
|
+
if (!disabled && !e.defaultPrevented) onSelect?.(value);
|
|
175
|
+
},
|
|
176
|
+
className: utils.cn(
|
|
177
|
+
"flex cursor-pointer items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none",
|
|
178
|
+
active && "bg-accent text-accent-foreground",
|
|
179
|
+
disabled && "pointer-events-none opacity-50",
|
|
180
|
+
className
|
|
181
|
+
),
|
|
182
|
+
...props,
|
|
183
|
+
children
|
|
184
|
+
}
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
CommandItem.displayName = "CommandItem";
|
|
189
|
+
var CommandEmpty = React__namespace.forwardRef(
|
|
190
|
+
({ className, children = "No results found.", ...props }, ref) => {
|
|
191
|
+
const { listRef, search } = useCommand("CommandEmpty");
|
|
192
|
+
const [empty, setEmpty] = React__namespace.useState(false);
|
|
193
|
+
React__namespace.useEffect(() => {
|
|
194
|
+
const count = listRef.current?.querySelectorAll("[data-command-item]").length ?? 0;
|
|
195
|
+
setEmpty(count === 0);
|
|
196
|
+
}, [search, listRef]);
|
|
197
|
+
if (!empty) return null;
|
|
198
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
199
|
+
"div",
|
|
200
|
+
{
|
|
201
|
+
ref,
|
|
202
|
+
role: "presentation",
|
|
203
|
+
className: utils.cn("py-6 text-center text-sm text-muted-foreground", className),
|
|
204
|
+
...props,
|
|
205
|
+
children
|
|
206
|
+
}
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
);
|
|
210
|
+
CommandEmpty.displayName = "CommandEmpty";
|
|
211
|
+
|
|
212
|
+
exports.Command = Command;
|
|
213
|
+
exports.CommandEmpty = CommandEmpty;
|
|
214
|
+
exports.CommandGroup = CommandGroup;
|
|
215
|
+
exports.CommandInput = CommandInput;
|
|
216
|
+
exports.CommandItem = CommandItem;
|
|
217
|
+
exports.CommandList = CommandList;
|
|
218
|
+
//# sourceMappingURL=index.cjs.map
|
|
219
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"names":["React","jsx","cn","jsxs","Search"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAaA,IAAM,cAAA,GAAuBA,+BAA0C,IAAI,CAAA;AAC3E,IAAM,UAAA,GAAa,CAAC,SAAA,KAAsB;AACxC,EAAA,MAAM,GAAA,GAAYA,4BAAW,cAAc,CAAA;AAC3C,EAAA,IAAI,CAAC,GAAA,EAAK,MAAM,IAAI,KAAA,CAAM,CAAA,EAAG,SAAS,CAAA,8BAAA,CAAgC,CAAA;AACtE,EAAA,OAAO,GAAA;AACT,CAAA;AAEA,IAAM,OAAA,GAAU,CAAC,IAAA,EAAc,MAAA,KAC7B,IAAA,CAAK,WAAA,EAAY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,QACA,SAAA,EAAWC,QAAA;AAAA,UACT,oFAAA;AAAA,UACA,MAAA,IAAU,kCAAA;AAAA,UACV,QAAA,IAAY,gCAAA;AAAA,UACZ;AAAA,SACF;AAAA,QACC,GAAG,KAAA;AAAA,QAEH;AAAA;AAAA,KACH;AAAA,EAEJ;AACF;AACA,WAAA,CAAY,WAAA,GAAc,aAAA;AAKnB,IAAM,YAAA,GAAqBF,gBAAA,CAAA,UAAA;AAAA,EAChC,CAAC,EAAE,SAAA,EAAW,QAAA,GAAW,qBAAqB,GAAG,KAAA,IAAS,GAAA,KAAQ;AAChE,IAAA,MAAM,EAAE,OAAA,EAAS,MAAA,EAAO,GAAI,WAAW,cAAc,CAAA;AACrD,IAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAUA,0BAAS,KAAK,CAAA;AAE9C,IAAMA,2BAAU,MAAM;AACpB,MAAA,MAAM,QACJ,OAAA,CAAQ,OAAA,EAAS,gBAAA,CAAiB,qBAAqB,EAAE,MAAA,IAAU,CAAA;AACrE,MAAA,QAAA,CAAS,UAAU,CAAC,CAAA;AAAA,IACtB,CAAA,EAAG,CAAC,MAAA,EAAQ,OAAO,CAAC,CAAA;AAEpB,IAAA,IAAI,CAAC,OAAO,OAAO,IAAA;AACnB,IAAA,uBACEC,cAAA;AAAA,MAAC,KAAA;AAAA,MAAA;AAAA,QACC,GAAA;AAAA,QACA,IAAA,EAAK,cAAA;AAAA,QACL,SAAA,EAAWC,QAAA,CAAG,gDAAA,EAAkD,SAAS,CAAA;AAAA,QACxE,GAAG,KAAA;AAAA,QAEH;AAAA;AAAA,KACH;AAAA,EAEJ;AACF;AACA,YAAA,CAAa,WAAA,GAAc,cAAA","file":"index.cjs","sourcesContent":["import * as React from \"react\";\nimport { Search } from \"lucide-react\";\nimport { cn } from \"@nucleux/utils\";\n\ninterface CommandContextValue {\n search: string;\n setSearch: (v: string) => void;\n activeValue: string | null;\n setActiveValue: (v: string | null) => void;\n listRef: React.RefObject<HTMLDivElement>;\n reportEmpty: (empty: boolean) => void;\n}\n\nconst CommandContext = React.createContext<CommandContextValue | null>(null);\nconst useCommand = (component: string) => {\n const ctx = React.useContext(CommandContext);\n if (!ctx) throw new Error(`${component} must be used within a Command`);\n return ctx;\n};\n\nconst matches = (text: string, search: string) =>\n text.toLowerCase().includes(search.trim().toLowerCase());\n\nexport type CommandProps = React.HTMLAttributes<HTMLDivElement>;\n\n/**\n * A composable command palette: a filterable, keyboard-navigable list of\n * actions. Compose with CommandInput, CommandList, CommandGroup, CommandItem,\n * and CommandEmpty.\n */\nexport const Command = React.forwardRef<HTMLDivElement, CommandProps>(\n ({ className, children, ...props }, ref) => {\n const [search, setSearch] = React.useState(\"\");\n const [activeValue, setActiveValue] = React.useState<string | null>(null);\n const [, setEmpty] = React.useState(false);\n const listRef = React.useRef<HTMLDivElement>(null);\n\n const reportEmpty = React.useCallback((e: boolean) => setEmpty(e), []);\n\n return (\n <div\n ref={ref}\n className={cn(\n \"flex w-full flex-col overflow-hidden rounded-lg border border-border bg-background text-foreground shadow-md\",\n className,\n )}\n {...props}\n >\n <CommandContext.Provider\n value={{ search, setSearch, activeValue, setActiveValue, listRef, reportEmpty }}\n >\n {children}\n </CommandContext.Provider>\n </div>\n );\n },\n);\nCommand.displayName = \"Command\";\n\nexport type CommandInputProps = Omit<\n React.InputHTMLAttributes<HTMLInputElement>,\n \"value\" | \"onChange\"\n>;\n\n/** The search field that filters the command list. */\nexport const CommandInput = React.forwardRef<HTMLInputElement, CommandInputProps>(\n ({ className, placeholder = \"Type a command or search…\", ...props }, ref) => {\n const { search, setSearch, listRef, activeValue, setActiveValue } = useCommand(\"CommandInput\");\n\n const visibleItems = () =>\n Array.from(listRef.current?.querySelectorAll<HTMLElement>(\"[data-command-item]\") ?? []);\n\n const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n const items = visibleItems();\n if (items.length === 0) return;\n const values = items.map((el) => el.dataset.value ?? \"\");\n const idx = values.indexOf(activeValue ?? \"\");\n if (e.key === \"ArrowDown\") {\n e.preventDefault();\n setActiveValue(values[(idx + 1) % values.length] ?? null);\n } else if (e.key === \"ArrowUp\") {\n e.preventDefault();\n setActiveValue(values[(idx - 1 + values.length) % values.length] ?? null);\n } else if (e.key === \"Home\") {\n e.preventDefault();\n setActiveValue(values[0] ?? null);\n } else if (e.key === \"End\") {\n e.preventDefault();\n setActiveValue(values[values.length - 1] ?? null);\n } else if (e.key === \"Enter\") {\n e.preventDefault();\n const el = items.find((i) => i.dataset.value === activeValue) ?? items[0];\n el?.click();\n }\n };\n\n return (\n <div className=\"flex items-center gap-2 border-b border-border px-3\">\n <Search aria-hidden className=\"h-4 w-4 shrink-0 text-muted-foreground\" />\n <input\n ref={ref}\n type=\"text\"\n role=\"combobox\"\n aria-expanded=\"true\"\n aria-controls=\"command-list\"\n value={search}\n placeholder={placeholder}\n onChange={(e) => {\n setSearch(e.target.value);\n setActiveValue(null);\n }}\n onKeyDown={handleKeyDown}\n className={cn(\n \"flex h-11 w-full bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground\",\n className,\n )}\n {...props}\n />\n </div>\n );\n },\n);\nCommandInput.displayName = \"CommandInput\";\n\nexport type CommandListProps = React.HTMLAttributes<HTMLDivElement>;\n\n/** Scrollable container for command groups and items. */\nexport const CommandList = React.forwardRef<HTMLDivElement, CommandListProps>(\n ({ className, children, ...props }, ref) => {\n const { listRef, search, reportEmpty, activeValue, setActiveValue } = useCommand(\"CommandList\");\n const setRefs = (node: HTMLDivElement | null) => {\n (listRef as React.MutableRefObject<HTMLDivElement | null>).current = node;\n if (typeof ref === \"function\") ref(node);\n else if (ref) (ref as React.MutableRefObject<HTMLDivElement | null>).current = node;\n };\n\n // After each search change, recount visible items and keep an active item.\n React.useEffect(() => {\n const items = Array.from(\n listRef.current?.querySelectorAll<HTMLElement>(\"[data-command-item]\") ?? [],\n );\n reportEmpty(items.length === 0);\n const values = items.map((el) => el.dataset.value ?? \"\");\n if (values.length && !values.includes(activeValue ?? \"\")) setActiveValue(values[0] ?? null);\n }, [search, listRef, reportEmpty, activeValue, setActiveValue]);\n\n return (\n <div\n ref={setRefs}\n id=\"command-list\"\n role=\"listbox\"\n className={cn(\"max-h-72 overflow-y-auto overflow-x-hidden p-1\", className)}\n {...props}\n >\n {children}\n </div>\n );\n },\n);\nCommandList.displayName = \"CommandList\";\n\nexport interface CommandGroupProps extends React.HTMLAttributes<HTMLDivElement> {\n heading?: React.ReactNode;\n}\n\n/** A labelled group of {@link CommandItem}s. */\nexport const CommandGroup = React.forwardRef<HTMLDivElement, CommandGroupProps>(\n ({ className, heading, children, ...props }, ref) => (\n <div ref={ref} role=\"group\" className={cn(\"overflow-hidden py-1\", className)} {...props}>\n {heading && (\n <div className=\"px-2 py-1.5 text-xs font-medium text-muted-foreground\">{heading}</div>\n )}\n {children}\n </div>\n ),\n);\nCommandGroup.displayName = \"CommandGroup\";\n\nexport interface CommandItemProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onSelect\"> {\n /** The value used for filtering + selection. Defaults to the text content. */\n value: string;\n disabled?: boolean;\n onSelect?: (value: string) => void;\n}\n\n/** A selectable command. Hidden automatically when it doesn't match the search. */\nexport const CommandItem = React.forwardRef<HTMLDivElement, CommandItemProps>(\n ({ className, value, disabled = false, onSelect, onClick, children, ...props }, ref) => {\n const { search, activeValue, setActiveValue } = useCommand(\"CommandItem\");\n if (search && !matches(value, search)) return null;\n const active = activeValue === value;\n\n return (\n <div\n ref={ref}\n data-command-item=\"\"\n data-value={value}\n role=\"option\"\n aria-selected={active}\n aria-disabled={disabled || undefined}\n onMouseMove={() => !disabled && setActiveValue(value)}\n onClick={(e) => {\n onClick?.(e);\n if (!disabled && !e.defaultPrevented) onSelect?.(value);\n }}\n className={cn(\n \"flex cursor-pointer items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none\",\n active && \"bg-accent text-accent-foreground\",\n disabled && \"pointer-events-none opacity-50\",\n className,\n )}\n {...props}\n >\n {children}\n </div>\n );\n },\n);\nCommandItem.displayName = \"CommandItem\";\n\nexport type CommandEmptyProps = React.HTMLAttributes<HTMLDivElement>;\n\n/** Shown when no items match the current search. */\nexport const CommandEmpty = React.forwardRef<HTMLDivElement, CommandEmptyProps>(\n ({ className, children = \"No results found.\", ...props }, ref) => {\n const { listRef, search } = useCommand(\"CommandEmpty\");\n const [empty, setEmpty] = React.useState(false);\n\n React.useEffect(() => {\n const count =\n listRef.current?.querySelectorAll(\"[data-command-item]\").length ?? 0;\n setEmpty(count === 0);\n }, [search, listRef]);\n\n if (!empty) return null;\n return (\n <div\n ref={ref}\n role=\"presentation\"\n className={cn(\"py-6 text-center text-sm text-muted-foreground\", className)}\n {...props}\n >\n {children}\n </div>\n );\n },\n);\nCommandEmpty.displayName = \"CommandEmpty\";\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
type CommandProps = React.HTMLAttributes<HTMLDivElement>;
|
|
4
|
+
/**
|
|
5
|
+
* A composable command palette: a filterable, keyboard-navigable list of
|
|
6
|
+
* actions. Compose with CommandInput, CommandList, CommandGroup, CommandItem,
|
|
7
|
+
* and CommandEmpty.
|
|
8
|
+
*/
|
|
9
|
+
declare const Command: React.ForwardRefExoticComponent<CommandProps & React.RefAttributes<HTMLDivElement>>;
|
|
10
|
+
type CommandInputProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "value" | "onChange">;
|
|
11
|
+
/** The search field that filters the command list. */
|
|
12
|
+
declare const CommandInput: React.ForwardRefExoticComponent<CommandInputProps & React.RefAttributes<HTMLInputElement>>;
|
|
13
|
+
type CommandListProps = React.HTMLAttributes<HTMLDivElement>;
|
|
14
|
+
/** Scrollable container for command groups and items. */
|
|
15
|
+
declare const CommandList: React.ForwardRefExoticComponent<CommandListProps & React.RefAttributes<HTMLDivElement>>;
|
|
16
|
+
interface CommandGroupProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
17
|
+
heading?: React.ReactNode;
|
|
18
|
+
}
|
|
19
|
+
/** A labelled group of {@link CommandItem}s. */
|
|
20
|
+
declare const CommandGroup: React.ForwardRefExoticComponent<CommandGroupProps & React.RefAttributes<HTMLDivElement>>;
|
|
21
|
+
interface CommandItemProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onSelect"> {
|
|
22
|
+
/** The value used for filtering + selection. Defaults to the text content. */
|
|
23
|
+
value: string;
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
onSelect?: (value: string) => void;
|
|
26
|
+
}
|
|
27
|
+
/** A selectable command. Hidden automatically when it doesn't match the search. */
|
|
28
|
+
declare const CommandItem: React.ForwardRefExoticComponent<CommandItemProps & React.RefAttributes<HTMLDivElement>>;
|
|
29
|
+
type CommandEmptyProps = React.HTMLAttributes<HTMLDivElement>;
|
|
30
|
+
/** Shown when no items match the current search. */
|
|
31
|
+
declare const CommandEmpty: React.ForwardRefExoticComponent<CommandEmptyProps & React.RefAttributes<HTMLDivElement>>;
|
|
32
|
+
|
|
33
|
+
export { Command, CommandEmpty, type CommandEmptyProps, CommandGroup, type CommandGroupProps, CommandInput, type CommandInputProps, CommandItem, type CommandItemProps, CommandList, type CommandListProps, type CommandProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
type CommandProps = React.HTMLAttributes<HTMLDivElement>;
|
|
4
|
+
/**
|
|
5
|
+
* A composable command palette: a filterable, keyboard-navigable list of
|
|
6
|
+
* actions. Compose with CommandInput, CommandList, CommandGroup, CommandItem,
|
|
7
|
+
* and CommandEmpty.
|
|
8
|
+
*/
|
|
9
|
+
declare const Command: React.ForwardRefExoticComponent<CommandProps & React.RefAttributes<HTMLDivElement>>;
|
|
10
|
+
type CommandInputProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "value" | "onChange">;
|
|
11
|
+
/** The search field that filters the command list. */
|
|
12
|
+
declare const CommandInput: React.ForwardRefExoticComponent<CommandInputProps & React.RefAttributes<HTMLInputElement>>;
|
|
13
|
+
type CommandListProps = React.HTMLAttributes<HTMLDivElement>;
|
|
14
|
+
/** Scrollable container for command groups and items. */
|
|
15
|
+
declare const CommandList: React.ForwardRefExoticComponent<CommandListProps & React.RefAttributes<HTMLDivElement>>;
|
|
16
|
+
interface CommandGroupProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
17
|
+
heading?: React.ReactNode;
|
|
18
|
+
}
|
|
19
|
+
/** A labelled group of {@link CommandItem}s. */
|
|
20
|
+
declare const CommandGroup: React.ForwardRefExoticComponent<CommandGroupProps & React.RefAttributes<HTMLDivElement>>;
|
|
21
|
+
interface CommandItemProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onSelect"> {
|
|
22
|
+
/** The value used for filtering + selection. Defaults to the text content. */
|
|
23
|
+
value: string;
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
onSelect?: (value: string) => void;
|
|
26
|
+
}
|
|
27
|
+
/** A selectable command. Hidden automatically when it doesn't match the search. */
|
|
28
|
+
declare const CommandItem: React.ForwardRefExoticComponent<CommandItemProps & React.RefAttributes<HTMLDivElement>>;
|
|
29
|
+
type CommandEmptyProps = React.HTMLAttributes<HTMLDivElement>;
|
|
30
|
+
/** Shown when no items match the current search. */
|
|
31
|
+
declare const CommandEmpty: React.ForwardRefExoticComponent<CommandEmptyProps & React.RefAttributes<HTMLDivElement>>;
|
|
32
|
+
|
|
33
|
+
export { Command, CommandEmpty, type CommandEmptyProps, CommandGroup, type CommandGroupProps, CommandInput, type CommandInputProps, CommandItem, type CommandItemProps, CommandList, type CommandListProps, type CommandProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { Search } from 'lucide-react';
|
|
4
|
+
import { cn } from '@nucleux/utils';
|
|
5
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
// src/index.tsx
|
|
8
|
+
var CommandContext = React.createContext(null);
|
|
9
|
+
var useCommand = (component) => {
|
|
10
|
+
const ctx = React.useContext(CommandContext);
|
|
11
|
+
if (!ctx) throw new Error(`${component} must be used within a Command`);
|
|
12
|
+
return ctx;
|
|
13
|
+
};
|
|
14
|
+
var matches = (text, search) => text.toLowerCase().includes(search.trim().toLowerCase());
|
|
15
|
+
var Command = React.forwardRef(
|
|
16
|
+
({ className, children, ...props }, ref) => {
|
|
17
|
+
const [search, setSearch] = React.useState("");
|
|
18
|
+
const [activeValue, setActiveValue] = React.useState(null);
|
|
19
|
+
const [, setEmpty] = React.useState(false);
|
|
20
|
+
const listRef = React.useRef(null);
|
|
21
|
+
const reportEmpty = React.useCallback((e) => setEmpty(e), []);
|
|
22
|
+
return /* @__PURE__ */ jsx(
|
|
23
|
+
"div",
|
|
24
|
+
{
|
|
25
|
+
ref,
|
|
26
|
+
className: cn(
|
|
27
|
+
"flex w-full flex-col overflow-hidden rounded-lg border border-border bg-background text-foreground shadow-md",
|
|
28
|
+
className
|
|
29
|
+
),
|
|
30
|
+
...props,
|
|
31
|
+
children: /* @__PURE__ */ jsx(
|
|
32
|
+
CommandContext.Provider,
|
|
33
|
+
{
|
|
34
|
+
value: { search, setSearch, activeValue, setActiveValue, listRef, reportEmpty },
|
|
35
|
+
children
|
|
36
|
+
}
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
Command.displayName = "Command";
|
|
43
|
+
var CommandInput = React.forwardRef(
|
|
44
|
+
({ className, placeholder = "Type a command or search\u2026", ...props }, ref) => {
|
|
45
|
+
const { search, setSearch, listRef, activeValue, setActiveValue } = useCommand("CommandInput");
|
|
46
|
+
const visibleItems = () => Array.from(listRef.current?.querySelectorAll("[data-command-item]") ?? []);
|
|
47
|
+
const handleKeyDown = (e) => {
|
|
48
|
+
const items = visibleItems();
|
|
49
|
+
if (items.length === 0) return;
|
|
50
|
+
const values = items.map((el) => el.dataset.value ?? "");
|
|
51
|
+
const idx = values.indexOf(activeValue ?? "");
|
|
52
|
+
if (e.key === "ArrowDown") {
|
|
53
|
+
e.preventDefault();
|
|
54
|
+
setActiveValue(values[(idx + 1) % values.length] ?? null);
|
|
55
|
+
} else if (e.key === "ArrowUp") {
|
|
56
|
+
e.preventDefault();
|
|
57
|
+
setActiveValue(values[(idx - 1 + values.length) % values.length] ?? null);
|
|
58
|
+
} else if (e.key === "Home") {
|
|
59
|
+
e.preventDefault();
|
|
60
|
+
setActiveValue(values[0] ?? null);
|
|
61
|
+
} else if (e.key === "End") {
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
setActiveValue(values[values.length - 1] ?? null);
|
|
64
|
+
} else if (e.key === "Enter") {
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
const el = items.find((i) => i.dataset.value === activeValue) ?? items[0];
|
|
67
|
+
el?.click();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 border-b border-border px-3", children: [
|
|
71
|
+
/* @__PURE__ */ jsx(Search, { "aria-hidden": true, className: "h-4 w-4 shrink-0 text-muted-foreground" }),
|
|
72
|
+
/* @__PURE__ */ jsx(
|
|
73
|
+
"input",
|
|
74
|
+
{
|
|
75
|
+
ref,
|
|
76
|
+
type: "text",
|
|
77
|
+
role: "combobox",
|
|
78
|
+
"aria-expanded": "true",
|
|
79
|
+
"aria-controls": "command-list",
|
|
80
|
+
value: search,
|
|
81
|
+
placeholder,
|
|
82
|
+
onChange: (e) => {
|
|
83
|
+
setSearch(e.target.value);
|
|
84
|
+
setActiveValue(null);
|
|
85
|
+
},
|
|
86
|
+
onKeyDown: handleKeyDown,
|
|
87
|
+
className: cn(
|
|
88
|
+
"flex h-11 w-full bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground",
|
|
89
|
+
className
|
|
90
|
+
),
|
|
91
|
+
...props
|
|
92
|
+
}
|
|
93
|
+
)
|
|
94
|
+
] });
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
CommandInput.displayName = "CommandInput";
|
|
98
|
+
var CommandList = React.forwardRef(
|
|
99
|
+
({ className, children, ...props }, ref) => {
|
|
100
|
+
const { listRef, search, reportEmpty, activeValue, setActiveValue } = useCommand("CommandList");
|
|
101
|
+
const setRefs = (node) => {
|
|
102
|
+
listRef.current = node;
|
|
103
|
+
if (typeof ref === "function") ref(node);
|
|
104
|
+
else if (ref) ref.current = node;
|
|
105
|
+
};
|
|
106
|
+
React.useEffect(() => {
|
|
107
|
+
const items = Array.from(
|
|
108
|
+
listRef.current?.querySelectorAll("[data-command-item]") ?? []
|
|
109
|
+
);
|
|
110
|
+
reportEmpty(items.length === 0);
|
|
111
|
+
const values = items.map((el) => el.dataset.value ?? "");
|
|
112
|
+
if (values.length && !values.includes(activeValue ?? "")) setActiveValue(values[0] ?? null);
|
|
113
|
+
}, [search, listRef, reportEmpty, activeValue, setActiveValue]);
|
|
114
|
+
return /* @__PURE__ */ jsx(
|
|
115
|
+
"div",
|
|
116
|
+
{
|
|
117
|
+
ref: setRefs,
|
|
118
|
+
id: "command-list",
|
|
119
|
+
role: "listbox",
|
|
120
|
+
className: cn("max-h-72 overflow-y-auto overflow-x-hidden p-1", className),
|
|
121
|
+
...props,
|
|
122
|
+
children
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
CommandList.displayName = "CommandList";
|
|
128
|
+
var CommandGroup = React.forwardRef(
|
|
129
|
+
({ className, heading, children, ...props }, ref) => /* @__PURE__ */ jsxs("div", { ref, role: "group", className: cn("overflow-hidden py-1", className), ...props, children: [
|
|
130
|
+
heading && /* @__PURE__ */ jsx("div", { className: "px-2 py-1.5 text-xs font-medium text-muted-foreground", children: heading }),
|
|
131
|
+
children
|
|
132
|
+
] })
|
|
133
|
+
);
|
|
134
|
+
CommandGroup.displayName = "CommandGroup";
|
|
135
|
+
var CommandItem = React.forwardRef(
|
|
136
|
+
({ className, value, disabled = false, onSelect, onClick, children, ...props }, ref) => {
|
|
137
|
+
const { search, activeValue, setActiveValue } = useCommand("CommandItem");
|
|
138
|
+
if (search && !matches(value, search)) return null;
|
|
139
|
+
const active = activeValue === value;
|
|
140
|
+
return /* @__PURE__ */ jsx(
|
|
141
|
+
"div",
|
|
142
|
+
{
|
|
143
|
+
ref,
|
|
144
|
+
"data-command-item": "",
|
|
145
|
+
"data-value": value,
|
|
146
|
+
role: "option",
|
|
147
|
+
"aria-selected": active,
|
|
148
|
+
"aria-disabled": disabled || void 0,
|
|
149
|
+
onMouseMove: () => !disabled && setActiveValue(value),
|
|
150
|
+
onClick: (e) => {
|
|
151
|
+
onClick?.(e);
|
|
152
|
+
if (!disabled && !e.defaultPrevented) onSelect?.(value);
|
|
153
|
+
},
|
|
154
|
+
className: cn(
|
|
155
|
+
"flex cursor-pointer items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none",
|
|
156
|
+
active && "bg-accent text-accent-foreground",
|
|
157
|
+
disabled && "pointer-events-none opacity-50",
|
|
158
|
+
className
|
|
159
|
+
),
|
|
160
|
+
...props,
|
|
161
|
+
children
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
CommandItem.displayName = "CommandItem";
|
|
167
|
+
var CommandEmpty = React.forwardRef(
|
|
168
|
+
({ className, children = "No results found.", ...props }, ref) => {
|
|
169
|
+
const { listRef, search } = useCommand("CommandEmpty");
|
|
170
|
+
const [empty, setEmpty] = React.useState(false);
|
|
171
|
+
React.useEffect(() => {
|
|
172
|
+
const count = listRef.current?.querySelectorAll("[data-command-item]").length ?? 0;
|
|
173
|
+
setEmpty(count === 0);
|
|
174
|
+
}, [search, listRef]);
|
|
175
|
+
if (!empty) return null;
|
|
176
|
+
return /* @__PURE__ */ jsx(
|
|
177
|
+
"div",
|
|
178
|
+
{
|
|
179
|
+
ref,
|
|
180
|
+
role: "presentation",
|
|
181
|
+
className: cn("py-6 text-center text-sm text-muted-foreground", className),
|
|
182
|
+
...props,
|
|
183
|
+
children
|
|
184
|
+
}
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
CommandEmpty.displayName = "CommandEmpty";
|
|
189
|
+
|
|
190
|
+
export { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList };
|
|
191
|
+
//# sourceMappingURL=index.js.map
|
|
192
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"names":[],"mappings":";;;;;;AAaA,IAAM,cAAA,GAAuB,oBAA0C,IAAI,CAAA;AAC3E,IAAM,UAAA,GAAa,CAAC,SAAA,KAAsB;AACxC,EAAA,MAAM,GAAA,GAAY,iBAAW,cAAc,CAAA;AAC3C,EAAA,IAAI,CAAC,GAAA,EAAK,MAAM,IAAI,KAAA,CAAM,CAAA,EAAG,SAAS,CAAA,8BAAA,CAAgC,CAAA;AACtE,EAAA,OAAO,GAAA;AACT,CAAA;AAEA,IAAM,OAAA,GAAU,CAAC,IAAA,EAAc,MAAA,KAC7B,IAAA,CAAK,WAAA,EAAY,CAAE,QAAA,CAAS,MAAA,CAAO,IAAA,EAAK,CAAE,WAAA,EAAa,CAAA;AASlD,IAAM,OAAA,GAAgB,KAAA,CAAA,UAAA;AAAA,EAC3B,CAAC,EAAE,SAAA,EAAW,UAAU,GAAG,KAAA,IAAS,GAAA,KAAQ;AAC1C,IAAA,MAAM,CAAC,MAAA,EAAQ,SAAS,CAAA,GAAU,eAAS,EAAE,CAAA;AAC7C,IAAA,MAAM,CAAC,WAAA,EAAa,cAAc,CAAA,GAAU,eAAwB,IAAI,CAAA;AACxE,IAAA,MAAM,GAAG,QAAQ,CAAA,GAAU,eAAS,KAAK,CAAA;AACzC,IAAA,MAAM,OAAA,GAAgB,aAAuB,IAAI,CAAA;AAEjD,IAAA,MAAM,WAAA,GAAoB,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,gBAAU,MAAM;AACpB,MAAA,MAAM,QACJ,OAAA,CAAQ,OAAA,EAAS,gBAAA,CAAiB,qBAAqB,EAAE,MAAA,IAAU,CAAA;AACrE,MAAA,QAAA,CAAS,UAAU,CAAC,CAAA;AAAA,IACtB,CAAA,EAAG,CAAC,MAAA,EAAQ,OAAO,CAAC,CAAA;AAEpB,IAAA,IAAI,CAAC,OAAO,OAAO,IAAA;AACnB,IAAA,uBACE,GAAA;AAAA,MAAC,KAAA;AAAA,MAAA;AAAA,QACC,GAAA;AAAA,QACA,IAAA,EAAK,cAAA;AAAA,QACL,SAAA,EAAW,EAAA,CAAG,gDAAA,EAAkD,SAAS,CAAA;AAAA,QACxE,GAAG,KAAA;AAAA,QAEH;AAAA;AAAA,KACH;AAAA,EAEJ;AACF;AACA,YAAA,CAAa,WAAA,GAAc,cAAA","file":"index.js","sourcesContent":["import * as React from \"react\";\nimport { Search } from \"lucide-react\";\nimport { cn } from \"@nucleux/utils\";\n\ninterface CommandContextValue {\n search: string;\n setSearch: (v: string) => void;\n activeValue: string | null;\n setActiveValue: (v: string | null) => void;\n listRef: React.RefObject<HTMLDivElement>;\n reportEmpty: (empty: boolean) => void;\n}\n\nconst CommandContext = React.createContext<CommandContextValue | null>(null);\nconst useCommand = (component: string) => {\n const ctx = React.useContext(CommandContext);\n if (!ctx) throw new Error(`${component} must be used within a Command`);\n return ctx;\n};\n\nconst matches = (text: string, search: string) =>\n text.toLowerCase().includes(search.trim().toLowerCase());\n\nexport type CommandProps = React.HTMLAttributes<HTMLDivElement>;\n\n/**\n * A composable command palette: a filterable, keyboard-navigable list of\n * actions. Compose with CommandInput, CommandList, CommandGroup, CommandItem,\n * and CommandEmpty.\n */\nexport const Command = React.forwardRef<HTMLDivElement, CommandProps>(\n ({ className, children, ...props }, ref) => {\n const [search, setSearch] = React.useState(\"\");\n const [activeValue, setActiveValue] = React.useState<string | null>(null);\n const [, setEmpty] = React.useState(false);\n const listRef = React.useRef<HTMLDivElement>(null);\n\n const reportEmpty = React.useCallback((e: boolean) => setEmpty(e), []);\n\n return (\n <div\n ref={ref}\n className={cn(\n \"flex w-full flex-col overflow-hidden rounded-lg border border-border bg-background text-foreground shadow-md\",\n className,\n )}\n {...props}\n >\n <CommandContext.Provider\n value={{ search, setSearch, activeValue, setActiveValue, listRef, reportEmpty }}\n >\n {children}\n </CommandContext.Provider>\n </div>\n );\n },\n);\nCommand.displayName = \"Command\";\n\nexport type CommandInputProps = Omit<\n React.InputHTMLAttributes<HTMLInputElement>,\n \"value\" | \"onChange\"\n>;\n\n/** The search field that filters the command list. */\nexport const CommandInput = React.forwardRef<HTMLInputElement, CommandInputProps>(\n ({ className, placeholder = \"Type a command or search…\", ...props }, ref) => {\n const { search, setSearch, listRef, activeValue, setActiveValue } = useCommand(\"CommandInput\");\n\n const visibleItems = () =>\n Array.from(listRef.current?.querySelectorAll<HTMLElement>(\"[data-command-item]\") ?? []);\n\n const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n const items = visibleItems();\n if (items.length === 0) return;\n const values = items.map((el) => el.dataset.value ?? \"\");\n const idx = values.indexOf(activeValue ?? \"\");\n if (e.key === \"ArrowDown\") {\n e.preventDefault();\n setActiveValue(values[(idx + 1) % values.length] ?? null);\n } else if (e.key === \"ArrowUp\") {\n e.preventDefault();\n setActiveValue(values[(idx - 1 + values.length) % values.length] ?? null);\n } else if (e.key === \"Home\") {\n e.preventDefault();\n setActiveValue(values[0] ?? null);\n } else if (e.key === \"End\") {\n e.preventDefault();\n setActiveValue(values[values.length - 1] ?? null);\n } else if (e.key === \"Enter\") {\n e.preventDefault();\n const el = items.find((i) => i.dataset.value === activeValue) ?? items[0];\n el?.click();\n }\n };\n\n return (\n <div className=\"flex items-center gap-2 border-b border-border px-3\">\n <Search aria-hidden className=\"h-4 w-4 shrink-0 text-muted-foreground\" />\n <input\n ref={ref}\n type=\"text\"\n role=\"combobox\"\n aria-expanded=\"true\"\n aria-controls=\"command-list\"\n value={search}\n placeholder={placeholder}\n onChange={(e) => {\n setSearch(e.target.value);\n setActiveValue(null);\n }}\n onKeyDown={handleKeyDown}\n className={cn(\n \"flex h-11 w-full bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground\",\n className,\n )}\n {...props}\n />\n </div>\n );\n },\n);\nCommandInput.displayName = \"CommandInput\";\n\nexport type CommandListProps = React.HTMLAttributes<HTMLDivElement>;\n\n/** Scrollable container for command groups and items. */\nexport const CommandList = React.forwardRef<HTMLDivElement, CommandListProps>(\n ({ className, children, ...props }, ref) => {\n const { listRef, search, reportEmpty, activeValue, setActiveValue } = useCommand(\"CommandList\");\n const setRefs = (node: HTMLDivElement | null) => {\n (listRef as React.MutableRefObject<HTMLDivElement | null>).current = node;\n if (typeof ref === \"function\") ref(node);\n else if (ref) (ref as React.MutableRefObject<HTMLDivElement | null>).current = node;\n };\n\n // After each search change, recount visible items and keep an active item.\n React.useEffect(() => {\n const items = Array.from(\n listRef.current?.querySelectorAll<HTMLElement>(\"[data-command-item]\") ?? [],\n );\n reportEmpty(items.length === 0);\n const values = items.map((el) => el.dataset.value ?? \"\");\n if (values.length && !values.includes(activeValue ?? \"\")) setActiveValue(values[0] ?? null);\n }, [search, listRef, reportEmpty, activeValue, setActiveValue]);\n\n return (\n <div\n ref={setRefs}\n id=\"command-list\"\n role=\"listbox\"\n className={cn(\"max-h-72 overflow-y-auto overflow-x-hidden p-1\", className)}\n {...props}\n >\n {children}\n </div>\n );\n },\n);\nCommandList.displayName = \"CommandList\";\n\nexport interface CommandGroupProps extends React.HTMLAttributes<HTMLDivElement> {\n heading?: React.ReactNode;\n}\n\n/** A labelled group of {@link CommandItem}s. */\nexport const CommandGroup = React.forwardRef<HTMLDivElement, CommandGroupProps>(\n ({ className, heading, children, ...props }, ref) => (\n <div ref={ref} role=\"group\" className={cn(\"overflow-hidden py-1\", className)} {...props}>\n {heading && (\n <div className=\"px-2 py-1.5 text-xs font-medium text-muted-foreground\">{heading}</div>\n )}\n {children}\n </div>\n ),\n);\nCommandGroup.displayName = \"CommandGroup\";\n\nexport interface CommandItemProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onSelect\"> {\n /** The value used for filtering + selection. Defaults to the text content. */\n value: string;\n disabled?: boolean;\n onSelect?: (value: string) => void;\n}\n\n/** A selectable command. Hidden automatically when it doesn't match the search. */\nexport const CommandItem = React.forwardRef<HTMLDivElement, CommandItemProps>(\n ({ className, value, disabled = false, onSelect, onClick, children, ...props }, ref) => {\n const { search, activeValue, setActiveValue } = useCommand(\"CommandItem\");\n if (search && !matches(value, search)) return null;\n const active = activeValue === value;\n\n return (\n <div\n ref={ref}\n data-command-item=\"\"\n data-value={value}\n role=\"option\"\n aria-selected={active}\n aria-disabled={disabled || undefined}\n onMouseMove={() => !disabled && setActiveValue(value)}\n onClick={(e) => {\n onClick?.(e);\n if (!disabled && !e.defaultPrevented) onSelect?.(value);\n }}\n className={cn(\n \"flex cursor-pointer items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none\",\n active && \"bg-accent text-accent-foreground\",\n disabled && \"pointer-events-none opacity-50\",\n className,\n )}\n {...props}\n >\n {children}\n </div>\n );\n },\n);\nCommandItem.displayName = \"CommandItem\";\n\nexport type CommandEmptyProps = React.HTMLAttributes<HTMLDivElement>;\n\n/** Shown when no items match the current search. */\nexport const CommandEmpty = React.forwardRef<HTMLDivElement, CommandEmptyProps>(\n ({ className, children = \"No results found.\", ...props }, ref) => {\n const { listRef, search } = useCommand(\"CommandEmpty\");\n const [empty, setEmpty] = React.useState(false);\n\n React.useEffect(() => {\n const count =\n listRef.current?.querySelectorAll(\"[data-command-item]\").length ?? 0;\n setEmpty(count === 0);\n }, [search, listRef]);\n\n if (!empty) return null;\n return (\n <div\n ref={ref}\n role=\"presentation\"\n className={cn(\"py-6 text-center text-sm text-muted-foreground\", className)}\n {...props}\n >\n {children}\n </div>\n );\n },\n);\nCommandEmpty.displayName = \"CommandEmpty\";\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nucleux/command",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "Searchable command palette — Nucleux agentic UI for React.",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"nucleux",
|
|
9
|
+
"react",
|
|
10
|
+
"ui",
|
|
11
|
+
"components",
|
|
12
|
+
"agentic",
|
|
13
|
+
"ai",
|
|
14
|
+
"tailwind",
|
|
15
|
+
"command"
|
|
16
|
+
],
|
|
17
|
+
"main": "./dist/index.cjs",
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"require": "./dist/index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md"
|
|
30
|
+
],
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=18",
|
|
33
|
+
"react-dom": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"lucide-react": "^0.469.0",
|
|
37
|
+
"@nucleux/utils": "0.2.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"tsup": "^8.3.5",
|
|
41
|
+
"typescript": "^5.7.2",
|
|
42
|
+
"@types/react": "^18.3.18",
|
|
43
|
+
"react": "^18.3.1"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/intelligaia-open-labs/nucleux.git",
|
|
52
|
+
"directory": "packages/command"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://github.com/intelligaia-open-labs/nucleux#readme",
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup"
|
|
57
|
+
}
|
|
58
|
+
}
|