createLocalStorageRecentSearchesPlugin
The createLocalStorageRecentSearchesPlugin
plugin connects with the user’s local storage to fetch and save recent searches. To plug your own storage, check createRecentSearchesPlugin
.
Installation
First, you need to install the plugin.
1
2
3
yarn add @algolia/autocomplete-plugin-recent-searches
# or
npm install @algolia/autocomplete-plugin-recent-searches
Then import it in your project:
1
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
If you don’t use a package manager, you can use the HTML script
element:
1
2
3
4
5
6
<script src="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-plugin-recent-searches"></script>
<script>
const { createLocalStorageRecentSearchesPlugin } = window[
'@algolia/autocomplete-plugin-recent-searches'
];
</script>
Example
This example uses the plugin within autocomplete-js
.
1
2
3
4
5
6
7
8
9
10
11
12
import { autocomplete } from '@algolia/autocomplete-js';
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
key: 'navbar',
});
autocomplete({
container: '#autocomplete',
openOnFocus: true,
plugins: [recentSearchesPlugin],
});
You can combine this plugin with the Query Suggestions plugin to leverage the empty screen with recent and popular queries.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import algoliasearch from 'algoliasearch/lite';
import { autocomplete } from '@algolia/autocomplete-js';
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
key: 'navbar',
});
const querySuggestionsPlugin = createQuerySuggestionsPlugin({
searchClient,
indexName: 'instant_search_demo_query_suggestions',
getSearchParams() {
return recentSearches.data.getAlgoliaSearchParams();
},
});
autocomplete({
container: '#autocomplete',
openOnFocus: true,
plugins: [recentSearchesPlugin, querySuggestionsPlugin],
});
To see it in action, check this demo on CodeSandbox.
Parameters
key
|
type: string
Required
A local storage key to identify where to save and retrieve the recent searches. For example:
The plugin namespaces all keys to avoid collisions. |
||||
limit
|
type: number
The number of recent searches to display. |
||||
search
|
type: (params: { query: string; items: TItem[]; limit: number; }) => TItem[]
A search function to retrieve recent searches from. This function is called in
Copy
|
||||
transformSource
|
type: (params: { source: AutocompleteSource, state: AutocompleteState, onRemove: () => void, onTapAhead: () => void })
A function to transform the provided source. Keeping the panel open on select:
Copy
Opening a link:
Copy
|
Returns
data
getAlgoliaSearchParams
|
type: SearchParameters => SearchParameters
Optimized Algolia search parameters. This is useful when using the plugin along with the Query Suggestions plugin. This function enhances the provided search parameters by:
|