Skip to content
# CommandPalette **Also known as:** CommandMenu, Cmdk, Spotlight ## Description BitCommandPalette is an accessible, keyboard-first command palette: the Ctrl+K (Cmd+K on a Mac) box that fuzzy-searches every page, action and setting of an app and runs the one picked, with nested pages, prompts, multi-select, async sources, recents and global shortcuts. Matching, ranking and every decision run in C#; the browser only shows the native dialog and listens for keys. ## Parameters | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | ChildContent | `RenderFragment?` | null | The declarative BitCommandPaletteGroup and BitCommandPaletteOption children of the palette. | | Classes | `BitCommandPaletteClassStyles?` | null | Custom CSS classes for the different parts of the command palette. | | Commands | `IEnumerable<BitCommandPaletteCommand>?` | null | Commands supplied directly to this palette, in addition to the ones registered through IBitCommandPaletteService and declared in its markup. | | DetailTemplate | `RenderFragment<BitCommandPaletteCommand>?` | null | Renders a detail pane beside the list for the active command, Raycast style. | | DisablePointerSelection | `bool` | false | Ignores the pointer hovering a row for choosing the active one: only the keys and a click move it. | | EmptyTemplate | `RenderFragment?` | null | What a page with no commands on it at all shows, in place of EmptyText. | | EmptyText | `string?` | null | The text a page with no commands on it at all shows. Default: the Empty of the palette's strings, which falls back to BitStrings.NoItemsFound. | | EnableHistory | `bool?` | null | Overrides BitCommandPaletteOptions.EnableHistory: recents, pins and frecency. | | FooterTemplate | `RenderFragment<BitCommandPaletteStateContext>?` | null | Replaces the footer with its keyboard hints and toasts, with a snapshot of the palette's state. | | GroupHeaderTemplate | `RenderFragment<string>?` | null | Replaces the content of a group heading; receives the heading text. | | GroupWhenSearching | `bool?` | null | Overrides BitCommandPaletteOptions.GroupWhenSearching: keeps the group headings while a query is typed. | | HeaderTemplate | `RenderFragment<BitCommandPaletteStateContext>?` | null | Content rendered above the search box, with a snapshot of the palette's state. | | HighlightMatches | `bool?` | null | Overrides BitCommandPaletteOptions.HighlightMatches: marks the matched characters. | | IconTemplate | `RenderFragment<BitCommandPaletteCommand>?` | null | Renders the icon of every row; receives the command. Render nothing for no icon. | | IncludeRegisteredCommands | `bool` | true | Whether the commands, sources and modes registered through IBitCommandPaletteService are part of this palette. Set it to false for a palette that lists only its own, such as an inline search of one page's settings. | | Inline | `bool` | false | Renders the palette in the page instead of in a modal dialog. It is always open, Escape goes back a page or clears the query, and it leaves the toggle shortcut, the global shortcuts and the service's Open/Close/Execute to the modal palette. Set before the first render. | | ItemTemplate | `RenderFragment<BitCommandPaletteItemContext>?` | null | Replaces the content of every row. The row element itself - its role, ARIA state and click handling - stays the palette's. | | LoadingTemplate | `RenderFragment?` | null | Replaces the loading state shown while a lazily loaded page or an async source has nothing to show yet. | | Loop | `bool?` | null | Overrides BitCommandPaletteOptions.Loop: the arrow keys wrap around the list. | | NoResultsTemplate | `RenderFragment<string>?` | null | What a search that matched nothing shows, in place of NoResultsText; receives the query. | | NoResultsText | `string?` | null | The text a search that matched nothing shows. Default: the NoResults of the palette's strings, which falls back to BitStrings.NoResultsFound. | | OnClosed | `EventCallback` | | Raised after the palette closed. | | OnCommandExecuted | `EventCallback<BitCommandPaletteCommandExecutedEventArgs>` | | Raised after a command of this palette ran, with its result and how long the handler took. | | OnOpened | `EventCallback` | | Raised after the palette opened. | | OnQueryChanged | `EventCallback<string>` | | Raised after every change of the query, with the raw text (a mode prefix included). | | Open | `bool` | false | Whether the modal palette is open. Two-way bindable. | | Placeholder | `string?` | null | The placeholder of the search box on the root page. | | SearchNestedCommands | `bool?` | null | Overrides BitCommandPaletteOptions.SearchNestedCommands: finds nested commands from the root page. | | ShowFooter | `bool?` | null | Overrides BitCommandPaletteOptions.ShowFooter. | | ShowShortcuts | `bool?` | null | Overrides BitCommandPaletteOptions.ShowShortcuts: shows each command's shortcut on its row. | | Strings | `BitCommandPaletteStrings?` | null | The words of the palette, the ones only screen readers hear included. Default: BitCommandPaletteOptions.Strings. | | Styles | `BitCommandPaletteClassStyles?` | null | Custom CSS styles for the different parts of the command palette. | | ToggleShortcut | `string?` | null | Overrides BitCommandPaletteOptions.ToggleShortcut (Mod+K), e.g. Ctrl+Shift+P. | | TypoTolerance | `bool?` | null | Overrides BitCommandPaletteOptions.TypoTolerance: retries a query that found nothing allowing a typo per term. | | VimBindings | `bool?` | null | Overrides BitCommandPaletteOptions.VimBindings: Ctrl+J / Ctrl+K move the selection. | | Virtualize | `bool?` | null | Overrides BitCommandPaletteOptions.Virtualize: renders the list through Virtualize. | | AriaLabel | `string?` | null | Gets or sets the accessible label for the component, used by assistive technologies. | | Class | `string?` | null | Gets or sets the CSS class name(s) to apply to the rendered element. | | Dir | `BitDir?` | null | Gets or sets the text directionality for the component's content. | | Disabled | `bool` | false | Gets or sets a value indicating whether the component is disabled and cannot respond to user interaction. | | ForceAnimation | `bool` | false | Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested. | | HtmlAttributes | `Dictionary<string, object>` | new Dictionary<string, object>() | Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters. An attribute written on the component wins over an entry of the same name. | | Id | `string?` | null | Gets or sets the unique identifier for the component's root element. | | Style | `string?` | null | Gets or sets the CSS style string to apply to the rendered element. | | TabIndex | `string?` | null | Gets or sets the tab order index for the component when navigating with the keyboard. | | Visibility | `BitVisibility` | BitVisibility.Visible | Gets or sets the visibility state (visible, hidden, or collapsed) of the component. | ## Public Members | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | IsOpen | `bool` | | Whether the dialog is open. | | Query | `string` | | The current raw query, a mode prefix included. | | ActiveCommand | `BitCommandPaletteCommand?` | | The command of the active row, if any. | | OpenAsync | `(string? query, string? pageCommandId) => Task` | | Opens the palette, optionally with a query (a mode prefix is honoured) or directly on a command's page. | | CloseAsync | `() => Task` | | Closes the palette. | | ToggleAsync | `() => Task` | | Opens the palette when it is closed and closes it when it is open. | | SetQueryAsync | `(string query) => Task` | | Sets the query as if typed, and renders the results. | | UniqueId | `Guid` | Guid.NewGuid() | Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed. | | RootElement | `ElementReference` | | Gets the reference to the root HTML element associated with this component. | ## Enums ### BitVisibility Enum | Name | Value | Description | | :--- | :--- | :---------- | | Visible | 0 | The content of the component is visible. | | Hidden | 1 | The content of the component is hidden, but the space it takes on the page remains (visibility:hidden). | | Collapsed | 2 | The component is hidden (display:none). | ### BitDir Enum | Name | Value | Description | | :--- | :--- | :---------- | | Ltr | 0 | Ltr (left to right) is to be used for languages that are written from the left to the right (like English). | | Rtl | 1 | Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic). | | Auto | 2 | Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element. | ## Sub Classes ### BitCommandPaletteClassStyles Properties | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | Root | `string?` | null | Custom CSS classes/styles for the root element of the BitCommandPalette: the modal dialog, or the section of an inline palette. | | Header | `string?` | null | Custom CSS classes/styles for the header of the BitCommandPalette, which holds the breadcrumbs and the search box. | | Input | `string?` | null | Custom CSS classes/styles for the search box of the BitCommandPalette. | | List | `string?` | null | Custom CSS classes/styles for the list of the BitCommandPalette. | | GroupHeading | `string?` | null | Custom CSS classes/styles for each group heading of the BitCommandPalette. | | Item | `string?` | null | Custom CSS classes/styles for each row of the BitCommandPalette. | | ActiveItem | `string?` | null | Custom CSS classes/styles for the active row of the BitCommandPalette. | | Empty | `string?` | null | Custom CSS classes/styles for what a page of the BitCommandPalette with no commands at all shows. | | NoResults | `string?` | null | Custom CSS classes/styles for what a search of the BitCommandPalette that matched nothing shows. | | Footer | `string?` | null | Custom CSS classes/styles for the footer of the BitCommandPalette, with its keyboard hints and toasts. | | Detail | `string?` | null | Custom CSS classes/styles for the detail pane of the BitCommandPalette. | ### BitCommandPaletteStrings Properties Every word the palette says, the ones only screen readers hear included. Set its properties, or subclass it and override them (to read them from an IStringLocalizer), to localize. The templates are composite format strings: {0} is replaced. | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | Placeholder | `string` | "Type a command or search…" | The search box placeholder on the root page. | | PromptPlaceholder | `string` | "Type a value and press Enter…" | The search box placeholder of a prompt that set none of its own. | | NoResults | `string` | BitStrings.Current.NoResultsFound | The state of a query that found nothing; {0} is the query. Default: NoResultsFound of Current, read when it is shown, so a BitStrings set later still reaches it. | | NoResultsHint | `string` | "Try a different search, or check for typos." | The hint under NoResults. | | Empty | `string` | BitStrings.Current.NoItemsFound | The state of a page with nothing on it. Default: NoItemsFound of Current, read when it is shown, so a BitStrings set later still reaches it. | | Loading | `string` | "Searching…" | The loading state, shown and announced while a lazily loaded page or an async source has not answered. | | Recent | `string` | "Recent" | The heading of the recently used commands. | | Pinned | `string` | "Pinned" | The heading of the pinned commands, and what a pinned row says to screen readers. | | Suggestions | `string` | "Suggestions" | The heading of the ungrouped commands when pinned or recent ones are listed above them. | | Modes | `string` | "Modes" | The heading of the mode hints on the empty query. | | Actions | `string` | "Actions" | The search box placeholder of the action panel. | | Results | `string` | "Results" | The accessible name of the result list on the root page. | | Other | `string` | "Other" | Not used by the palette itself; kept for templates. | | Navigate | `string` | "to navigate" | Footer hint after the ↑ ↓ keys. | | Select | `string` | "to select" | Footer hint after the Enter key. | | Close | `string` | "to close" | Footer hint after the Esc key on the root page. | | ClearQuery | `string` | "to clear" | Footer hint after the Esc key in an inline palette with a query typed. | | Back | `string` | "to go back" | Footer hint after the Esc key on a nested page and in a prompt. | | OpenActions | `string` | "for actions" | Footer hint after the Tab key. | | Toggle | `string` | "to toggle" | Footer hint after the Enter key on a multi-select page. | | Apply | `string` | "to apply" | Footer hint after Ctrl+Enter on a multi-select page, and after Enter in a prompt. | | Pin | `string` | "Pin to top" | The action panel entry that pins a command. | | Unpin | `string` | "Unpin" | The action panel entry that unpins a command. | | RemoveFromRecents | `string` | "Remove from recents" | The action panel entry that forgets a command's uses. | | ClearRecents | `string` | "Clear recent commands" | The action panel entry that forgets every recent command (pins stay). | | ConfirmPrefix | `string` | "Confirm: " | Put before the title of a command waiting for its confirming Enter. | | ConfirmHint | `string` | "Press Enter again to confirm, Esc to cancel" | Announced with the confirmation message of a command waiting for its second Enter. | | SelectedCount | `string` | "{0} selected" | How many rows are checked on a multi-select page; {0} is the count. | | ResultsAnnouncement | `string` | "{0} results available" | Announced after a search with several results; {0} is the count. See ResultsAnnouncementFor. | | OneResultAnnouncement | `string` | "1 result available" | Announced after a search with exactly one result. | | NoResultsAnnouncement | `string` | "No results" | Announced after a search with no result. | | PageAnnouncement | `string` | "{0}. {1}" | Announced when a page opens or the palette goes back to one; {0} is its title, {1} what it lists. | | Disabled | `string` | "Unavailable" | Shown when a disabled command without a reason is chosen. | | ModeHintTitle | `string` | "{0} — {1}" | Not used by the palette itself; kept for templates. | | DialogLabel | `string` | "Command palette" | The accessible name of the dialog. | | InputLabel | `string` | "Search commands" | The accessible name of the search box. | | CloseButton | `string` | "Close" | The accessible name of the close button. | | BackButton | `string` | "Back" | The accessible name of the back button. | | Breadcrumb | `string` | "Breadcrumb" | The accessible name of the breadcrumb navigation. | | OpenPalette | `string` | "Open command palette" | What BitCommandPaletteTrigger's accessible name says it does, after its visible label and TriggerNameSeparator (WCAG 2.5.3: the name starts with what is shown), and its whole name when the trigger shows no label. A string that already holds the label - a whole name such as "Search commands" for the label "Search" - is the name on its own, as is a label that already holds this string. | | TriggerNameSeparator | `string` | ", " | What joins BitCommandPaletteTrigger's visible label and OpenPalette in its accessible name; a language with a comma of its own uses it ("، " in Persian and Arabic). | | Search | `string` | "Search" | The label of BitCommandPaletteTrigger when it sets none of its own. | | Home | `string` | "Home" | The title of the root page. | | MoreResults | `string` | "Show {0} more…" | The row that loads a source's next page when it said how many results there are; {0} is how many are left. | | LoadMore | `string` | "Show more…" | The row that loads a source's next page when it did not say how many results there are. | | SequencePending | `string` | "waiting for the next key…" | Shown, and announced, after the first chords of a shortcut sequence (G of G S), until it completes or times out. | | CommandFailed | `string` | "Couldn’t run “{0}”." | Shown when a command failed; {0} is its title. See Message. | | CommandRefused | `string` | "You can’t run “{0}”." | Shown when a guard refused a command without a message of its own (see IBitCommandPaletteCommandGuard); {0} is its title. | | CommandUnavailable | `string` | "“{0}” is no longer available." | Shown when a listed command was chosen after it stopped being available - its VisibleWhen, or an ancestor's, turned false since the list was built; {0} is its title. | | LoadFailed | `string` | "Couldn’t load this list." | Shown when the children of a page failed to load. | | Retry | `string` | "Try again" | The button and hint that load a failed list again. | | ApproximateResults | `string` | "No exact matches. Showing close ones." | Shown above results found only by allowing typos (see TypoTolerance). | | ApproximateResultsAnnouncement | `string` | "{0} {1}" | The announcement of such results: {0} is ApproximateResults, {1} the result count. | | Running | `string` | "Working…" | Announced, and shown by a spinner, while a command's handler has been running for a moment. | | SourceFailed | `string` | "Couldn’t search {0}" | The row an async source that failed or timed out leaves; {0} is its name. Choosing it retries. | | On | `string` | "On" | What screen readers hear for a toggle that is on. | | Off | `string` | "Off" | What screen readers hear for a toggle that is off. | | ShortcutThen | `string` | "then" | The word between the chords of a sequence: "G then S". | ### BitCommandPaletteOptions Properties The app-wide configuration, set through AddBitBlazorUIExtrasServices(commandPalette: options => ...). The palette's own parameters, and a BitCommandPaletteParams cascaded through a BitParams, override what they share with it. | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | ToggleShortcut | `BitCommandPaletteKeyboardShortcut?` | BitCommandPaletteKeyboardShortcut.Parse("Mod+K") | Shortcut that toggles the palette. Default Mod+K (⌘K on macOS, Ctrl+K elsewhere). | | AdditionalToggleShortcuts | `List<BitCommandPaletteKeyboardShortcut>` | [] | Additional toggle shortcuts (e.g. Ctrl+Shift+P, /). | | SuppressShortcutsInEditableElements | `bool` | true | Don't fire global command shortcuts while an input, textarea or contenteditable has focus. | | Loop | `bool` | true | Wrap around when arrowing past the first/last item. | | VimBindings | `bool` | false | Enable Ctrl+J/Ctrl+K navigation inside the list (Ctrl+N/Ctrl+P are always on). | | EnableHistory | `bool` | true | Track recents and frecency. | | HistoryStorageKey | `string` | "bit-command-palette.history" | localStorage key for BitCommandPaletteBrowserStorageHistoryStore. | | GroupWhenSearching | `bool` | true | Keep group headings while searching (cmdk style). When false, results are a flat ranked list. | | GroupOrder | `List<string>` | [] | Explicit group order. Groups not listed come after, alphabetically. | | PriorityWeight | `double` | 0.25 | Weight of Priority when a query is present (score points per priority unit). | | FrecencyWeight | `double` | 6 | Weight of frecency when a query is present. | | SearchNestedCommands | `bool` | true | Find nested commands from the root page too (default true), as kbar does: typing dark lists "Theme › Dark" without opening Theme first, and theme dark matches the ancestors' titles as well. Only commands registered with the service or declared in markup are found - a lazily loaded page's children do not exist until it opens - and only where every ancestor is visible and enabled and none is a multi-select page. Their recents and pins are listed on the root page as well. | | TypoTolerance | `bool` | true | When a query matches nothing, look again allowing a typo per term - one missing, extra, wrong or swapped letter in a term of 4 to 7 characters, two from 8 on - so settigns still finds "Settings" (default true). Only a query that found nothing is retried, so this never adds noise to one that works; the list says the results are close matches. | | ShowSequenceHint | `bool` | true | After the first chords of a shortcut sequence (G of G S), show and announce that the next key is awaited, as VS Code does in its status bar (default true). SequencePending reports it to code either way. | | ShowModeHints | `bool` | true | Show a "Modes" hint group on the empty query listing registered prefixes. | | CloseOnBackdropClick | `bool` | true | Close when clicking the backdrop. | | CloseOnNavigation | `bool` | true | Close when the app navigates (e.g. a handler called NavigationManager.NavigateTo). | | ResetOnClose | `bool` | true | Reset query and page stack when closed. When false, reopening restores the previous state. | | HighlightMatches | `bool` | true | Highlight matched characters in titles and subtitles. | | ShowFooter | `bool` | true | Show the footer with keyboard hints. | | ShowShortcuts | `bool` | true | Render shortcut hints on rows. | | Virtualize | `bool` | false | Use <Virtualize> for the list (recommended above ~500 rows). Requires fixed RowHeight. | | Matcher | `IBitCommandPaletteMatcher` | BitCommandPaletteFuzzyMatcher.Default | Text matcher used for scoring. Default BitCommandPaletteFuzzyMatcher. | | Weights | `BitCommandPaletteMatchWeights` | new() | Per-field weights for scoring. | | Strings | `BitCommandPaletteStrings` | new() | UI strings. Subclass or set properties to localize. | | Commands | `List<BitCommandPaletteCommand>` | [] | Commands registered at startup. | | Modes | `List<BitCommandPaletteCommandMode>` | [] | Modes registered at startup. | | Sources | `List<IBitCommandPaletteSource>` | [] | Async sources queried in the default mode. | | RecordCommandIdsInTraces | `bool` | true | Whether the bit.command_palette.command activities the palette traces carry the command's id (default true) - see BitCommandPaletteDiagnostics. Turn it off when ids embed personal data, as ids made from an API's records can (person:[email protected]). Metrics never carry ids, and queries, prompt values and titles are never recorded. Off, it also keeps a command's id and title out of the palette's log messages about running it - a failure, a refusal, a guard that threw, an unsafe link - which usually leave through the same pipeline; they read (hidden) instead. The configuration warnings about shortcuts and duplicate ids, which name the commands an app registers, still carry them. | | AllowedUrlSchemes | `HashSet<string>` | new(StringComparer.OrdinalIgnoreCase) { "http", "https", "mailto", "tel" } | The URL schemes a command's Href may use; relative links are always allowed. A link with any other scheme - javascript:, data:, vbscript: - is refused and logged instead of followed, because commands often carry data from an API (see IBitCommandPaletteSource). Default http, https, mailto and tel; compared case-insensitively. | | SequenceTimeoutMs | `int` | 1000 | Milliseconds a multi-chord sequence (G I) waits for the next chord. Must be positive. | | DebounceMs | `int` | 150 | Debounce for async sources, in milliseconds. Must not be negative. | | SourceTimeoutMs | `int` | 10000 | How long an async source may take, in milliseconds, before it counts as failed and its results are replaced by a retry row. 0 waits for ever. Must not be negative. | | MaxRecents | `int` | 5 | How many recent commands to show on the empty query. Must not be negative. | | MaxHistoryEntries | `int` | 200 | Upper bound of history entries kept; pinned commands are never pruned. Must not be negative. | | MaxResults | `int` | 150 | Max rows rendered (groups excluded) for a query. Protects against huge registries. Must be positive. | | RowHeight | `float` | 44 | Row height in pixels used by virtualization. Must be positive. | ### BitCommandPaletteTrigger Properties A button that opens the modal palette - optionally with a query or on a command's page - and shows its toggle shortcut in the platform's own symbols. | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | ChildContent | `RenderFragment?` | null | Replaces the button's content (icon, label and shortcut hint). Without a Label the button is named by what the content shows, followed by ", Open command palette"; content that shows only an icon sets Label="", so the name does not start with the separator. | | Classes | `BitCommandPaletteTriggerClassStyles?` | null | Custom CSS classes for the different parts of the trigger. | | Label | `string?` | null | The label. Default: "Search"; an empty one shows the icon alone. The button's accessible name starts with it. With ChildContent it is not shown, only named: set it to the text the content shows, or to an empty string for content that shows only an icon, and the button is named as the default one is. | | MacKeys | `bool?` | null | Forces the macOS key symbols on or off. Default: the platform the palette detected. | | PageCommandId | `string?` | null | Opens the palette directly inside the page of the command with this id. | | Query | `string?` | null | Opens the palette with this query (e.g. a mode prefix). | | ShowShortcut | `bool` | true | Shows the toggle shortcut of the palette. Default: true. | | Styles | `BitCommandPaletteTriggerClassStyles?` | null | Custom CSS styles for the different parts of the trigger. | ### BitCommandPaletteTriggerClassStyles Properties | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | Root | `string?` | null | Custom CSS classes/styles for the root element (the button) of the BitCommandPaletteTrigger. | | Icon | `string?` | null | Custom CSS classes/styles for the search icon of the BitCommandPaletteTrigger. | | Label | `string?` | null | Custom CSS classes/styles for the label of the BitCommandPaletteTrigger. | | Shortcut | `string?` | null | Custom CSS classes/styles for the shortcut hint of the BitCommandPaletteTrigger. | ### BitCommandPaletteOption Properties A command declared in markup inside a BitCommandPalette, directly or in a BitCommandPaletteGroup. Nested options turn it into a page. | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | AlwaysShow | `bool` | false | Listed whatever the query, e.g. for a "couldn't find it?" row. | | Badge | `string?` | null | A small pill at the end of the row, e.g. "New" or "Beta". | | Checked | `bool?` | null | The state of a toggle; renders a switch. | | ChildContent | `RenderFragment?` | null | Nested BitCommandPaletteOptions turn this item into a page. | | ChildrenPlaceholder | `string?` | null | Placeholder for the nested page when this item has child items. | | ConfirmMessage | `string?` | null | Asks for a second Enter before running, showing this message. | | Description | `string?` | null | Longer text for the detail pane. Searched with a low weight. | | Destructive | `bool` | false | Styled as dangerous. With ConfirmMessage, Enter has to be pressed twice. | | Disabled | `bool` | false | Listed but not runnable. | | DisabledReason | `string?` | null | Why the item is disabled; shown instead of the subtitle and when it is selected. | | GlobalShortcut | `bool` | true | Whether Shortcut also fires while the palette is closed. Default true. | | Href | `string?` | null | Navigation target (used when OnExecute is not set). | | Icon | `RenderFragment?` | null | An icon rendered before the title. | | IconCss | `string?` | null | A CSS class for an icon element, e.g. bi bi-house. | | Id | `string?` | null | Stable id. Defaults to a slug of Title. | | KeepOpen | `bool` | false | Keep the palette open after OnExecute. | | Keywords | `string?` | null | Comma or space separated keywords. | | Kind | `BitCommandPaletteCommandKind?` | null | The semantic kind; picks the default icon. Inferred when not set. | | MultiSelect | `bool` | false | Allow checking several children before applying with Ctrl+Enter (calls OnExecute with the selection). | | NewTab | `bool` | false | Open Href in a new tab. | | OnExecute | `EventCallback<BitCommandPaletteCommandContext>` | | Invoked when the item is selected. The palette closes afterwards unless KeepOpen is set. | | Priority | `int?` | null | A ranking nudge; see BitCommandPaletteCommandPriority. Defaults to the group's priority. | | Shortcut | `string?` | null | Shortcut, e.g. Mod+Shift+D or G I. | | Subtitle | `string?` | null | A secondary label next to the title. Searched and highlighted. | | Tags | `string[]?` | null | Free-form labels that modes can scope to; combined with the group's. | | Title | `string` | string.Empty | The label. Searched with the highest weight and highlighted. | | Value | `string?` | null | The current value of a setting, e.g. "Dark". | ### BitCommandPaletteGroup Properties Groups the BitCommandPaletteOptions declared inside it under one heading. | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | ChildContent | `RenderFragment?` | null | The BitCommandPaletteOptions of the group. | | Heading | `string` | string.Empty | Group heading applied to all BitCommandPaletteOption children. | | Priority | `int?` | null | Priority applied to children that don't set their own. | | Tags | `string[]?` | null | Tags applied to all children (for mode scoping). | ## CSS Variables Public CSS custom properties read off the component's root. Set one on `:root` to restyle every instance, on any ancestor to restyle the ones inside it, or on the `Style` of one instance to restyle it and every instance nested inside it, which inherits the value too - a parameter changes one alone, as does the real CSS property on its `Style` / `Styles` when that property does not inherit (`opacity`); an inherited one (`color`, `font-family`) reaches the nested instances that do not set it themselves. | Name | Default Value | Description | | :--- | :------------ | :---------- | | `--bit-CommandPalette-width` | 640px | Width of the dialog (920px with a detail pane). | | `--bit-CommandPalette-max-height` | min(70vh, 560px) | Tallest the list grows, with its header and footer. | | `--bit-CommandPalette-top` | 12vh | Distance of the dialog from the top of the viewport. | | `--bit-CommandPalette-radius` | var(--bit-shp-radius-dialog) | Corner radius of the dialog. | | `--bit-CommandPalette-background` | var(--bit-clr-bg-pri) | Fill of the dialog and of an inline palette. | | `--bit-CommandPalette-color` | var(--bit-clr-fg-pri) | Text of the palette. | | `--bit-CommandPalette-border-color` | var(--bit-clr-brd-sec) | Lines between the header, the list and the footer, and the edge of an inline palette. | | `--bit-CommandPalette-shadow` | var(--bit-shd-dialog) | Elevation of the dialog. | | `--bit-CommandPalette-backdrop` | var(--bit-clr-bg-overlay) | Fill of the page behind the dialog. | | `--bit-CommandPalette-row-height` | var(--bit-siz-item-lg) | Least height of a row. Virtualize pins the rows to BitCommandPaletteOptions.RowHeight instead. | | `--bit-CommandPalette-item-radius` | var(--bit-shp-radius-md) | Corner radius of a row. | | `--bit-CommandPalette-active-background` | var(--bit-clr-bg-pri-hover) | Fill of the active row. | | `--bit-CommandPalette-accent` | var(--bit-clr-pri) | The mode chip, badges, checked rows, switches that are on and the busy spinner. | | `--bit-CommandPalette-mark-background` | var(--bit-clr-pri-tint) | Fill of the matched characters. | | `--bit-CommandPalette-detail-width` | 40% | Width of the detail pane. | | `--bit-CommandPalette-focus-color` | the global focus ring | Focus ring of the palette's buttons. | | `--bit-CommandPaletteTrigger-height` | var(--bit-siz-ctrl-md) | Height of a BitCommandPaletteTrigger. | | `--bit-CommandPaletteTrigger-background` | var(--bit-clr-bg-sec) | Fill of a BitCommandPaletteTrigger. | | `--bit-CommandPaletteTrigger-color` | var(--bit-clr-fg-sec) | Text and icon of a BitCommandPaletteTrigger. | | `--bit-CommandPaletteTrigger-border-color` | var(--bit-clr-brd-pri) | Edge of a BitCommandPaletteTrigger. | | `--bit-CommandPaletteTrigger-radius` | var(--bit-shp-radius-control) | Corner radius of a BitCommandPaletteTrigger. | | `--bit-CommandPaletteTrigger-focus-color` | the global focus ring | Focus ring of a BitCommandPaletteTrigger. | ## Examples \n**Basic**: ```razor Last run: @aLastRun @code { private string? aLastRun; } ``` \n**Getting started**: ```razor @* File: App.razor (or wwwroot/index.html) - the Extras stylesheet and script, next to the core ones *@ @* File: MainLayout.razor - one palette, rendered interactively on every page *@ @inherits LayoutComponentBase
@Body
@* File: any page or component - registers its own commands, which leave with it *@ @inject IBitCommandPaletteService Palette @inject ProjectsApi Api @implements IDisposable @code { private IDisposable? _commands; private bool _sidebarOpen; protected override void OnInitialized() { _commands = Palette.Register( BitCommandPaletteCmd.Navigate("Home", "/").Group("Navigation").Shortcut("G H"), BitCommandPaletteCmd.Navigate("Settings", "/settings").Group("Navigation").Shortcut("G S").Keywords("preferences", "options"), BitCommandPaletteCmd.Action("Toggle sidebar", () => _sidebarOpen = !_sidebarOpen).Group("View").Shortcut("Mod+B"), BitCommandPaletteCmd.New("Theme").Group("Preferences").Children( BitCommandPaletteCmd.Action("Light", () => SetTheme("light")), BitCommandPaletteCmd.Action("Dark", () => SetTheme("dark"))), BitCommandPaletteCmd.New("Delete project") .Group("Danger zone") .Destructive("This cannot be undone") .Handler(async ctx => { await Api.DeleteProjectAsync(ctx.CancellationToken); return BitCommandPaletteCommandResult.Toast("Project deleted"); })); } private void SetTheme(string theme) { /* ... */ } public void Dispose() => _commands?.Dispose(); } ``` ```csharp // File: Program.cs - in a Blazor Web App, in both the server and the client project (one shared method) builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { // Ctrl+K / Cmd+K is the default; this adds VS Code's Ctrl+Shift+P as well. options.AdditionalToggleShortcuts.Add("Mod+Shift+P"); // Groups appear in this order on the empty query; any others follow alphabetically. options.GroupOrder.AddRange(["Navigation", "Actions", "Preferences", "Help"]); }); // File: any component with IBitCommandPaletteService injected as Palette - opening it from code await Palette.OpenAsync(); await Palette.OpenAsync(">"); // commands mode await Palette.OpenAsync(pageCommandId: "preferences.theme"); // straight to the Theme page await Palette.ExecuteAsync("navigation.settings"); // run a command, nothing opened ``` \n**Hosting models**: ```razor @* File: PaletteHost.razor - per-page interactivity: a static layout renders it as *@ @code { // Registered from inside the interactive component, so they land in the same scope as the palette. private readonly List _appCommands = [ /* ... */ ]; } @* File: YourApp.Client.csproj - lazy loading: mark the assembly in the project that runs in the browser *@ @* File: App.razor (Routes.razor in a Blazor Web App) - the router waits for OnNavigateAsync before it renders the page *@ @using Microsoft.AspNetCore.Components.WebAssembly.Services @inject LazyAssemblyLoader LazyAssemblyLoader ... @code { private async Task OnNavigateAsync(NavigationContext context) { // The pages with the palette: fetch Bit.BlazorUI.Extras before they render. A second call loads nothing again. // Routes match regardless of case, so /Admin/users is one of them too. if (context.Path.StartsWith("admin", StringComparison.OrdinalIgnoreCase)) await LazyAssemblyLoader.LoadAssembliesAsync(["Bit.BlazorUI.Extras.wasm"]); } } @* File: AdminLayout.razor - only the lazily loaded pages use it, so it may use the palette's types *@ @inherits LayoutComponentBase @inject IServiceProvider Services @implements IDisposable @Body @code { private IDisposable? _commands; protected override void OnInitialized() => _commands = Services.GetBitCommandPaletteService().Register( BitCommandPaletteCmd.Navigate("Users", "/admin/users").Group("Navigation"), BitCommandPaletteCmd.Navigate("Billing", "/admin/billing").Group("Navigation")); public void Dispose() => _commands?.Dispose(); } ``` ```csharp // File: Program.cs of the browser project, when it lazy loads Bit.BlazorUI.Extras builder.Services.AddBitBlazorUIServices(); // not AddBitBlazorUIExtrasServices // The one "services are not registered" warning is expected here; this silences it without naming the palette's types. builder.Logging.AddFilter("Bit.BlazorUI.BitCommandPaletteServiceCollectionExtensions", LogLevel.None); // File: Program.cs of the server - a content security policy; the palette needs nothing beyond script-src 'self' // ('wasm-unsafe-eval' is for Blazor WebAssembly itself). app.Use(async (context, next) => { context.Response.Headers.ContentSecurityPolicy = "script-src 'self' 'wasm-unsafe-eval'"; await next(); }); ``` \n**Modal**: ```razor @inject IBitCommandPaletteService paletteService @implements IDisposable Open by binding Open Open with "hello" typed Run "Say hello" by its id Open: @aModalOpen, opened @aOpenedCount and closed @aClosedCount times, last run: @aModalLastRun ``` ```csharp private bool aModalOpen; private bool aAltToggle; private int aOpenedCount; private int aClosedCount; private string? aModalLastRun; private BitCommandPaletteCommand[] aModalCommands = default!; private readonly List registrations = []; // null leaves the toggle shortcut to the options: Mod+K. private string? aToggleShortcut => aAltToggle ? "Alt+K" : null; protected override void OnInitialized() { aModalCommands = [ BitCommandPaletteCmd.Action("Say hello", () => aModalLastRun = "Say hello") .Id("a-say-hello").Group("Actions").Keywords("greet", "hi"), BitCommandPaletteCmd.Page("Choose a greeting", BitCommandPaletteCmd.Action("Hola", () => aModalLastRun = "Hola"), BitCommandPaletteCmd.Action("Bonjour", () => aModalLastRun = "Bonjour"), BitCommandPaletteCmd.Action("Salam", () => aModalLastRun = "Salam")) .Id("a-greetings").Group("Actions"), BitCommandPaletteCmd.Navigate("Go to getting started", "/getting-started").Group("Navigation"), ]; // Registered with the service rather than passed to the palette: the modal palette lists it all the same. // Register returns an IDisposable - dispose it to remove the command again. registrations.Add(paletteService.Register( BitCommandPaletteCmd.Action("Wave goodbye", () => aModalLastRun = "Wave goodbye") .Id("a-wave-goodbye").Group("Registered through the service"))); } private Task AOpenWithQuery() => paletteService.OpenAsync("hello"); private Task ARunSayHello() => paletteService.ExecuteAsync("a-say-hello"); public void Dispose() { foreach (var registration in registrations) registration.Dispose(); } ``` \n**Trigger**: ```razor @* Each of these opens the page's modal palette (see the Modal section). *@ @* An icon only: Label="" names it "Open command palette". *@ @* A word, given as Label too: named "Greetings, Open command palette". *@ Greetings @* No Label: named by its content, then a visually hidden ", Open command palette". *@ Say hello ``` \n**Commands from code**: ```razor @(bHello is null ? "Register \"Say hello\"" : "Dispose the registration") Search "hello" in the modal palette Last run: @bLastRun · "Say hello" is @(paletteService.Find(BHelloId) is null ? "not registered" : "registered") ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private const string BHelloId = "b-say-hello"; private string bLastRun = "Nothing yet"; private IDisposable? bHello; private BitCommandPaletteCommand[] bCommands = []; protected override void OnInitialized() { // Icon takes a RenderFragment as well as a CSS class: any markup, here an inline SVG drawn in the row's color. RenderFragment star = builder => builder.AddMarkupContent(0, ""); bCommands = [ BitCommandPaletteCmd.New("Export…") .Group("Builder") .Subtitle("A blank command, filled in step by step") .Description("New starts a command from its title; every other member has a builder method of the same name.") .Keywords("download", "save as") .Icon("bit-icon bit-icon--Download") .Handler(() => BRun("Export…")), BitCommandPaletteCmd.Action("Save", () => BRun("Save")) .Group("Builder") .Icon("bit-icon bit-icon--Save") .Shortcut("Mod+S", global: false), BitCommandPaletteCmd.Action("Reload", BReloadAsync) .Group("Builder") .Icon("bit-icon bit-icon--Refresh"), BitCommandPaletteCmd.Action("Rename…", ctx => { BRun($"Rename… (typed \"{ctx.Query}\")"); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }) .Group("Builder") .Icon("bit-icon bit-icon--Rename"), BitCommandPaletteCmd.Page("Theme", BitCommandPaletteCmd.Action("Light", () => BRun("Theme › Light")).Id("b-builder-light"), BitCommandPaletteCmd.Action("Dark", () => BRun("Theme › Dark")).Id("b-builder-dark")) .Group("Builder") .ChildrenPlaceholder("Pick a theme…"), BitCommandPaletteCmd.Navigate("Getting started", "/getting-started") .Group("Links") .Keywords("install", "setup"), BitCommandPaletteCmd.New("bit platform on GitHub") .Href("https://github.com/bitfoundation/bitplatform", newTab: true) .Group("Links") .Keywords("source", "repository"), BitCommandPaletteCmd.New("What's new") .Group("Links") .Badge("New") .Icon(star) .Handler(() => BRun("What's new")) .Kind(BitCommandPaletteCommandKind.Help), ]; } private void BRun(string title) { bLastRun = title; _ = InvokeAsync(StateHasChanged); } private async Task BReloadAsync() { await Task.Delay(600); BRun("Reload (after 600 ms)"); } private void BToggleHello() { if (bHello is not null) { bHello.Dispose(); // gone again, from every palette bHello = null; return; } bHello = paletteService.Register( BitCommandPaletteCmd.Action("Say hello", () => BRun("Say hello (registered from code)")) .Id(BHelloId) .Group("Registered from code") .Keywords("wave", "hi")); } public void Dispose() => bHello?.Dispose(); ``` \n**Identity**: ```razor @foreach (var (built, id) in bIdentityExamples) { }
Built asId
@built@id
@(bGreetOverride is null ? "Register an override with the same id" : "Dispose the override") Search "greet" in the modal palette Find("b-greet"): @(paletteService.Find(BGreetId)?.Title) · Last run: @bGreetStatus ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private const string BGreetId = "b-greet"; private string bGreetStatus = "Nothing yet"; private IDisposable? bGreet; private IDisposable? bGreetOverride; // Worked out by the library itself, so the table cannot drift from what it does. private static readonly (string Built, string Id)[] bIdentityExamples = [ ("New(\"Settings\").Group(\"Navigation\")", BitCommandPaletteCmd.New("Settings").Group("Navigation").Build().Id), ("New(\"Rename project…\").Group(\"Actions\")", BitCommandPaletteCmd.New("Rename project…").Group("Actions").Build().Id), ("New(\"What's new\").Group(\"Help\")", BitCommandPaletteCmd.New("What's new").Group("Help").Build().Id), ("New(\"Dark\").Parent(\"preferences.theme\")", BitCommandPaletteCmd.New("Dark").Parent("preferences.theme").Build().Id), ("New(\"C#\")", BitCommandPaletteCmd.New("C#").Build().Id), ("New(\"C++\")", BitCommandPaletteCmd.New("C++").Build().Id), ("New(\"🚀\")", BitCommandPaletteCmd.New("🚀").Build().Id), ("New(\"Word wrap\").Id(\"toggle:wordwrap\")", BitCommandPaletteCmd.New("Word wrap").Id("toggle:wordwrap").Build().Id), ("New(\"Word wrap\").Group(\"Editor\").Build().With(b => b.Title(\"Soft wrap\"))", BitCommandPaletteCmd.New("Word wrap").Group("Editor").Build().With(b => b.Title("Soft wrap")).Id), ]; protected override void OnInitialized() { bGreet = paletteService.Register( BitCommandPaletteCmd.Action("Greet the team", () => BGreet("the first registration")) .Id(BGreetId) .Group("Identity") .Keywords("welcome")); } private void BToggleGreetOverride() { if (bGreetOverride is not null) { bGreetOverride.Dispose(); // the first registration's command is back bGreetOverride = null; return; } // The same id, registered later: it wins. bGreetOverride = paletteService.Register( BitCommandPaletteCmd.Action("Greet this page's visitors", () => BGreet("the override")) .Id(BGreetId) .Group("Identity") .Keywords("welcome")); } private void BGreet(string which) { bGreetStatus = which; _ = InvokeAsync(StateHasChanged); } public void Dispose() { bGreetOverride?.Dispose(); bGreet?.Dispose(); } ``` \n**Groups and ordering**: ```razor ``` ```csharp // Program.cs: the app-wide order of the groups, and how a search is listed. builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { // First on the empty query, in this order; the other groups follow. options.GroupOrder.AddRange(["Edit", "View", "Help"]); // While searching: one flat list ranked by match, instead of a heading per group. options.GroupWhenSearching = false; }); // The page: private bool bGroupWhenSearching = true; private static readonly BitCommandPaletteCommand[] bOrderingCommands = [ BitCommandPaletteCmd.New("Search everywhere"), BitCommandPaletteCmd.New("Zoom in").Group("View"), BitCommandPaletteCmd.New("Zoom out").Group("View"), BitCommandPaletteCmd.New("Reset zoom").Group("View").Priority(BitCommandPaletteCommandPriority.Low), BitCommandPaletteCmd.New("Cut").Group("Edit"), BitCommandPaletteCmd.New("Copy").Group("Edit"), BitCommandPaletteCmd.New("Paste").Group("Edit"), BitCommandPaletteCmd.New("Undo").Group("Edit").Priority(BitCommandPaletteCommandPriority.High), BitCommandPaletteCmd.New("Send feedback").Group("Help").Priority(BitCommandPaletteCommandPriority.Lowest).AlwaysShow(), BitCommandPaletteCmd.New("Keyboard shortcuts").Group("Help"), ]; ``` \n**Toggles and settings**: ```razor @foreach (var theme in bEditorThemes) { @theme } @(bFocusMode ? "Leave focus mode" : "Enter focus mode") from the page Search "focus" in the modal palette Registered as b-focus-mode: @(paletteService.Find(BFocusModeId)?.Title) ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private const string BFocusModeId = "b-focus-mode"; private static readonly string[] bEditorThemes = ["Light", "Dark", "High contrast"]; private bool bWordWrap = true; private bool bMinimap; private string bEditorTheme = "Light"; private bool bFocusMode; private IDisposable? bFocusModeRegistration; private BitCommandPaletteCommand[] bSettings = []; protected override void OnInitialized() { BBuildSettings(); bFocusModeRegistration = paletteService.Register(BFocusModeCommand()); } // A new collection every time, so the palette re-reads it and every switch and value is current. private void BBuildSettings() { bSettings = [ BitCommandPaletteCmd.New("Word wrap") .Id("b-setting-wordwrap") .Group("Editor") .Keywords("soft wrap", "lines") .Checked(bWordWrap) .Handler(_ => { bWordWrap = !bWordWrap; BSettingsChanged(); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }), BitCommandPaletteCmd.New("Show minimap") .Id("b-setting-minimap") .Group("Editor") .Keywords("outline", "overview") .Checked(bMinimap) .Handler(_ => { bMinimap = !bMinimap; BSettingsChanged(); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }), BitCommandPaletteCmd.New("Editor theme") .Id("b-setting-theme") .Group("Editor") .Keywords("appearance", "color scheme") .Value(bEditorTheme) .ChildrenPlaceholder("Pick a theme…") .Children(bEditorThemes.Select(BEditorThemeOption)), ]; } private BitCommandPaletteCommand BEditorThemeOption(string theme) => BitCommandPaletteCmd.New(theme) .Id($"b-editor-theme-{theme.ToLowerInvariant().Replace(' ', '-')}") .Checked(bEditorTheme == theme) .Handler(_ => { bEditorTheme = theme; BSettingsChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Back); }) .Build(); private void BSetEditorTheme(string theme) { bEditorTheme = theme; BBuildSettings(); } private void BSettingsChanged() { BBuildSettings(); _ = InvokeAsync(StateHasChanged); } // The title changes with the state; the id does not. private BitCommandPaletteCommand BFocusModeCommand() => BitCommandPaletteCmd.New(bFocusMode ? "Leave focus mode" : "Enter focus mode") .Id(BFocusModeId) .Group("Editor") .Keywords("zen", "distraction free") .Checked(bFocusMode) .Handler(_ => { BToggleFocusMode(); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }); private void BToggleFocusMode() { bFocusMode = !bFocusMode; paletteService.Update(BFocusModeCommand()); // replaces the command with the id b-focus-mode _ = InvokeAsync(StateHasChanged); } public void Dispose() => bFocusModeRegistration?.Dispose(); ``` \n**Disabled, hidden and destructive commands**: ```razor @foreach (var file in bFiles) { @file } Search "merge" in the modal palette Last run: @bAvailabilityStatus ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private static readonly string[] bFiles = ["README.md", "Program.cs", "App.razor", "site.css"]; private readonly HashSet bSelectedFiles = []; private string bAvailabilityStatus = "Nothing yet"; private IDisposable? bMergeRegistration; private BitCommandPaletteCommand[] bAvailabilityCommands = []; protected override void OnInitialized() { // Registered with the service, so its global shortcut works while the modal palette is closed. bMergeRegistration = paletteService.Register(BMergeCommand()); bAvailabilityCommands = [ BMergeCommand(), BitCommandPaletteCmd.New("Export as PDF") .Group("Files") .Badge("Pro") .Icon("bit-icon bit-icon--Download") .Disabled(true, "Requires the Pro plan") .Handler(() => BAvailabilityRun("Exported as PDF")), BitCommandPaletteCmd.New("Publish the draft") .Group("Files") .Confirm("Publish the draft to everyone?") .Handler(() => BAvailabilityRun("Published the draft (not really)")), BitCommandPaletteCmd.New("Delete the draft") .Group("Danger zone") .Icon("bit-icon bit-icon--Delete") .Priority(BitCommandPaletteCommandPriority.Low) .Destructive("This permanently deletes the draft") .Handler(() => BAvailabilityRun("Deleted the draft (not really)")), ]; } private BitCommandPaletteCommand BMergeCommand() => BitCommandPaletteCmd.New("Merge selected files") .Id("b-merge-files") .Group("Files") .Shortcut("Alt+M") .Keywords("combine", "join") .Icon("bit-icon bit-icon--Merge") .VisibleWhen(() => bSelectedFiles.Count >= 2) // asked every time the list is built .Handler(() => { var count = bSelectedFiles.Count; bSelectedFiles.Clear(); paletteService.Refresh(); // the selection the predicate reads just changed BAvailabilityRun($"Merged {count} files (not really)"); }); private void BToggleFile(string file) { if (bSelectedFiles.Remove(file) is false) { bSelectedFiles.Add(file); } // The modal palette is closed: tell it the predicate's answer may have changed, so Alt+M is claimed or released now. paletteService.Refresh(); } private void BAvailabilityRun(string what) { bAvailabilityStatus = what; _ = InvokeAsync(StateHasChanged); } public void Dispose() => bMergeRegistration?.Dispose(); ``` \n**Page-scoped commands**: ```razor Counter: @bCount Search "counter" in the modal palette +1 from the page ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private const string BIncrementId = "b-counter-increment"; private const string BDecrementId = "b-counter-decrement"; private const string BResetId = "b-counter-reset"; private const string BZeroReason = "The counter is already at zero"; private int bCount; private BitCommandPaletteCommand[] bPageCommands = []; protected override void OnInitialized() { bPageCommands = [ BitCommandPaletteCmd.New("Increment counter") .Id(BIncrementId) .Group("Counter") .Shortcut("Alt+Up") .Keywords("plus", "add") .Badge("0") .Icon("bit-icon bit-icon--Add") .Description("Registered by this page; gone when you leave it.") .Handler(_ => BStep(+1)), BitCommandPaletteCmd.New("Decrement counter") .Id(BDecrementId) .Group("Counter") .Shortcut("Alt+Down") .Keywords("minus", "subtract") .Badge("0") .Icon("bit-icon bit-icon--Remove") .Description("Registered by this page; gone when you leave it.") .Handler(_ => BStep(-1)), BitCommandPaletteCmd.New("Reset counter") .Id(BResetId) .Group("Counter") .Keywords("zero", "clear") .Destructive("Reset the counter to zero?") .Disabled(true, BZeroReason) .Handler(() => BSetCount(0)), ]; } private Task BStep(int delta) { BSetCount(bCount + delta); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); } private void BSetCount(int count) { bCount = count; // Commands are immutable, so a change is a copy - With keeps the id, the handler and the shortcut - and Update // swaps it in wherever it was registered. var badge = count.ToString(System.Globalization.CultureInfo.InvariantCulture); foreach (var id in new[] { BIncrementId, BDecrementId }) { if (paletteService.Find(id) is { } command) { paletteService.Update(command.With(b => b.Badge(badge))); } } if (paletteService.Find(BResetId) is { } reset) { paletteService.Update(reset.With(b => b.Disabled(count == 0, count == 0 ? BZeroReason : null))); } _ = InvokeAsync(StateHasChanged); } ``` \n**Declarative markup**: ```razor Last run: @bMarkupLastRun · Word wrap: @bMarkupWrap · Font size: @bMarkupFontSize px ``` ```csharp private string bMarkupLastRun = "Nothing yet"; private bool bMarkupWrap; private int bMarkupFontSize = 14; // Ctrl+Enter on the multi-select page: the checked options are in SelectedCommands. private void BApplyLabels(BitCommandPaletteCommandContext ctx) { bMarkupLastRun = ctx.SelectedCommands.Count == 0 ? "No labels" : $"Labels: {string.Join(", ", ctx.SelectedCommands.Select(c => c.Title))}"; } ``` \n**Inline palettes**: ```razor @if (bInlinePanelOpen) {
Side panel Close
} else { Reopen the side panel } Last run: @bInlineStatus ``` ```csharp private bool bInlinePanelOpen = true; private string bInlineStatus = "Nothing yet"; private BitCommandPaletteCommand[] bInlineCommands = []; protected override void OnInitialized() { bInlineCommands = [ BitCommandPaletteCmd.Action("Show line numbers", () => BInlineRun("Show line numbers")).Group("View"), BitCommandPaletteCmd.Action("Fold all regions", () => BInlineRun("Fold all regions")).Group("View"), BitCommandPaletteCmd.Action("Format document", () => BInlineRun("Format document")).Group("Edit").Shortcut("Alt+Shift+F"), BitCommandPaletteCmd.Page("Go to symbol", BitCommandPaletteCmd.Action("OnInitialized", () => BInlineRun("Went to OnInitialized")).Id("b-symbol-oninitialized"), BitCommandPaletteCmd.Action("BuildRenderTree", () => BInlineRun("Went to BuildRenderTree")).Id("b-symbol-buildrendertree"), BitCommandPaletteCmd.Action("Dispose", () => BInlineRun("Went to Dispose")).Id("b-symbol-dispose")) .Group("Navigate") .ChildrenPlaceholder("Filter symbols…"), ]; } private void BInlineRun(string what) { bInlineStatus = what; // the inline palette resets itself to its root page afterwards _ = InvokeAsync(StateHasChanged); } // The palette claims Escape only when it has something to undo; the rest reaches the panel. private void BInlinePanelKeyDown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs e) { if (e.Key == "Escape") { bInlinePanelOpen = false; } } ``` \n**The service**: ```razor ExecuteAsync("b-focus-mode") ExecuteAsync("b-sync") ExecuteAsync("b-transfer") @(paletteService.Find(BScratchId) is null ? "Register \"Scratch note\" again" : "Unregister(\"b-scratch-note\")") Open the modal palette The palette is @(paletteService.IsOpen ? "open" : "closed") · IsMac: @paletteService.IsMac, so the toggle reads · the registry holds @paletteService.Commands.Count commands · Find("b-focus-mode"): @(paletteService.Find(BFocusModeId)?.Title ?? "null") · Find("b-scratch-note"): @(paletteService.Find(BScratchId)?.Title ?? "null")
@if (bEvents.Count == 0) {
Open the palette or run a command.
} @foreach (var entry in bEvents) {
@entry
}
``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; // b-focus-mode is the command the "Toggles and settings" example registers. private const string BFocusModeId = "b-focus-mode"; private const string BFailId = "b-sync"; private const string BRefusedId = "b-transfer"; private const string BScratchId = "b-scratch-note"; private static readonly BitCommandPaletteKeyboardShortcut bToggleShortcut = "Mod+K"; private readonly List bEvents = []; private readonly List registrations = []; protected override void OnInitialized() { registrations.Add(paletteService.Register( BitCommandPaletteCmd.New("Sync with the server") .Id(BFailId) .Group("Service") .Description("Fails on purpose, to show CommandFailed.") .Handler(async _ => { await Task.Delay(300); throw new InvalidOperationException("The sync server is unreachable (on purpose)."); }), BitCommandPaletteCmd.Action("Transfer ownership", () => { }) .Id(BRefusedId) .Group("Service") .Description("Refused by its guard, to show CommandRefused.") .Guard(_ => false, "Only the owner can transfer the project"), BScratchCommand())); // The service outlives the page: named handlers, removed in Dispose. paletteService.CommandsChanged += BOnCommandsChanged; paletteService.Opened += BOnOpened; paletteService.Closed += BOnClosed; paletteService.CommandExecuted += BOnCommandExecuted; paletteService.CommandFailed += BOnCommandFailed; paletteService.CommandRefused += BOnCommandRefused; paletteService.PlatformDetected += BOnPlatformDetected; paletteService.SequencePending += BOnSequencePending; } private BitCommandPaletteCommand BScratchCommand() => BitCommandPaletteCmd.Action("Scratch note", () => BLog("Scratch note ran")) .Id(BScratchId) .Group("Service"); private void BToggleScratch() { if (paletteService.Find(BScratchId) is null) { registrations.Add(paletteService.Register(BScratchCommand())); return; } BLog($"Unregister(\"{BScratchId}\") returned {paletteService.Unregister(BScratchId)}"); } private void BOnCommandsChanged() => BLog($"CommandsChanged {paletteService.Commands.Count} commands registered", collapse: true); private void BOnOpened() => BLog("Opened"); private void BOnClosed() => BLog("Closed"); private void BOnCommandExecuted(object? sender, BitCommandPaletteCommandExecutedEventArgs e) => BLog($"CommandExecuted {e.Command.Id} → {e.Result.GetType().Name.Replace("Result", "")} in {e.Duration.TotalMilliseconds:0} ms"); private void BOnCommandFailed(object? sender, BitCommandPaletteCommandErrorEventArgs e) { if (e.Command.Id == BFailId) { e.Message = "The server did not answer. Nothing was changed."; // what the palette shows e.Handled = true; // reported here: not logged, and not rethrown to ExecuteAsync } BLog($"CommandFailed {e.Command.Id}: {e.Exception.Message}"); } private void BOnCommandRefused(object? sender, BitCommandPaletteCommandRefusedEventArgs e) => BLog($"CommandRefused {e.Command.Id} ({e.Trigger}): {e.Message}"); private void BOnPlatformDetected() => BLog($"PlatformDetected IsMac = {paletteService.IsMac}"); private void BOnSequencePending(BitCommandPaletteKeyboardShortcut? pending) => BLog(pending is null ? "SequencePending (over)" : $"SequencePending {pending.ToDisplayString(paletteService.IsMac)}…"); private void BLog(string entry, bool collapse = false) => _ = InvokeAsync(() => { // The registry changes in bursts, so a run of CommandsChanged is one line. if (collapse && bEvents.Count > 0 && bEvents[0].Contains("CommandsChanged", StringComparison.Ordinal)) { bEvents.RemoveAt(0); } bEvents.Insert(0, $"{DateTime.Now:HH:mm:ss} {entry}"); if (bEvents.Count > 10) { bEvents.RemoveAt(bEvents.Count - 1); } StateHasChanged(); }); public void Dispose() { paletteService.CommandsChanged -= BOnCommandsChanged; paletteService.Opened -= BOnOpened; paletteService.Closed -= BOnClosed; paletteService.CommandExecuted -= BOnCommandExecuted; paletteService.CommandFailed -= BOnCommandFailed; paletteService.CommandRefused -= BOnCommandRefused; paletteService.PlatformDetected -= BOnPlatformDetected; paletteService.SequencePending -= BOnSequencePending; foreach (var registration in registrations) { registration.Dispose(); } } ``` \n**Handlers and results**: ```razor @cResultStatus ``` ```csharp private string cResultStatus = "Nothing run yet."; private bool cWordWrap; private string cDensity = "Comfortable"; private int cTodoCount; private readonly List<(string Id, string Text)> cTodos = []; private BitCommandPaletteCommand[] cResultCommands = []; protected override void OnInitialized() { CBuildResultCommands(); } private void CBuildResultCommands() { cResultCommands = [ // The to-dos added with "Add a to-do…": Enter completes one, and the palette stays open on the list. .. cTodos.Select(todo => BitCommandPaletteCmd.New(todo.Text) .Id(todo.Id) .Subtitle("To-do · Enter completes it (KeepOpen)") .ExcludeFromHistory() .Handler(_ => { cTodos.Remove(todo); cResultStatus = $"Done: {todo.Text}"; CBuildResultCommands(); StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }) .Build()), // No result: Close, the default. BitCommandPaletteCmd.Action("Say hello", () => { cResultStatus = "Hello!"; StateHasChanged(); }) .Subtitle("Close - an inline palette goes back to its root"), // Stay, and rebuild the list: the commands are built again so the switch shows the new state. BitCommandPaletteCmd.New("Word wrap") .Subtitle("KeepOpen - the switch flips, the palette stays") .Checked(cWordWrap) .Handler(_ => { cWordWrap = !cWordWrap; cResultStatus = $"Word wrap: {(cWordWrap ? "on" : "off")}"; CBuildResultCommands(); StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }), // Each option answers Back, to land on the list again. BitCommandPaletteCmd.Page("Density", CDensityOption("Compact"), CDensityOption("Comfortable"), CDensityOption("Spacious")) .Subtitle("Back - each option returns to this list") .Value(cDensity), // A prompt, then Reset: back to the root, the query cleared, where the new to-do is listed. BitCommandPaletteCmd.New("Add a to-do…") .Subtitle("Prompt, then Reset") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Prompt("New to-do", submit => { var text = submit.PromptValue!.Trim(); if (text.Length == 0) { return Task.FromResult(BitCommandPaletteCommandResult.Error("A to-do needs some text.")); } cTodos.Add(($"c-todo-{++cTodoCount}", text)); cResultStatus = $"To-do added: {text}"; CBuildResultCommands(); StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Reset); }, placeholder: "What needs doing?"))), // A message in the footer; keepOpen keeps the palette open under it. BitCommandPaletteCmd.New("Check for updates") .Subtitle("Toast with keepOpen") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("You are on the latest version.", keepOpen: true))), BitCommandPaletteCmd.New("Save all") .Subtitle("Toast - then the palette closes") .Handler(_ => { cResultStatus = "All changes saved."; StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Toast("All changes saved.")); }), // An expected failure: announced, and the palette stays. BitCommandPaletteCmd.New("Join the beta") .Subtitle("Error") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Error("The beta is full. Try again next week."))), // A page the handler decides when it runs; these rows are not registered anywhere. BitCommandPaletteCmd.New("Export…") .Subtitle("Page") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Page("Export format", [ BitCommandPaletteCmd.Action("CSV", () => CExported("CSV")).Subtitle("Comma separated values").Build(), BitCommandPaletteCmd.Action("JSON", () => CExported("JSON")).Subtitle("Pretty printed").Build(), BitCommandPaletteCmd.Action("PDF", () => CExported("PDF")).Subtitle("Print friendly").Badge("Pro").Disabled(true, "Requires the Pro plan").Build(), ], placeholder: "Pick a format…"))), ]; } private BitCommandPaletteCommand CDensityOption(string density) => BitCommandPaletteCmd.New(density) .Id($"c-density-{density.ToLowerInvariant()}") .Checked(cDensity == density) .Handler(_ => { cDensity = density; cResultStatus = $"Density: {density}"; CBuildResultCommands(); StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Back); }); private void CExported(string format) { cResultStatus = $"Exported as {format}"; StateHasChanged(); } ``` \n**The command context**: ```razor @if (cContextLines.Count == 0) { Run "Inspect the context". } else {
    @foreach (var (member, value) in cContextLines) {
  • @member: @value
  • }
} ``` ```csharp private readonly List<(string Member, string Value)> cContextLines = []; private BitCommandPaletteCommand[] cContextCommands = []; protected override void OnInitialized() { cContextCommands = [ BitCommandPaletteCmd.New("Inspect the context") .Keywords("ctx", "handler") .Handler(ctx => { cContextLines.Clear(); cContextLines.Add(("Command", $"{ctx.Command.Title} ({ctx.Command.Id})")); cContextLines.Add(("Query", ctx.Query.Length == 0 ? "(empty)" : $"\"{ctx.Query}\"")); cContextLines.Add(("ModePrefix", ctx.ModePrefix ?? "null - the default mode")); cContextLines.Add(("PromptValue", ctx.PromptValue ?? "null - not a prompt step")); cContextLines.Add(("SelectedCommands", $"{ctx.SelectedCommands.Count} - not a multi-select page")); cContextLines.Add(("GetRequiredService()", ctx.GetRequiredService() == ctx.Palette ? "the same service as Palette" : "another service")); cContextLines.Add(("Navigation.Uri", ctx.Navigation.Uri)); cContextLines.Add(("Palette.Commands", $"{ctx.Palette.Commands.Count} registered commands")); cContextLines.Add(("CancellationToken.CanBeCanceled", ctx.CancellationToken.CanBeCanceled.ToString())); StateHasChanged(); // Stay, with the query kept, so it can be run again with another one. return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }), // Listed whatever was typed, last: the query it was reached with is the feedback. BitCommandPaletteCmd.New("Feedback: couldn't find a command?") .AlwaysShow() .Priority(BitCommandPaletteCommandPriority.Lowest) .Handler(ctx => Task.FromResult(BitCommandPaletteCommandResult.Toast( ctx.Query.Length > 0 ? $"Thanks! We logged “{ctx.Query}”." : "Thanks for the feedback!", keepOpen: true))), ]; } ``` \n**Prompts**: ```razor Project: @cProjectName · @cPromptStatus ``` ```csharp // using System.Net.Mail; private string cProjectName = "Apollo"; private string cPromptStatus = "Nothing submitted yet."; private static readonly string[] cMembers = ["[email protected]", "[email protected]"]; private BitCommandPaletteCommand[] cPromptCommands = []; protected override void OnInitialized() { cPromptCommands = [ // The simplest prompt: a title and what to do with the text. BitCommandPaletteCmd.New("Add a note…") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Prompt("New note", submit => { cPromptStatus = $"Note: {submit.PromptValue}"; StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Toast("Note added")); }))), // Pre-filled with the current value, with a placeholder for when it is cleared. BitCommandPaletteCmd.New("Rename project…") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Prompt( "New project name", submit => { var name = submit.PromptValue!.Trim(); if (name.Length == 0) { return Task.FromResult(BitCommandPaletteCommandResult.Error("A project needs a name.")); } cProjectName = name; cPromptStatus = $"Renamed to {name}"; StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Toast($"Renamed to “{name}”")); }, placeholder: "Project name…", initialValue: cProjectName))), // Validated as it is typed; checked by the "server" on submit. BitCommandPaletteCmd.New("Invite by e-mail…") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Prompt( "E-mail address", async submit => { var email = submit.PromptValue!.Trim(); await Task.Delay(400, submit.CancellationToken); // asking the server if (cMembers.Contains(email, StringComparer.OrdinalIgnoreCase)) { // Expected, so a result and not an exception: shown, and the prompt stays for a correction. return BitCommandPaletteCommandResult.Error($"{email} is already a member."); } cPromptStatus = $"Invited {email}"; StateHasChanged(); return BitCommandPaletteCommandResult.Toast($"Invitation sent to {email}"); }, // Null for a value Enter may submit, the message to show otherwise. validate: value => MailAddress.TryCreate(value.Trim(), out var parsedAddress) ? null : "Enter an e-mail address, like [email protected].", placeholder: "[email protected]"))), ]; } ``` \n**Multi-select**: ```razor @cMultiStatus ``` ```csharp private string cMultiStatus = "Nothing applied yet."; private BitCommandPaletteCommand[] cMultiCommands = []; private static readonly string[] cWebhookEvents = ["push", "pull_request", "issues", "release", "deployment"]; private static readonly (string Name, string Role)[] cPeople = [ ("Ada Lovelace", "Engineering"), ("Grace Hopper", "Engineering"), ("Alan Turing", "Research"), ("Katherine Johnson", "Data"), ("Margaret Hamilton", "Engineering"), ("Linus Torvalds", "Platform"), ("Barbara Liskov", "Architecture"), ("Dennis Ritchie", "Platform"), ("Hedy Lamarr", "Research"), ("Tim Berners-Lee", "Web"), ]; protected override void OnInitialized() { cMultiCommands = [ BitCommandPaletteCmd.Page("Set labels…", BitCommandPaletteCmd.New("bug").Id("c-label-bug"), BitCommandPaletteCmd.New("enhancement").Id("c-label-enhancement"), BitCommandPaletteCmd.New("documentation").Id("c-label-documentation"), BitCommandPaletteCmd.New("performance").Id("c-label-performance")) .MultiSelect() .ChildrenPlaceholder("Check labels, Ctrl+Enter to apply…") // Runs on Ctrl+Enter, with the checked rows. .Handler(ctx => { var labels = ctx.SelectedCommands.Select(c => c.Title).ToList(); cMultiStatus = labels.Count == 0 ? "Labels cleared." : $"Labels: {string.Join(", ", labels)}"; StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Toast(labels.Count == 1 ? "1 label applied" : $"{labels.Count} labels applied")); }), // Searched as the user types; what is checked stays checked across searches. BitCommandPaletteCmd.New("Invite people…") .MultiSelect() .ChildrenPlaceholder("Search people, Enter checks, Ctrl+Enter invites…") .Source("People", async (query, cancellationToken) => { await Task.Delay(250, cancellationToken); // the directory API return cPeople .Where(p => p.Name.Contains(query, StringComparison.OrdinalIgnoreCase) || p.Role.Contains(query, StringComparison.OrdinalIgnoreCase)) .Select(p => BitCommandPaletteCmd.New(p.Name).Id($"c-invite-{p.Name.Replace(' ', '-').ToLowerInvariant()}").Subtitle(p.Role).Build()) .ToList(); }) .Handler(ctx => { cMultiStatus = $"Invited: {string.Join(", ", ctx.SelectedCommands.Select(c => c.Title))}"; StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Toast(ctx.SelectedCommands.Count == 1 ? "1 person invited" : $"{ctx.SelectedCommands.Count} people invited")); }), // A prompt, then a multi-select page its submit returns. BitCommandPaletteCmd.New("Create webhook…") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Prompt( "Payload URL", address => Task.FromResult(BitCommandPaletteCommandResult.Page( "Events to send", [.. cWebhookEvents.Select(e => BitCommandPaletteCmd.New(e).Id($"c-event-{e}").Build())], placeholder: "Check events, Ctrl+Enter to create…", multiSelect: true, onSubmit: picked => { var events = picked.SelectedCommands.Select(c => c.Title).ToList(); if (events.Count == 0) { return Task.FromResult(BitCommandPaletteCommandResult.Error("Pick at least one event.")); } cMultiStatus = $"Webhook to {address.PromptValue!.Trim()} for {string.Join(", ", events)}"; StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Toast(events.Count == 1 ? "Webhook created for 1 event" : $"Webhook created for {events.Count} events")); })), validate: value => Uri.TryCreate(value.Trim(), UriKind.Absolute, out var uri) && uri.Scheme == Uri.UriSchemeHttps && uri.Host.Length > 0 ? null : "Enter an https:// address.", placeholder: "https://example.com/hooks", initialValue: "https://"))), ]; } ``` \n**Long-running commands**: ```razor @implements IDisposable Find it in the palette @cReportStatus ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private readonly List registrations = []; private string cReportStatus = "Not run yet."; protected override void OnInitialized() { registrations.Add(paletteService.Register( BitCommandPaletteCmd.New("Generate report…") .Id("c-report") .Group("Long-running") .Keywords("slow", "busy") .Handler(async ctx => { cReportStatus = "Generating…"; StateHasChanged(); try { // Esc closes the palette and cancels this token. await Task.Delay(3000, ctx.CancellationToken); } catch (OperationCanceledException) { cReportStatus = "Cancelled: the palette was closed while the report was being generated."; StateHasChanged(); throw; // stops the handler; a cancellation is not reported as a failure } cReportStatus = "Report generated."; StateHasChanged(); return BitCommandPaletteCommandResult.Toast("Report ready", keepOpen: true); }))); } public void Dispose() { foreach (var registration in registrations) { registration.Dispose(); } } ``` \n**Errors**: ```razor @implements IDisposable Run "Sync with server" from code @cExecuteStatus
@if (cFailures.Count == 0) {
The CommandFailed subscriber has seen nothing yet.
} @foreach (var failure in cFailures) {
@failure
}
``` ```csharp // using System.ComponentModel.DataAnnotations; [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private readonly List registrations = []; private bool cHandleFailures = true; private string cExecuteStatus = ""; private readonly List cFailures = []; private BitCommandPaletteCommand[] cErrorCommands = []; protected override void OnInitialized() { cErrorCommands = [ BitCommandPaletteCmd.New("Sync with server") .Id("c-sync") .Group("Errors") .Handler(async ctx => { await Task.Delay(300, ctx.CancellationToken); // Never shown: it names a server and a user. throw new InvalidOperationException("Connection to db-prod-7.internal:5432 refused for user 'svc_sync'."); }), BitCommandPaletteCmd.New("Publish release") .Id("c-publish") .Group("Errors") .Handler(async ctx => { await Task.Delay(300, ctx.CancellationToken); throw new ValidationException("The changelog is empty: write one before publishing."); }), ]; // Registered too, so ExecuteAsync can run them from code. registrations.Add(paletteService.Register(cErrorCommands)); paletteService.CommandFailed += COnCommandFailed; } private void COnCommandFailed(object? sender, BitCommandPaletteCommandErrorEventArgs e) { if (e.Command.Id is not ("c-sync" or "c-publish" or "c-deployments")) { return; // any other command's failure is left to the palette, which logs it } if (e.Exception is ValidationException) { e.Message = e.Exception.Message; // written for users } e.Handled = cHandleFailures; // reported by this page: not logged, not rethrown var shown = e.Command.Id == "c-deployments" ? "the page's error state, with Try again" : $"“{e.Message}”"; cFailures.Insert(0, $"{DateTime.Now:HH:mm:ss} {e.Command.Title}: {e.Exception.GetType().Name}, shown as {shown}"); if (cFailures.Count > 6) { cFailures.RemoveAt(cFailures.Count - 1); } _ = InvokeAsync(StateHasChanged); } private async Task CRunFromCodeAsync(string commandId) { try { await paletteService.ExecuteAsync(commandId); cExecuteStatus = $"ExecuteAsync(\"{commandId}\") returned normally: the subscriber handled the failure."; } catch (Exception ex) { // Unhandled by the subscriber, the failure reaches the code that ran the command. cExecuteStatus = $"ExecuteAsync(\"{commandId}\") threw {ex.GetType().Name}."; } } public void Dispose() { paletteService.CommandFailed -= COnCommandFailed; foreach (var registration in registrations) { registration.Dispose(); } } ``` \n**Guards**: ```razor @implements IDisposable Run "Export as PDF" from code Open the Admin page @cGuardStatus
@if (cRefusals.Count == 0) {
Nothing refused yet.
} @foreach (var refusal in cRefusals) {
@refusal
}
``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private readonly List registrations = []; private bool cIsPro; private bool cIsAdmin; private string cGuardStatus = "Nothing run yet."; private readonly List cRefusals = []; private BitCommandPaletteCommand[] cGuardCommands = []; protected override void OnInitialized() { cGuardCommands = [ BitCommandPaletteCmd.New("Export as PDF") .Id("c-guard-export") .Group("Guards") .Shortcut("G X") // A check that needs nothing async, and what to say when it refuses. .Guard(_ => cIsPro, "Exports need the Pro plan.") .Handler(_ => { cGuardStatus = "Exported as PDF."; StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Toast("Exported as PDF")); }), BitCommandPaletteCmd.New("Admin") .Id("c-guard-admin") .Group("Guards") // An async check; on a page, it guards every command under it too. .Guard(async (ctx, cancellationToken) => { await Task.Delay(400, cancellationToken); // the server deciding return cIsAdmin ? BitCommandPaletteGuardResult.Allow : BitCommandPaletteGuardResult.Deny("Admins only."); }) .Children( BitCommandPaletteCmd.Action("Purge the cache", () => { cGuardStatus = "Cache purged."; StateHasChanged(); }).Id("c-guard-admin-purge"), BitCommandPaletteCmd.Action("Read the audit log", () => { cGuardStatus = "Audit log opened."; StateHasChanged(); }).Id("c-guard-admin-audit")), ]; // Registered too, so the shortcut and ExecuteAsync reach them. registrations.Add(paletteService.Register(cGuardCommands)); paletteService.CommandRefused += COnCommandRefused; } private void COnCommandRefused(object? sender, BitCommandPaletteCommandRefusedEventArgs e) { cRefusals.Insert(0, $"{DateTime.Now:HH:mm:ss} {e.Command.Title}, from {e.Trigger}: {e.Message ?? "(the default message)"}"); if (cRefusals.Count > 6) { cRefusals.RemoveAt(cRefusals.Count - 1); } _ = InvokeAsync(StateHasChanged); } public void Dispose() { paletteService.CommandRefused -= COnCommandRefused; foreach (var registration in registrations) { registration.Dispose(); } } // ---- A guard for many commands, registered at startup (not used by this demo page) ---- // Program.cs builder.Services.AddBitCommandPaletteCommandGuard(); // A command opts in with .Data(new RequiredPermission("reports.export")). public sealed record RequiredPermission(string Name); public sealed class PermissionGuard(AuthenticationStateProvider auth) : IBitCommandPaletteCommandGuard { public async ValueTask CheckAsync(BitCommandPaletteGuardContext context, CancellationToken cancellationToken) { if (context.Command.Data is not RequiredPermission { Name: var permission }) { return BitCommandPaletteGuardResult.Allow; } var user = (await auth.GetAuthenticationStateAsync()).User; return user.HasClaim("permission", permission) ? BitCommandPaletteGuardResult.Allow : BitCommandPaletteGuardResult.Deny($"You need the {permission} permission."); } } ``` \n**Nested pages**: ```razor @implements IDisposable @cNestedStatus Open on Editor theme Search "solarized" from the root Editor theme: @cEditorTheme ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private readonly List registrations = []; private string cNestedStatus = "Nothing picked yet."; private string cTheme = "Light"; private string cLanguage = "English"; private string cEditorTheme = "Light"; private int cDeploymentLoads; private BitCommandPaletteCommand[] cNestedCommands = []; private static readonly string[] cLanguages = ["English", "Deutsch", "Français", "Español"]; private static readonly string[] cBranches = ["main", "develop", "release/10.4", "feature/command-palette"]; private static readonly (string Name, string Role)[] cPeople = [ ("Ada Lovelace", "Engineering"), ("Grace Hopper", "Engineering"), ("Alan Turing", "Research"), ("Katherine Johnson", "Data"), ("Margaret Hamilton", "Engineering"), ("Linus Torvalds", "Platform"), ("Barbara Liskov", "Architecture"), ("Dennis Ritchie", "Platform"), ("Hedy Lamarr", "Research"), ("Tim Berners-Lee", "Web"), ]; private static readonly (string Version, string Environment, string When)[] cDeployments = [ ("v4.2.1", "Production", "2 hours ago"), ("v4.2.1", "Staging", "5 hours ago"), ("v4.2.0", "Production", "yesterday"), ("v4.1.9", "Production", "last week"), ]; protected override void OnInitialized() { CBuildNestedCommands(); // Registered with the service: reachable by id from OpenAsync(pageCommandId:) and a trigger's PageCommandId. registrations.Add(paletteService.Register( BitCommandPaletteCmd.New("Preferences") .Id("c-prefs") .Group("Nested pages") .Children( BitCommandPaletteCmd.New("Editor theme") .Id("c-editor-theme") .ChildrenPlaceholder("Pick an editor theme…") .Children(CEditorThemeOption("Light"), CEditorThemeOption("Dark"), CEditorThemeOption("Solarized")), BitCommandPaletteCmd.Action("Reset the editor", () => { cEditorTheme = "Light"; StateHasChanged(); }) .Id("c-prefs-reset")))); } private void CBuildNestedCommands() { cNestedCommands = [ // Static children; each option goes Back to the list. BitCommandPaletteCmd.New("Language") .Value(cLanguage) .ChildrenPlaceholder("Pick a language…") .Children(cLanguages.Select(language => BitCommandPaletteCmd.New(language) .Id($"c-language-{language.ToLowerInvariant()}") .Checked(cLanguage == language) .Handler(_ => { cLanguage = language; cNestedStatus = $"Language: {language}"; CBuildNestedCommands(); StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Back); }) .Build())), // Loaded each time the page opens. BitCommandPaletteCmd.New("Assign to…") .Children(async (ctx, cancellationToken) => { await Task.Delay(400, cancellationToken); // the API call return cPeople.Select(p => BitCommandPaletteCmd.New(p.Name) .Subtitle(p.Role) .Handler(() => { cNestedStatus = $"Assigned to {p.Name}"; StateHasChanged(); }) .Build()) .ToList(); }, placeholder: "Pick an assignee…"), // A loader that fails every other time: an error state with Try again. BitCommandPaletteCmd.New("Recent deployments…") .Id("c-deployments") .Children(async (ctx, cancellationToken) => { await Task.Delay(500, cancellationToken); if (++cDeploymentLoads % 2 == 1) { throw new HttpRequestException("503 Service Unavailable from deploy-api.internal"); } return cDeployments.Select(d => BitCommandPaletteCmd.New($"{d.Version} to {d.Environment}") .Subtitle(d.When) .ExcludeFromHistory() .Handler(() => { cNestedStatus = $"Opened {d.Version} on {d.Environment}"; StateHasChanged(); }) .Build()) .ToList(); }, placeholder: "Pick a deployment…"), // A page the handler decides once it found out what there is. BitCommandPaletteCmd.New("Switch branch…") .Handler(async ctx => { await Task.Delay(300, ctx.CancellationToken); // fetching the branches return BitCommandPaletteCommandResult.Page("Switch branch", [.. cBranches.Select(branch => BitCommandPaletteCmd.Action(branch, () => { cNestedStatus = $"Switched to {branch}"; StateHasChanged(); }).Build())], placeholder: "Pick a branch…"); }), ]; } private void CSetTheme(string theme) { cTheme = theme; cNestedStatus = $"Theme: {theme}"; } private BitCommandPaletteCommand CEditorThemeOption(string theme) => BitCommandPaletteCmd.New(theme) .Id($"c-editor-theme-{theme.ToLowerInvariant()}") .Handler(() => { cEditorTheme = theme; StateHasChanged(); }); public void Dispose() { foreach (var registration in registrations) { registration.Dispose(); } } ``` \n**Recents, pins and the action panel**: ```razor @implements IDisposable @cHistoryStatus Pinned: @CHistoryText(cHistory.Pinned(), "nothing - press Tab on a row and pick Pin to top") Recent: @CHistoryText(cHistory.Recents(int.MaxValue), "nothing used yet") Clear recent commands ``` ```csharp // The history behind every palette's recents, pins and frecency. [Inject] private BitCommandPaletteHistory cHistory { get; set; } = default!; private bool cEnableHistory = true; private string cHistoryStatus = "Nothing run yet."; private BitCommandPaletteCommand[] cHistoryCommands = []; protected override void OnInitialized() { cHistoryCommands = [ CPersonCommand("c-history-ada", "Ada Lovelace", "Engineering"), CPersonCommand("c-history-grace", "Grace Hopper", "Engineering"), CPersonCommand("c-history-alan", "Alan Turing", "Research"), BitCommandPaletteCmd.New("Start a timer") .Id("c-history-timer") .Handler(_ => CHistoryRun("Timer started")), BitCommandPaletteCmd.Navigate("Go to the Button demo", "/components/button") .Id("c-history-button") .Subtitle("A link: Ctrl+Enter opens it in a new tab"), BitCommandPaletteCmd.New("Scratch note") .Id("c-history-scratch") .Subtitle("ExcludeFromHistory: never recent, no action panel") .ExcludeFromHistory() .Handler(_ => CHistoryRun("Scratch note opened")), ]; // Raised when the palette loads the history, and on every record, pin and clear. cHistory.Changed += COnHistoryChanged; } // Listed first in the row's action panel (Tab), before Pin to top; Ctrl+Enter runs the first one. private BitCommandPaletteCommand CPersonCommand(string id, string name, string role) => BitCommandPaletteCmd.New(name) .Id(id) .Subtitle(role) .Handler(_ => CHistoryRun($"Opened {name}'s profile")) .Actions( BitCommandPaletteCmd.New("Send an e-mail").Id($"{id}-email").ExcludeFromHistory().Handler(_ => CHistoryRun($"E-mail to {name} drafted")), BitCommandPaletteCmd.New("Mention in an issue").Id($"{id}-mention").ExcludeFromHistory().Handler(_ => CHistoryRun($"{name} mentioned"))); private Task CHistoryRun(string message) { cHistoryStatus = message; StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.Toast(message, keepOpen: true)); } private string CHistoryText(IReadOnlyList ids, string none) { var titles = ids.Select(id => cHistoryCommands.FirstOrDefault(c => c.Id == id)) .OfType() .Select(c => $"{c.Title} ({cHistory.Get(c.Id)?.Count ?? 0}×)") .ToList(); return titles.Count == 0 ? none : string.Join(", ", titles); } private void COnHistoryChanged() => _ = InvokeAsync(StateHasChanged); public void Dispose() { cHistory.Changed -= COnHistoryChanged; } // ---- Keeping the history elsewhere, registered at startup (not used by this demo page) ---- // Program.cs builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { options.MaxRecents = 8; options.HistoryStorageKey = "my-app.palette-history"; }); builder.Services.AddBitCommandPaletteHistoryStore(); // per user, on your server // or: builder.Services.AddBitCommandPaletteInMemoryHistoryStore(); // for the lifetime of the scope only public sealed class ServerHistoryStore(HttpClient http) : IBitCommandPaletteHistoryStore { public async ValueTask> LoadAsync(CancellationToken cancellationToken = default) => await http.GetFromJsonAsync>("api/palette-history", cancellationToken) ?? new Dictionary(); public async ValueTask SaveAsync(IReadOnlyDictionary usage, CancellationToken cancellationToken = default) => (await http.PutAsJsonAsync("api/palette-history", usage, cancellationToken)).EnsureSuccessStatusCode(); } ``` \n**Fuzzy matching**: ```razor @if (dLabMatches.Count == 0) { } @foreach (var (match, index) in dLabMatches.Select((m, i) => (m, i))) { }
#CommandScoreRank
No command matches "@dLabQuery" as typed.
@(index + 1) @DFormat(match.Score) @DFormat(match.Rank)
``` ```csharp // The ranker the palette uses, over the default options: score, then priority and frecency. private static readonly BitCommandPaletteRanker dRanker = new(new BitCommandPaletteOptions()); private static readonly BitCommandPaletteCommand[] dLabCommands = [ BitCommandPaletteCmd.New("Go to settings").Id("d-lab-settings"), BitCommandPaletteCmd.New("Git: stash changes").Id("d-lab-stash"), BitCommandPaletteCmd.New("Toggle sidebar").Id("d-lab-sidebar"), BitCommandPaletteCmd.New("FuzzyMatcher.cs").Id("d-lab-fuzzy"), BitCommandPaletteCmd.New("Format document").Id("d-lab-format"), BitCommandPaletteCmd.New("Open recent file").Id("d-lab-recent"), BitCommandPaletteCmd.New("Reload window").Id("d-lab-reload"), BitCommandPaletteCmd.New("Rename symbol").Id("d-lab-rename"), BitCommandPaletteCmd.New("MCP server").Id("d-lab-mcp"), ]; private string dLabQuery = string.Empty; private List dLabMatches = []; protected override void OnInitialized() { DLabQueryChanged(string.Empty); } private void DLabQueryChanged(string query) { dLabQuery = query; // What the palette does on every keystroke: parse the query once, match every command, rank the matches. var parsed = BitCommandPaletteQuery.Parse(query); var matches = new List(); foreach (var command in dLabCommands) { if (BitCommandPaletteFuzzyMatcher.Default.Match(command, parsed) is { } match) { matches.Add(match); } } // No frecency here, as the palette has EnableHistory="false". dLabMatches = dRanker.Rank(matches, hasQuery: parsed.Terms.Count > 0, frecencyOf: _ => 0); } private static string DFormat(double value) => value.ToString("0.##", CultureInfo.InvariantCulture); ``` \n**Text as people type it**: ```razor @foreach (var example in dFoldExamples) { var match = DFoldMatch(example.Typed); }
TypedFindsWhy
@example.Typed @if (match is null) { nothing } else { } @example.Why
``` ```csharp // Stored exactly as written: accents, Arabic-script letters, ASCII digits. private static readonly BitCommandPaletteCommand[] dFoldCommands = [ BitCommandPaletteCmd.New("Café").Id("d-fold-cafe"), BitCommandPaletteCmd.New("Łódź").Id("d-fold-lodz"), BitCommandPaletteCmd.New("Türkçe").Id("d-fold-turkce"), BitCommandPaletteCmd.New("علي").Id("d-fold-ali"), BitCommandPaletteCmd.New("فارسی").Id("d-fold-farsi"), BitCommandPaletteCmd.New("كتاب").Id("d-fold-ketab"), BitCommandPaletteCmd.New("إسم").Id("d-fold-esm"), BitCommandPaletteCmd.New("العربية").Id("d-fold-arabic"), BitCommandPaletteCmd.New("Order 12").Id("d-fold-order"), BitCommandPaletteCmd.New("Step 3").Id("d-fold-step"), ]; private static readonly (string Typed, string Why)[] dFoldExamples = [ ("cafe", "Latin letters lose their diacritics."), ("café", "A term that types a diacritic means that letter, and is matched as written."), ("lodz", "Letters with a stroke fold too: Ł is L, as in fzf."), ("turkce", "ü and ç are u and c."), ("علی", "Typed with the Persian yeh (ی), stored with the Arabic one (ي)."), ("فارسي", "The other way round: typed with the Arabic yeh, stored with the Persian one."), ("کتاب", "Typed with the Persian kaf (ک), stored with the Arabic one (ك)."), ("اسم", "Alef with hamza or madda is plain alef."), ("العربيه", "Typed with heh (ه), stored with teh marbuta (ة)."), ("۱۲", "Persian digits are ASCII digits."), ("٣", "Arabic-Indic digits too."), ("order", "Fullwidth letters, which some input methods type, are ASCII."), ]; // fzf's --literal: every character as it is. private static readonly IBitCommandPaletteMatcher dLiteralMatcher = new BitCommandPaletteFuzzyMatcher { Normalize = false }; private bool dFoldLiteral; private BitCommandPaletteMatch? DFoldMatch(string typed) { var matcher = dFoldLiteral ? dLiteralMatcher : BitCommandPaletteFuzzyMatcher.Default; var query = BitCommandPaletteQuery.Parse(typed); return dFoldCommands.Select(command => matcher.Match(command, query)) .OfType() .MaxBy(match => match.Score); } ``` \n**Query operators**: ```razor
BitCommandPaletteQuery.Parse: @if (dOperatorQuery.IsTooLong) { longer than @BitCommandPaletteQuery.MaxLength characters - no command matches. } else if (dOperatorQuery.Terms.Count == 0) { no terms - every command matches. } @for (var i = 0; i < dOperatorQuery.Terms.Count; i++) { var term = dOperatorQuery.Terms[i]; if (i > 0) { @(dOperatorQuery.Terms[i - 1].Group == term.Group ? "or" : "and") } @term.Operator @term.Text }
@foreach (var example in dOperatorExamples) { }
QueryMeans
@example.Query @example.Means
``` ```csharp private static readonly BitCommandPaletteCommand[] dOperatorCommands = [ BitCommandPaletteCmd.New("Toggle word wrap").Id("d-op-wrap").Group("View"), BitCommandPaletteCmd.New("Show minimap").Id("d-op-minimap").Group("View"), BitCommandPaletteCmd.New("Show breadcrumbs").Id("d-op-breadcrumbs").Group("View"), BitCommandPaletteCmd.New("Wrap selection in a tag").Id("d-op-wrap-tag").Group("Edit"), BitCommandPaletteCmd.New("Export project").Id("d-op-export").Group("File"), BitCommandPaletteCmd.New("Open project").Id("d-op-open").Group("File"), BitCommandPaletteCmd.New("Close project").Id("d-op-close").Group("File"), BitCommandPaletteCmd.New("Settings").Id("d-op-settings").Group("Preferences"), BitCommandPaletteCmd.New("Set the language").Id("d-op-language").Group("Preferences"), BitCommandPaletteCmd.New("Change the theme").Id("d-op-theme").Group("Preferences"), ]; private static readonly (string Query, string Means)[] dOperatorExamples = [ ("wrap word", "Two fuzzy terms, both required, in any order."), ("'export", "Exact: the text itself, no gaps."), ("^set", "Prefix: a field starts with it."), ("project$", "Suffix: a field ends with it."), ("show !minimap", "Negation: drops every command that contains the term."), ("\"word wrap\"", "A quoted phrase: matched exactly, the space included."), ("!\"open project\" project", "A negated phrase: the projects, except Open project."), ("^\"set the\"", "An anchored phrase: a field starts with it."), ("^change | ^set", "Alternatives: starts with change, or with set."), ]; private BitCommandPaletteQuery dOperatorQuery = BitCommandPaletteQuery.Empty; private void DOperatorQueryChanged(string query) => dOperatorQuery = BitCommandPaletteQuery.Parse(query); ``` \n**Typos**: ```razor ``` ```csharp private bool dTypoTolerance = true; private static readonly BitCommandPaletteCommand[] dTypoCommands = [ BitCommandPaletteCmd.New("Settings").Id("d-typo-settings"), BitCommandPaletteCmd.New("Language").Id("d-typo-language"), BitCommandPaletteCmd.New("Privacy").Id("d-typo-privacy"), BitCommandPaletteCmd.New("Notifications").Id("d-typo-notifications"), BitCommandPaletteCmd.New("Appearance").Id("d-typo-appearance"), BitCommandPaletteCmd.New("Keyboard shortcuts").Id("d-typo-shortcuts"), ]; ``` \n**Field weights**: ```razor @foreach (var row in DWeightRows()) { }
CommandField that wonRaw scoreWeightWeighted
@row.Command @row.Field @DFormat(row.Raw) @DFormat(row.Weight) @DFormat(row.Raw * row.Weight)
``` ```csharp // The default weights; options.Weights holds the app's. private static readonly BitCommandPaletteMatchWeights dWeights = new(); // "color" in a different field of each one. private static readonly BitCommandPaletteCommand[] dWeightCommands = [ BitCommandPaletteCmd.New("Color picker").Id("d-weight-title"), BitCommandPaletteCmd.New("Brand kit").Id("d-weight-keyword").Keywords("logo", "color"), BitCommandPaletteCmd.New("Appearance").Id("d-weight-subtitle").Subtitle("Fonts, color and spacing"), BitCommandPaletteCmd.New("Contrast checker").Id("d-weight-group").Group("Color"), BitCommandPaletteCmd.New("Design tokens").Id("d-weight-description").Description("Radius, shadow and color values"), BitCommandPaletteCmd.New("Export assets").Id("d-weight-tag").Tags("color"), ]; private string dWeightQuery = string.Empty; private sealed record DWeightRow(string Command, string Field, double Raw, double Weight); private void DWeightQueryChanged(string query) => dWeightQuery = query; // What one term scores in each field: the field with the best weighted score counts. private List DWeightRows() { var term = dWeightQuery.Trim(); var rows = new List(); if (term.Length == 0) return rows; foreach (var command in dWeightCommands) { DWeightRow? best = null; foreach (var (field, text, weight) in DWeightFields(command)) { var match = BitCommandPaletteFuzzyMatcher.Default.Match(term, text ?? string.Empty); if (match.Matched && (best is null || match.Score * weight > best.Raw * best.Weight)) { best = new(command.Title, field, match.Score, weight); } } if (best is not null) { rows.Add(best); } } return [.. rows.OrderByDescending(row => row.Raw * row.Weight)]; } private static IEnumerable<(string Field, string? Text, double Weight)> DWeightFields(BitCommandPaletteCommand command) { yield return ("Title", command.Title, dWeights.Title); foreach (var keyword in command.Keywords) yield return ($"Keyword \"{keyword}\"", keyword, dWeights.Keyword); yield return ("Subtitle", command.Subtitle, dWeights.Subtitle); yield return ("Group", command.Group, dWeights.Group); yield return ("Description", command.Description, dWeights.Description); foreach (var tag in command.Tags) yield return ($"Tag \"{tag}\"", tag, dWeights.Tag); } private static string DFormat(double value) => value.ToString("0.##", CultureInfo.InvariantCulture); ``` \n**Priority and frecency**: ```razor rank = score + priority × @DFormat(dPriorityWeight) + ln(1 + frecency) × @DFormat(dFrecencyWeight) @foreach (var (match, index) in DRankRows().Select((m, i) => (m, i))) { }
#CommandPriorityFrecencyScoreRank
@(index + 1) @match.Command.Priority @DFormat(match.Frecency) @DFormat(match.Score) @DFormat(match.Rank)
``` ```csharp private static readonly BitCommandPaletteCommand[] dRankCommands = [ BitCommandPaletteCmd.New("Reports overview").Id("d-rank-overview").Priority(BitCommandPaletteCommandPriority.High), BitCommandPaletteCmd.New("Sales report").Id("d-rank-sales"), BitCommandPaletteCmd.New("Weekly report").Id("d-rank-weekly"), BitCommandPaletteCmd.New("Report a bug").Id("d-rank-bug").Priority(BitCommandPaletteCommandPriority.Low), BitCommandPaletteCmd.New("Archived reports").Id("d-rank-archived").Priority(BitCommandPaletteCommandPriority.Lowest), ]; private string? dRankQuery = "report"; private double dPriorityWeight = 0.25; // the default of options.PriorityWeight private double dFrecencyWeight = 6; // the default of options.FrecencyWeight private double dRankWeeklyUses = 0; private List DRankRows() { // A ranker over options of its own; the palette's ranker uses the app's options. var ranker = new BitCommandPaletteRanker(new BitCommandPaletteOptions { PriorityWeight = dPriorityWeight, FrecencyWeight = dFrecencyWeight, }); var query = BitCommandPaletteQuery.Parse(dRankQuery); var matches = dRankCommands.Select(command => BitCommandPaletteFuzzyMatcher.Default.Match(command, query)) .OfType(); var now = DateTimeOffset.UtcNow; return ranker.Rank(matches, hasQuery: query.Terms.Count > 0, frecencyOf: id => DRankFrecency(id, now)); } // Made-up usage: what the palette's history would have recorded. private double DRankFrecency(string commandId, DateTimeOffset now) => commandId switch { "d-rank-weekly" when dRankWeeklyUses > 0 => BitCommandPaletteHistory.ComputeFrecency(new((int)dRankWeeklyUses, now.AddHours(-2), false), now), "d-rank-bug" => BitCommandPaletteHistory.ComputeFrecency(new(3, now.AddDays(-5), false), now), _ => 0, }; private static string DFormat(double value) => value.ToString("0.##", CultureInfo.InvariantCulture); ``` \n**A custom matcher**: ```razor @foreach (var (match, index) in DMatcherRows().Select((m, i) => (m, i))) { }
#CommandScore
@(index + 1) @DFormat(match.Score)
``` ```csharp // For the whole app, in Program.cs: // builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => options.Matcher = BitCommandPaletteSubstringMatcher.Default); // builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => options.Matcher = new DPrefixMatcher()); // A matcher of your own: a field matches when it starts with the term. private sealed class DPrefixMatcher : IBitCommandPaletteMatcher { public BitCommandPaletteFieldMatch Match(string query, string text) => text.StartsWith(query, StringComparison.OrdinalIgnoreCase) ? new BitCommandPaletteFieldMatch(true, 100 - text.Length, [.. Enumerable.Range(0, query.Length)]) : BitCommandPaletteFieldMatch.None; } private static readonly IBitCommandPaletteMatcher dIgnoreCaseMatcher = new BitCommandPaletteFuzzyMatcher { IgnoreCase = true }; private static readonly IBitCommandPaletteMatcher dLiteralMatcher = new BitCommandPaletteFuzzyMatcher { Normalize = false }; private static readonly IBitCommandPaletteMatcher dPrefixMatcher = new DPrefixMatcher(); private static readonly BitCommandPaletteRanker dRanker = new(new BitCommandPaletteOptions()); private static readonly BitCommandPaletteCommand[] dLabCommands = [ BitCommandPaletteCmd.New("Go to settings").Id("d-lab-settings"), BitCommandPaletteCmd.New("Git: stash changes").Id("d-lab-stash"), BitCommandPaletteCmd.New("Toggle sidebar").Id("d-lab-sidebar"), BitCommandPaletteCmd.New("FuzzyMatcher.cs").Id("d-lab-fuzzy"), BitCommandPaletteCmd.New("Format document").Id("d-lab-format"), BitCommandPaletteCmd.New("Open recent file").Id("d-lab-recent"), BitCommandPaletteCmd.New("Reload window").Id("d-lab-reload"), BitCommandPaletteCmd.New("Rename symbol").Id("d-lab-rename"), BitCommandPaletteCmd.New("MCP server").Id("d-lab-mcp"), ]; private string? dMatcherName = "fuzzy"; private string? dMatcherQuery = "re"; private IBitCommandPaletteMatcher DSelectedMatcher() => dMatcherName switch { "ignore-case" => dIgnoreCaseMatcher, "literal" => dLiteralMatcher, "substring" => BitCommandPaletteSubstringMatcher.Default, "prefix" => dPrefixMatcher, _ => BitCommandPaletteFuzzyMatcher.Default, }; private List DMatcherRows() { var matcher = DSelectedMatcher(); var query = BitCommandPaletteQuery.Parse(dMatcherQuery); var matches = new List(); foreach (var command in dLabCommands) { if (matcher.Match(command, query) is { } match) { matches.Add(match); } } return dRanker.Rank(matches, hasQuery: query.Terms.Count > 0, frecencyOf: _ => 0); } private static string DFormat(double value) => value.ToString("0.##", CultureInfo.InvariantCulture); ``` \n**Modes**: ```razor @foreach (var mode in paletteService.Modes) { }
PrefixModeListsHint
@mode.Prefix @mode.Name @mode.Description @DModeScope(mode) @(mode.ShowHint ? "Yes" : "No")
``` ```csharp // In Program.cs, for the whole app: // builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => // { // options.Modes.Add(new BitCommandPaletteCommandMode { Prefix = ">", Name = "Commands", Tags = ["command"] }); // options.ShowModeHints = false; // no "Modes" group on the empty query // }); [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private readonly List registrations = []; private static readonly BitCommandPaletteCommand[] dModeCommands = [ BitCommandPaletteCmd.New("Sort the lines").Id("d-mode-sort").Tags("command") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Sorted the lines"))), BitCommandPaletteCmd.New("Toggle word wrap").Id("d-mode-wrap").Tags("command") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Toggled word wrap"))), BitCommandPaletteCmd.New("Format the document").Id("d-mode-format").Tags("command") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Formatted the document"))), BitCommandPaletteCmd.New("Keyboard shortcuts").Id("d-mode-shortcuts").Group("Help") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Ctrl+K opens the palette"))), BitCommandPaletteCmd.New("Search syntax").Id("d-mode-syntax").Group("Help") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("'exact ^prefix suffix$ !not \"a phrase\""))), BitCommandPaletteCmd.New("Release notes").Id("d-mode-notes").Group("Help") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Nothing new today"))), ]; protected override void OnInitialized() { registrations.Add(paletteService.Register(dModeCommands)); registrations.Add(paletteService.RegisterMode(new BitCommandPaletteCommandMode { Prefix = ">", Name = "Commands", Description = "Run an editor command", Placeholder = "Run a command…", Tags = ["command"], // only the commands tagged "command" })); registrations.Add(paletteService.RegisterMode(new BitCommandPaletteCommandMode { Prefix = "?", Name = "Help", Description = "Browse the help topics", Groups = ["Help"], // only the Help group })); registrations.Add(paletteService.RegisterMode(new BitCommandPaletteCommandMode { Prefix = ">>", // the longest prefix wins over ">" Name = "Everything", Description = "Every registered command", ShowHint = false, // not listed under Modes })); } private static string DModeScope(BitCommandPaletteCommandMode mode) => mode.HideRegistryCommands ? $"only its sources: {string.Join(", ", mode.Sources.Select(s => s.Name))}" : mode.Tags is { Count: > 0 } tags ? $"commands tagged {string.Join(", ", tags)}" : mode.Groups is { Count: > 0 } groups ? $"the {string.Join(", ", groups)} group" : "every command"; public void Dispose() { foreach (var registration in registrations) registration.Dispose(); } ``` \n**Async sources**: ```razor Open in People mode Search @@ada lovelace Search "status" Clear the calls @{ var calls = DSourceCallsSnapshot(); } @if (calls.Length == 0) { } @foreach (var call in calls) { }
StartedSourceQueryOutcomeResultsTook
No source has been asked yet.
@call.StartedAt.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) @call.Source @(call.Query.Length == 0 ? "(empty)" : call.Query) @call.Outcome @(call.Outcome == "Completed" ? call.Results.ToString(CultureInfo.InvariantCulture) : "-") @(call.Outcome == "Running" ? "-" : DMs(call.Took))
``` ```csharp // In Program.cs, for the whole app: // builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => // { // options.Sources.Add(new DocsSource()); // asked in the default mode // options.DebounceMs = 150; // quiet time after the last keystroke before a source is asked // options.SourceTimeoutMs = 10_000; // then it counts as failed; 0 waits forever // }); // builder.Services.AddBitCommandPaletteSource(); // a default-mode source resolved from DI, scoped [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private readonly List registrations = []; private sealed record DPerson(string Name, string Role, string Email); private static readonly DPerson[] dPeople = [ new("Ada Lovelace", "Engineering", "[email protected]"), new("Grace Hopper", "Engineering", "[email protected]"), new("Alan Turing", "Research", "[email protected]"), new("Katherine Johnson", "Data", "[email protected]"), new("Margaret Hamilton", "Engineering", "[email protected]"), new("Linus Torvalds", "Platform", "[email protected]"), new("Barbara Liskov", "Architecture", "[email protected]"), new("Dennis Ritchie", "Platform", "[email protected]"), new("Hedy Lamarr", "Research", "[email protected]"), new("Tim Berners-Lee", "Web", "[email protected]"), ]; private static readonly BitCommandPaletteCommand[] dTeams = [ BitCommandPaletteCmd.New("Platform team").Id("d-team-platform").Subtitle("Builds and runs the platform"), BitCommandPaletteCmd.New("Research team").Id("d-team-research").Subtitle("Looks ahead"), BitCommandPaletteCmd.New("Data team").Id("d-team-data").Subtitle("Owns the warehouse"), BitCommandPaletteCmd.New("Web team").Id("d-team-web").Subtitle("Ships the sites"), ]; private static readonly (string Name, string State)[] dServiceStates = [ ("API", "Operational"), ("Web app", "Operational"), ("Database", "Degraded performance"), ("Search index", "Operational"), ("E-mail delivery", "Operational"), ]; private string? dStatusBehaviour = "answers"; protected override void OnInitialized() { registrations.Add(paletteService.RegisterMode(new BitCommandPaletteCommandMode { Prefix = "@", Name = "People", Description = "Find a teammate or a team", Placeholder = "Search people…", HideRegistryCommands = true, // only the sources' rows Sources = [new DPeopleSource(this), new BitCommandPaletteStaticSource("Teams", dTeams)], })); // Asked in the default mode, from 3 characters on. registrations.Add(paletteService.RegisterSource(new BitCommandPaletteDelegateSource("Service status", DSearchStatusAsync, minQueryLength: 3))); } // Re-ranked: returns candidates, the palette scores and highlights them with everything else. private sealed class DPeopleSource(BitCommandPaletteDemo page) : IBitCommandPaletteSource { public string Name => "People"; // the heading of its results public int MinQueryLength => 1; // 0 also asks on the empty query public int MaxResults => 20; public bool ApplyMatcher => true; // re-rank with the palette's matcher public Task> SearchAsync(string query, BitCommandPaletteSourceContext context, CancellationToken cancellationToken) => page.DTrackAsync(Name, query, async ct => { await Task.Delay(250, ct); // stands in for the API call; the token cancels it when the query moves on return (IReadOnlyList)[.. dPeople .Where(p => p.Name.Contains(query, StringComparison.OrdinalIgnoreCase) || p.Role.Contains(query, StringComparison.OrdinalIgnoreCase)) .Select(p => BitCommandPaletteCmd.New(p.Name) .Id($"d-person-{p.Email}") // an id of its own: names are not unique .Subtitle(p.Role) .Keywords(p.Email) .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast($"Opened the profile of {p.Name}"))) .Build())]; }, cancellationToken); } // Ordered (ApplyMatcher is false): trouble first, kept as one block under the source's name. private Task> DSearchStatusAsync(string query, CancellationToken cancellationToken) => DTrackAsync("Service status", query, async ct => { switch (dStatusBehaviour) { case "throws": await Task.Delay(200, ct); throw new InvalidOperationException("The status service answered 502 Bad Gateway."); case "hangs": await Task.Delay(Timeout.Infinite, ct); // cancelled when SourceTimeoutMs passes break; } await Task.Delay(200, ct); var everything = query.Contains("status", StringComparison.OrdinalIgnoreCase); return (IReadOnlyList)[.. dServiceStates .Where(s => everything || s.Name.Contains(query, StringComparison.OrdinalIgnoreCase)) .OrderBy(s => s.State == "Operational") .Select(s => BitCommandPaletteCmd.New(s.Name) .Id($"d-status-{s.Name}") .Subtitle(s.State) .ExcludeFromHistory() .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast($"{s.Name}: {s.State}", keepOpen: true))) .Build())]; }, cancellationToken); // The call log: every call that reached a source, and how it ended. private sealed class DSourceCall(string source, string query) { public string Source { get; } = source; public string Query { get; } = query; public DateTime StartedAt { get; } = DateTime.Now; public string Outcome { get; set; } = "Running"; public int Results { get; set; } public TimeSpan Took { get; set; } } private readonly List dSourceCalls = []; private async Task> DTrackAsync(string source, string query, Func>> search, CancellationToken cancellationToken) { var call = new DSourceCall(source, query); lock (dSourceCalls) { dSourceCalls.Insert(0, call); if (dSourceCalls.Count > 12) dSourceCalls.RemoveAt(dSourceCalls.Count - 1); } _ = InvokeAsync(StateHasChanged); var started = System.Diagnostics.Stopwatch.GetTimestamp(); try { var results = await search(cancellationToken); call.Results = results.Count; call.Outcome = "Completed"; return results; } catch (OperationCanceledException) { call.Outcome = "Cancelled"; throw; } catch { call.Outcome = "Failed"; throw; } finally { call.Took = System.Diagnostics.Stopwatch.GetElapsedTime(started); _ = InvokeAsync(StateHasChanged); } } private DSourceCall[] DSourceCallsSnapshot() { lock (dSourceCalls) return [.. dSourceCalls]; } private void DClearSourceCalls() { lock (dSourceCalls) dSourceCalls.Clear(); } private static string DMs(TimeSpan value) => $"{value.TotalMilliseconds.ToString("0.##", CultureInfo.InvariantCulture)} ms"; public void Dispose() { foreach (var registration in registrations) registration.Dispose(); } ``` \n**Paging**: ```razor Open in Packages mode Search $acme ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private readonly List registrations = []; private const int DPackagePageSize = 10; // Sixty packages: four vendors, fifteen areas. private static readonly string[] dPackages = [ .. from vendor in new[] { "Acme", "Contoso", "Fabrikam", "Northwind" } from area in new[] { "Core", "Http", "Json", "Logging", "Caching", "Auth", "Data", "Testing", "Blazor", "Grpc", "Metrics", "Search", "Storage", "Mail", "Queue" } select $"{vendor}.{area}" ]; protected override void OnInitialized() { registrations.Add(paletteService.RegisterMode(new BitCommandPaletteCommandMode { Prefix = "$", Name = "Packages", Description = $"Browse {dPackages.Length} packages, {DPackagePageSize} at a time", Placeholder = "Search packages…", HideRegistryCommands = true, Sources = [ new BitCommandPaletteDelegateSource("Packages", (query, cursor, context, ct) => DSearchPackagesAsync(query, cursor, ct), minQueryLength: 0, // opens on a list maxResults: DPackagePageSize), ], })); } // One page of the packages matching the query. private async Task DSearchPackagesAsync(string query, string? cursor, CancellationToken cancellationToken) { // The cursor is the offset of the page here; a real API would hand out an opaque token. var offset = int.TryParse(cursor, NumberStyles.None, CultureInfo.InvariantCulture, out var from) ? from : 0; var hits = dPackages.Where(p => p.Contains(query, StringComparison.OrdinalIgnoreCase)).ToList(); await Task.Delay(200, cancellationToken); // stands in for the API call IReadOnlyList commands = [.. hits.Skip(offset).Take(DPackagePageSize).Select(name => BitCommandPaletteCmd.New(name) .Id($"d-package-{name}") .Subtitle($"{2 + name.Length % 5}.{name.Length % 10}.0") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast($"Opened {name}"))) .Build())]; var next = offset + DPackagePageSize < hits.Count ? (offset + DPackagePageSize).ToString(CultureInfo.InvariantCulture) : null; return new BitCommandPaletteSourcePage(commands, next, hits.Count); // the total feeds "Show N more…" } public void Dispose() { foreach (var registration in registrations) registration.Dispose(); } ``` \n**Searching from a page**: ```razor @dAssignStatus ``` ```csharp private string dAssignStatus = "Nobody assigned yet."; private BitCommandPaletteCommand[] dPageSourceCommands = default!; protected override void OnInitialized() { dPageSourceCommands = [ BitCommandPaletteCmd.New("Assign to…") .Id("d-page-assign") .ChildrenPlaceholder("Search people…") .Source("People", DSearchPeopleForPageAsync), BitCommandPaletteCmd.New("Request reviews…") .Id("d-page-reviewers") .ChildrenPlaceholder("Search people, check several, Ctrl+Enter to apply…") .MultiSelect() .Source("People", DSearchPeopleForPageAsync) .Handler(ctx => DSetAssignStatus(ctx.SelectedCommands.Count == 0 ? "No reviews requested." : $"Reviews requested from {string.Join(", ", ctx.SelectedCommands.Select(c => c.Title))}.")), BitCommandPaletteCmd.New("Browse packages…") .Id("d-page-packages") .Source("Packages", (query, cursor, ct) => DSearchPackagesAsync(query, cursor, ct), maxResults: DPackagePageSize), ]; } private async Task> DSearchPeopleForPageAsync(string query, CancellationToken cancellationToken) { await Task.Delay(250, cancellationToken); // stands in for the API call return [.. dPeople .Where(p => p.Name.Contains(query, StringComparison.OrdinalIgnoreCase)) .Select(p => BitCommandPaletteCmd.Action(p.Name, () => DSetAssignStatus($"Assigned to {p.Name}.")) .Id($"d-assign-{p.Email}") .Subtitle(p.Role) .Build())]; } private void DSetAssignStatus(string status) { dAssignStatus = status; _ = InvokeAsync(StateHasChanged); } // dPeople as in "Async sources", DSearchPackagesAsync and DPackagePageSize as in "Paging". ``` \n**Performance**: ```razor @(dPerfBusy ? "Measuring…" : "Measure searches") @(dPerfRegistration is null ? "Register them with the service" : "Unregister them") Open the modal with "cmd 1234" @if (dPerfRegisterTime is { } took) { The last registration took @DMs(took). } @if (dPerfResults.Count > 0) { @foreach (var result in dPerfResults) { }
QueryMatchedMatchRankTotalStatic source
@result.Query @result.Matched.ToString("N0", CultureInfo.InvariantCulture) @DMs(result.Match) @DMs(result.Rank) @DMs(result.Match + result.Rank) @DMs(result.Source)
} ``` ```csharp // In Program.cs, for long lists: // builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => // { // options.MaxResults = 1000; // rows kept per query // options.Virtualize = true; // render only the visible ones // options.RowHeight = 44; // every row's height while virtualized, in pixels // }); [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; [Inject] private IServiceProvider dServices { get; set; } = default!; private readonly List registrations = []; private static readonly BitCommandPaletteRanker dRanker = new(new BitCommandPaletteOptions()); private static readonly string[] dPerfQueries = ["c12", "cmd 1234", "command", "synthetic 19", "^command 7", "zzz"]; private sealed record DPerfMeasurement(string Query, int Matched, TimeSpan Match, TimeSpan Rank, TimeSpan Source); private readonly List dPerfResults = []; private List dPerfCommands = []; private int dPerfCount = 2000; private bool dPerfVirtualize; private bool dPerfBusy; private IDisposable? dPerfRegistration; private TimeSpan? dPerfRegisterTime; protected override void OnInitialized() { DBuildPerfCommands(); } // A new list each time: the palette takes a new snapshot when Commands gets a new collection. private void DBuildPerfCommands() { dPerfCommands = [.. Enumerable.Range(1, dPerfCount).Select(i => BitCommandPaletteCmd.New($"Command {i}") .Id($"d-perf-{i}") .Group("Bulk") .Subtitle($"Synthetic entry #{i}") .Priority(BitCommandPaletteCommandPriority.Lowest) .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast($"Ran command {i}", keepOpen: true))) .Build())]; dPerfResults.Clear(); } private void DTogglePerfRegistration() { var started = System.Diagnostics.Stopwatch.GetTimestamp(); if (dPerfRegistration is null) { dPerfRegistration = paletteService.Register(dPerfCommands); registrations.Add(dPerfRegistration); } else { dPerfRegistration.Dispose(); registrations.Remove(dPerfRegistration); dPerfRegistration = null; } dPerfRegisterTime = System.Diagnostics.Stopwatch.GetElapsedTime(started); } private async Task DMeasureAsync() { dPerfBusy = true; dPerfResults.Clear(); var commands = dPerfCommands; var source = new BitCommandPaletteStaticSource("Catalog", commands); var context = new BitCommandPaletteSourceContext(null, null, dServices); foreach (var text in dPerfQueries) { // WebAssembly runs on the page's only thread: yielding lets the table fill in as it goes. await Task.Delay(1); var query = BitCommandPaletteQuery.Parse(text); List matchTimes = [], rankTimes = [], sourceTimes = []; var matched = 0; for (var run = 0; run < 5; run++) { var started = System.Diagnostics.Stopwatch.GetTimestamp(); var matches = new List(); foreach (var command in commands) { if (BitCommandPaletteFuzzyMatcher.Default.Match(command, query) is { } match) { matches.Add(match); } } matchTimes.Add(System.Diagnostics.Stopwatch.GetElapsedTime(started)); started = System.Diagnostics.Stopwatch.GetTimestamp(); dRanker.Rank(matches, hasQuery: true, frecencyOf: _ => 0); rankTimes.Add(System.Diagnostics.Stopwatch.GetElapsedTime(started)); matched = matches.Count; started = System.Diagnostics.Stopwatch.GetTimestamp(); await source.SearchAsync(text, context, CancellationToken.None); sourceTimes.Add(System.Diagnostics.Stopwatch.GetElapsedTime(started)); } dPerfResults.Add(new(text, matched, DMedian(matchTimes), DMedian(rankTimes), DMedian(sourceTimes))); StateHasChanged(); } dPerfBusy = false; } private static TimeSpan DMedian(List values) { values.Sort(); return values[values.Count / 2]; } private static string DMs(TimeSpan value) => $"{value.TotalMilliseconds.ToString("0.##", CultureInfo.InvariantCulture)} ms"; public void Dispose() { foreach (var registration in registrations) registration.Dispose(); } ``` \n**Keyboard inside the palette**: ```razor @eKeyStatus ``` ```csharp private bool eLoop = true; private bool eVimBindings; private bool eDisablePointerSelection; private bool eRtl; private string eKeyStatus = "Nothing run yet."; private BitCommandPaletteCommand[] eKeyCommands = default!; protected override void OnInitialized() { eKeyCommands = [ BitCommandPaletteCmd.Action("Open the inbox", () => EKeyRan("Opened the inbox")).Id("e-key-inbox").Group("Mail") .Actions( BitCommandPaletteCmd.Action("Mark all as read", () => EKeyRan("Marked all as read")).Id("e-key-inbox-read"), BitCommandPaletteCmd.Action("Archive everything", () => EKeyRan("Archived everything")).Id("e-key-inbox-archive")), BitCommandPaletteCmd.Action("Compose a message", () => EKeyRan("Composed a message")).Id("e-key-compose").Group("Mail"), BitCommandPaletteCmd.Action("Reply to all", () => EKeyRan("Replied to all")).Id("e-key-reply-all").Group("Mail"), BitCommandPaletteCmd.Action("Forward the thread", () => EKeyRan("Forwarded the thread")).Id("e-key-forward").Group("Mail"), BitCommandPaletteCmd.Action("Snooze the thread", () => EKeyRan("Snoozed the thread")).Id("e-key-snooze").Group("Mail"), BitCommandPaletteCmd.Action("Mute the thread", () => EKeyRan("Muted the thread")).Id("e-key-mute").Group("Mail"), BitCommandPaletteCmd.Page("Folders", BitCommandPaletteCmd.Action("Drafts", () => EKeyRan("Opened Drafts")).Id("e-key-drafts"), BitCommandPaletteCmd.Action("Sent", () => EKeyRan("Opened Sent")).Id("e-key-sent"), BitCommandPaletteCmd.Action("Spam", () => EKeyRan("Opened Spam")).Id("e-key-spam")) .Id("e-key-folders").Group("Organize"), BitCommandPaletteCmd.Page("Label the thread", BitCommandPaletteCmd.New("Work").Id("e-key-label-work"), BitCommandPaletteCmd.New("Family").Id("e-key-label-family"), BitCommandPaletteCmd.New("Travel").Id("e-key-label-travel")) .Id("e-key-labels").Group("Organize") .MultiSelect() .Handler(ctx => EKeyRan(ctx.SelectedCommands.Count == 0 ? "Removed every label" : $"Labelled: {string.Join(", ", ctx.SelectedCommands.Select(c => c.Title))}")), BitCommandPaletteCmd.Action("Print the thread", () => EKeyRan("Printed the thread (not really)")).Id("e-key-print").Group("Organize"), BitCommandPaletteCmd.Action("Block the sender", () => EKeyRan("Blocked the sender")).Id("e-key-block").Group("Organize"), BitCommandPaletteCmd.Action("Report spam", () => EKeyRan("Reported spam")).Id("e-key-report").Group("Organize"), BitCommandPaletteCmd.Action("Empty the trash", () => EKeyRan("Emptied the trash (not really)")).Id("e-key-trash").Group("Organize") .Destructive("Empty the trash? Press Enter again."), ]; } private void EKeyRan(string what) { eKeyStatus = what; // A handler run from a key press re-renders the palette, not this page. _ = InvokeAsync(StateHasChanged); } ``` \n**Global shortcuts**: ```razor @foreach (var command in paletteService.Commands.Where(c => c.Shortcut is not null && c.GlobalShortcut)) { }
@command.Title @(command.Shortcut!.IsSequence ? "sequence" : "chord")
Last run by a shortcut: @(eShortcutLast ?? "nothing yet") ``` ```csharp // Program.cs - on by default: a plain key pressed in a text field is typing. Off, G then N fires there too. builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => options.SuppressShortcutsInEditableElements = true); // The page [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private readonly List registrations = []; private string? eShortcutLast; protected override void OnInitialized() { registrations.Add(paletteService.Register( BitCommandPaletteCmd.Action("New note", () => EShortcutRan("New note")) .Id("e-note-new").Group("Notes").Shortcut("Alt+Shift+N"), // a chord BitCommandPaletteCmd.Action("Go to notes", () => EShortcutRan("Go to notes")) .Id("e-note-go").Group("Notes").Shortcut("G N"), // a sequence: G, then N BitCommandPaletteCmd.Page("Change the note color", BitCommandPaletteCmd.Action("Yellow", () => EShortcutRan("Color: yellow")).Id("e-note-yellow"), BitCommandPaletteCmd.Action("Blue", () => EShortcutRan("Color: blue")).Id("e-note-blue"), BitCommandPaletteCmd.Action("Green", () => EShortcutRan("Color: green")).Id("e-note-green")) .Id("e-note-color").Group("Notes").Shortcut("G C"), // opens the palette on its page BitCommandPaletteCmd.Action("Discard the draft", () => EShortcutRan("Discard the draft")) .Id("e-note-discard").Group("Notes").Shortcut("Alt+Shift+D") .Destructive("Discard the draft?"), // opens the palette to confirm BitCommandPaletteCmd.Action("Preview the note", () => EShortcutRan("Preview the note")) .Id("e-note-preview").Group("Notes").Shortcut("Space", global: false))); // a hint on its row only // The table lists the registry: render again when it changes, and once the platform (⌘ or Ctrl) is known. paletteService.CommandsChanged += ERefresh; paletteService.PlatformDetected += ERefresh; } private void EShortcutRan(string title) { eShortcutLast = $"{title} at {DateTime.Now:HH:mm:ss}"; // The modal palette ran the handler: re-render this page. _ = InvokeAsync(StateHasChanged); } private void ERefresh() => _ = InvokeAsync(StateHasChanged); public void Dispose() { // The service outlives the page: unregister the commands and unsubscribe, or the page stays alive and rendering. foreach (var registration in registrations) registration.Dispose(); paletteService.CommandsChanged -= ERefresh; paletteService.PlatformDetected -= ERefresh; } ``` \n**Keyboard layouts, held keys and input methods**: ```razor Last run by a shortcut: @(eShortcutLast ?? "nothing yet") ``` ```csharp // Written once, in Latin letters: G N also fires where a Persian layout types ل then د, // and Alt+Shift+N where a Russian one types т. paletteService.Register( BitCommandPaletteCmd.Action("New note", () => EShortcutRan("New note")).Shortcut("Alt+Shift+N"), BitCommandPaletteCmd.Action("Go to notes", () => EShortcutRan("Go to notes")).Shortcut("G N")); ``` \n**Sequences in progress**: ```razor Sequence: @(eSequencePending is null ? "none in progress" : $"{eSequencePending.ToDisplayString(paletteService.IsMac)}, waiting for the next key") ``` ```csharp // Program.cs builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { options.SequenceTimeoutMs = 1500; // how long G waits for the next key; must be positive options.ShowSequenceHint = false; // no built-in status: the app shows it, as below }); // The page private BitCommandPaletteKeyboardShortcut? eSequencePending; protected override void OnInitialized() { paletteService.SequencePending += EOnSequencePending; } private void EOnSequencePending(BitCommandPaletteKeyboardShortcut? pending) { // "G" after the first key of G N, null once it completed, broke off or timed out. eSequencePending = pending; _ = InvokeAsync(StateHasChanged); } public void Dispose() => paletteService.SequencePending -= EOnSequencePending; ``` \n**Shortcut conflicts**: ```razor @EConflictButtonText Alt Q runs @(eConflictOverride is null ? "Save draft" : "Save and publish"), last pressed: @(eConflictLast ?? "not yet") ``` ```csharp private IDisposable? eConflictOverride; private string? eConflictLast; private string EConflictButtonText => eConflictOverride is null ? "Register \"Save and publish\" on Alt+Q" : "Dispose \"Save and publish\""; protected override void OnInitialized() { // The page's own command on Alt+Q. registrations.Add(paletteService.Register( BitCommandPaletteCmd.Action("Save draft", () => EConflictRan("Save draft")) .Id("e-conflict-save").Group("Conflicts").Shortcut("Alt+Q"))); } private void EToggleConflict() { if (eConflictOverride is not null) { // Disposed, and Alt+Q is Save draft's again. eConflictOverride.Dispose(); eConflictOverride = null; return; } // A second command on the same keys: registered later, so it wins them, and the palette logs a warning. eConflictOverride = paletteService.Register( BitCommandPaletteCmd.Action("Save and publish", () => EConflictRan("Save and publish")) .Id("e-conflict-publish").Group("Conflicts").Shortcut("Alt+Q")); } private void EConflictRan(string title) { eConflictLast = $"{title} at {DateTime.Now:HH:mm:ss}"; _ = InvokeAsync(StateHasChanged); } public void Dispose() => eConflictOverride?.Dispose(); ``` \n**Shortcut syntax**: ```razor @foreach (var sample in eShortcutSamples) { @sample } @if (BitCommandPaletteKeyboardShortcut.TryParse(eShortcutText, out var eParsed)) {
On macOS
Elsewhere
In words@eParsed.ToDisplayString(isMac: false)
Normalized@eParsed.Normalized
Kind@(eParsed.IsSequence ? $"a sequence of {eParsed.Chords.Count} chords" : "a chord")
} else { Not a shortcut: TryParse returns false, and Parse would throw a FormatException. } ``` ```csharp private string? eShortcutText = "Mod+Shift+K"; private static readonly string[] eShortcutSamples = ["Mod+K", "Ctrl+Shift+P", "G I", "Cmd+Option+Up", "Shift+?", "Alt+Comma", "Ctrl+Shift", "A+B"]; // Everywhere a shortcut is expected, a string converts implicitly: BitCommandPaletteCmd.New("Copy share link").Shortcut("Mod+Shift+C"); // a chord: ⌘⇧C or Ctrl+Shift+C BitCommandPaletteCmd.Navigate("Settings", "/settings").Shortcut("G S"); // a sequence: G, then S BitCommandPaletteKeyboardShortcut shortcut = "Ctrl+Shift+P"; var chord = new BitCommandPaletteKeyChord("p", Ctrl: true, Shift: true); // the same chord, built bool ok = BitCommandPaletteKeyboardShortcut.TryParse("Ctrl+Shift", out _); // false: no key ``` \n**Platform-aware hints**: ```razor This browser is @(paletteService.IsMac ? "a Mac" : "not a Mac"). ``` ```csharp [Inject] private IBitCommandPaletteService paletteService { get; set; } = default!; private static readonly BitCommandPaletteKeyboardShortcut eHintChord = "Mod+Shift+K"; private static readonly BitCommandPaletteKeyboardShortcut eHintSequence = "G S"; // IsMac is known once the palette has started in the browser. protected override void OnInitialized() => paletteService.PlatformDetected += ERefresh; private void ERefresh() => _ = InvokeAsync(StateHasChanged); public void Dispose() => paletteService.PlatformDetected -= ERefresh; ``` \n**The toggle shortcut**: ```razor @inject BitCommandPaletteOptions eOptions @foreach (var toggle in EToggleShortcuts) { } paletteService.ToggleAsync() @* One palette with its own main toggle: *@ ``` ```csharp // Program.cs builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { options.ToggleShortcut = "Mod+K"; // the default; null for none options.AdditionalToggleShortcuts.Add("Mod+Shift+P"); // more keys for the same }); // The page private IEnumerable EToggleShortcuts => eOptions.ToggleShortcut is null ? eOptions.AdditionalToggleShortcuts : [eOptions.ToggleShortcut, .. eOptions.AdditionalToggleShortcuts]; ``` \n**Options**: ```razor @foreach (var query in eFinderQueries) { SetQueryAsync("@query") } OnQueryChanged: @(eFinderQuery.Length == 0 ? "(empty)" : eFinderQuery), picked: @(eOptionPicked ?? "nothing yet") ``` ```csharp // Program.cs builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { options.ToggleShortcut = "Mod+K"; options.SequenceTimeoutMs = 1500; // must be positive: 0 throws ArgumentOutOfRangeException options.MaxResults = 100; // must be positive options.DebounceMs = 200; // must not be negative options.TypoTolerance = false; options.ResetOnClose = false; // reopen where it was left options.GroupOrder.AddRange(["Navigation", "Actions"]); options.Commands.Add(BitCommandPaletteCmd.Navigate("Home", "/").Group("Navigation")); }); // The page [Inject] private BitCommandPaletteOptions eOptions { get; set; } = default!; private BitCommandPalette eFinder = default!; private string eFinderQuery = ""; private string? eOptionPicked; private BitCommandPaletteCommand[] eOptionCommands = default!; private static readonly string[] eFinderQueries = ["history", "shortcut", "source", "^show", ""]; private sealed record EOptionInfo(string Name, string Group, Func Read, string Effect); private static readonly EOptionInfo[] eOptionInfos = [ new("ToggleShortcut", "Keyboard", o => o.ToggleShortcut?.ToString() ?? "none", "Opens and closes the palette; null for none."), new("Loop", "Keyboard", o => EBool(o.Loop), "Arrowing past the last row comes back to the first."), new("MaxRecents", "History", o => ENumber(o.MaxRecents), "Recent commands listed on the empty query."), // ... one per option ]; protected override void OnInitialized() { eOptionCommands = [ .. eOptionInfos.Select(o => (BitCommandPaletteCommand)BitCommandPaletteCmd.New(o.Name) .Id($"e-option-{o.Name}") .Group(o.Group) .Subtitle(o.Effect) .Value(o.Read(eOptions)) // the value this app runs with, at the end of the row .Handler(() => EOptionChosen(o))), ]; } private void EOptionChosen(EOptionInfo option) { eOptionPicked = $"{option.Name} = {option.Read(eOptions)}"; _ = InvokeAsync(StateHasChanged); } private static string EBool(bool value) => value ? "true" : "false"; private static string ENumber(double value) => value.ToString("0.##", System.Globalization.CultureInfo.InvariantCulture); ``` \n**Options, parameters and BitParams**: ```razor ``` ```csharp private readonly BitCommandPaletteParams[] eCascadedParams = [ new() { Placeholder = "Placeholder from BitParams…", ShowFooter = false, HighlightMatches = false, EnableHistory = false, VimBindings = true, } ]; private static readonly BitCommandPaletteCommand[] eCascadeCommands = [ BitCommandPaletteCmd.New("Banana").Id("e-cascade-banana").Group("Fruits"), BitCommandPaletteCmd.New("Mango").Id("e-cascade-mango").Group("Fruits"), BitCommandPaletteCmd.New("Orange").Id("e-cascade-orange").Group("Fruits"), BitCommandPaletteCmd.New("Pineapple").Id("e-cascade-pineapple").Group("Fruits"), ]; ``` \n**Security**: ```razor Last run: @(eSecurityLast ?? "nothing yet") ``` ```csharp // Program.cs builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { options.AllowedUrlSchemes.Add("vscode"); // let commands open vscode:// links options.AllowedUrlSchemes.Remove("tel"); options.RecordCommandIdsInTraces = false; // ids embed personal data: keep them out of traces and logs }); // The page private const string eUnsafeHref = "javascript:alert('This never runs')"; private string? eSecurityLast; private static readonly BitCommandPaletteCommand[] eSecurityCommands = [ // Not in AllowedUrlSchemes: refused, logged and reported, never followed. BitCommandPaletteCmd.New("Follow a javascript: link").Id("e-security-unsafe-link").Group("Links") .Subtitle(eUnsafeHref) .Href(eUnsafeHref), // Rendered as text, never as HTML. BitCommandPaletteCmd.New(" stays text").Id("e-security-html-title").Group("Text") .Handler(() => { }), // An opaque id rather than the e-mail address, and kept out of the history. BitCommandPaletteCmd.New("Sam Rivera").Id("e-security-person-1042").Group("People") .Subtitle("[email protected]") .ExcludeFromHistory() .Handler(() => { }), ]; ``` \n**History storage**: ```razor Read localStorage Key: @eOptions.HistoryStorageKey, MaxRecents: @eOptions.MaxRecents, MaxHistoryEntries: @eOptions.MaxHistoryEntries
@(eStoredHistory ?? "Not read yet.")
``` ```csharp // Program.cs builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { options.HistoryStorageKey = "my-app.palette-history"; // the default store's localStorage key options.MaxRecents = 8; // recents on the empty query options.MaxHistoryEntries = 500; // pinned commands are never pruned }); // Either: nothing in the browser, for the lifetime of the circuit (Server) or the tab (WebAssembly). builder.Services.AddBitCommandPaletteInMemoryHistoryStore(); // Or: per user, on the server. builder.Services.AddBitCommandPaletteHistoryStore(); // IUserSettings is your own service: a database, a user profile API... public sealed class UserHistoryStore(IUserSettings settings) : IBitCommandPaletteHistoryStore { public async ValueTask> LoadAsync(CancellationToken cancellationToken = default) => await settings.LoadPaletteHistoryAsync(cancellationToken) ?? new Dictionary(StringComparer.Ordinal); public async ValueTask SaveAsync(IReadOnlyDictionary usage, CancellationToken cancellationToken = default) => await settings.SavePaletteHistoryAsync(usage, cancellationToken); } // The page [Inject] private BitCommandPaletteOptions eOptions { get; set; } = default!; [Inject] private IJSRuntime eJs { get; set; } = default!; private string? eStoredHistory; private static readonly BitCommandPaletteCommand[] eHistoryCommands = [ BitCommandPaletteCmd.New("Open the calendar").Id("e-history-calendar").Group("Apps").Handler(() => { }), BitCommandPaletteCmd.New("Open the contacts").Id("e-history-contacts").Group("Apps").Handler(() => { }), BitCommandPaletteCmd.New("Open the notes").Id("e-history-notes").Group("Apps").Handler(() => { }), BitCommandPaletteCmd.New("Open the tasks").Id("e-history-tasks").Group("Apps").Handler(() => { }), BitCommandPaletteCmd.New("Open the weather").Id("e-history-weather").Group("Apps").Handler(() => { }), BitCommandPaletteCmd.New("Open the photos").Id("e-history-photos").Group("Apps").Handler(() => { }), ]; private async Task EReadStoredHistory() { var json = await eJs.InvokeAsync("localStorage.getItem", eOptions.HistoryStorageKey); eStoredHistory = string.IsNullOrEmpty(json) ? "(nothing stored yet)" : json; } ``` \n**Item template**: ```razor @if (item.Row.Command.Subtitle is not null) { } @if (item.Row.Command.Shortcut is not null) { } @if (item.IsActive) { } ``` ```csharp private sealed record FFileInfo(string Name, string Folder, string Size, string Modified); private static readonly FFileInfo[] fRecentFiles = [ new("Program.cs", "src/Server", "4 KB", "Today, 09:12"), new("App.razor", "src/Client", "2 KB", "Today, 08:40"), new("README.md", "/", "11 KB", "Yesterday"), new("appsettings.json", "src/Server", "1 KB", "Monday"), new("site.css", "src/Client/wwwroot", "7 KB", "Last week"), ]; private static readonly FFileInfo[] fArchivedFiles = [ new("Startup.cs", "archive/2023", "6 KB", "March 2023"), new("web.config", "archive/2023", "2 KB", "March 2023"), new("bundleconfig.json", "archive/2022", "1 KB", "June 2022"), ]; private List fFiles = []; protected override void OnInitialized() { fFiles = [ .. fRecentFiles.Select((file, i) => FFile(file, "Recent files", i < 2 ? $"Alt+{i + 1}" : null)), BitCommandPaletteCmd.New("The archive…").Id("f-archive").Group("Folders").Subtitle("Loads for a second") .Children(async (_, ct) => { // Long enough to see the loading state (a real app would call its API here). await Task.Delay(1000, ct); return fArchivedFiles.Select(file => FFile(file, null, null)).ToList(); }, placeholder: "Search the archive…"), BitCommandPaletteCmd.New("An empty folder").Id("f-empty-folder").Group("Folders").Subtitle("Nothing in it") .Children(async (_, ct) => { await Task.Delay(300, ct); return []; }), ]; } private static BitCommandPaletteCommand FFile(FFileInfo file, string? group, string? shortcut) { var builder = BitCommandPaletteCmd.New(file.Name) .Id($"f-file:{file.Folder}/{file.Name}") .Group(group) .Subtitle(file.Folder) .Data(file) .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast($"Opened {file.Name}", keepOpen: true))); // A shortcut shown on the row only: global: false keeps it from being claimed on the whole page. return shortcut is null ? builder : builder.Shortcut(shortcut, global: false); } private static string FExtension(BitCommandPaletteCommand command) { if (command.Children is not null || command.ChildrenProvider is not null) return "dir"; var dot = command.Title.LastIndexOf('.'); return dot < 0 ? "txt" : command.Title[(dot + 1)..]; } ``` \n**Icon & group header templates**: ```razor @(FExtension(command)[..1].ToUpperInvariant()) @heading ``` ```csharp // fFiles, FFileInfo, FFile and FExtension: as in the Item template sample. ``` \n**Header & footer templates**: ```razor
@(state.Breadcrumbs.Count > 0 ? string.Join(" › ", state.Breadcrumbs) : "All files") · @(state.IsLoading ? "loading…" : state.ResultCount == 1 ? "1 file" : $"{state.ResultCount} files") @if (state.Query.Length > 0) { matching “@state.Query” }
``` ```csharp // fFiles, FFileInfo and FFile: as in the Item template sample. The files carry their FFileInfo as Data. ``` \n**Detail pane**: ```razor
@command.Title @if (command.Data is FFileInfo file) {
Folder
@file.Folder
Size
@file.Size
Modified
@file.Modified
} else {

@command.Subtitle

} @if (command.Shortcut is not null) { }
``` ```csharp // fFiles, FFileInfo and FFile: as in the Item template sample. The files carry their FFileInfo as Data. ``` \n**Loading, no results & empty**: ```razor

Opening the folder…

No file matches “@query”. Try json.

This folder is empty.

No commands yet. They appear here once they are registered.

``` ```csharp // fFiles, FFileInfo and FFile: as in the Item template sample. "The archive…" and "An empty folder" are lazily loaded pages: BitCommandPaletteCmd.New("The archive…").Id("f-archive").Group("Folders").Subtitle("Loads for a second") .Children(async (_, ct) => { await Task.Delay(1000, ct); // a real app would call its API here return fArchivedFiles.Select(file => FFile(file, null, null)).ToList(); }, placeholder: "Search the archive…"), BitCommandPaletteCmd.New("An empty folder").Id("f-empty-folder").Group("Folders").Subtitle("Nothing in it") .Children(async (_, ct) => { await Task.Delay(300, ct); return []; }), ``` \n**Localization**: ```razor
@foreach (var language in fLanguages) { @language.Name }
``` ```csharp // What the sample commands say, in one language. private sealed record FTexts( string General, string DangerZone, string OpenSettings, string SettingsOpened, string DarkMode, string Export, string ChooseFormat, string AsPdf, string ExportedPdf, string AsCsv, string NeedsPro, string DeleteFiles, string ConfirmDelete, string Deleted); private sealed record FLanguage(string Code, string Name, BitCommandPaletteStrings Strings, FTexts Texts); private static readonly FLanguage[] fLanguages = [ new("en", "English", new BitCommandPaletteStrings(), new( "General", "Danger zone", "Open settings", "Settings opened", "Dark mode", "Export…", "Choose a format…", "As PDF", "Exported as PDF", "As CSV", "Requires the Pro plan", "Delete all files", "Delete every file for good?", "Deleted (not really)")), new("de", "Deutsch", new BitCommandPaletteStrings { Placeholder = "Befehl eingeben oder suchen…", PromptPlaceholder = "Wert eingeben und Eingabetaste drücken…", NoResults = "Keine Ergebnisse für „{0}“", NoResultsHint = "Versuchen Sie eine andere Suche, oder prüfen Sie die Schreibweise.", Empty = "Noch nichts anzuzeigen.", Loading = "Wird gesucht…", Recent = "Zuletzt verwendet", Pinned = "Angeheftet", Suggestions = "Vorschläge", Modes = "Modi", Actions = "Aktionen", Results = "Ergebnisse", Other = "Sonstiges", Navigate = "zum Navigieren", Select = "zum Auswählen", Close = "zum Schließen", ClearQuery = "zum Leeren", Back = "zurück", OpenActions = "für Aktionen", Toggle = "zum Umschalten", Apply = "zum Anwenden", Pin = "Oben anheften", Unpin = "Lösen", RemoveFromRecents = "Aus „Zuletzt verwendet“ entfernen", ClearRecents = "Zuletzt verwendete Befehle löschen", ConfirmPrefix = "Bestätigen: ", ConfirmHint = "Zum Bestätigen erneut die Eingabetaste drücken, Esc zum Abbrechen", SelectedCount = "{0} ausgewählt", ResultsAnnouncement = "{0} Ergebnisse verfügbar", OneResultAnnouncement = "1 Ergebnis verfügbar", NoResultsAnnouncement = "Keine Ergebnisse", Disabled = "Nicht verfügbar", DialogLabel = "Befehlspalette", InputLabel = "Befehle durchsuchen", CloseButton = "Schließen", BackButton = "Zurück", Breadcrumb = "Navigationspfad", OpenPalette = "Befehlspalette öffnen", TriggerNameSeparator = ", ", Search = "Suchen", Home = "Start", MoreResults = "{0} weitere anzeigen…", LoadMore = "Mehr anzeigen…", SequencePending = "wartet auf die nächste Taste…", CommandFailed = "„{0}“ konnte nicht ausgeführt werden.", CommandRefused = "„{0}“ darf nicht ausgeführt werden.", CommandUnavailable = "„{0}“ ist nicht mehr verfügbar.", LoadFailed = "Diese Liste konnte nicht geladen werden.", Retry = "Erneut versuchen", ApproximateResults = "Keine genauen Treffer. Ähnliche werden angezeigt.", Running = "Wird ausgeführt…", SourceFailed = "{0} konnte nicht durchsucht werden", On = "Ein", Off = "Aus", ShortcutThen = "dann", }, new( "Allgemein", "Gefahrenbereich", "Einstellungen öffnen", "Einstellungen geöffnet", "Dunkelmodus", "Exportieren…", "Format wählen…", "Als PDF", "Als PDF exportiert", "Als CSV", "Erfordert den Pro-Tarif", "Alle Dateien löschen", "Wirklich alle Dateien endgültig löschen?", "Gelöscht (nicht wirklich)")), new("es", "Español", new BitCommandPaletteStrings { Placeholder = "Escribe un comando o busca…", PromptPlaceholder = "Escribe un valor y pulsa Intro…", NoResults = "No hay resultados para «{0}»", NoResultsHint = "Prueba con otra búsqueda o revisa la ortografía.", Empty = "Todavía no hay nada que mostrar.", Loading = "Buscando…", Recent = "Recientes", Pinned = "Fijados", Suggestions = "Sugerencias", Modes = "Modos", Actions = "Acciones", Results = "Resultados", Other = "Otros", Navigate = "para moverte", Select = "para elegir", Close = "para cerrar", ClearQuery = "para borrar", Back = "para volver", OpenActions = "para ver acciones", Toggle = "para marcar", Apply = "para aplicar", Pin = "Fijar arriba", Unpin = "Dejar de fijar", RemoveFromRecents = "Quitar de recientes", ClearRecents = "Borrar los comandos recientes", ConfirmPrefix = "Confirmar: ", ConfirmHint = "Pulsa Intro de nuevo para confirmar, o Esc para cancelar", SelectedCount = "{0} seleccionados", ResultsAnnouncement = "{0} resultados disponibles", OneResultAnnouncement = "1 resultado disponible", NoResultsAnnouncement = "Sin resultados", Disabled = "No disponible", DialogLabel = "Paleta de comandos", InputLabel = "Buscar comandos", CloseButton = "Cerrar", BackButton = "Volver", Breadcrumb = "Ruta de navegación", OpenPalette = "Abrir la paleta de comandos", TriggerNameSeparator = ", ", Search = "Buscar", Home = "Inicio", MoreResults = "Mostrar {0} más…", LoadMore = "Mostrar más…", SequencePending = "esperando la siguiente tecla…", CommandFailed = "No se pudo ejecutar «{0}».", CommandRefused = "No puedes ejecutar «{0}».", CommandUnavailable = "«{0}» ya no está disponible.", LoadFailed = "No se pudo cargar esta lista.", Retry = "Reintentar", ApproximateResults = "No hay coincidencias exactas. Se muestran las más parecidas.", Running = "Trabajando…", SourceFailed = "No se pudo buscar en {0}", On = "Activado", Off = "Desactivado", ShortcutThen = "luego", }, new( "General", "Zona de peligro", "Abrir la configuración", "Configuración abierta", "Modo oscuro", "Exportar…", "Elige un formato…", "Como PDF", "Exportado como PDF", "Como CSV", "Requiere el plan Pro", "Eliminar todos los archivos", "¿Eliminar todos los archivos para siempre?", "Eliminados (no de verdad)")), ]; private FLanguage fLanguage = fLanguages[0]; private List fL10nCommands = []; private bool fL10nDark; protected override void OnInitialized() => fL10nCommands = FBuildL10nCommands(); private void FSetLanguage(FLanguage language) { fLanguage = language; // A new list: the palette re-reads Commands when it is handed a new collection. fL10nCommands = FBuildL10nCommands(); } private List FBuildL10nCommands() { var t = fLanguage.Texts; var english = fLanguages[0].Texts; return [ // The same id in every language; the English title stays findable as a keyword. BitCommandPaletteCmd.New(t.OpenSettings).Id("f-l10n:settings").Group(t.General).Keywords(english.OpenSettings) .Shortcut("G S", global: false) .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast(t.SettingsOpened, keepOpen: true))), BitCommandPaletteCmd.New(t.DarkMode).Id("f-l10n:dark").Group(t.General).Keywords(english.DarkMode) .Checked(fL10nDark) .Handler(_ => { fL10nDark = !fL10nDark; fL10nCommands = FBuildL10nCommands(); StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }), BitCommandPaletteCmd.New(t.Export).Id("f-l10n:export").Group(t.General).Keywords(english.Export) .ChildrenPlaceholder(t.ChooseFormat) .Children( BitCommandPaletteCmd.New(t.AsPdf).Id("f-l10n:export.pdf").Keywords(english.AsPdf) .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast(t.ExportedPdf, keepOpen: true))), BitCommandPaletteCmd.New(t.AsCsv).Id("f-l10n:export.csv").Keywords(english.AsCsv).Disabled(true, t.NeedsPro)), BitCommandPaletteCmd.New(t.DeleteFiles).Id("f-l10n:delete").Group(t.DangerZone).Keywords(english.DeleteFiles) .Destructive(t.ConfirmDelete) .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast(t.Deleted, keepOpen: true))), ]; } // ---------- Every palette: Program.cs ---------- builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => { options.Strings = new BitCommandPaletteStrings { Placeholder = "Befehl eingeben oder suchen…", NoResults = "Keine Ergebnisse für „{0}“", ResultsAnnouncement = "{0} Ergebnisse verfügbar", On = "Ein", Off = "Aus", ShortcutThen = "dann", }; }); // ---------- Resource files: read the strings from an IStringLocalizer ---------- public sealed class LocalizedCommandPaletteStrings(IStringLocalizer localizer) : BitCommandPaletteStrings { public override string Placeholder => localizer[nameof(Placeholder)]; public override string NoResults => localizer[nameof(NoResults)]; public override string ResultsAnnouncement => localizer[nameof(ResultsAnnouncement)]; // ... } // @inject IStringLocalizer Localizer // // ---------- Plural forms: Polish has three ---------- public sealed class PolishCommandPaletteStrings : BitCommandPaletteStrings { public override string ResultsAnnouncementFor(int count) => count switch { 0 => "Brak wyników", 1 => "1 wynik", _ when count % 10 is >= 2 and <= 4 && count % 100 is < 12 or > 14 => $"{count} wyniki", _ => $"{count} wyników", }; } // ---------- NoResults and Empty fall back to BlazorUI's own strings ---------- public sealed class GermanBitStrings : BitStrings { public override string NoResultsFound => "Keine Ergebnisse"; public override string NoItemsFound => "Keine Einträge"; // ... } builder.Services.AddBitBlazorUIServices(options => options.Strings = new GermanBitStrings()); ``` \n**CSS variables**: ```razor @command.Title

@(command.Subtitle ?? command.Group)

``` ```csharp private const string fVariablesStyle = "--bit-CommandPalette-width: 760px;" + // the modal dialog only "--bit-CommandPalette-max-height: 320px;" + "--bit-CommandPalette-top: 8vh;" + // the modal dialog only "--bit-CommandPalette-radius: 1.25rem;" + "--bit-CommandPalette-background: color-mix(in srgb, #ea580c 5%, var(--bit-clr-bg-pri));" + "--bit-CommandPalette-color: color-mix(in srgb, #9a3412 35%, var(--bit-clr-fg-pri));" + "--bit-CommandPalette-border-color: color-mix(in srgb, #ea580c 35%, var(--bit-clr-brd-sec));" + "--bit-CommandPalette-shadow: 0 1.5rem 3rem rgba(154, 52, 18, .3);" + // the modal dialog only "--bit-CommandPalette-backdrop: rgba(67, 20, 7, .45);" + // the modal dialog only "--bit-CommandPalette-row-height: 3rem;" + "--bit-CommandPalette-item-radius: 999px;" + "--bit-CommandPalette-active-background: color-mix(in srgb, #ea580c 16%, transparent);" + "--bit-CommandPalette-accent: #ea580c;" + "--bit-CommandPalette-mark-background: color-mix(in srgb, #ea580c 30%, transparent);" + "--bit-CommandPalette-detail-width: 35%;" + "--bit-CommandPalette-focus-color: #ea580c;"; private const string fTriggerStyle = "--bit-CommandPaletteTrigger-height: 2.5rem;" + "--bit-CommandPaletteTrigger-background: color-mix(in srgb, #ea580c 8%, var(--bit-clr-bg-pri));" + "--bit-CommandPaletteTrigger-color: color-mix(in srgb, #9a3412 50%, var(--bit-clr-fg-pri));" + "--bit-CommandPaletteTrigger-border-color: #ea580c;" + "--bit-CommandPaletteTrigger-radius: 999px;" + "--bit-CommandPaletteTrigger-focus-color: #ea580c;"; private bool fNotifications = true; private List fSkinCommands = []; protected override void OnInitialized() => fSkinCommands = FBuildSkinCommands(); private List FBuildSkinCommands() => [ BitCommandPaletteCmd.New("Open the inbox").Id("f-skin:inbox").Group("Mail").Subtitle("3 unread").Badge("12") .Shortcut("G I", global: false) .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Inbox opened", keepOpen: true))), BitCommandPaletteCmd.New("Archive all mail").Id("f-skin:archive").Group("Mail").Destructive("Archive every message?") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Archived (not really)", keepOpen: true))), BitCommandPaletteCmd.New("Export the mailbox").Id("f-skin:export").Group("Mail").Disabled(true, "Requires the Pro plan"), BitCommandPaletteCmd.New("Notifications").Id("f-skin:notifications").Group("Settings").Checked(fNotifications) .Handler(_ => { fNotifications = !fNotifications; fSkinCommands = FBuildSkinCommands(); StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }), BitCommandPaletteCmd.Page("Theme", BitCommandPaletteCmd.New("Light").Id("f-skin:theme.light") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Light (not really)", keepOpen: true))), BitCommandPaletteCmd.New("Dark").Id("f-skin:theme.dark") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("Dark (not really)", keepOpen: true)))) .Id("f-skin:theme").Group("Settings"), ]; /* ---------- Or for every palette and trigger of the app, in its stylesheet (app.css) ---------- :root { --bit-CommandPalette-accent: #ea580c; --bit-CommandPalette-width: 760px; --bit-CommandPaletteTrigger-radius: 999px; } */ ``` \n**Ready-made skins**: ```razor @foreach (var skin in fSkins) { @skin.Name } Style: @(fSkin.Style.Length == 0 ? "(none)" : fSkin.Style) ``` ```csharp private sealed record FSkin(string Name, string Style); private static readonly FSkin[] fSkins = [ new("Default", ""), new("Indigo", "--bit-CommandPalette-accent: #4f46e5;" + "--bit-CommandPalette-mark-background: rgba(79, 70, 229, .2);" + "--bit-CommandPalette-active-background: rgba(79, 70, 229, .1);" + "--bit-CommandPalette-radius: 14px;" + "--bit-CommandPalette-item-radius: 8px;"), new("Teal", "--bit-CommandPalette-accent: #0d9488;" + "--bit-CommandPalette-background: color-mix(in srgb, #0d9488 4%, var(--bit-clr-bg-pri));" + "--bit-CommandPalette-border-color: color-mix(in srgb, #0d9488 30%, var(--bit-clr-brd-sec));" + "--bit-CommandPalette-active-background: color-mix(in srgb, #0d9488 14%, transparent);" + "--bit-CommandPalette-mark-background: color-mix(in srgb, #0d9488 28%, transparent);"), new("Rose", "--bit-CommandPalette-accent: #db2777;" + "--bit-CommandPalette-background: color-mix(in srgb, #db2777 4%, var(--bit-clr-bg-pri));" + "--bit-CommandPalette-border-color: color-mix(in srgb, #db2777 30%, var(--bit-clr-brd-sec));" + "--bit-CommandPalette-active-background: color-mix(in srgb, #db2777 12%, transparent);" + "--bit-CommandPalette-mark-background: color-mix(in srgb, #db2777 25%, transparent);"), new("Flat", "--bit-CommandPalette-radius: 0;" + "--bit-CommandPalette-item-radius: 0;" + "--bit-CommandPalette-shadow: none;" + "--bit-CommandPalette-border-color: var(--bit-clr-brd-pri);" + "--bit-CommandPalette-active-background: color-mix(in srgb, var(--bit-clr-fg-pri) 10%, transparent);"), new("Pill", "--bit-CommandPalette-radius: 1.75rem;" + "--bit-CommandPalette-item-radius: 999px;" + "--bit-CommandPalette-row-height: 3rem;"), new("Dense list", "--bit-CommandPalette-row-height: 2rem;" + "--bit-CommandPalette-max-height: 240px;" + "--bit-CommandPalette-item-radius: 4px;" + "--bit-CommandPalette-radius: 6px;"), new("Terminal", "font-family: ui-monospace, SFMono-Regular, Consolas, monospace;" + "--bit-CommandPalette-accent: #16a34a;" + "--bit-CommandPalette-radius: 4px;" + "--bit-CommandPalette-item-radius: 2px;" + "--bit-CommandPalette-active-background: color-mix(in srgb, #16a34a 16%, transparent);" + "--bit-CommandPalette-mark-background: color-mix(in srgb, #16a34a 30%, transparent);"), ]; private FSkin fSkin = fSkins[1]; // fSkinCommands and FBuildSkinCommands: as in the CSS variables sample. ``` \n**Light & dark**: ```razor @foreach (var (scheme, label) in fSchemes) { @label } @* The attribute on the palette itself (or on any element around it): *@ @* A theme of its own: *@ ``` ```csharp private static readonly (string? Scheme, string Label)[] fSchemes = [ (null, "Follows the page"), ("light", "Light"), ("dark", "Dark"), ]; private string? fScheme = "dark"; private readonly BitTheme fDarkTheme = BitThemeFactory.CreateDarkThemeFromSeed(BitAccentColorPresets.Teal); // fSkinCommands and FBuildSkinCommands: as in the CSS variables sample. ``` \n**Density**: ```razor @foreach (var (name, scale) in fDensities) { @name (@scale) } ``` ```csharp // Strings, not numbers: a number would be written in the current culture ("0,8" in German), which CSS does not read. private static readonly (string Name, string Scale)[] fDensities = [ ("Compact", "0.8"), ("Default", "1"), ("Spacious", "1.2"), ]; private string fDensity = "0.8"; // fSkinCommands and FBuildSkinCommands: as in the CSS variables sample. ``` \n**Forced colors, contrast & motion**: ```razor ``` ```csharp // fSkinCommands and FBuildSkinCommands: as in the CSS variables sample. ``` \n**Restyling from scratch**: ```razor ``` ```csharp private static readonly BitCommandPaletteCommand[] fPantry = [ BitCommandPaletteCmd.New("Apples").Id("f-pantry:apples").Group("Fruit").Subtitle("6 left") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.KeepOpen)), BitCommandPaletteCmd.New("Bananas").Id("f-pantry:bananas").Group("Fruit").Subtitle("Ripe") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.KeepOpen)), BitCommandPaletteCmd.New("Cherries").Id("f-pantry:cherries").Group("Fruit").Disabled(true, "Out of season"), BitCommandPaletteCmd.New("Carrots").Id("f-pantry:carrots").Group("Vegetables").Subtitle("1 kg") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.KeepOpen)), BitCommandPaletteCmd.New("Leeks").Id("f-pantry:leeks").Group("Vegetables").Subtitle("3") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.KeepOpen)), BitCommandPaletteCmd.New("Throw out the leftovers").Id("f-pantry:leftovers").Group("Vegetables") .Destructive("Throw them all out?") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.KeepOpen)), ]; ``` \n**Accessibility**: ```razor @if (item.IsConfirming) { Confirm: } @if (item.Row.Command.Disabled && item.Row.Command.DisabledReason is not null) { @item.Row.Command.DisabledReason } @if (item.Row.Command.Checked is { } on) { @* Drawn for the eye, said in words for screen readers. *@ @(on ? "On" : "Off") } @if (item.Row.Command.Shortcut is not null) { } ``` ```csharp // fSkinCommands and FBuildSkinCommands: as in the CSS variables sample. The .f-row, .f-text, .f-title and .f-sub // classes: as in the Item template sample. ``` \n**Accessibility of the dialog and the list**: ```razor @* Nothing to set up: the dialog, the combobox, the grouped listbox and the live regions come with the palette. *@ ``` \n**Diagnostics**: ```csharp // File: Program.cs - OpenTelemetry builder.Services.AddOpenTelemetry() .WithMetrics(m => m.AddMeter(BitCommandPaletteDiagnostics.MeterName)) .WithTracing(t => t.AddSource(BitCommandPaletteDiagnostics.ActivitySourceName)); // File: Program.cs - or Bit.Belemetry builder.Services.AddBitBelemetryServices(options => { options.Metrics.Meters.Add(BitCommandPaletteDiagnostics.MeterName); options.Traces.Sources.Add(BitCommandPaletteDiagnostics.ActivitySourceName); }); // File: Program.cs - keep command ids out of traces and logs when they embed personal data builder.Services.AddBitBlazorUIExtrasServices(commandPalette: options => options.RecordCommandIdsInTraces = false); ``` \n**Troubleshooting**: ```csharp // File: ServiceCollectionExtensions.cs in a project both the server and the client reference - // so the two Program.cs files register the same services with the same options. public static class ServiceCollectionExtensions { public static IServiceCollection AddClientServices(this IServiceCollection services) { services.AddBitBlazorUIExtrasServices(commandPalette: options => options.GroupOrder.AddRange(["Navigation", "Actions"])); return services; } } // File: Program.cs of the server, and Program.cs of the client builder.Services.AddClientServices(); // File: any component - after the state a VisibleWhen predicate reads has changed Palette.Refresh(); ``` \n**Style & Class**: ```razor @command.Title

@command.Description

@command.Title

@command.Description

``` ```csharp private static readonly BitCommandPaletteCommand[] fColorCommands = [ BitCommandPaletteCmd.New("Violet").Id("f-color:violet").Group("Colors").Badge("Accent") .Description("#7c3aed, the accent of these samples."), BitCommandPaletteCmd.New("Indigo").Id("f-color:indigo").Group("Colors") .Description("#4f46e5, a deep blue-violet."), BitCommandPaletteCmd.New("Teal").Id("f-color:teal").Group("Colors") .Description("#0d9488, a cool blue-green."), BitCommandPaletteCmd.New("Nothing inside").Id("f-color:empty").Group("Pages").Subtitle("Opens an empty page") .Children((_, _) => Task.FromResult>([])), ]; ``` \n**RTL**: ```razor ``` ```csharp // Persian strings that announce the result count in Persian digits. private sealed class FPersianStrings : BitCommandPaletteStrings { public override string ResultsAnnouncementFor(int count) => count switch { 0 => NoResultsAnnouncement, 1 => OneResultAnnouncement, _ => Format(ResultsAnnouncement, FPersianDigits(count)), }; } private static string FPersianDigits(int value) => string.Concat(value.ToString(System.Globalization.CultureInfo.InvariantCulture) .Select(c => char.IsAsciiDigit(c) ? (char)('۰' + (c - '0')) : c)); private static readonly FPersianStrings fPersianStrings = new() { Placeholder = "فرمانی بنویسید یا جست‌وجو کنید…", PromptPlaceholder = "مقداری بنویسید و Enter را بزنید…", NoResults = "نتیجه‌ای برای «{0}» نیست", NoResultsHint = "عبارت دیگری را امتحان کنید، یا املای آن را بررسی کنید.", Empty = "هنوز چیزی برای نمایش نیست.", Loading = "در حال جست‌وجو…", Recent = "اخیر", Pinned = "سنجاق‌شده", Suggestions = "پیشنهادها", Modes = "حالت‌ها", Actions = "کارها", Results = "نتایج", Other = "سایر", Navigate = "برای حرکت", Select = "برای انتخاب", Close = "برای بستن", ClearQuery = "برای پاک کردن", Back = "برای بازگشت", OpenActions = "برای کارها", Toggle = "برای تغییر وضعیت", Apply = "برای اعمال", Pin = "سنجاق کردن به بالا", Unpin = "برداشتن سنجاق", RemoveFromRecents = "حذف از اخیر", ClearRecents = "پاک کردن فرمان‌های اخیر", ConfirmPrefix = "تأیید: ", ConfirmHint = "برای تأیید دوباره Enter و برای لغو Esc را بزنید", SelectedCount = "{0} مورد انتخاب شد", ResultsAnnouncement = "{0} نتیجه موجود است", OneResultAnnouncement = "یک نتیجه موجود است", NoResultsAnnouncement = "نتیجه‌ای نیست", Disabled = "در دسترس نیست", DialogLabel = "پالت فرمان", InputLabel = "جست‌وجوی فرمان‌ها", CloseButton = "بستن", BackButton = "بازگشت", Breadcrumb = "مسیر", OpenPalette = "باز کردن پالت فرمان", TriggerNameSeparator = "، ", Search = "جست‌وجو", Home = "خانه", MoreResults = "نمایش {0} مورد دیگر…", LoadMore = "نمایش بیشتر…", SequencePending = "در انتظار کلید بعدی…", CommandFailed = "اجرای «{0}» ممکن نشد.", CommandRefused = "اجازهٔ اجرای «{0}» را ندارید.", CommandUnavailable = "«{0}» دیگر در دسترس نیست.", LoadFailed = "این فهرست بارگذاری نشد.", Retry = "تلاش دوباره", ApproximateResults = "تطابق دقیقی پیدا نشد؛ نزدیک‌ترین‌ها نمایش داده می‌شوند.", Running = "در حال انجام…", SourceFailed = "جست‌وجو در {0} ممکن نشد", On = "روشن", Off = "خاموش", ShortcutThen = "سپس", }; private List fRtlCommands = []; private bool fRtlDark; protected override void OnInitialized() => fRtlCommands = FBuildRtlCommands(); private List FBuildRtlCommands() => [ BitCommandPaletteCmd.New("باز کردن تنظیمات").Id("f-rtl:settings").Group("عمومی").Keywords("Open settings") .Shortcut("G S", global: false) .Actions(BitCommandPaletteCmd.New("کپی کردن پیوند").Id("f-rtl:settings.copy") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("پیوند کپی شد (نه واقعاً)", keepOpen: true)))) .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("تنظیمات باز شد", keepOpen: true))), BitCommandPaletteCmd.New("حالت تاریک").Id("f-rtl:dark").Group("عمومی").Keywords("Dark mode") .Checked(fRtlDark) .Handler(_ => { fRtlDark = !fRtlDark; fRtlCommands = FBuildRtlCommands(); StateHasChanged(); return Task.FromResult(BitCommandPaletteCommandResult.KeepOpen); }), BitCommandPaletteCmd.New("Open in VS Code…").Id("f-rtl:vscode").Group("عمومی").Subtitle("ویرایشگر پیش‌فرض") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("در VS Code باز شد (نه واقعاً)", keepOpen: true))), BitCommandPaletteCmd.New("خروجی گرفتن…").Id("f-rtl:export").Group("عمومی").Keywords("Export").Subtitle("۲ قالب") .ChildrenPlaceholder("قالبی را انتخاب کنید…") .Children( BitCommandPaletteCmd.New("به صورت PDF").Id("f-rtl:export.pdf") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("خروجی PDF گرفته شد", keepOpen: true))), BitCommandPaletteCmd.New("به صورت CSV").Id("f-rtl:export.csv").Disabled(true, "به طرح Pro نیاز دارد")), BitCommandPaletteCmd.New("حذف همه فایل‌ها").Id("f-rtl:delete").Group("منطقه خطر").Keywords("Delete all files") .Destructive("همه فایل‌ها برای همیشه حذف شوند؟") .Handler(_ => Task.FromResult(BitCommandPaletteCommandResult.Toast("حذف شد (نه واقعاً)", keepOpen: true))), ]; ```