@rrstudioapps/ngx-command-palette 0.3.1 β 0.4.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
|
@@ -27,8 +27,8 @@ standalone components, signals, zoneless-ready and **no dependencies besides Ang
|
|
|
27
27
|
- Custom **templates** for items and the empty state
|
|
28
28
|
- No browser globals at import time (SSR friendly), works with or without zone.js
|
|
29
29
|
|
|
30
|
-
| Light
|
|
31
|
-
|
|
|
30
|
+
| Light | Dark |
|
|
31
|
+
| ----------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
|
|
32
32
|
|  |  |
|
|
33
33
|
|
|
34
34
|
## Installation
|
|
@@ -48,10 +48,7 @@ Requires Angular 21 or 22 (`@angular/core`, `@angular/common`, `@angular/router`
|
|
|
48
48
|
import { provideCommandPalette } from '@rrstudioapps/ngx-command-palette';
|
|
49
49
|
|
|
50
50
|
export const appConfig: ApplicationConfig = {
|
|
51
|
-
providers: [
|
|
52
|
-
provideRouter(routes),
|
|
53
|
-
provideCommandPalette({ shortcut: 'mod+k' }),
|
|
54
|
-
],
|
|
51
|
+
providers: [provideRouter(routes), provideCommandPalette({ shortcut: 'mod+k' })],
|
|
55
52
|
};
|
|
56
53
|
```
|
|
57
54
|
|
|
@@ -76,7 +73,13 @@ export class App {
|
|
|
76
73
|
constructor() {
|
|
77
74
|
injectCommands([
|
|
78
75
|
{ id: 'home', label: 'Home', group: 'Navigation', route: '/' },
|
|
79
|
-
{
|
|
76
|
+
{
|
|
77
|
+
id: 'settings',
|
|
78
|
+
label: 'Settings',
|
|
79
|
+
group: 'Navigation',
|
|
80
|
+
route: '/settings',
|
|
81
|
+
keywords: ['preferences'],
|
|
82
|
+
},
|
|
80
83
|
{ id: 'logout', label: 'Log out', icon: 'πͺ', run: () => this.auth.logout() },
|
|
81
84
|
]);
|
|
82
85
|
}
|
|
@@ -89,7 +92,7 @@ export class App {
|
|
|
89
92
|
Pass a signal to keep them in sync with your state:
|
|
90
93
|
|
|
91
94
|
```ts
|
|
92
|
-
@Component({
|
|
95
|
+
@Component({/* β¦ */})
|
|
93
96
|
export class ProjectsPage {
|
|
94
97
|
projects = signal<Project[]>([]);
|
|
95
98
|
|
|
@@ -135,16 +138,32 @@ injectCommandProvider({
|
|
|
135
138
|
|
|
136
139
|
### `provideCommandPalette(options?)`
|
|
137
140
|
|
|
138
|
-
| Option
|
|
139
|
-
|
|
|
140
|
-
| `shortcut`
|
|
141
|
-
| `theme`
|
|
142
|
-
| `recent`
|
|
143
|
-
| `debounceMs`
|
|
144
|
-
| `maxResults`
|
|
145
|
-
| `maxEmptyResults` | `number`
|
|
146
|
-
| `filter`
|
|
147
|
-
| `
|
|
141
|
+
| Option | Type | Default | Description |
|
|
142
|
+
| ----------------- | ------------------------------------------------ | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
143
|
+
| `shortcut` | `string \| string[]` | `'mod+k'` | Toggle shortcut(s). `mod` is β on macOS/iOS and Ctrl elsewhere. Examples: `'ctrl+shift+p'`, `'/'`. Shortcuts without Ctrl/β/Alt are ignored while typing in a field. |
|
|
144
|
+
| `theme` | `'light' \| 'dark' \| 'auto'` | `'auto'` | `auto` follows `prefers-color-scheme`. |
|
|
145
|
+
| `recent` | `{ max?, storageKey?, rank? } \| false` | `{ max: 5, storageKey: 'rr-cp:recent' }` | "Recent" section shown when the query is empty. `rank: 'frequent'` orders it by how often and how recently commands ran (count halves every 7 days). |
|
|
146
|
+
| `debounceMs` | `number` | `200` | Debounce for async providers. |
|
|
147
|
+
| `maxResults` | `number` | `50` | Max local results for a non-empty query. |
|
|
148
|
+
| `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). |
|
|
149
|
+
| `filter` | `(query, command) => { score, indices } \| null` | built-in fuzzy search | Your own matching and ranking of local commands. See [Custom search](#custom-search). |
|
|
150
|
+
| `prefixes` | `{ prefix, groups?, kind? }[]` | `[]` | Search modes. See [Prefix modes](#prefix-modes). |
|
|
151
|
+
| `labels` | `Partial<CommandPaletteLabels>` | English | All texts, for translations: `placeholder`, `dialog`, `listbox`, `empty`, `loading`, `recent`, `pinned`, `defaultGroup`, `results` (`'{count} results'`), `closeHint`, `back`, `level` (`'{name}: {count} results'`), `submenu`, `prompt` (`'{name}: type a value and press Enter'`). |
|
|
152
|
+
|
|
153
|
+
### Prefix modes
|
|
154
|
+
|
|
155
|
+
Start the search with a character to narrow the list, like `>` in VS Code. The prefix is removed from the
|
|
156
|
+
search text (and from `ctx.query`); the recents are not shown and async providers only run when the mode allows them.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
provideCommandPalette({
|
|
160
|
+
prefixes: [
|
|
161
|
+
{ prefix: '>', kind: 'action' }, // only commands that run a function
|
|
162
|
+
{ prefix: '/', kind: 'route' }, // only routes
|
|
163
|
+
{ prefix: '#', groups: ['Customers'] }, // only these sections (and providers)
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
```
|
|
148
167
|
|
|
149
168
|
### Many commands
|
|
150
169
|
|
|
@@ -207,55 +226,58 @@ injectCommands([
|
|
|
207
226
|
|
|
208
227
|
### `Command`
|
|
209
228
|
|
|
210
|
-
| Field
|
|
211
|
-
|
|
|
212
|
-
| `id`
|
|
213
|
-
| `label`
|
|
214
|
-
| `group`
|
|
215
|
-
| `description`
|
|
216
|
-
| `icon`
|
|
217
|
-
| `shortcut`
|
|
218
|
-
| `
|
|
219
|
-
| `
|
|
220
|
-
| `
|
|
221
|
-
| `
|
|
222
|
-
| `
|
|
223
|
-
| `
|
|
229
|
+
| Field | Type | Description |
|
|
230
|
+
| ---------------------------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
231
|
+
| `id` | `string` | Unique and stable (used for recents). |
|
|
232
|
+
| `label` | `string` | Shown and searched. |
|
|
233
|
+
| `group` | `string?` | Section. Default: `labels.defaultGroup`. |
|
|
234
|
+
| `description` | `string?` | Second line, searched with a lower weight. |
|
|
235
|
+
| `icon` | `string \| TemplateRef?` | Emoji/text/icon-font ligature, or a template (e.g. an SVG). |
|
|
236
|
+
| `shortcut` | `string[]?` | Keys shown on the right, e.g. `['β', 'S']`. Display only: use `hotkey` to bind a key. |
|
|
237
|
+
| `hotkey` | `string \| string[]?` | Key combination(s) that run the command even when the palette is closed, e.g. `'mod+s'`. It needs Ctrl, β or Alt; Alt-only combinations do not fire while typing in a text field (on macOS Alt types special characters), Ctrl and β ones do. Ignored for parents, disabled/hidden commands and while the palette is open. Shown on the row when there is no `shortcut`. |
|
|
238
|
+
| `keywords` | `string[]?` | Extra search terms, not shown. |
|
|
239
|
+
| `disabled` | `boolean?` | Shown but not selectable. |
|
|
240
|
+
| `children` | `Command[]?` | Makes it a parent: selecting it opens a sub-palette with these commands instead of running something. See [Sub-palettes](#sub-palettes). |
|
|
241
|
+
| `hidden` | `boolean \| (() => boolean)?` | Not listed and not searched. A function or a signal hides it depending on your app state, e.g. `hidden: () => !user()`. |
|
|
242
|
+
| `pinned` | `boolean \| (() => boolean)?` | Also listed in a "Pinned" section at the top while the search is empty (and not repeated in Recent). A function or a signal pins it depending on your app state. |
|
|
243
|
+
| `route` + `navigationExtras` | `string \| any[]`, `NavigationExtras?` | Navigate with the Angular router⦠|
|
|
244
|
+
| `run` | `(ctx: { query, value? }) => void \| Promise` | β¦or run a function. |
|
|
245
|
+
| `prompt` | `string?` | With `run`: asks for a value first. Selecting the command opens a second step whose field has this placeholder; Enter runs it with the text in `ctx.value` (nothing happens while empty), Backspace or the back button leaves the step. Example: `{ id: 'user', label: 'Go to user', prompt: 'User idβ¦', run: ({ value }) => router.navigate(['/users', value]) }`. |
|
|
224
246
|
|
|
225
247
|
### `CommandPaletteService`
|
|
226
248
|
|
|
227
|
-
| Member
|
|
228
|
-
|
|
|
229
|
-
| `isOpen`, `query`, `loading`
|
|
230
|
-
| `commands`, `sections`
|
|
231
|
-
| `path`
|
|
232
|
-
| `back(levels?)`
|
|
233
|
-
| `open(query?)`, `close()`, `toggle()`
|
|
234
|
-
| `setQuery(query)`
|
|
249
|
+
| Member | Description |
|
|
250
|
+
| ------------------------------------------- | -------------------------------------------------------------------------------------- |
|
|
251
|
+
| `isOpen`, `query`, `loading` | Read-only signals. |
|
|
252
|
+
| `commands`, `sections` | Registered commands and the filtered, grouped results of the current level (signals). |
|
|
253
|
+
| `path` | The sub-palettes currently open, outermost first (signal). |
|
|
254
|
+
| `back(levels?)` | Goes back one or more levels of a sub-palette; returns `false` at the top level. |
|
|
255
|
+
| `open(query?)`, `close()`, `toggle()` | Open/close programmatically (e.g. from a "Searchβ¦" button). |
|
|
256
|
+
| `setQuery(query)` | Change the search text. |
|
|
235
257
|
| `register(commands \| signal, destroyRef?)` | Returns an unregister function. Inside an injection context it unregisters on destroy. |
|
|
236
|
-
| `registerProvider(provider, destroyRef?)`
|
|
237
|
-
| `execute(command)`
|
|
238
|
-
| `clearRecent()`
|
|
258
|
+
| `registerProvider(provider, destroyRef?)` | Same, for async providers. |
|
|
259
|
+
| `execute(command)` | Run a command as if selected. |
|
|
260
|
+
| `clearRecent()` | Forget the recents. |
|
|
239
261
|
|
|
240
262
|
`injectCommands(commands)` and `injectCommandProvider(provider)` are shortcuts for
|
|
241
263
|
`register`/`registerProvider` bound to the current component.
|
|
242
264
|
|
|
243
265
|
### `CommandProvider`
|
|
244
266
|
|
|
245
|
-
| Field
|
|
246
|
-
|
|
|
247
|
-
| `id`, `group`
|
|
267
|
+
| Field | Type | Description |
|
|
268
|
+
| ----------------------- | --------------------------------------------- | ----------------------------------------------------------------- |
|
|
269
|
+
| `id`, `group` | `string` | Provider id and section title. |
|
|
248
270
|
| `search(query, signal)` | `Promise<Command[]> \| Observable<Command[]>` | `signal` is aborted when the query changes or the palette closes. |
|
|
249
|
-
| `minQueryLength`
|
|
250
|
-
| `debounceMs`
|
|
271
|
+
| `minQueryLength` | `number?` | Default `1`. |
|
|
272
|
+
| `debounceMs` | `number?` | Overrides the global debounce. |
|
|
251
273
|
|
|
252
274
|
Async results keep the order returned by your API. Route commands from a provider are kept
|
|
253
275
|
in "Recent" even when they are no longer returned by the search.
|
|
254
276
|
|
|
255
277
|
### `<rr-command-palette>`
|
|
256
278
|
|
|
257
|
-
| Input
|
|
258
|
-
|
|
|
279
|
+
| Input | Description |
|
|
280
|
+
| ------- | -------------------------------------------------------------------------------------- |
|
|
259
281
|
| `theme` | Overrides the configured theme, e.g. `[theme]="appTheme()"` to follow your own toggle. |
|
|
260
282
|
|
|
261
283
|
The component also exposes `shortcutLabel` (`'βK'` or `'Ctrl+K'`) for your search button:
|
|
@@ -268,9 +290,8 @@ The component also exposes `shortcutLabel` (`'βK'` or `'Ctrl+K'`) for your sea
|
|
|
268
290
|
<ng-template rrCommandItem let-command let-segments="segments" let-active="active">
|
|
269
291
|
<my-icon [name]="command.id" />
|
|
270
292
|
<span>
|
|
271
|
-
@for (s of segments; track $index) {
|
|
272
|
-
|
|
273
|
-
}
|
|
293
|
+
@for (s of segments; track $index) { @if (s.match) { <b>{{ s.text }}</b> } @else { {{ s.text
|
|
294
|
+
}} } }
|
|
274
295
|
</span>
|
|
275
296
|
@if (active) { <span class="hint">β΅</span> }
|
|
276
297
|
</ng-template>
|
|
@@ -290,6 +311,96 @@ The item context also has `result` (score and indices) and `query`.
|
|
|
290
311
|
splits a string for highlighting. `parseShortcut`, `matchesShortcut` and `formatShortcut`
|
|
291
312
|
handle shortcut strings. All of them are exported if you want the same behaviour elsewhere.
|
|
292
313
|
|
|
314
|
+
## Recipes
|
|
315
|
+
|
|
316
|
+
**Translations (i18n).** Pass every text through `labels` (`t` stands for your translation function; the library does not need `@angular/localize`). Translate your own command labels the same way when you build them:
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
provideCommandPalette({
|
|
320
|
+
labels: {
|
|
321
|
+
placeholder: t('palette.placeholder'),
|
|
322
|
+
empty: t('palette.empty'),
|
|
323
|
+
recent: t('palette.recent'),
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
**Commands that depend on the current page.** Register from the component that owns the page: they are removed when it is destroyed.
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
@Component({/* β¦ */})
|
|
332
|
+
export class InvoicePage {
|
|
333
|
+
protected readonly invoice = input.required<Invoice>();
|
|
334
|
+
constructor() {
|
|
335
|
+
injectCommands(
|
|
336
|
+
computed<Command[]>(() => [
|
|
337
|
+
{ id: 'invoice.send', label: `Send ${this.invoice().number}`, run: () => this.send() },
|
|
338
|
+
]),
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
**A button that opens the palette.** Inject `CommandPaletteService` as `palette` and show the shortcut label of the component:
|
|
345
|
+
|
|
346
|
+
```html
|
|
347
|
+
<button (click)="palette.open()">Search <kbd>{{ cp.shortcutLabel }}</kbd></button>
|
|
348
|
+
<rr-command-palette #cp />
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
**Restricting commands by permission.** Use `hidden` with a signal so the command disappears from the list, the search and the recents:
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
{ id: 'admin.users', label: 'Manage users', route: '/admin/users', hidden: () => !auth.isAdmin() }
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
**Matching your UI kit.** The palette is self-contained and styled with CSS variables (see [Theming](#theming)), so you map them to your kit's tokens instead of wrapping it in another dialog. These tokens follow the kit's light/dark switch, so one block covers both themes. Token names checked against Angular Material 22.2, PrimeNG 22.1 (Aura preset) and Bootstrap 5.3.8.
|
|
358
|
+
|
|
359
|
+
```css
|
|
360
|
+
/* Angular Material (M3 system tokens) */
|
|
361
|
+
.rr-cp {
|
|
362
|
+
--rr-cp-bg: var(--mat-sys-surface-container-high);
|
|
363
|
+
--rr-cp-fg: var(--mat-sys-on-surface);
|
|
364
|
+
--rr-cp-muted: var(--mat-sys-on-surface-variant);
|
|
365
|
+
--rr-cp-border: var(--mat-sys-outline-variant);
|
|
366
|
+
--rr-cp-accent: var(--mat-sys-primary);
|
|
367
|
+
--rr-cp-highlight: var(--mat-sys-primary);
|
|
368
|
+
--rr-cp-item-active-bg: var(--mat-sys-secondary-container);
|
|
369
|
+
--rr-cp-radius: var(--mat-sys-corner-extra-large);
|
|
370
|
+
--rr-cp-backdrop: color-mix(in srgb, var(--mat-sys-scrim) 32%, transparent);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/* PrimeNG (design tokens) */
|
|
374
|
+
.rr-cp {
|
|
375
|
+
--rr-cp-bg: var(--p-content-background);
|
|
376
|
+
--rr-cp-fg: var(--p-text-color);
|
|
377
|
+
--rr-cp-muted: var(--p-text-muted-color);
|
|
378
|
+
--rr-cp-border: var(--p-content-border-color);
|
|
379
|
+
--rr-cp-accent: var(--p-primary-color);
|
|
380
|
+
--rr-cp-highlight: var(--p-primary-color);
|
|
381
|
+
--rr-cp-highlight-bg: var(--p-highlight-background);
|
|
382
|
+
--rr-cp-item-active-bg: var(--p-content-hover-background);
|
|
383
|
+
--rr-cp-radius: var(--p-content-border-radius);
|
|
384
|
+
--rr-cp-backdrop: var(--p-mask-background);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/* Bootstrap 5.3 (also follows data-bs-theme="dark") */
|
|
388
|
+
.rr-cp {
|
|
389
|
+
--rr-cp-bg: var(--bs-body-bg);
|
|
390
|
+
--rr-cp-fg: var(--bs-body-color);
|
|
391
|
+
--rr-cp-muted: var(--bs-secondary-color);
|
|
392
|
+
--rr-cp-border: var(--bs-border-color);
|
|
393
|
+
--rr-cp-accent: var(--bs-primary);
|
|
394
|
+
--rr-cp-highlight: var(--bs-primary);
|
|
395
|
+
--rr-cp-item-active-bg: var(--bs-tertiary-bg);
|
|
396
|
+
--rr-cp-radius: var(--bs-border-radius-lg);
|
|
397
|
+
--rr-cp-font: var(--bs-body-font-family);
|
|
398
|
+
--rr-cp-shadow: var(--bs-box-shadow-lg);
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
Tailwind and shadcn-style setups have no fixed token names (each project defines its own), so map the variables the same way to yours, e.g. `--rr-cp-bg: var(--background)`.
|
|
403
|
+
|
|
293
404
|
## Theming
|
|
294
405
|
|
|
295
406
|
The styles use CSS variables with zero-specificity defaults, so any rule of yours wins.
|
|
@@ -308,18 +419,18 @@ Set them on `.rr-cp` (the overlay), and on `.rr-cp[data-theme='dark']` for the d
|
|
|
308
419
|
}
|
|
309
420
|
```
|
|
310
421
|
|
|
311
|
-
| Variable
|
|
312
|
-
|
|
|
422
|
+
| Variable | Purpose |
|
|
423
|
+
| ------------------------------------------------------------- | -------------------------------------- |
|
|
313
424
|
| `--rr-cp-bg`, `--rr-cp-fg`, `--rr-cp-muted`, `--rr-cp-border` | Surface, text, secondary text, borders |
|
|
314
|
-
| `--rr-cp-accent`
|
|
315
|
-
| `--rr-cp-item-active-bg`
|
|
316
|
-
| `--rr-cp-highlight`, `--rr-cp-highlight-bg`
|
|
317
|
-
| `--rr-cp-kbd-bg`, `--rr-cp-kbd-fg`
|
|
318
|
-
| `--rr-cp-backdrop`, `--rr-cp-shadow`
|
|
319
|
-
| `--rr-cp-font`, `--rr-cp-font-size`
|
|
320
|
-
| `--rr-cp-width`, `--rr-cp-max-height`, `--rr-cp-top`
|
|
321
|
-
| `--rr-cp-radius`, `--rr-cp-item-radius`
|
|
322
|
-
| `--rr-cp-z-index`, `--rr-cp-duration`
|
|
425
|
+
| `--rr-cp-accent` | Active row marker, spinner |
|
|
426
|
+
| `--rr-cp-item-active-bg` | Active row background |
|
|
427
|
+
| `--rr-cp-highlight`, `--rr-cp-highlight-bg` | Matched characters |
|
|
428
|
+
| `--rr-cp-kbd-bg`, `--rr-cp-kbd-fg` | Shortcut keys |
|
|
429
|
+
| `--rr-cp-backdrop`, `--rr-cp-shadow` | Overlay and dialog shadow |
|
|
430
|
+
| `--rr-cp-font`, `--rr-cp-font-size` | Typography |
|
|
431
|
+
| `--rr-cp-width`, `--rr-cp-max-height`, `--rr-cp-top` | Dialog size and position |
|
|
432
|
+
| `--rr-cp-radius`, `--rr-cp-item-radius` | Corners |
|
|
433
|
+
| `--rr-cp-z-index`, `--rr-cp-duration` | Stacking and animation length |
|
|
323
434
|
|
|
324
435
|
Class names (`rr-cp-dialog`, `rr-cp-item`, `rr-cp-item-active`, `rr-cp-match`, `rr-cp-group-label`,
|
|
325
436
|
`rr-cp-empty`, β¦) are stable too, if you need more than variables.
|
|
@@ -345,6 +456,7 @@ npm start # demo on http://localhost:4200
|
|
|
345
456
|
npm test # unit tests (Vitest)
|
|
346
457
|
npm run lint
|
|
347
458
|
npm run build # library (dist/ngx-command-palette) + demo
|
|
459
|
+
npm run build:demo && npm run e2e # browser flows and axe-core on the demo (needs Chromium)
|
|
348
460
|
```
|
|
349
461
|
|
|
350
462
|
See [CONTRIBUTING.md](CONTRIBUTING.md).
|