@rrstudioapps/ngx-command-palette 0.1.0 → 0.3.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/README.md CHANGED
@@ -142,7 +142,68 @@ injectCommandProvider({
142
142
  | `recent` | `{ max?, storageKey? } \| false` | `{ max: 5, storageKey: 'rr-cp:recent' }` | "Recent" section shown when the query is empty. |
143
143
  | `debounceMs` | `number` | `200` | Debounce for async providers. |
144
144
  | `maxResults` | `number` | `50` | Max local results for a non-empty query. |
145
- | `labels` | `Partial<CommandPaletteLabels>` | English | All texts, for translations: `placeholder`, `dialog`, `listbox`, `empty`, `loading`, `recent`, `defaultGroup`, `results` (`'{count} results'`), `closeHint`. |
145
+ | `maxEmptyResults` | `number` | `100` | Max commands listed when the search is empty (recents not counted). The rest is still found by typing. See [Many commands](#many-commands). |
146
+ | `filter` | `(query, command) => { score, indices } \| null` | built-in fuzzy search | Your own matching and ranking of local commands. See [Custom search](#custom-search). |
147
+ | `labels` | `Partial<CommandPaletteLabels>` | English | All texts, for translations: `placeholder`, `dialog`, `listbox`, `empty`, `loading`, `recent`, `defaultGroup`, `results` (`'{count} results'`), `closeHint`, `back`, `level` (`'{name}: {count} results'`), `submenu`. |
148
+
149
+ ### Many commands
150
+
151
+ Searching is cheap: with 5,000 commands a keystroke costs a few tens of milliseconds. With an empty search,
152
+ though, every command is rendered, which takes roughly 0.2 s for a thousand commands and 0.6 s for five
153
+ thousand on a slow virtual server. That is why, until the user types, only the first `maxEmptyResults`
154
+ commands are listed (100 by default, in registration order; recents not counted). The rest is found by
155
+ searching, and opening takes the same time whatever the number of commands.
156
+
157
+ ```ts
158
+ provideCommandPalette({ maxEmptyResults: Infinity }); // list them all
159
+ ```
160
+
161
+ For very large catalogs, also consider [sub-palettes](#sub-palettes) or an async [provider](#commandprovider).
162
+
163
+ ### Custom search
164
+
165
+ By default commands are matched with the built-in fuzzy search (label, keywords, description). Pass `filter`
166
+ to decide what matches and how results are ranked. Return `null` to hide a command; a higher `score` ranks first,
167
+ and `indices` are the label characters to highlight:
168
+
169
+ ```ts
170
+ provideCommandPalette({
171
+ // Only commands whose label starts with the query
172
+ filter: (query, command) =>
173
+ command.label.toLowerCase().startsWith(query.toLowerCase())
174
+ ? { score: 1, indices: Array.from({ length: query.length }, (_, i) => i) }
175
+ : null,
176
+ });
177
+ ```
178
+
179
+ `filter` only applies to local commands with a non-empty query. Results of async providers keep the order
180
+ returned by your API. To build on the default behavior, call the exported `matchCommand(query, command)`.
181
+
182
+ ### Sub-palettes
183
+
184
+ Give a command `children` and selecting it lists those commands instead of running something. The path of
185
+ opened parents shows above the search field, and the search only looks inside the current level:
186
+
187
+ ```ts
188
+ injectCommands([
189
+ {
190
+ id: 'theme',
191
+ label: 'Change theme',
192
+ keywords: ['appearance'],
193
+ children: [
194
+ { id: 'theme-light', label: 'Light', run: () => setTheme('light') },
195
+ { id: 'theme-dark', label: 'Dark', run: () => setTheme('dark') },
196
+ ],
197
+ },
198
+ ]);
199
+ ```
200
+
201
+ - **Backspace** on an empty search, the back button or a step of the path goes back; **Esc** closes the whole palette.
202
+ - Children can have children. They support `hidden` and `disabled` like any command.
203
+ - Running a child saves it in the recents, so it shows up at the top level afterwards. Parents are never saved.
204
+ - Async providers search the top level only, and only registered commands can have `children`
205
+ (not the results of a provider).
206
+ - Screen readers hear the level name and the number of results when a sub-palette opens.
146
207
 
147
208
  ### `Command`
148
209
 
@@ -156,6 +217,8 @@ injectCommandProvider({
156
217
  | `shortcut` | `string[]?` | Keys shown on the right, e.g. `['⌘', 'S']`. Display only. |
157
218
  | `keywords` | `string[]?` | Extra search terms, not shown. |
158
219
  | `disabled` | `boolean?` | Shown but not selectable. |
220
+ | `children` | `Command[]?` | Makes it a parent: selecting it opens a sub-palette with these commands instead of running something. See [Sub-palettes](#sub-palettes). |
221
+ | `hidden` | `boolean \| (() => boolean)?` | Not listed and not searched. A function or a signal hides it depending on your app state, e.g. `hidden: () => !user()`. |
159
222
  | `route` + `navigationExtras` | `string \| any[]`, `NavigationExtras?` | Navigate with the Angular router… |
160
223
  | `run` | `(ctx: { query }) => void \| Promise` | …or run a function. |
161
224
 
@@ -164,7 +227,9 @@ injectCommandProvider({
164
227
  | Member | Description |
165
228
  | --- | --- |
166
229
  | `isOpen`, `query`, `loading` | Read-only signals. |
167
- | `commands`, `sections` | Registered commands and the filtered, grouped results (signals). |
230
+ | `commands`, `sections` | Registered commands and the filtered, grouped results of the current level (signals). |
231
+ | `path` | The sub-palettes currently open, outermost first (signal). |
232
+ | `back(levels?)` | Goes back one or more levels of a sub-palette; returns `false` at the top level. |
168
233
  | `open(query?)`, `close()`, `toggle()` | Open/close programmatically (e.g. from a "Search…" button). |
169
234
  | `setQuery(query)` | Change the search text. |
170
235
  | `register(commands \| signal, destroyRef?)` | Returns an unregister function. Inside an injection context it unregisters on destroy. |