ContentSearch
A documentation search: a command palette in a modal over a list of pages and their headings, opened by a shortcut.
Examples
Pages and headings
Ctrl+K (Cmd+K on a Mac) opens the search of this site, over the whole catalog. This example has its own Name and shortcut. Filtering and the shortcut run in the browser.
Last result: none
@* The search of this site (Ctrl+K, or the button in the header) is a ContentSearch over the catalog. This one has its own
Name and shortcut, so it opens next to it: use the button, or Alt+O. Pages are listed with their headings. *@
<div class="flex flex-col items-start gap-3" data-testid="content-search-example">
<KlyContentSearchButton For="example" Collapsed="false" Keys="_keys" data-testid="content-search-example-button" />
<p class="text-sm text-(--klyroo-text-muted)" data-testid="content-search-example-last">Last result: @(_last ?? "none")</p>
<KlyContentSearch Name="example" Shortcut="alt+o" Pages="_pages" OnSelect="item => _last = item.Label"
Label="Search the example docs" Placeholder="Search the example docs..." />
</div>
@code {
private string? _last;
private static readonly string[] _keys = ["Alt", "O"];
private static readonly KlyContentSearchPage[] _pages =
[
new("Introduction", "?section=introduction")
{
Group = "Getting started",
Description = "What klyroo-ui is",
Headings = [new("Why another library", "why"), new("What you get", "features")],
},
new("Installation", "?section=installation")
{
Group = "Getting started",
Description = "Add the package",
Keywords = "nuget dotnet add package",
Headings = [new("Register the services", "services")],
},
new("Theming", "?section=theming") { Group = "Guides", Description = "Colors, radius and fonts", Keywords = "dark mode tokens css" },
];
}Index loaded when it opens
With IndexUrl the pages are a JSON file that the browser fetches the first time the search opens, so a large index is not part of every page. The button here, or Alt+L, opens it. The fetch is shown while it runs, and a failure says so with a button to try again.
Last result: none
@* The pages are not in this component: IndexUrl names a JSON file, which the browser fetches the first time the search opens (the button here, or
Alt+L) and keeps for the page. A relative address is resolved against <base href>. Try it with the network tab open: nothing is requested until
the first open. *@
<div class="flex flex-col items-start gap-3" data-testid="content-search-lazy-example">
<KlyContentSearchButton For="lazy" Collapsed="false" Keys="_keys" data-testid="content-search-lazy-button" />
<p class="text-sm text-(--klyroo-text-muted)" data-testid="content-search-lazy-last">Last result: @(_last ?? "none")</p>
<KlyContentSearch Name="lazy" Shortcut="alt+l" IndexUrl="examples/lazy-search-index.json" OnSelect="item => _last = item.Label"
Label="Search the lazy docs" Placeholder="Search the lazy docs..." />
</div>
@code {
private string? _last;
private static readonly string[] _keys = ["Alt", "L"];
}API
KlyContentSearch
Documentation search: a command palette in a modal over a list of pages and headings, opened by a keyboard shortcut or a KlyContentSearchButton .
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
IndexUrl |
string |
The address of a JSON file with the pages to search, instead of (or in addition to) Pages : an array of {"title", "href", "description"?, "group"?, "keywords"?, "headings"?: [{"text", "id"}]} , the fields of KlyContentSearchPage . The browser fetches it the first time the search opens, shows a loading message, keeps the result for the page and, when the fetch fails, says so in a live region with a button to try again. It is resolved against <base href> , so a relative address works below a path base, and it must be on the same origin as the page. Works in every render mode, because the browser does it. | |
Label |
string |
Accessible name of the dialog and the search field. Defaults to the built-in text. | |
Name |
string |
"" |
Names this search so that a KlyContentSearchButton with the same For opens it. Needed only when a page has more than one search; the unnamed one is the default. |
Open (@bind-Open) |
bool |
false |
Open state. Use @bind-Open ; the browser opens and closes the search on its own. |
Pages |
IReadOnlyList |
The pages to search, written into the page. A page is listed with its headings, under the heading of its Group . For a long list use IndexUrl instead. | |
Placeholder |
string |
Hint of the search field. Defaults to the built-in text. | |
Shortcut |
string |
"mod+k" |
The keyboard shortcut that opens the search: "mod+k" by default, where mod is Cmd on Apple platforms and Ctrl elsewhere. Ctrl, meta, alt and shift are accepted too. |
Ui |
CommandPaletteUi |
Slot overrides of the command palette. |
Events
| Name | Type | Description |
|---|---|---|
OnSelect |
EventCallback |
Called with the activated command. Pages and headings navigate as well. The search closes in either case. |
Ui slots
Ui takes a CommandPaletteUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
InputWrapper |
Classes merged into the inputWrapper slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
InputIcon |
Classes merged into the inputIcon slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
Input |
Classes merged into the input slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
Listbox |
Classes merged into the listbox slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
Group |
Classes merged into the group slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
GroupLabel |
Classes merged into the groupLabel slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
Item |
Classes merged into the item slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
ItemIcon |
Classes merged into the itemIcon slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
ItemWrapper |
Classes merged into the itemWrapper slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
ItemLabel |
Classes merged into the itemLabel slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
ItemDescription |
Classes merged into the itemDescription slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
ItemShortcut |
Classes merged into the itemShortcut slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
ItemCount |
Classes merged into the itemCount slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
Empty |
Classes merged into the empty slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |
Status |
Classes merged into the status slot of KlyCommandPalette , KlyContentSearch and KlyDashboardSearch , after the theme and the app configuration. |