CommandPalette
Description
A command palette utility
CommandPalette provides a fast and efficient way for users to navigate an application and trigger commands using only their keyboard. Inspired by modern editors and tools, it allows users to search through a list of actions and execute them with a single keystroke.
The search field is a real OmniBox in Search mode rather than a bare input, so a palette can offer the snaps, value filters, history and suggestions the app's own search box offers. Reach it with .SearchBox, or configure it with .ConfigureSearchBox(...). OnSearch hands over the parsed OmniBox.SearchQuery — the string overload is still there — and adding or removing a chip re-runs the search the way typing does.
Clicking beside the palette closes it, the way it does on a Modal: CanLightDismiss (on by default), LightDismiss() and NoLightDismiss() control that. Escape closes the palette wherever focus ended up after a row was activated.
Sample
API reference
public sealed class CommandPalette : Layer<CommandPalette>A keyboard-driven full-screen command launcher (Ctrl/Cmd-K style) that lets users search and invoke application commands.
- Namespace
- Tesserae
- Inheritance
- Layer<CommandPalette> → CommandPalette
Constructors
| Name | Description |
|---|---|
| CommandPalette | Creates a CommandPalette whose global Ctrl/Cmd keyboard listener is bound to the lifetime of host: the listener is attached when host first mounts to the DOM and detached when it is removed. This prevents the palette from leaking listeners (and continuing to respond to its shortcut) after the owning view has been navigated away. |
public CommandPalette(IComponent host, IEnumerable<CommandPaletteAction> actions = null)Creates a CommandPalette whose global Ctrl/Cmd keyboard listener is bound to the lifetime of host: the listener is attached when host first mounts to the DOM and detached when it is removed. This prevents the palette from leaking listeners (and continuing to respond to its shortcut) after the owning view has been navigated away.
Properties
| Name | Description |
|---|---|
| EnableGlobalShortcut | Enables the global shortcut on the component. |
| EnableGlobalActionShortcuts | Enables the global action shortcuts on the component. |
| HideOnAction | Hides the on action. |
| GlobalShortcutKey | Key (combined with Ctrl/Cmd) that toggles the palette globally. Case-insensitive. Defaults to "k". |
| CanLightDismiss | Whether clicking beside the palette closes it, the way it does on a Modal. On by default. |
| SearchBox | The box the palette is typed into. It is a full OmniBox, so a host can give the palette the same snaps, value filters, history and suggestions its own search box has instead of the palette being a lesser search than the page it stands in for. |
| Placeholder | Gets or sets the placeholder text shown when the component is empty. |
| EmptyText | What is said when there is nothing to show. Defaults to "No results", which is right for a palette that filters a list it already has; a palette that goes and searches usually wants to say what to do instead ("Type to search"). |
| CurrentQuery | What is typed in the palette's search box right now, trimmed. |
public bool EnableGlobalShortcut { get; set; }Enables the global shortcut on the component.
public bool EnableGlobalActionShortcuts { get; set; }Enables the global action shortcuts on the component.
public string GlobalShortcutKey { get; set; }Key (combined with Ctrl/Cmd) that toggles the palette globally. Case-insensitive. Defaults to "k".
public bool CanLightDismiss { get ; set ; }Whether clicking beside the palette closes it, the way it does on a Modal. On by default.
public OmniBox SearchBoxThe box the palette is typed into. It is a full OmniBox, so a host can give the palette the same snaps, value filters, history and suggestions its own search box has instead of the palette being a lesser search than the page it stands in for.
public string Placeholder { get ; set ; }Gets or sets the placeholder text shown when the component is empty.
public string EmptyText { get ; set ; }What is said when there is nothing to show. Defaults to "No results", which is right for a palette that filters a list it already has; a palette that goes and searches usually wants to say what to do instead ("Type to search").
Methods
| Name | Description |
|---|---|
| LightDismiss | Enables light-dismiss behaviour (clicking outside the palette closes it). |
| NoLightDismiss | Removes / disables the light dismiss on the component. |
| ConfigureSearchBox | Configures SearchBox - the place to hand the palette the search box configuration the rest of the app shares. |
| SetActions | Sets the actions of the component. |
| AddAction | Adds the given action to the component. |
| SetResults | Puts rows of the host's own at the top of the palette, above its actions - search results drawn as OmniResult{T}s, recent files, anything a list of actions can't say. Each carries what Enter does with it, and they take part in the arrow-key walk like any other row. These are shown as given: the palette does not filter them, because the host that produced them for a query already knows which ones answer it. Use OnSearch to have them refreshed as the query changes, or call this whenever the host has new ones. |
| OnSearch | Asks the host for the rows to show, every time the query changes and once when the palette opens. The call is debounced, and an answer that arrives after a newer query was typed is dropped, so a slow search can never overwrite a faster one behind it. |
| Open | Opens the component. |
| Close | Closes the component. |
| Toggle | Toggles the component's state. |
| Show | Shows the component. |
| Hide | Hides the component. |
public CommandPalette LightDismiss()Enables light-dismiss behaviour (clicking outside the palette closes it).
public CommandPalette NoLightDismiss()Removes / disables the light dismiss on the component.
public CommandPalette ConfigureSearchBox(Action<OmniBox> configure)Configures SearchBox - the place to hand the palette the search box configuration the rest of the app shares.
public CommandPalette SetActions(IEnumerable<CommandPaletteAction> actions)Sets the actions of the component.
public CommandPalette AddAction(CommandPaletteAction action)Adds the given action to the component.
| Overload | |
|---|---|
| SetResults(IEnumerable<CommandPaletteResult>) | Puts rows of the host's own at the top of the palette, above its actions - search results drawn as OmniResult{T}s, recent files, anything a list of actions can't say. Each carries what Enter does with it, and they take part in the arrow-key walk like any other row. These are shown as given: the palette does not filter them, because the host that produced them for a query already knows which ones answer it. Use OnSearch to have them refreshed as the query changes, or call this whenever the host has new ones. |
| SetResults(CommandPaletteResult[]) | Puts one row of the host's own at the top of the palette. See SetResults. |
SetResults(IEnumerable<CommandPaletteResult>)
public CommandPalette SetResults(IEnumerable<CommandPaletteResult> results)Puts rows of the host's own at the top of the palette, above its actions - search results drawn as OmniResult{T}s, recent files, anything a list of actions can't say. Each carries what Enter does with it, and they take part in the arrow-key walk like any other row. These are shown as given: the palette does not filter them, because the host that produced them for a query already knows which ones answer it. Use OnSearch to have them refreshed as the query changes, or call this whenever the host has new ones.
Parameters
- results IEnumerable<CommandPaletteResult>
| Overload | |
|---|---|
| OnSearch(Func<OmniBox.SearchQuery, Task<IEnumerable<CommandPaletteResult>>>, int) | Asks the host for the rows to show, every time the query changes and once when the palette opens. The call is debounced, and an answer that arrives after a newer query was typed is dropped, so a slow search can never overwrite a faster one behind it. |
| OnSearch(Func<string, Task<IEnumerable<CommandPaletteResult>>>, int) | Asks the host for the rows to show, for a palette that only needs the text that was typed. See OnSearch for the overload that also hands over the box's filters. |
OnSearch(Func<OmniBox.SearchQuery, Task<IEnumerable<CommandPaletteResult>>>, int)
public CommandPalette OnSearch(Func<OmniBox.SearchQuery, Task<IEnumerable<CommandPaletteResult>>> search, int debounceMs = 200)Asks the host for the rows to show, every time the query changes and once when the palette opens. The call is debounced, and an answer that arrives after a newer query was typed is dropped, so a slow search can never overwrite a faster one behind it.
Parameters
- search Func<OmniBox.SearchQuery, Task<IEnumerable<CommandPaletteResult>>>
- Given what the box says - parsed, with whatever snaps and value filters the host registered on
SearchBoxalready picked out of it - the rows to show. Null clears the search. - debounceMs int
- How long typing has to stop before the search runs.
OnSearch(Func<string, Task<IEnumerable<CommandPaletteResult>>>, int)
public CommandPalette OnSearch(Func<string, Task<IEnumerable<CommandPaletteResult>>> search, int debounceMs = 200)Asks the host for the rows to show, for a palette that only needs the text that was typed. See OnSearch for the overload that also hands over the box's filters.
Parameters
- search Func<string, Task<IEnumerable<CommandPaletteResult>>>
- debounceMs int
Events
| Name | Description |
|---|---|
| ActionExecuted | Raised when action executed occurs. |
| ResultActivated | Raised when one of the host's own rows is activated - see SetResults. |
public event Action<CommandPaletteAction> ActionExecutedRaised when action executed occurs.
Fields
| Name | Description |
|---|---|
| LAYER_FADE_OUT_MS | How long Layer{T} takes to fade a hidden layer out before removing it. |
public sealed class CommandPaletteResultA row of the host's own in a CommandPalette - a search result drawn as an OmniResult{T}, a recent file, a preview card - rather than one of the palette's actions.
- Namespace
- Tesserae
Constructors
| Name | Description |
|---|---|
| CommandPaletteResult | A row and what Enter does with it. With no activate the row is only clickable, which is what a component that already answers its own click (an OmniResult{T} with an OpenWith) wants. |
public sealed class CommandPaletteAction- Namespace
- Tesserae
Constructors
| Name | Description |
|---|---|
| CommandPaletteAction | Initializes a new instance of this class. |
Properties
| Name | Description |
|---|---|
| Id | Sets the DOM id of the component. |
| Name | Gets or sets the name of the component. |
| Subtitle | Gets or sets the subtitle. |
| Keywords | Gets or sets the keywords. |
| Section | Gets or sets the section. |
| ParentId | Gets or sets the parent id. |
| Icon | Gets or sets the icon shown by the component. |
| Shortcut | Gets or sets the shortcut. |
| IsEnabled | Gets or sets a value indicating whether the component is interactive (enabled). |
| IsVisible | Gets a value indicating whether the component is currently visible. |
| Perform | Gets or sets the perform. |
public string Name { get; set; }Gets or sets the name of the component.
public string ParentId { get; set; }Gets or sets the parent id.
public UIcons? Icon { get; set; }Gets or sets the icon shown by the component.
public string[] Shortcut { get; set; }Gets or sets the shortcut.
public bool IsEnabled { get; set; }Gets or sets a value indicating whether the component is interactive (enabled).
public bool IsVisible { get; set; }Gets a value indicating whether the component is currently visible.