# SearchBar

Search field: an `<input type="search">` in a `role="search"` landmark, with the magnifier, a button that empties it and an optional search button. `onsearch` receives the text `debounce` milliseconds after the user stops typing (unless `searchOnType` is `false`), at once on Enter, on the search button and when the field is emptied; `oninput` and the other native events reach the input as usual. `loading` shows a spinner while results arrive. Name it with `label` (or `aria-label`), since the placeholder is not a label. Its state is exposed as `data-filled` and `data-loading` for app CSS.

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

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

## Examples

### Search as you type

`onsearch` receives the text 300ms after the last keystroke (`debounce`), at once on Enter and when the field is emptied with the × or Escape. `loading` shows a spinner.

```svelte
<script lang="ts">
	import { SearchBar } from '@likable-hair/svelte';

	const clients = ['Giulia Marini', 'Luca Rossi', 'Sara Bianchi', 'Giuseppe Verdi', 'Marta Neri'];

	let value = $state('');
	let loading = $state(false);
	let results = $state(clients);

	function onsearch(text: string) {
		loading = true;
		setTimeout(() => {
			results = clients.filter((client) => client.toLowerCase().includes(text.toLowerCase()));
			loading = false;
		}, 400);
	}
</script>

<div class="example">
	<SearchBar bind:value label="Search clients" placeholder="Search clients" {loading} {onsearch} />
	<ul>
		{#each results as client (client)}
			<li>{client}</li>
		{/each}
	</ul>
</div>

<style>
	.example {
		display: grid;
		gap: 12px;
		width: min(420px, 100%);
	}

	ul {
		margin: 0;
		padding-inline-start: 20px;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

### With a search button

`buttonLabel` adds a button at the end, as in the prototype; with `searchOnType={false}` the search runs only on Enter, on the button and when the field is emptied.

```svelte
<script lang="ts">
	import { SearchBar } from '@likable-hair/svelte';

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

<div class="example">
	<SearchBar
		label="Search"
		placeholder="Search clients, services, products…"
		buttonLabel="Search"
		searchOnType={false}
		onsearch={(text) => (searched = text)}
		--search-bar-height="48px"
	/>
	<p>{searched ? `Searched: ${searched}` : 'Nothing searched yet'}</p>
</div>

<style>
	.example {
		display: grid;
		gap: 12px;
		width: min(420px, 100%);
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (bindable) | `string` |  | The text. |
| `label` | `string` |  | Accessible name of the field (and of the search landmark). Defaults to the placeholder. |
| `placeholder` | `string` |  | Native `placeholder`. |
| `debounce` | `number` | `300` | Milliseconds between the last keystroke and `onsearch`. `0` calls it at every change. |
| `searchOnType` | `boolean` | `true` | Calls `onsearch` while typing. With `false` only Enter, the search button and emptying the field do. |
| `clearable` | `boolean` | `true` | Shows a button that empties the field while it has text. |
| `clearLabel` | `string` |  | Accessible name of the clear button. Defaults to "Clear search", 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`. |
| `loading` | `boolean` | `false` | Shows a spinner at the end of the field, for example while results load. |
| `buttonLabel` | `string` |  | Text of a search button at the end of the field. Without it there is no button. |
| `input` (bindable) | `HTMLInputElement` |  | The native `<input>`. |
| `class` | `{ container?: string; icon?: string; input?: string }` | `{}` | Extra classes for each part. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `iconSnippet` |  | Replaces the magnifier. |
| `inputSnippet` |  | Replaces the input (for example with a field of the app). |
| `buttonSnippet` | `{ search: () => void }` | Replaces the search button. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onsearch` | `(text: string) => void` | Called with the text: after `debounce`, on Enter, on the search button and when the field is emptied. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--search-bar-ring-color` | `--search-bar-default-ring-color`: `var(--global-input-border-color)` |
| `--search-bar-icon-size` | `--search-bar-default-icon-size`: `18px` |
| `--search-bar-gap` | `--search-bar-default-gap`: `8px` |
| `--search-bar-width` | `--search-bar-default-width`: `100%` |
| `--search-bar-max-width` | `--search-bar-default-max-width`: `none` |
| `--search-bar-height` | `--search-bar-default-height`: `40px` |
| `--search-bar-padding` | `--search-bar-default-padding`: `0 6px 0 12px` |
| `--search-bar-border-radius` | `--search-bar-default-border-radius`: `var(--global-input-radius)` |
| `--search-bar-background-color` | `--search-bar-default-background-color`: `var(--global-input-background)` |
| `--search-bar-ring-width` | `--search-bar-default-ring-width`: `var(--global-input-border-width)` |
| `--search-bar-icon-color` | `--search-bar-default-icon-color`: `var(--global-input-icon-color)` |
| `--search-bar-hover-ring-color` | `--search-bar-default-hover-ring-color`: `var(--global-input-hover-border-color)` |
| `--search-bar-focus-ring-color` | `--search-bar-default-focus-ring-color`: `var(--global-input-focus-border-color)` |
| `--search-bar-focus-glow-color` | `--search-bar-default-focus-glow-color`: `var(--global-input-focus-ring-color)` |
| `--search-bar-color` | `--search-bar-default-color`: `var(--global-input-color)` |
| `--search-bar-font-size` | `--search-bar-default-font-size`: `var(--global-input-font-size)` |
| `--search-bar-line-height` | `--search-bar-default-line-height`: `1.5` |
| `--search-bar-placeholder-color` | `--search-bar-default-placeholder-color`: `var(--global-input-placeholder-color)` |
