SearchField
This is a text input for a search query. The Escape key and a clear button empty the text, and the Enter key submits the query.
Value: ""
Anatomy
SearchField.Root holds the text and gives the state to each part. Label names the input. Input is the native search input. Clear is the button that empties the text.
<script>
import { SearchField } from '@human-kit/ui';
</script>
<SearchField.Root>
<SearchField.Label />
<SearchField.Input />
<SearchField.Clear />
</SearchField.Root>Submit and clear
onSubmit receives the text when the user presses Enter. onClear tells you when the Escape key or the clear button empties the text. Inside a form, Enter also submits the form, and name on Root gives the text a name in the form data.
Usage guidelines
- Give the input an accessible name. Use
SearchField.Label, or putaria-labeloraria-labelledbyonSearchField.Input. - Use
bind:valuefor the state in the two directions. The value is always a string. - Use the
data-emptyattribute to hide the clear button while the text is empty. Usevisibility: hiddenand notdisplay: none, thus the layout does not move. - Some browsers show their own clear button in a search input. Hide it with the
::-webkit-search-cancel-buttonpseudo-element if you showSearchField.Clear.
Accessibility
SearchField.Inputmakes an<input type="search">, which has thesearchboxrole. It also setsenterkeyhint="search"for the virtual keyboard.- The
Escapekey empties the text and stops the key. When the text is already empty, the key goes on, thus a dialog or a popover around the field can close. - A key in an IME composition belongs to the composition. Thus
EscapeandEnterdo not clear or submit while the user composes text. - The clear button is not in the tab order, because the
Escapekey does the same operation. A screen reader can still find it, andaria-controlspoints it at the input. - A press on the clear button keeps the focus in the input. On a phone, the virtual keyboard stays open.
- The name of the clear button is "Clear search", in the language of the
LocaleProvider. Put anaria-labelonSearchField.Clearto change it.
API reference
Root
The container of the search field. It holds the search text and the state, and it gives the context to all of the parts.
* required. Native HTML attributes of the underlying element are also accepted.
Label
A native label for the input. A click on it moves the focus to the input.
* required. Native HTML attributes of the underlying element are also accepted.
Input
The native search input. It holds the text, and it handles the `Escape` key and the `Enter` key.
* required. Native HTML attributes of the underlying element are also accepted.
Clear
The button that empties the search text. It is not in the tab order, and a press on it keeps the focus in the input. It is disabled while the text is empty.
* required. Native HTML attributes of the underlying element are also accepted.