What is @algolia/autocomplete-js?
@algolia/autocomplete-js is a JavaScript library that provides a highly customizable autocomplete experience. It is designed to be flexible and easy to integrate with various data sources, including Algolia's search API. The package allows developers to create powerful and responsive autocomplete interfaces with minimal effort.
What are @algolia/autocomplete-js's main functionalities?
Basic Autocomplete Setup
This code sets up a basic autocomplete interface with a static list of items. The `autocomplete` function initializes the autocomplete widget, and the `getSources` method defines the data sources for the autocomplete suggestions.
```javascript
import { autocomplete } from '@algolia/autocomplete-js';
autocomplete({
container: '#autocomplete',
getSources() {
return [
{
sourceId: 'example-source',
getItems() {
return [
{ label: 'Item 1' },
{ label: 'Item 2' },
{ label: 'Item 3' }
];
},
templates: {
item({ item }) {
return item.label;
}
}
}
];
}
});
```
Using Algolia Search API
This example demonstrates how to integrate the autocomplete widget with Algolia's search API. The `getItems` method performs a search query using Algolia's client and returns the search results as autocomplete suggestions.
```javascript
import { autocomplete } from '@algolia/autocomplete-js';
import algoliasearch from 'algoliasearch/lite';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
autocomplete({
container: '#autocomplete',
getSources() {
return [
{
sourceId: 'products',
getItems({ query }) {
return searchClient
.initIndex('products')
.search(query)
.then(({ hits }) => hits);
},
templates: {
item({ item }) {
return item.name;
}
}
}
];
}
});
```
Customizing Autocomplete Templates
This code sample shows how to customize the templates used for rendering autocomplete suggestions. The `item` template is modified to include both a label and a description for each suggestion.
```javascript
import { autocomplete } from '@algolia/autocomplete-js';
autocomplete({
container: '#autocomplete',
getSources() {
return [
{
sourceId: 'example-source',
getItems() {
return [
{ label: 'Item 1', description: 'Description 1' },
{ label: 'Item 2', description: 'Description 2' },
{ label: 'Item 3', description: 'Description 3' }
];
},
templates: {
item({ item }) {
return `<div>
<strong>${item.label}</strong>
<p>${item.description}</p>
</div>`;
}
}
}
];
}
});
```
Other packages similar to @algolia/autocomplete-js
react-autosuggest
react-autosuggest is a React component for building autocomplete and autosuggest input fields. It is highly customizable and supports various data sources. Compared to @algolia/autocomplete-js, react-autosuggest is more focused on React applications and provides a more React-centric API.
downshift
downshift is a library that provides primitives to build flexible and accessible autocomplete, combobox, and dropdown components. It is framework-agnostic and can be used with React, Preact, or other libraries. Unlike @algolia/autocomplete-js, downshift offers lower-level building blocks, giving developers more control over the behavior and appearance of their components.
awesomplete
awesomplete is a lightweight, zero-dependency JavaScript library for creating autocomplete widgets. It is easy to use and highly customizable. Compared to @algolia/autocomplete-js, awesomplete is more lightweight and does not include built-in support for integrating with external APIs like Algolia.
@algolia/autocomplete-js
The autocomplete-js
package is an agnostic virtual DOM renderer. You can use it in JavaScript, Preact, React, or Vue projects.
Installation
yarn add @algolia/autocomplete-js
npm install @algolia/autocomplete-js
Documentation
See Documentation.