- Read the Core concepts to learn more about underlying principles, like Sources and State.
- Follow the Guides to understand how to build common UX patterns.
- Refer to the API reference for a comprehensive list of parameters and options.
- Try out the Playground where you can fork a basic implementation and play around.
Installation
The recommended way to get started is with theautocomplete-js
package. It includes everything you need to render a JavaScript autocomplete experience.
Otherwise, you can install the autocomplete-core
package if you want to build a renderer from scratch.
All Autocomplete packages are available on the npm registry.
HTML
autocomplete-js
is also available through unpkg.
Install the Autocomplete classic theme
The Autocomplete library provides theautocomplete-theme-classic
package so that you can have sleek styling out of the box.
If you want a custom theme, use this classic theme and customize it with CSS variables. You can also create an entirely new theme using the classic theme as a starting point.
This example uses the out of the box classic theme. You can import it like any other Autocomplete package.
JavaScript
HTML
Algolia doesn’t provide support regarding third party services like jsDelivr or other CDNs.
Define where to put your autocomplete
To get started, you need a container for your autocomplete to go in. If you don’t have one already, you can insert one into your markup:HTML
autocomplete
function and providing the container
. It can be a CSS selector or an Element.
Make sure to provide a container (for example, a div
), not an input
. Autocomplete generates a fully accessible search box for you.
JavaScript
placeholder
and getSources
.
The placeholder
option defines the text to show until users start typing in the input.
Autocomplete is now plugged in. But you won’t see anything appear until you define your sources.
Define what items to display
Sources define where to retrieve the items to display in your Autocomplete drop-down menu. You define your sources in thegetSources
function by returning an array of source objects.
Each source object needs to include a sourceId
and a getItems
function that returns the items to display. Sources can be static or dynamic.
This example uses an Algolia index of ecommerce products as a source. The autocomplete-js
package provides a built-in getAlgoliaResults
function for just this purpose.
JavaScript
getAlgoliaResults
function requires an Algolia search client
initialized with an Algolia application ID and API key.
It lets you search into your Algolia index using an array of queries
, which defines one or more queries to send to the index.
This example makes just one query to the “autocomplete” index using the query
from getSources
. For now, it passes one additional parameter, hitsPerPage
to define how many items to display, but you could pass any other Algolia query parameters.
Although you’ve now declared what items to display using getSources
, you still won’t see anything until you’ve defined how to display the items you’ve retrieved.
Define how to display items
Sources also define how to display items in your Autocomplete usingtemplates
. Templates can return a string or anything that’s a valid Virtual DOM element. The example creates a Preact component called ProductItem
to use as the template for each item.
The CSS classes correspond to the classic theme imported earlier.
ProductItem
component uses the Snippet
component to only display part of the item’s name and description,
if they go beyond a certain length.
Each attribute’s allowed length and the characters to show when truncated are defined in the attributesToSnippet
and snippetEllipsisText
Algolia query parameters in params
.
This is what the truncated JSON record looks like:
JSON
Keep the panel open during development
When you’re building, styling, or debugging your autocomplete experience, you might want to inspect it in your web developer tools. Set thedebug
option to true
to keep the panel open when inspecting it.
Only use the
debug
option during development.Send click and conversion events
To send click and conversion events when users interact with your autocomplete experience, set theinsights
option to true
.
JavaScript
Browser support
Algolia supports the last two versions of the major browsers: Chrome, Edge, Firefox, Safari.Going further
This is all you need for a basic implementation. To go further, you can use thegetItemUrl
to add keyboard accessibility features.
It lets users open items directly from the autocomplete menu.
JavaScript
- Define templates for headers, footers, or when there’s no results
- Add multiple sources, including suggested searches and recent searches
- Send Algolia Insights events when a user clicks on an item or adds it to their cart