Autocomplete is also available as an experimental widget in InstantSearch, making it easier to integrate into your search experience.
To learn more, see the API reference for InstantSearch.js or React InstantSearch.
Installation
First, you need to install the package.JavaScript
script element:
HTML
Example
Make sure to define an empty container in your HTML where to inject your autocomplete.JavaScript
algoliasearch API client.
All Algolia utility functions to retrieve hits and parse results are available directly in the package.
Parameters
The container for the Autocomplete search box.
You can either pass a CSS selector
or an Element.
If there are several containers matching the selector,
Autocomplete picks up the first one.
The container for the Autocomplete panel.
You can either pass a CSS selector
or an Element.
If there are several containers matching the selector,
Autocomplete picks up the first one.
The panel’s horizontal position.
Whether to enable the Algolia Insights plugin.This option accepts an object to configure the plugin.
You can see the available options in the plugin’s documentation.If you don’t pass an
insightsClient,
it will be automatically detected from the window object,
or downloaded from the jsDelivr CDN.If you manually enable the Insights plugin, this option won’t have any effect.Type definition
Class names to inject for each created DOM element.
This is useful to style your autocomplete with external CSS frameworks.
Type definition
components
Components to register in the Autocomplete rendering lifecycles.
Registered components become available in Four components are registered by default:
templates,
render,
and in renderNoResults.Highlightto highlight matches in Algolia results.Snippetto snippet matches in Algolia results.ReverseHighlightto reverse highlight matches in Algolia results.ReverseSnippetto reverse highlight and snippet matches in Algolia results.
Type definition
JavaScript
sections, which holds the components tree of your autocomplete,
to customize the wrapping layout.elements instead to pick which source to display based on its sourceId.Type definition
JavaScript
renderer
The virtual DOM implementation to plug to Autocomplete.
It defaults to Preact.
The detached mode turns the dropdown display into a full screen, modal experience.For more information, see Detached mode.
autocomplete function also accepts all the props that createAutocomplete supports:
The function called to reshape the sources after they’re resolved.This is useful to transform sources before rendering them.
You can group sources by attribute, remove duplicates, create shared limits between sources, etc.See Reshaping sources for more information.
TypeScript
Whether to enable the Algolia Insights plugin.This option accepts an object to configure the plugin.
You can see the available options in the plugin’s documentation.If you don’t pass an
insightsClient,
it will be automatically detected from the window object,
or downloaded from the jsDelivr CDN.If you manually enable the Insights plugin, this option won’t have any effect.An ID for the autocomplete to create accessible attributes.
The function called when the internal state changes.
The action label or icon to present for the enter key on virtual keyboards.
Whether to update the search input value in the middle of a composition session.
This is useful when users need to search using non-latin characters.
The placeholder text to show in the search input when there’s no query.
Whether to focus the search input or not when the page is loaded.
The default item index to pre-select.You should use
0 when the autocomplete is used to open links,
instead of triggering a search in an application.Whether to open the panel on focus when there’s no query.
How many milliseconds must elapse before considering the autocomplete experience stalled.
The initial state to apply when autocomplete is created.
The environment in which your application is running.This is useful if you’re using autocomplete in a different context than
window.An implementation of Autocomplete’s Navigator API to redirect users when opening a link.Learn more on the Navigator API documentation.
The function called to determine whether the panel should open or not.By default, the panel opens when there are items in the state.
The function called when submitting the Autocomplete form.
The function called when resetting the Autocomplete form.
A flag to activate the debug mode.This is useful while developing because it keeps the panel open even when the blur event occurs.
Make sure to turn it off in production.See Debugging for more information.
plugins
The plugins that encapsulate and distribute custom Autocomplete behaviors.See Plugins for more information.
Components
Autocomplete exposescomponents
to all templates to share them everywhere in the instance.
Highlight
The Algolia hit whose attribute to retrieve the highlighted parts from.
The attribute to retrieve the highlighted parts from.
The tag name to use for highlighted parts.
Snippet
The Algolia hit whose attribute to retrieve the snippeted parts from.
The attribute to retrieve the snippeted parts from.
The tag name to use for snippeted parts.
ReverseHighlight
The Algolia hit whose attribute to retrieve the reverse highlighted parts from.
The attribute to retrieve the reverse highlighted parts from.
The tag name to use for reverse highlighted parts.
ReverseSnippet
The Algolia hit whose attribute to retrieve the reverse snippeted parts from.
The attribute to retrieve the reverse snippeted parts from.
The tag name to use for reverse snippeted parts.
Returns
Theautocomplete function returns state setters
and a refresh method that updates the UI state with fresh sources.
These setters are useful to control the autocomplete experience from external events.
JavaScript
autocomplete function returns state setters and helpers:
Updates the UI state with fresh sources.
You must call this function whenever you mutate the state with setters and want to reflect the changes in the UI.
Updates the Autocomplete instance with new options.
Destroys the Autocomplete instance and removes it from the DOM.