# GlobalSearchTextField

Search of the whole app, as a command palette: a button that looks like a search field opens a dialog with a search box and the results of `searcher`, grouped by `group`, with the searched text highlighted. Ctrl+K (⌘K on a Mac) opens and closes it from anywhere outside a text field; `shortcut={false}` turns that off. A search starts when the text reaches `searchThreshold` characters, `debounceTimeout` ms after the last keystroke, and a newer search aborts the older one through `signal`. The arrows move through the results, Enter opens the highlighted one: a result with `href` is a link, and choosing any result calls `onselect` and closes the dialog. On screens up to 768px wide the button shows only the magnifier and the dialog takes the whole screen. Its state is exposed as `data-open` and `data-compact` on the root and `data-state` (`idle`, `loading`, `results`, `empty`, `error`) on the dialog for app CSS.

```ts
import { GlobalSearchTextField } from '@likable-hair/svelte';
```

Category: composed. Source: `src/lib/components/composed/search/GlobalSearchTextField.svelte`.

## Examples

### Search

Click the field or press Ctrl+K (⌘K on a Mac). `searcher` receives the text and an abort `signal`; results with `href` are links, and `group` puts them under a title. Try "giu" or "agenda".

```svelte
<script lang="ts">
	import { GlobalSearchTextField, type GlobalSearchItem } from '@likable-hair/svelte';
	import { mdiAccountOutline, mdiCalendarOutline, mdiCogOutline } from '@mdi/js';

	const everything: GlobalSearchItem[] = [
		{ value: 'c1', label: 'Giulia Marini', description: '+39 333 812 4410 · Milano', icon: mdiAccountOutline, group: 'Clients', href: '#giulia' },
		{ value: 'c2', label: 'Giuseppe Verdi', description: 'Last visit 12 Aug', icon: mdiAccountOutline, group: 'Clients', href: '#giuseppe' },
		{ value: 'c3', label: 'Sara Bianchi', description: 'Last visit 3 Sep', icon: mdiAccountOutline, group: 'Clients', href: '#sara' },
		{ value: 'p1', label: 'Agenda · June', description: '/agenda/2026-06', icon: mdiCalendarOutline, group: 'Pages', href: '#agenda-june' },
		{ value: 'p2', label: 'Agenda · September', description: '/agenda/2026-09', icon: mdiCalendarOutline, group: 'Pages', href: '#agenda-september' },
		{ value: 'p3', label: 'Settings', description: '/settings', icon: mdiCogOutline, group: 'Pages', href: '#settings' }
	];

	async function searcher({ searchText, signal }: { searchText: string; signal: AbortSignal }) {
		await new Promise((resolve) => setTimeout(resolve, 200));
		signal.throwIfAborted();
		const text = searchText.toLowerCase();
		return everything.filter((item) => item.label.toLowerCase().includes(text));
	}

	let chosen = $state('');
</script>

<div class="demo">
	<GlobalSearchTextField {searcher} onselect={({ item }) => (chosen = item.label)} />
	<p>Last chosen: {chosen || 'none'}</p>
</div>

<style>
	.demo {
		display: grid;
		gap: 12px;
	}

	p {
		margin: 0;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `searcher` | `(params: { searchText: string; signal: AbortSignal }) => Promise<GlobalSearchItem<Data>[]>` |  | Returns the results for the text; `signal` aborts when a newer search starts or the dialog closes. Results are shown in the order they come, grouped by `group`. |
| `open` (bindable) | `boolean` |  | Whether the dialog is open. |
| `label` | `string` |  | Text of the button, also the name of the search box. Defaults to "Search", in the language of `locale`. |
| `placeholder` | `string` |  | Placeholder of the search box. Defaults to "Search…", in the language of `locale`. |
| `shortcut` | `string \| false` | `'k'` | Letter that opens the dialog together with Ctrl (⌘ on a Mac). `false` turns the shortcut off. |
| `searchThreshold` | `number` | `1` | Characters needed to start a search. |
| `debounceTimeout` | `number` | `300` | Milliseconds after the last keystroke before searching. |
| `emptyText` | `string` |  | Text shown when a search finds nothing; `{text}` is the searched text. Defaults to "No results for “{text}”", in the language of `locale`. |
| `errorText` | `string` |  | Text shown when `searcher` fails. Defaults to "The search failed. Try again.", in the language of `locale`. |
| `cancelLabel` | `string` |  | Text of the button that closes the dialog on screens up to 768px wide, where there is no Escape key. Defaults to "Cancel", in the language of `locale`. |
| `locale` | `string` |  | BCP 47 locale of the texts: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `class` | `{ container?: string; button?: string; shortcut?: string; dialog?: string }` | `{}` | Extra classes for each part. |
| `buttonElement` (bindable) | `HTMLButtonElement` |  | The button that opens the dialog. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `triggerSnippet` | `{ open: () => void; shortcut: string }` | Replaces the button. Call `open` to open the dialog; `shortcut` is the text of the shortcut, such as "⌘K". |
| `itemSnippet` | `{ item: GlobalSearchItem<Data>; active: boolean; parts: TextPart[] }` | Replaces the content of a result row. `parts` is the label split into highlighted and plain pieces. |
| `groupSnippet` | `{ group: string; items: GlobalSearchItem<Data>[] }` | Replaces the title of a group. |
| `emptySnippet` | `{ searchText: string }` | Replaces the message shown when nothing is found. |
| `footerSnippet` |  | Replaces the bar under the results with the keyboard hints. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onselect` | `(selection: { item: GlobalSearchItem<Data>; event: MouseEvent \| KeyboardEvent }) => void` | Called when a result is chosen, with the result and the click or key event, before the dialog closes (and before a link navigates). |
| `onclose` | `() => void` | Called whenever the dialog closes. |
| `onerror` | `(error: unknown) => void` | Called when `searcher` throws or rejects, except for aborted searches. |

