Command Palette
Keyboard-launched command menu (Ctrl+K or Cmd+K)
CommandPalette opens a searchable command menu with a keyboard shortcut, Ctrl+K or ⌘K by default. Commands can be grouped and can show a description and a shortcut hint.
Installation
shellui add command-palette
The CLI also installs command, dialog, the CommandItem model and shellui.js, and adds the models @using to _Imports.razor. With the NuGet package, add @using ShellUI.Components.Models.
Usage
@inject NavigationManager Nav
<CommandPalette Commands="_commands" />
@code {
private List<CommandItem> _commands = new();
protected override void OnInitialized()
{
_commands = new()
{
new() { Title = "Dashboard", Action = () => Go("/") },
new() { Title = "Settings", Shortcut = "G S", Action = () => Go("/settings") },
new() { Title = "New project", Description = "Create an empty project", Action = () => Task.CompletedTask },
};
}
private Task Go(string url)
{
Nav.NavigateTo(url);
return Task.CompletedTask;
}
}
Custom shortcut
@* Ctrl+Shift+P, like VS Code *@
<CommandPalette Commands="_commands" HotkeyChar="p" UseMeta="false" UseShift="true" />
@code {
private List<CommandItem> _commands = new()
{
new() { Title = "Open file", Shortcut = "Ctrl O" },
new() { Title = "Toggle theme", Description = "Switch light and dark" },
};
}
Opening it from code
New in v0.4: IsOpen is bindable, so a button can open the palette as well as the shortcut.
Palette is closed · last action: none
<Button Variant="ButtonVariant.Outline" OnClick="() => open = true">
Search <Kbd Class="ml-2">Ctrl</Kbd> <Kbd>J</Kbd>
</Button>
<CommandPalette @bind-IsOpen="open" Commands="@commands" HotkeyChar="j" />
<p class="mt-3 text-sm text-muted-foreground">Palette is @(open ? "open" : "closed") · last action: @last</p>
@code {
private bool open;
private string last = "none";
private List<CommandItem> commands = new();
protected override void OnInitialized()
{
commands =
[
new() { Title = "Go to dashboard", Group = "Navigation", Action = () => Set("Go to dashboard") },
new() { Title = "Go to settings", Group = "Navigation", Action = () => Set("Go to settings") },
new() { Title = "Sign out", Group = "Account", Action = () => Set("Sign out") },
];
}
private Task Set(string value)
{
last = value;
return Task.CompletedTask;
}
}Selecting a command closes the palette, runs its Action, then raises CommandSelected. The list filters as you type and moves with the arrow keys, Home and End, the same as Command.
UseCtrl and UseMeta are alternatives: with both on (the default), either Ctrl or ⌘ opens the palette.
CommandItem
| Property | Type | Default | Description |
|---|---|---|---|
Title |
string |
"" |
Command text |
Description |
string? |
null |
Secondary text |
Icon |
string? |
null |
Icon markup |
Shortcut |
string? |
null |
Shortcut hint shown on the right |
Group |
string? |
null |
Heading the command is listed under |
Action |
Func<Task>? |
null |
Runs when the command is chosen |
API Reference
| Property | Type | Default | Description |
|---|---|---|---|
Commands |
List<CommandItem> |
empty list | The commands |
IsOpen |
bool |
false |
Whether the palette is open; use @bind-IsOpen |
IsOpenChanged |
EventCallback<bool> |
- | Raised when it opens or closes |
CommandSelected |
EventCallback<CommandItem> |
— | Raised when a command is chosen |
Placeholder |
string |
"Type a command or search..." |
Search box placeholder |
HotkeyChar |
string |
"k" |
Key that opens the palette |
UseCtrl |
bool |
true |
Ctrl + key opens it |
UseMeta |
bool |
true |
⌘ + key opens it |
UseShift |
bool |
false |
Also require Shift |
UseAlt |
bool |
false |
Also require Alt |