Search Field
A box for a search query, with a magnifier, a cross that empties it and Escape that does the same.
Installation
pnpm dlx @human-kit/design add search-fieldnpx @human-kit/design add search-fieldyarn dlx @human-kit/design add search-fieldbunx @human-kit/design add search-fieldThe command copies the files to src/lib/components/ui/search-field. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as SearchField from '$lib/components/ui/search-field';
let query = $state('');
</script>
<SearchField.Root aria-label="Search patients" placeholder="Name or ID" bind:value={query} /> Guidelines
- Use it to narrow what is already on the screen, or to start a search. For a value the user picks from a list, use a combobox.
- Use the
defaultvariant in a form, andghostwhere a box would crowd the row. - Use
ghostat the head of a list or in a toolbar, where a box would weigh more than the list it filters. - Use
underlineamong other underline fields, and nowhere else. - Keep the size of the fields and buttons beside it:
smbeside small buttons,defaultbeside the default ones. - Say in the placeholder what the search matches, as "Name or ID". It is not the name of the field: that goes in
aria-labelor the label of aField.Root.
Accessibility
- The input is an
<input type="search">, which has thesearchboxrole. - It has no visible label: name it with
aria-label, or put it in aField.Root, whose label names it througharia-labelledby. Escapeempties the text and keeps the key in the field. When the text is already empty, the key goes on, thus a dialog around the field can close.EntercallsonSubmitwith the text.- The cross is not a tab stop, since
Escapedoes the same. A press on it keeps the focus in the input. invalidputsaria-invalidon the input.disabledtakes it out of the tab order;readonlykeeps it there.
API reference
| Prop | Type | Default |
|---|---|---|
value | string The search text. Bindable. | — |
defaultValue | string The text at the start, when you give no | — |
variant | 'default' | 'filled' | 'ghost' | 'underline' The box: white, none until used, or a line under the text. | 'default' |
size | 'sm' | 'default' | 'lg' The height, the same as the button of that size. | 'default' |
placeholder | string The text in the empty input: what the search matches. | — |
onChange | (value: string) => void Called when the text changes. | — |
onSubmit | (value: string) => void Called with the text when the user presses | — |
onClear | () => void Called after | — |
disabled | boolean Turns off the input and the cross. | false |
readonly | boolean The text cannot change: | false |
required | boolean The form needs a value. The input gets the native | false |
invalid | boolean Marks the value as wrong: the ring turns red. | false |
name | string The name the form submits the text with. | — |
aria-label | string The name of the input, when there is no | — |
aria-labelledby | string The id of the element that names the input. | — |
aria-describedby | string The id of the element that describes the input, as an error message. | — |
Plus every attribute of a <div>, which go on the box. Its id goes on the box too, and the input gets that id plus -input.