## Types

Exported by the package: `import type { GlobalSearchItem } from '@likable-hair/svelte';`

### GlobalSearchItem

A result of `GlobalSearchTextField`.

`interface GlobalSearchItem<Data = unknown>`

| Name | Type | Description |
| --- | --- | --- |
| `value` (required) | `string \| number` | Identity of the result, unique among the results. |
| `label` (required) | `string` | Main text, with the searched text highlighted. |
| `description` | `string` | Secondary text under the label, such as a phone number or a path. |
| `icon` | `string` | SVG path of the icon in the tile at the start of the row. |
| `href` | `string` | Makes the result a link: choosing it opens this URL. |
| `group` | `string` | Name of the section of the result, such as "Clients" or "Pages". Results with the same group are shown together, in the order of their first one. |
| `data` | `Data` | Any app data, passed back to the snippets and to `onselect`. |

## CSS variables

For a single instance, pass them as style props: `<GlobalSearchTextField --global-search-text-field-background="tomato">`. The `--global-search-text-field-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--global-search-text-field-width` | `--global-search-text-field-default-width`: `100%` |
| `--global-search-text-field-max-width` | `--global-search-text-field-default-max-width`: `220px` |
| `--global-search-text-field-height` | `--global-search-text-field-default-height`: `32px` |
| `--global-search-text-field-padding` | `--global-search-text-field-default-padding`: `0 6px 0 10px` |
| `--global-search-text-field-border-color` | `--global-search-text-field-default-border-color`: `var(--global-input-border-color)` |
| `--global-search-text-field-border-radius` | `--global-search-text-field-default-border-radius`: `var(--global-input-radius)` |
| `--global-search-text-field-background` | `--global-search-text-field-default-background`: `var(--global-input-background)` |
| `--global-search-text-field-color` | `--global-search-text-field-default-color`: `var(--global-input-placeholder-color)` |
| `--global-search-text-field-font-size` | `--global-search-text-field-default-font-size`: `13px` |
| `--global-search-text-field-hover-border-color` | `--global-search-text-field-default-hover-border-color`: `var(--global-input-hover-border-color)` |
| `--global-search-text-field-icon-color` | `--global-search-text-field-default-icon-color`: `var(--global-input-icon-color)` |
| `--global-search-text-field-icon-size` | `--global-search-text-field-default-icon-size`: `16px` |
| `--global-search-text-field-kbd-border-color` | `--global-search-text-field-default-kbd-border-color`: `var(--global-color-border)` |
| `--global-search-text-field-kbd-background` | `--global-search-text-field-default-kbd-background`: `var(--global-color-surface-2)` |
| `--global-search-text-field-kbd-color` | `--global-search-text-field-default-kbd-color`: `var(--global-color-text-3)` |
| `--global-search-text-field-kbd-font-size` | `--global-search-text-field-default-kbd-font-size`: `11px` |
| `--global-search-text-field-dialog-width` | `--global-search-text-field-default-dialog-width`: `560px` |
| `--global-search-text-field-dialog-max-height` | `--global-search-text-field-default-dialog-max-height`: `min(560px, 80dvh)` |
| `--global-search-text-field-dialog-top` | `--global-search-text-field-default-dialog-top`: `12dvh` |
| `--global-search-text-field-box-height` | `--global-search-text-field-default-box-height`: `52px` |
| `--global-search-text-field-divider-color` | `--global-search-text-field-default-divider-color`: `var(--global-color-border)` |
| `--global-search-text-field-box-icon-color` | `--global-search-text-field-default-box-icon-color`: `var(--global-color-primary)` |
| `--global-search-text-field-box-font-size` | `--global-search-text-field-default-box-font-size`: `15px` |
| `--global-search-text-field-group-color` | `--global-search-text-field-default-group-color`: `var(--global-color-text-3)` |
| `--global-search-text-field-group-font-size` | `--global-search-text-field-default-group-font-size`: `10.5px` |
| `--global-search-text-field-option-active-background` | `--global-search-text-field-default-option-active-background`: `var(--global-color-primary-soft)` |
| `--global-search-text-field-option-icon-background` | `--global-search-text-field-default-option-icon-background`: `var(--global-color-surface-2)` |
| `--global-search-text-field-option-icon-color` | `--global-search-text-field-default-option-icon-color`: `var(--global-color-text-2)` |
| `--global-search-text-field-option-active-icon-background` | `--global-search-text-field-default-option-active-icon-background`: `var(--global-color-primary)` |
| `--global-search-text-field-option-active-icon-color` | `--global-search-text-field-default-option-active-icon-color`: `var(--global-color-on-primary)` |
| `--global-search-text-field-mark-color` | `--global-search-text-field-default-mark-color`: `var(--global-color-primary)` |
| `--global-search-text-field-description-color` | `--global-search-text-field-default-description-color`: `var(--global-color-text-3)` |
