JavaScript
autocomplete widget guide.
It creates a federated autocomplete experience with recent searches and Query Suggestions on the left, and product previews on the right.
This widget is and is subject to change in minor versions.
- Desktop
- Mobile

Open CodeSandbox
Run and edit the federated autocomplete example in CodeSandbox.
Explore source code
Browse the source code for the federated autocomplete example on GitHub.
The sections below build each piece, then combine them in a single
panel template.
The panel template arranges the sources into two columns, and each source brings its own header and item templates:
JavaScript
- Two-column grids
- Product rows
- Quick-access cards
- “See all” and no-results states
Add a quick-access panel
You can show promotional cards or shortcuts when the query is empty. Define them as static data and render them in thepanel template.
When users type a query, show product previews instead:
JavaScript
For dynamic cards, return custom data from an Algolia rule.
In the
panel template, read the data from results.userData for the products index instead of using a static array.Add a category suggestion
To show a category suggestion in the left column, find the products index and read the top product’scategories from results.hits[0] in the panel template.
Guard against a missing or empty categories attribute, then render the category after the suggestions:
JavaScript
Show a “See all” link
The index source has nofooter template.
To show a See all link with the total number of matching results,
read the products index’s results.nbHits in the panel template and render the link after the product previews:
JavaScript
Show the no-results state
When a result source has no items, the widget doesn’t render itsitem template.
Use the source’s noResults template to show a message such as “No products found”, as shown in the main example.
Combine the empty and typing states
The finalpanel template combines two states:
- For an empty query, show recent and popular searches in the left column and quick-access cards in the right column.
- After users enter a query, show Query Suggestions and a category in the left column and product previews in the right column.
- Remove the
headertemplate fromshowQuerySuggestions. - Render the suggestions heading in the
paneltemplate. Use “Popular searches” for an empty query and “Suggestions” for a non-empty query. - Show the heading only when the Query Suggestions source has results.
panel template, see src/app.js.
See also
- Autocomplete guide for the widget basics.
- Rich text box with mentions for an example of a headless, connector-based pattern.
