Before you begin
This guide assumes that you know HTML, CSS, and JavaScript and that you have existing HTML with an input element where you want to insert the autocomplete drop-down menu.Get started
First, begin with some code for the Autocomplete implementation. Create a file calledindex.js
in your src
directory, and add:
JavaScript
autocomplete
as an id
.
You should change the container
to match your markup.
Setting openOnFocus
to true
ensures that the drop-down menu appears as soon as a user focuses the input.
Implement the empty query state
JavaScript
getSources
function provides access to the current query
, which you can use to return sources conditionally.
You can use this pattern to display predefined items like these, recent searches, and Query Suggestions when the query is empty.
When users begin to type, you can show search results.
When there isn’t a query, this Autocomplete instance returns useful links.
When there is, it searches an Algolia index.
For more information about using Algolia as a source, see Get started with Autocomplete.
Implement the no results search state
Now that you can display a custom source when the query is empty, you can display a message if the query returned no results.JavaScript
renderNoResults
function provides a way to render a no results section when there are no hits.
You have access to the full Autocomplete state.
It lets you compute sources based on various parameters,
such as the query, Autocomplete status, whether the Autocomplete display panel is open or not,
and the context.