# Command Palette
> Keyboard-launched command menu (Ctrl+K or Cmd+K)
Source: https://shellui.dev/docs/v0.4/components/advanced/command-palette
`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
```bash
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
```razor
@inject NavigationManager Nav
@code {
private List _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
```razor
@* Ctrl+Shift+P, like VS Code *@
@code {
private List _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.
```razor
Palette is @(open ? "open" : "closed") · last action: @last
@code {
private bool open;
private string last = "none";
private List 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](https://shellui.dev/docs/v0.4/components/advanced/command.md).
`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?` | `null` | Runs when the command is chosen |
## API Reference
| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `Commands` | `List` | empty list | The commands |
| `IsOpen` | `bool` | `false` | Whether the palette is open; use `@bind-IsOpen` |
| `IsOpenChanged` | `EventCallback` | - | Raised when it opens or closes |
| `CommandSelected` | `EventCallback` | — | 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 |