Skip to the content
human-kit design
13 — Form

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-field
npx @human-kit/design add search-field
yarn dlx @human-kit/design add search-field
bunx @human-kit/design add search-field

The 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 default variant in a form, and ghost where a box would crowd the row.
  • Use ghost at the head of a list or in a toolbar, where a box would weigh more than the list it filters.
  • Use underline among other underline fields, and nowhere else.
  • Keep the size of the fields and buttons beside it: sm beside small buttons, default beside 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-label or the label of a Field.Root.

Accessibility

  • The input is an <input type="search">, which has the searchbox role.
  • It has no visible label: name it with aria-label, or put it in a Field.Root, whose label names it through aria-labelledby.
  • Escape empties 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.
  • Enter calls onSubmit with the text.
  • The cross is not a tab stop, since Escape does the same. A press on it keeps the focus in the input.
  • invalid puts aria-invalid on the input. disabled takes it out of the tab order; readonly keeps it there.

API reference

PropTypeDefault
valuestring

The search text. Bindable.

—
defaultValuestring

The text at the start, when you give no value. A form reset goes back to it.

—
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'
placeholderstring

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 Enter.

—
onClear() => void

Called after Escape or the cross empties the input.

—
disabledboolean

Turns off the input and the cross.

false
readonlyboolean

The text cannot change: Escape and the cross do nothing, and the box goes.

false
requiredboolean

The form needs a value. The input gets the native required.

false
invalidboolean

Marks the value as wrong: the ring turns red.

false
namestring

The name the form submits the text with.

—
aria-labelstring

The name of the input, when there is no Field.Root around it.

—
aria-labelledbystring

The id of the element that names the input.

—
aria-describedbystring

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.