Search as you type
Many websites implement search as you type for their search engine. Search as you type is a UX pattern that makes suggestions to the user while typing a search query and before she clicks the submit button.
The first example that comes to my mind is Google’s search bar:
From Google’s examples, there are a few things to note:
- While the user types text on the input, it computes suggestions based on the query and displays them below the input (often called a combobox).
- When the user clicks outside the input, the suggestions disappear and reappear when the user focuses the input again.
Google’s search bar works well because results are computed and returned fast. It will make an HTTP request for each keystroke, even if the user corrects a typo in between. The response to these requests could take some time due to network latency.
Implementing a robust system against latencies shouldn’t be an afterthought; the example below takes it seriously. The consequences are the following:
- We’ll debounce the request. The request will be started only 500ms after the user stops typing to reduce the number of requests being made.
- If the user types in the input while a request is pending, the response will be ignored, and a new request will be made 500ms later.
Example
Try to type any character in the input. It will autocomplete based on its tiny database.
Type more characters while debouncing or fetching happens. Try to break the machine!
Code
Get news from XState by Example
Sign up for the newsletter to be notified when more machines or an interactive tutorial are released. I respect your privacy and will only send emails once in a while.